Skip to main content

tablo_ui/components/composites/
controls.rs

1//! Tablo's form controls: text fields, boxes, radios and switches on the control surface. Each
2//! carries its `data-slot` (`input`, `textarea`, `checkbox`, `radio`, `switch`), which each
3//! theme's skin keys on.
4
5use topcoat::{
6    Result,
7    context::Cx,
8    icon::{icon, iconify::iconify_icon},
9    view::{Attributes, Child, StaticClass, View, class, component, view},
10};
11
12/// What every text field shares: the control surface, its edge, and the focus halo.
13const FIELD: StaticClass = class!(
14    "w-full min-w-0 rounded-md border border-input bg-control text-sm text-foreground",
15    "shadow-control transition-[border-color,box-shadow] outline-none",
16    "placeholder:text-muted-foreground/75",
17    "hover:border-[color-mix(in_oklab,var(--input),var(--foreground)_22%)]",
18    "focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/20",
19    "aria-invalid:border-destructive aria-invalid:focus-visible:ring-destructive/20",
20    "disabled:pointer-events-none disabled:opacity-50 read-only:bg-muted/60",
21);
22
23const INPUT: StaticClass = class!(
24    "h-9 px-3",
25    "file:mr-3 file:h-full file:border-0 file:bg-transparent file:text-sm file:font-medium",
26);
27
28const TEXTAREA: StaticClass = class!("field-sizing-content min-h-20 px-3 py-2 leading-relaxed");
29
30/// A one-line `<input>`; its `type` is text unless `attrs` names another.
31#[component]
32pub async fn input(cx: &Cx, #[default] mut attrs: Attributes) -> Result<impl View> {
33    Ok(view! {
34        <input
35            class=(class!(FIELD, INPUT, attrs.remove("class")))
36            data-slot="input"
37            (attrs)
38        >
39    })
40}
41
42/// A `<textarea>` that grows with its text.
43#[component]
44pub async fn textarea(
45    cx: &Cx,
46    #[default] mut attrs: Attributes,
47    #[default] child: Child<'_>,
48) -> Result<impl View> {
49    Ok(view! {
50        <textarea
51            class=(class!(FIELD, TEXTAREA, attrs.remove("class")))
52            data-slot="textarea"
53            (attrs)
54        >
55            (child)
56        </textarea>
57    })
58}
59
60/// A box's edge and the fill it takes once checked.
61const BOX: StaticClass = class!(
62    "peer size-4 shrink-0 cursor-pointer appearance-none border border-input bg-control",
63    "shadow-control transition-colors outline-none checked:border-primary checked:bg-primary",
64    "hover:border-[color-mix(in_oklab,var(--input),var(--foreground)_25%)]",
65    "focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2",
66    "focus-visible:ring-offset-background disabled:pointer-events-none",
67);
68
69/// The wrapper the mark overlays: the `<input>` renders no children or pseudo-elements, so the
70/// mark is a sibling revealed by the input's `peer` state.
71const HOLDER: StaticClass = class!("peer relative inline-flex shrink-0 has-[:disabled]:opacity-50");
72
73/// A checkbox.
74#[component]
75pub async fn checkbox(cx: &Cx, #[default] mut attrs: Attributes) -> Result<impl View> {
76    Ok(view! {
77        <span class=(class!(HOLDER, attrs.remove("class"))) data-slot="checkbox">
78            <input
79                type="checkbox"
80                class=(class!(BOX, "rounded-[min(var(--radius-sm),5px)]"))
81                (attrs)
82            >
83            icon(
84                data: iconify_icon!("lucide:check"),
85                attrs: topcoat::view::attributes! {
86                    class="pointer-events-none absolute inset-0 m-auto size-3 stroke-[3] \
87                        text-primary-foreground opacity-0 peer-checked:opacity-100"
88                }
89            )
90        </span>
91    })
92}
93
94/// The group a set of [`radio_group_item`]s sits in.
95#[component]
96pub async fn radio_group(
97    cx: &Cx,
98    #[default] mut attrs: Attributes,
99    #[default] child: Child<'_>,
100) -> Result<impl View> {
101    Ok(view! {
102        <div
103            role="radiogroup"
104            class=(class!("grid gap-3", attrs.remove("class")))
105            (attrs)
106        >
107            (child)
108        </div>
109    })
110}
111
112/// One radio; checked, its ring takes the primary color around a dot of it.
113#[component]
114pub async fn radio_group_item(cx: &Cx, #[default] mut attrs: Attributes) -> Result<impl View> {
115    Ok(view! {
116        <span class=(class!(HOLDER, attrs.remove("class"))) data-slot="radio">
117            <input
118                type="radio"
119                class=(class!(BOX, "rounded-full checked:bg-control"))
120                (attrs)
121            >
122            <span
123                class="pointer-events-none absolute inset-0 m-auto size-2 rounded-full bg-primary \
124                    opacity-0 transition-opacity peer-checked:opacity-100"
125            ></span>
126        </span>
127    })
128}
129
130/// A checkbox drawn as a track and a thumb, for a setting that takes effect as it is.
131#[component]
132pub async fn switch(cx: &Cx, #[default] mut attrs: Attributes) -> Result<impl View> {
133    Ok(view! {
134        <span class=(class!(HOLDER, attrs.remove("class"))) data-slot="switch">
135            <input
136                type="checkbox"
137                role="switch"
138                class="peer h-5 w-9 shrink-0 cursor-pointer appearance-none rounded-full \
139                    bg-foreground/15 shadow-[inset_0_1px_2px_oklch(0_0_0/0.08)] \
140                    transition-colors outline-none checked:bg-primary \
141                    focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 \
142                    focus-visible:ring-offset-background disabled:pointer-events-none"
143                (attrs)
144            >
145            <span
146                class="pointer-events-none absolute top-1/2 left-0.5 size-4 -translate-y-1/2 \
147                    rounded-full bg-white shadow-[0_1px_2px_oklch(0_0_0/0.2)] \
148                    transition-transform duration-150 peer-checked:translate-x-4"
149                data-thumb=""
150            ></span>
151        </span>
152    })
153}