Collapsible
A button that shows and hides one region, bound to a bool signal.
Result
use hypersig::html::{div, li, p, ul};
use hypersig::style::pattern::{hstack, stack};
use hypersig::style::value::{JustifyContent, rem};
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Collapsible};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Releases {
older: bool,
}
pub(super) fn page() -> Page<Releases> {
let older = Collapsible::new(Releases::older());
Page::new(
"Releases",
Releases { older: false },
)
.child(
div()
.css(stack(rem(0.5)))
.child(
div()
.css(hstack(rem(1)).justify_content(JustifyContent::SpaceBetween))
.child(p().text("0.8.2 is the latest release."))
.child(
older
.trigger(ButtonVariant::Outline, ButtonSize::Sm)
.text("Older releases"),
),
)
.child(
older.content().child(
ul().css(stack(rem(0.25)))
.child(li().text("0.8.1"))
.child(li().text("0.8.0"))
.child(li().text("0.7.1")),
),
),
)
}Accessibility
It follows the WAI-ARIA disclosure pattern. The button is a native <button> with aria-expanded, so a screen reader announces it as expanded or collapsed, and aria-controls names the region it shows. A hidden region is out of the page for assistive technology too.
| Key | What it does |
|---|---|
| Enter, Space | Shows or hides the region. |
The button and the region
Collapsible::new takes the signal that says whether the region is shown, and returns a handle whose two methods build the button and the region:
impl<S> Collapsible<S> {
pub fn new(open: Sig<S, bool>) -> Self;
pub fn trigger(self, variant: ButtonVariant, size: ButtonSize) -> El<S>;
pub fn content(self) -> El<S>;
}Collapsible is Copy, so one handle builds both parts. trigger returns a button of the given look that flips the signal on a click, and content returns an empty <div>, shown while the signal is true, for the children you add with El::child. The two are separate elements, so the button can sit in a heading or a toolbar away from the region.
The state is a signal of the page, so the server sets it with the page's first value or a patch, and any statement can open the region, such as Releases::older().set(true) on another button. The signal must be a bool. A signal of another type is E0308:
use hypersig::Signals;
use hypersig_ui::Collapsible;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Releases {
latest: String,
}
// error[E0308]: mismatched types: expected `Sig<_, bool>`, found `Sig<Releases, String>`
let older = Collapsible::new(Releases::latest());The HTML
With the signal false, the button and the region render as below. The value of aria-controls is the region's id, which the component derives from the signal's path:
<button type="button" aria-controls="…" aria-expanded="false">Show older</button>
…
<div id="…">
<ul><li>0.8.1</li><li>0.8.0</li></ul>
</div>The region is in the HTML while it is hidden. A click sets the signal to true, which turns aria-expanded to "true" and shows the region, with no request to the server.
What is not checked
The region's id comes from the signal, so calling content twice on one handle, or building two collapsibles on one signal, puts one id on two elements. Page::render panics on such a page:
use hypersig::html::p;
use hypersig::{Page, Signals};
use hypersig_ui::Collapsible;
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Order {
details: bool,
}
let details = Collapsible::new(Order::details());
// Panics: the two regions carry one id.
let html = Page::new("Order", Order { details: false })
.child(details.content().child(p().text("Shipped on Monday.")))
.child(details.content().child(p().text("Paid by card.")))
.render();Nothing checks that the page has the region at all. A page with only the button still flips the signal, and its aria-controls names an element that is not there. The button's name is its text, which trigger leaves to you, so a button with no text or label has no name for a screen reader.
Example: older releases
The button sits in the page's heading, and the list it shows comes after a paragraph:
use hypersig::html::{div, h2, li, ul};
use hypersig::{Page, Signals};
use hypersig_ui::{ButtonSize, ButtonVariant, Collapsible};
use serde::{Deserialize, Serialize};
#[derive(Signals, Serialize, Deserialize)]
struct Releases {
older: bool,
}
let older = Collapsible::new(Releases::older());
// The button sits in the heading, away from the list it shows.
let html = Page::new("Releases", Releases { older: false })
.child(
h2().text("Releases ").child(
older
.trigger(ButtonVariant::Ghost, ButtonSize::Sm)
.text("Show older"),
),
)
.child(div().text("0.8.2 is the latest release."))
.child(
older
.content()
.child(ul().child(li().text("0.8.1")).child(li().text("0.8.0"))),
)
.render();Next, Card groups related content in a bordered box.