日本語

Kbd

A key or a shortcut the visitor presses, drawn as a small cap in running text.

Result

kbd.rs
use hypersig::html::{div, p, span};
use hypersig::style::pattern::{hstack, stack};
use hypersig::style::value::rem;
use hypersig::{Page, Signals};
use hypersig_ui::kbd;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
pub(super) struct Help {
    open: bool,
}

pub(super) fn page() -> Page<Help> {
    Page::new(
        "Help",
        Help { open: false },
    )
    .child(
        div()
            .css(stack(rem(0.75)))
            .child(
                span()
                    .css(hstack(rem(0.25)))
                    .child(kbd().text("Ctrl"))
                    .child(kbd().text("Shift"))
                    .child(kbd().text("P")),
            )
            .child(
                p().text("Press ")
                .child(kbd().text("Esc"))
                .text(" to close the dialog."),
            ),
    )
}

Accessibility

It is a native <kbd>, which a screen reader reads as the text it holds, as part of the sentence around it. Write the key's name in words where a symbol would be read as something else, such as "Ctrl" rather than "^". The key cannot be selected with the pointer, so copying the sentence around it leaves the key out. It takes no keys of its own.

Building a key

kbd returns an empty <kbd>, whose text comes from El::text. For a shortcut, place one kbd per key side by side:

pub fn kbd<S>() -> El<S>;

The key is drawn in token::muted with the font of the text around it, rather than a monospace font, and stays on one line.

The HTML

The example at the end of this page renders this HTML, with the classes left out:

<span><kbd>Ctrl</kbd><kbd>Shift</kbd><kbd>P</kbd></span>
<p>Press <kbd>Esc</kbd> to close the dialog.</p>

What fails to compile

kbd takes no arguments and holds only text, and its types stop no mistake. Text is escaped like any element's, so a key such as < shows as itself.

What is not checked

Nothing checks that the text names a key, or that it matches the shortcut the page answers to. A shortcut written as one kbd holding "Ctrl+Shift+P" renders as one wide key. Ctrl and Command differ by platform, and the text shows only what you write, so a page with Key::primary names both or picks one per visitor on the server.

Example: a shortcut and a key in a sentence

use hypersig::html::{p, span};
use hypersig::{Page, Signals};
use hypersig_ui::kbd;
use serde::{Deserialize, Serialize};

#[derive(Signals, Serialize, Deserialize)]
struct Help {}

let html = Page::new("Help", Help {})
    // One `kbd` per key, written as a word a screen reader reads correctly.
    .child(
        span()
            .child(kbd().text("Ctrl"))
            .child(kbd().text("Shift"))
            .child(kbd().text("P")),
    )
    .child(p().text("Press ").child(kbd().text("Esc")).text(" to close the dialog."))
    .render();

Keyboard patterns covers the shortcuts a page answers to with Page::on_key. A tooltip that shows a button's shortcut is a Tooltip.