Handles
A dialog, a menu, a popover, a tooltip, and a combobox are const handles. The button that opens one and the element it opens are built from the same handle, so they agree on the element's id.
Result
use hypersig::html::{Role, div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Menu, MenuItem};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Project {
archived: bool,
status: String,
}
hypersig::node_ref!(const PROJECT_ACTIONS: NodeRef<Project>);
const ACTIONS: Menu<Project> = Menu::new(PROJECT_ACTIONS);
pub(super) fn page() -> Page<Project> {
let archived = Project::archived();
let status = Project::status();
let initial = Project {
archived: false,
status: String::new(),
};
Page::new(
"Project",
initial,
)
.child(
div()
.css(stack(rem(1)))
.child(
div()
.child(
ACTIONS
.trigger(ButtonVariant::Outline, ButtonSize::Default)
.text("Project actions"),
)
.child(
ACTIONS.menu([
MenuItem::new(
"Rename",
status.set("Renamed the project."),
),
MenuItem::new(
"Duplicate",
status.set("Duplicated the project."),
),
MenuItem::new(
"Archive",
archived.set(true).then(status.set("Archived the project.")),
)
.disabled(archived),
MenuItem::new(
"Delete",
status.set("Deleted the project."),
)
.destructive(),
]),
),
)
.child(p().role(Role::Status).bind_text(status)),
)
}Building a handle
Five components are split into a button and the element it opens, or a field and its list, which the page places apart. Each is a handle built by a const fn new from a reference that node_ref! or dialog_ref! declares:
| Handle | Constructor | Built from |
|---|---|---|
Dialog<S> | const fn new(dialog: DialogRef<S>) -> Self | one dialog_ref! |
Menu<S> | const fn new(menu: NodeRef<S>) -> Self | one node_ref! |
ContextMenu<S> | const fn new(menu: NodeRef<S>) -> Self | one node_ref! |
Popover<S> | const fn new(panel: NodeRef<S>) -> Self | one node_ref! |
Tooltip<S> | const fn new(tip: NodeRef<S>) -> Self | one node_ref! |
Combobox<S> | const fn new(field: NodeRef<S>, list: NodeRef<S>) -> Self | two node_ref!, the text field's and the list's |
A handle is Copy, and each of its methods takes it by value and builds one part: trigger the button, and dialog, menu, popover, tooltip, or render the element. The type parameter S is the page's signal struct, so a handle of one page cannot build parts on another, and each part is an El<S> the page places like any element. A menu's two parts render as:
<button type="button" aria-haspopup="menu" commandfor="shop::DOC_ACTIONS" command="toggle-popover">Actions</button>
<div id="shop::DOC_ACTIONS" popover="auto" role="menu">
<button type="button" role="menuitem">Archive</button>
</div>The menu's id and the button's commandfor are both the node ref's name, which is the constant's path, shop::DOC_ACTIONS here, so the button opens this menu and no other. The aria-labelledby that names the menu after its button is left out above.
Why a handle is `const`
node_ref! and dialog_ref! declare a constant at module level and fail to compile inside a function body, so every name is a path that only one constant has. The handle's new is a const fn, so the handle is a module-level constant too, and the function that builds the button and the function that builds the menu read the same value wherever they are. A const also lets the compiler check what new asserts: Combobox::new given one node ref for both its field and its list fails to compile with E0080:
use hypersig::{NodeRef, Signals};
use hypersig_ui::Combobox;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Shipping {}
hypersig::node_ref!(const COUNTRY_FIELD: NodeRef<Shipping>);
// The field's node ref given for the list as well.
const COUNTRY: Combobox<Shipping> = Combobox::new(COUNTRY_FIELD, COUNTRY_FIELD);The error reads evaluation panicked: a combobox's field and list are two node refs. Built with let inside a function instead, the same call compiles and panics when it runs.
A name used twice
The compiler does not see how many times a page places a part. The element a handle opens carries its node ref, so one handle's menu, popover, tooltip, combobox, or dialog placed twice where the browser can reach both makes Page::render panic:
use hypersig::html::div;
use hypersig::{NodeRef, Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Menu, MenuItem};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Doc {
archived: bool,
}
hypersig::node_ref!(const DOC_ACTIONS: NodeRef<Doc>);
const ACTIONS: Menu<Doc> = Menu::new(DOC_ACTIONS);
let menu = || ACTIONS.menu([MenuItem::new("Archive", Doc::archived().set(true))]);
let page = Page::new("Doc", Doc { archived: false }).child(
div()
.child(ACTIONS.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Actions"))
.child(menu())
// The same menu a second time.
.child(menu()),
);
// Panics: "two elements carry the id `...::DOC_ACTIONS` where the runtime can reach both".
let _ = page.render();- A test that renders the page finds it before a visitor does.
- A dialog may have several buttons: two
triggerbuttons of oneDialogopen the same dialog. Twotriggerbuttons of oneTooltipboth point at the tooltip, which opens under the later of the two in the page, whichever button opened it. A menu or a popover is named after its button, so its button carries an id too, and a secondtriggerof oneMenuorPopovermakesPage::renderpanic the same way.
Example: all five on one page
use hypersig::html::{div, p};
use hypersig::{DialogRef, NodeRef, Page, Signals};
use hypersig_ui::{
ButtonSize, ButtonVariant, Combobox, ComboboxState, ComboboxText, Dialog, DialogText, Menu,
MenuItem, Popover, Tooltip, dialog_footer,
};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Profile {
archived: bool,
country: ComboboxState,
}
// Each handle is a `const` built from a reference that names its element.
hypersig::dialog_ref!(const EDIT_PROFILE: DialogRef<Profile>);
const EDIT: Dialog<Profile> = Dialog::new(EDIT_PROFILE);
hypersig::node_ref!(const PROFILE_ACTIONS: NodeRef<Profile>);
const ACTIONS: Menu<Profile> = Menu::new(PROFILE_ACTIONS);
hypersig::node_ref!(const PROFILE_DETAILS: NodeRef<Profile>);
const DETAILS: Popover<Profile> = Popover::new(PROFILE_DETAILS);
hypersig::node_ref!(const ARCHIVE_TIP: NodeRef<Profile>);
const ARCHIVE: Tooltip<Profile> = Tooltip::new(ARCHIVE_TIP);
hypersig::node_ref!(const COUNTRY_FIELD: NodeRef<Profile>);
hypersig::node_ref!(const COUNTRY_OPTIONS: NodeRef<Profile>);
const COUNTRY: Combobox<Profile> = Combobox::new(COUNTRY_FIELD, COUNTRY_OPTIONS);
// The button and the element it opens are built from the same handle.
let page = Page::new("Profile", Profile { archived: false, country: ComboboxState::new("") })
.child(
div()
.child(EDIT.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Edit"))
.child(EDIT.dialog(
"Edit profile",
"Changes are saved when you press Save.",
dialog_footer()
.child(EDIT.close(ButtonVariant::Default, ButtonSize::Default).text("Save")),
&DialogText::default(),
))
.child(ACTIONS.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Actions"))
.child(ACTIONS.menu([MenuItem::new("Archive", Profile::archived().set(true))]))
.child(DETAILS.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Details"))
.child(DETAILS.popover().child(p().text("Joined in 2024.")))
.child(ARCHIVE.icon_trigger(ButtonVariant::Ghost, "Archive").text("A"))
.child(ARCHIVE.tooltip("Moves the profile out of the list."))
.child(COUNTRY.render(
"Country",
Profile::country(),
["France", "Japan", "Kenya"],
&ComboboxText::default(),
)),
);