English

ハンドル

ダイアログ、メニュー、ポップオーバー、ツールチップ、コンボボックスは const のハンドルです。開くボタンと開かれる要素を同じハンドルから作るので、両者は要素の id で食い違いません。

実行結果

menu.rs
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(
        "プロジェクト",
        initial,
    )
    .child(
        div()
            .css(stack(rem(1)))
            .child(
                div()
                    .child(
                        ACTIONS
                            .trigger(ButtonVariant::Outline, ButtonSize::Default)
                            .text("プロジェクトの操作"),
                    )
                    .child(
                        ACTIONS.menu([
                            MenuItem::new(
                                "名前を変更",
                                status.set("プロジェクトの名前を変更しました。"),
                            ),
                            MenuItem::new(
                                "複製",
                                status.set("プロジェクトを複製しました。"),
                            ),
                            MenuItem::new(
                                "アーカイブ",
                                archived.set(true).then(status.set("プロジェクトをアーカイブしました。")),
                            )
                            .disabled(archived),
                            MenuItem::new(
                                "削除",
                                status.set("プロジェクトを削除しました。"),
                            )
                            .destructive(),
                        ]),
                    ),
            )
            .child(p().role(Role::Status).bind_text(status)),
    )
}

ハンドルを作る

5 つのコンポーネントは、ボタンとそれが開く要素、または入力欄とその一覧に分かれていて、ページはそれらを別々の場所に置きます。どれも、node_ref! か dialog_ref! で宣言した参照から const fn new で作るハンドルです。

ハンドルコンストラクタ元になる参照
Dialog<S>const fn new(dialog: DialogRef<S>) -> Selfdialog_ref! 1 つ
Menu<S>const fn new(menu: NodeRef<S>) -> Selfnode_ref! 1 つ
ContextMenu<S>const fn new(menu: NodeRef<S>) -> Selfnode_ref! 1 つ
Popover<S>const fn new(panel: NodeRef<S>) -> Selfnode_ref! 1 つ
Tooltip<S>const fn new(tip: NodeRef<S>) -> Selfnode_ref! 1 つ
Combobox<S>const fn new(field: NodeRef<S>, list: NodeRef<S>) -> Selfnode_ref! 2 つ(入力欄と一覧)

ハンドルは Copy で、各メソッドはハンドルを値で受け取って部品を 1 つ作ります。ボタンを作るのは trigger で、要素を作るのは dialog、menu、popover、tooltip、render です。型パラメーター S はページのシグナル構造体なので、あるページのハンドルで別のページの部品は作れません。どの部品も El<S> なので、ページはほかの要素と同じように配置します。メニューの 2 つの部品は次のように描画されます。

<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>

メニューの id とボタンの commandfor はどちらも node ref の名前です。名前は定数のパスで、ここでは shop::DOC_ACTIONS です。そのため、ボタンはほかのメニューではなく、このメニューを開きます。上の例ではメニューの名前をボタンから取る aria-labelledby を省いています。

ハンドルが `const` である理由

node_ref! と dialog_ref! はモジュールのレベルで定数を宣言し、関数の本体の中ではコンパイルできません。そのため、どの名前も、ただ 1 つの定数だけが持つパスになります。ハンドルの new は const fn なので、ハンドルもモジュールのレベルの定数になり、ボタンを作る関数とメニューを作る関数は、どこにあっても同じ値を読みます。const にすると、new の中の検査もコンパイラが行います。Combobox::new に入力欄と一覧で同じ node ref を渡すと、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);

エラーは evaluation panicked: a combobox's field and list are two node refs です。関数の中で let を使って作ると、同じ呼び出しでもコンパイルは通り、実行時に panic します。

同じ名前を 2 回使う

ページが部品を何回置いたかは、コンパイラには分かりません。ハンドルが開く要素は node ref を持つので、1 つのハンドルのメニュー、ポップオーバー、ツールチップ、コンボボックス、ダイアログを、ブラウザが両方に届く場所へ 2 回置くと、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();
  • ページを描画するテストを書けば、訪問者より先に見つけられます。
  • ダイアログには複数のボタンを置けます。1 つの Dialog の trigger を 2 つ置けば、どちらも同じダイアログを開きます。1 つの Tooltip の trigger を 2 つ置くと、どちらも同じツールチップを指し、ツールチップはどちらのボタンで開いても、ページの後ろにあるほうのボタンの下に出ます。メニューとポップオーバーは名前をボタンから取るので、ボタンも id を持ちます。そのため、1 つの Menu か Popover の trigger を 2 つ置くと、同じように Page::render が panic します。

例: 5 つを 1 ページに置く

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(),
            )),
    );

次に読むページ

node_ref! と dialog_ref! は ノード参照 で説明しています。各ハンドルについてはダイアログ、メニュー、ポップオーバー、ツールチップ、コンボボックスのページで説明しています。