Popover
A panel of content that a button opens beside itself, while the rest of the page stays usable.
Result
use hypersig::html::{div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::{FontWeight, rem};
use hypersig::style::Style;
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Popover, TextField, button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Layer {
width: String,
}
hypersig::node_ref!(const SIZE_PANEL: NodeRef<Layer>);
const SIZE: Popover<Layer> = Popover::new(SIZE_PANEL);
pub(super) fn page() -> Page<Layer> {
Page::new(
"Layer",
Layer {
width: "100%".to_owned(),
},
)
.child(
div()
.child(
SIZE.trigger(ButtonVariant::Outline, ButtonSize::Default)
.text("Size"),
)
.child(
SIZE.popover().child(
div()
.css(stack(rem(0.75)))
.child(
p().css(Style::new().font_weight(FontWeight::Number(500.0)))
.text("Set the size of the layer."),
)
.child(TextField::new(
"Width",
Layer::width(),
))
.child(
div().child(
button(ButtonVariant::Default, ButtonSize::Sm)
.on_click(SIZE_PANEL.hide_popover())
.text("Done"),
),
),
),
)
)
}Accessibility
The panel is a popover="auto" with role="dialog", named by its button through aria-labelledby. The button has aria-haspopup="dialog", so a screen reader announces that it opens a dialog, and reads the panel as a dialog with the button's text as its name.
The page stays usable while the panel is open, unlike a modal Dialog. Focus stays on the button as the panel opens, so the panel goes right after its button, where Tab moves from the button into it. The browser closes the panel on Escape or on a click outside, and returns focus to the button when focus was inside the panel.
| Key | What it does |
|---|---|
| Enter, Space | On the button, opens or closes the panel |
| Tab | From the button, moves into the open panel |
| Escape | Closes the panel, and returns focus to the button when focus was inside it |
Building a popover
A Popover<S> is a Copy handle built from a NodeRef<S>, declared as a const so that the button and the panel share it, as Handles covers:
impl<S> Popover<S> {
pub const fn new(panel: NodeRef<S>) -> Self;
pub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S>;
pub fn popover(self) -> El<S>;
}| Item | What it gives |
|---|---|
trigger | The button that opens and closes the panel. Its text, added with El::text, names the panel |
popover | The panel, empty, for the content added with El::child |
The handle has no statement that closes the panel. A button inside it closes it with NodeRef::hide_popover on the node ref the handle is built from, and show_popover opens it the same way. Node refs covers both.
The HTML
The example at the end of this page renders this HTML, with the classes left out. Each … is an id made from the node ref's name, so the button opens the panel with commandfor and the panel takes its name from the button's id:
<button id="…" type="button" aria-haspopup="dialog" commandfor="…" command="toggle-popover">Size</button>
<div id="…" popover="auto" role="dialog" aria-labelledby="…">
<div>
<p>Set the size of the layer.</p>
<!-- the text field -->
<button type="button">Done</button>
</div>
</div>What fails to compile
The panel is a <div popover>, so Popover::new takes a NodeRef. A DialogRef names a <dialog> and is E0308, mismatched types:
use hypersig::{DialogRef, Signals};
use hypersig_ui::Popover;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Layer {}
// A dialog ref names a `<dialog>`, and the panel is a `<div popover>`.
hypersig::dialog_ref!(const SIZE_DIALOG: DialogRef<Layer>);
const SIZE: Popover<Layer> = Popover::new(SIZE_DIALOG);hide_popover returns a Stmt<S> of the node ref's page, so a node ref of another page is E0308 in the panel's on_click. popover takes no arguments, and its content goes through El::child like any element's.
What is not checked
One handle's popover placed twice makes Page::render panic with two elements carry the node ref … where the runtime can reach both. A trigger with no panel on the page renders, and pressing it does nothing. Nothing checks that the panel follows its button.
Another popover="auto", such as a Menu, opening outside the panel closes the panel, as the browser does for any two auto popovers that are not nested.
Example: setting a size
The panel holds a text field bound to a signal and a Done button that closes it:
use hypersig::html::{div, p};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{NodeRef, Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Popover, TextField, button};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Layer {
width: String,
}
hypersig::node_ref!(const SIZE_PANEL: NodeRef<Layer>);
const SIZE: Popover<Layer> = Popover::new(SIZE_PANEL);
let html = Page::new("Layer", Layer { width: "100%".into() })
.child(
div()
.child(SIZE.trigger(ButtonVariant::Outline, ButtonSize::Default).text("Size"))
// Right after its button, so Tab moves from the button into it.
.child(
SIZE.popover().child(
div()
.css(stack(rem(0.75)))
.child(p().text("Set the size of the layer."))
.child(TextField::new("Width", Layer::width()))
.child(
button(ButtonVariant::Default, ButtonSize::Sm)
.on_click(SIZE_PANEL.hide_popover())
.text("Done"),
),
),
),
)
.render();A list of actions is a Menu, and a description of one button is a Tooltip. Content that needs the visitor's full attention is a Dialog.