Skip to main content

hypersig_ui/
kbd.rs

1//! Keyboard keys drawn as small caps in running text.
2
3use hypersig::html::{El, kbd as kbd_el};
4use hypersig::style::Style;
5use hypersig::style::value::{
6    AlignItems, Display, FontWeight, JustifyContent, LineHeight, UserSelect, WhiteSpace, px, raw,
7    rem,
8};
9
10use crate::token;
11
12/// A `<kbd>` that shows a key or a shortcut the visitor presses.
13///
14/// A screen reader reads its text as it stands, so write the key's name in
15/// words where a symbol would be read as something else, such as "Ctrl"
16/// rather than "^". For a shortcut, place one `kbd` per key side by side.
17///
18/// ```
19/// use hypersig::Signals;
20/// use hypersig::html::p;
21/// use hypersig_ui::kbd;
22/// use serde::{Deserialize, Serialize};
23///
24/// #[derive(Signals, Serialize, Deserialize)]
25/// struct Help {
26///     open: bool,
27/// }
28///
29/// let hint = p::<Help>().text("Press ").child(kbd().text("Esc")).text(" to close.");
30/// assert!(hint.render().contains("<kbd"));
31/// ```
32#[must_use]
33pub fn kbd<S>() -> El<S> {
34    kbd_el().css(
35        Style::new()
36            .display(Display::InlineFlex)
37            .align_items(AlignItems::Center)
38            .justify_content(JustifyContent::Center)
39            .gap(rem(0.25))
40            .height(rem(1.25))
41            .min_width(rem(1.25))
42            .padding_inline(rem(0.25))
43            .border_radius(px(4))
44            .background_color(token::muted().var())
45            .color(token::muted_foreground().var())
46            .font_family(raw("inherit"))
47            .font_size(rem(0.75))
48            .line_height(LineHeight::Length(rem(1)))
49            .font_weight(FontWeight::Number(500.0))
50            .white_space(WhiteSpace::Nowrap)
51            .user_select(UserSelect::None),
52    )
53}