English

ポップオーバー

ボタンが自分の横に開く、内容を入れるパネルです。開いている間もページのほかの部分は操作できます。

実行結果

popover.rs
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 {
            width: "100%".to_owned(),
        },
    )
    .child(
        div()
            .child(
                SIZE.trigger(ButtonVariant::Outline, ButtonSize::Default)
                    .text("サイズ"),
            )
            .child(
                SIZE.popover().child(
                    div()
                        .css(stack(rem(0.75)))
                        .child(
                            p().css(Style::new().font_weight(FontWeight::Number(500.0)))
                                .text("レイヤーのサイズを設定します。"),
                        )
                        .child(TextField::new(
                            "幅",
                            Layer::width(),
                        ))
                        .child(
                            div().child(
                                button(ButtonVariant::Default, ButtonSize::Sm)
                                    .on_click(SIZE_PANEL.hide_popover())
                                    .text("完了"),
                            ),
                        ),
                ),
            )
    )
}

アクセシビリティ

パネルは role="dialog" を持つ popover="auto" で、aria-labelledby によってボタンのテキストがパネルの名前になります。ボタンは aria-haspopup="dialog" を持つので、スクリーンリーダーはボタンがダイアログを開くことを読み上げ、パネルをボタンのテキストを名前とするダイアログとして読み上げます。

モーダルの ダイアログ と違い、パネルが開いている間もページは操作できます。パネルが開いてもフォーカスはボタンに残るので、パネルはボタンの直後に置きます。そうすると、ボタンから Tab でパネルの中に移れます。ブラウザは Escape かパネルの外のクリックでパネルを閉じ、フォーカスがパネルの中にあった場合はボタンに戻します。

キー動作
Enter、Spaceボタンの上で押すと、パネルを開くか閉じます
Tabボタンから、開いているパネルの中に移ります
Escapeパネルを閉じます。フォーカスがパネルの中にあった場合は、ボタンに戻します

ポップオーバーを作る

Popover<S> は NodeRef<S> から作る Copy のハンドルです。ボタンとパネルで共有するように const として宣言します。宣言のしかたは ハンドル で説明しています。

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>;
}
項目返すもの
triggerパネルを開閉するボタン。El::text で加えたテキストがパネルの名前になります
popoverEl::child で内容を加える、空のパネル

ハンドルにはパネルを閉じる文がありません。パネルの中のボタンから閉じるには、ハンドルを作ったノード参照の NodeRef::hide_popover を使います。開くときは同じように show_popover を使います。どちらも ノード参照 で説明しています。

出力される HTML

このページの最後の例は次の HTML を出力します(クラスは省いています)。… はどれもノード参照の名前から作った id です。ボタンは commandfor でパネルを開き、パネルはボタンの 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>

コンパイルエラーになるもの

パネルは <div popover> なので、Popover::new は NodeRef を受け取ります。DialogRef は <dialog> を指す参照なので、渡すと E0308(型の不一致)になります。

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 はノード参照のページの Stmt<S> を返すので、ほかのページのノード参照を使うと、パネルの on_click で E0308 になります。popover は引数を取らず、内容はほかの要素と同じく El::child で加えます。

検査されないこと

1 つのハンドルの popover を 2 回置くと、Page::render が two elements carry the node ref … where the runtime can reach both というメッセージで panic します。ページにパネルがないトリガーは描画されますが、押しても何も起きません。パネルがボタンの直後にあるかどうかは検査されません。

パネルの外で メニュー のような別の popover="auto" が開くと、パネルは閉じます。入れ子になっていない 2 つの auto のポップオーバーに対して、ブラウザがそう動作するからです。

例: サイズを設定する

パネルにはシグナルに束縛したテキスト欄と、パネルを閉じる Done のボタンがあります。

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

操作の一覧には メニュー を、1 つのボタンの説明には ツールチップ を使います。訪問者に集中して見てほしい内容には ダイアログ を使います。