日本語

Accordion

A stack of sections that open and close, one at a time or each on its own.

Result

accordion.rs
use hypersig::html::{div, p, span};
use hypersig::style::pattern::stack;
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::{Accordion, ButtonSize, ButtonVariant, button};
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub struct Faq {
    helpful: i32,
}

pub fn page() -> Page<Faq> {
    let helpful = Faq::helpful();
    let answers = Accordion::exclusive("faq")
        .item(
            "Does it need JavaScript?",
            p().text("No. Each section is a <details>, which opens without it."),
        )
        .item(
            "Can I restyle it?",
            p().text("Set the tokens in a Theme, or add a Style to any element."),
        )
        .item(
            "Is it accessible?",
            p().text("The browser announces each summary as a button that opens."),
        );
    Page::new(
        "FAQ",
        Faq { helpful: 0 },
    )
    .child(
        div().css(stack(rem(1))).child(answers).child(
            p().child(
                button(ButtonVariant::Outline, ButtonSize::Sm)
                    .on_click(helpful.set(helpful + 1))
                    .text("This helped"),
            )
            .text(" ")
            .child(span().bind_text(helpful.to_text()))
            .text(" found it helpful"),
        ),
    )
}

Accessibility

Each section is a native <details> whose <summary> opens and closes it. The browser handles the keyboard, and each summary is announced as a button that is expanded or collapsed, and find-in-page opens a closed section that holds a match.

An exclusive accordion gives its sections one name, so the browser closes the open one when another opens.

KeyWhat it does
Enter, SpaceOpens or closes the focused section.

API

ItemSummary
Accordion::newAn accordion whose sections open and close independently.
Accordion::exclusiveAn accordion that keeps at most one section open, grouped by name.
itemAdds a section titled title whose content is content.