@sofyan/glass 0.1.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,10 +1,12 @@
1
1
  # @sofyan/glass
2
2
 
3
3
  Liquid Glass for the web: a translucent material (CSS), React components, and a native
4
- `<dialog>` sheet that closes with a swipe down on phones.
4
+ `<dialog>` sheet that closes with a swipe down on phones. Toasts, a confirm dialog, tabs and
5
+ labelled form fields come with it, so apps don't each keep their own.
5
6
 
6
7
  - **CSS first.** Plain CSS in cascade layers. Works with Tailwind v4 (its utilities always
7
- win) and without Tailwind.
8
+ win) and without Tailwind. An unlayered build is there too, for apps whose own CSS is
9
+ unlayered or that support browsers without cascade layers.
8
10
  - **Accessible.** Fallbacks for no `backdrop-filter`, reduced transparency, more contrast
9
11
  and reduced motion. A tap outside the sheet is the single-tap alternative to the swipe
10
12
  (WCAG 2.5.1, 2.5.7); the close button stays for screen readers and keyboards.
@@ -39,6 +41,21 @@ Optional dark theme, after either of the above (see [Dark mode](#dark-mode)):
39
41
  @import '@sofyan/glass/dark.css';
40
42
  ```
41
43
 
44
+ Unlayered, when your app's own CSS is unlayered, or when it must support browsers without
45
+ cascade layers (before Safari 15.4, Chrome 99, Firefox 97), which drop layered CSS
46
+ entirely. The dark theme is optional here too:
47
+
48
+ ```css
49
+ @import '@sofyan/glass/glass.unlayered.css';
50
+ @import '@sofyan/glass/dark.unlayered.css';
51
+ ```
52
+
53
+ They hold the same rules in the same order, without the layers, so normal specificity and
54
+ source order decide: Tailwind utilities no longer win automatically. Load them before your
55
+ own CSS. There is no unlayered `tailwind.css`, since it only makes sense with layered
56
+ Tailwind; see [Using it in each kind of app](#using-it-in-each-kind-of-app) for the `lg-*`
57
+ colours.
58
+
42
59
  ### Classes
43
60
 
44
61
  | Class | What |
@@ -48,7 +65,11 @@ Optional dark theme, after either of the above (see [Dark mode](#dark-mode)):
48
65
  | `glass-menu`, `glass-menu-item`, `glass-menu-separator` | Popover menus |
49
66
  | `glass-sheet`, `glass-scrim` | A panel and backdrop you position yourself |
50
67
  | `glass-dialog` | The `<dialog>` sheet (use `<GlassSheet>`) |
68
+ | `glass-confirm` | The confirm dialog (use `<GlassConfirm>`) |
69
+ | `glass-toasts`, `glass-toast` (+ `glass-toast-danger`, `glass-toast-dismiss`) | Toasts (use `<GlassToasts>`) |
70
+ | `glass-tabs`, `glass-tab`, `glass-tab-panel` | Tabs (use `<GlassTabs>`) |
51
71
  | `glass-input`, `glass-chip` | Text field, badge |
72
+ | `glass-field` (+ `-label`, `-hint`, `-error`), `glass-select` | Labelled fields (use `<GlassInput>`, ...) |
52
73
  | `glass-dark`, `glass-clear`, `glass-tinted`, `glass-tinted-secondary`, `glass-danger` | Tones |
53
74
  | `glass-thin`, `glass-thick`, `glass-flat` | Thickness |
54
75
  | `glass-sm`, `glass-lg`, `glass-xl`, `glass-pill` | Shape |
@@ -76,12 +97,13 @@ See the top of [`src/glass.css`](src/glass.css) for the full list.
76
97
 
77
98
  #### Dark mode
78
99
 
79
- Import `dark.css` after `glass.css` or `tailwind.css`, then add `class="dark"` or
80
- `data-theme="dark"` to any element: `<html>` for the whole page, or a wrapper for one part
81
- of it. It sets `color-scheme: dark`, retunes the neutrals, the light glass and the shadows,
82
- and gives menus and sheets denser dark fills. Surfaces with a tone of their own
83
- (`glass-dark`, `glass-clear`, `glass-tinted`, ...) keep it. The reduced transparency, more
84
- contrast and no `backdrop-filter` fallbacks still apply.
100
+ Import `dark.css` after `glass.css` or `tailwind.css` (or `dark.unlayered.css` after
101
+ `glass.unlayered.css`), then add `class="dark"` or `data-theme="dark"` to any element:
102
+ `<html>` for the whole page, or a wrapper for one part of it. It sets `color-scheme: dark`,
103
+ retunes the neutrals, the light glass and the shadows, and gives menus and sheets denser
104
+ dark fills. Surfaces with a tone of their own (`glass-dark`, `glass-clear`, `glass-tinted`,
105
+ ...) keep it. The reduced transparency, more contrast and no `backdrop-filter` fallbacks
106
+ still apply.
85
107
 
86
108
  ```html
87
109
  <html class="dark">
@@ -128,7 +150,8 @@ focus returns to the opener. A tap outside closes it too.
128
150
 
129
151
  - **Phones (< 640px):** a bottom sheet with a grabber. Pull it down to close: past a quarter
130
152
  of its height, or with a quick flick. Otherwise it settles back. The header always drags;
131
- the body drags only when scrolled to the top, so scrolling the text never closes it.
153
+ the body, and any scrolling list inside it, drags only when scrolled to the top, so
154
+ scrolling never closes it. The scrolling areas under the finger are found by themselves.
132
155
  - After a swipe, `onClose` runs once the sheet has slid away. If `open` is still true a
133
156
  moment later (say you ask "discard changes?" first), the sheet slides back.
134
157
  - On phones there is no visible ✕, like a native sheet: the grabber, a pull down or a tap
@@ -136,19 +159,47 @@ focus returns to the opener. A tap outside closes it too.
136
159
  keyboard focuses it.
137
160
  - While a finger is on the sheet, the page behind stays still: the hook cancels the touch
138
161
  moves that have nothing to scroll (iOS Safari would pan the page otherwise). Mark a
139
- sideways scroller in the header or footer with `data-glass-no-drag`.
162
+ sideways scroller outside a scrolling area (in the header or footer, say) with
163
+ `data-glass-no-drag`.
140
164
  - **640px and up:** a centred card. No swipe.
141
165
  - Opening focuses the dialog itself, so a screen reader starts at the title and no focus
142
166
  ring shows on the close button.
143
167
  - `swipeToClose={false}` turns the gesture off. Add `data-glass-no-drag` to an element to
144
168
  stop a drag starting on it (e.g. a horizontal carousel).
145
169
 
146
- To add the gesture to your own sheet, use the hook:
170
+ ### Use it on your own sheet
171
+
172
+ `useSwipeToClose` gives any sheet the same gesture. All it needs is a ref on the panel
173
+ that moves:
147
174
 
148
175
  ```tsx
149
- useSwipeToClose(sheetRef, { enabled: open, scrollRef: bodyRef, onClose });
176
+ import { useRef, type ReactNode } from 'react';
177
+ import { useSwipeToClose } from '@sofyan/glass/react';
178
+
179
+ type SheetProps = { open: boolean; onClose: () => void; children: ReactNode };
180
+
181
+ function Sheet({ open, onClose, children }: SheetProps) {
182
+ const panelRef = useRef<HTMLDivElement>(null);
183
+ useSwipeToClose(panelRef, { enabled: open, onClose });
184
+
185
+ if (!open) return null;
186
+ return (
187
+ <div className="my-backdrop">
188
+ <div ref={panelRef} className="my-panel">
189
+ {children}
190
+ </div>
191
+ </div>
192
+ );
193
+ }
150
194
  ```
151
195
 
196
+ There is no need to say which part scrolls, however deep it sits. At each press the hook
197
+ looks at the elements from the finger up to the panel (the panel too): one with
198
+ `overflow-y` auto or scroll and more content than fits is a scrolling area, and while any
199
+ of them is scrolled down, a pull scrolls it instead of moving the sheet. `scrollRef` is
200
+ optional, for a scroller it cannot see that way (say, `overflow-y: hidden` scrolled by a
201
+ script). The gesture applies below 640px; pass `media` to change that.
202
+
152
203
  Pass `enabled: false` once the sheet is closed. After `onClose` the hook keeps the sheet
153
204
  off-screen and clears its inline styles when `enabled` turns false; if that has not
154
205
  happened a moment later, it slides the sheet back.
@@ -166,11 +217,173 @@ dialog.my-sheet[data-closing]::backdrop {
166
217
  }
167
218
  ```
168
219
 
220
+ When the backdrop is a parent element instead, select it with `:has()`:
221
+ `.my-backdrop:has(> [data-closing]) { background-color: transparent; }`.
222
+
223
+ ### Toasts
224
+
225
+ Mount `<GlassToasts />` once near the root, then call `toast()` from anywhere:
226
+
227
+ ```tsx
228
+ import { GlassToasts, toast, dismissToast } from '@sofyan/glass/react';
229
+
230
+ <GlassToasts dismissLabel={t('dismiss')} />; // position="bottom" to stack them at the bottom
231
+
232
+ toast('Saved');
233
+ const id = toast('Upload failed', { tone: 'danger' });
234
+ dismissToast(id); // or dismissToast() for all of them
235
+ ```
236
+
237
+ - Neutral toasts are read politely (`role="status"`) and leave after 5 seconds
238
+ (`duration` in ms; `Infinity` or `0` keeps one, with a dismiss button). The clock stops
239
+ while the pointer is on the toast or focus is inside it.
240
+ - `danger` toasts are read at once (`role="alert"`) and stay until dismissed: alerts that
241
+ vanish by themselves are easy to miss.
242
+ - Both live regions are in the page from the start, so a screen reader is already listening
243
+ when a toast arrives. Toasts never take focus.
244
+ - At most three show at once; a fourth pushes out the oldest neutral one, so news never pushes out an error. The stack clears the notch and
245
+ the home indicator, and lets taps through to the page around the toasts.
246
+
247
+ ### `GlassConfirm`
248
+
249
+ A question that has to be answered, on a native `<dialog>` with `role="alertdialog"`. Esc
250
+ cancels; a tap outside does nothing. Focus starts on the confirm button, or on Cancel when
251
+ `tone="danger"`, so a stray Enter never deletes anything. Put `autoFocus` on an element in
252
+ `children` to start there instead. Focus returns to the opener on close.
253
+
254
+ ```tsx
255
+ import { GlassConfirm, useGlassConfirm } from '@sofyan/glass/react';
256
+
257
+ <GlassConfirm
258
+ open={open}
259
+ onConfirm={deleteChat}
260
+ onCancel={() => setOpen(false)}
261
+ title="Delete this chat?"
262
+ message="It is deleted for you only."
263
+ confirmLabel={t('delete')}
264
+ cancelLabel={t('cancel')}
265
+ tone="danger"
266
+ />;
267
+
268
+ // Or await the answer, and render `dialog` once:
269
+ function LeaveButton() {
270
+ const { confirm, dialog } = useGlassConfirm();
271
+ const leave = async () => {
272
+ if (await confirm({ title: 'Leave the group?', tone: 'danger', confirmLabel: 'Leave' })) {
273
+ leaveGroup();
274
+ }
275
+ };
276
+ return (
277
+ <>
278
+ <GlassButton onClick={leave}>Leave</GlassButton>
279
+ {dialog}
280
+ </>
281
+ );
282
+ }
283
+ ```
284
+
285
+ `children` (or `details` in `confirm()`) shows between the message and the buttons, for a
286
+ list of what goes or an input; `confirmDisabled` holds the confirm button until it is
287
+ filled in. The promise resolves once: `true` on confirm, `false` on cancel, when another
288
+ `confirm()` replaces it, or when the component unmounts.
289
+
290
+ ### `GlassTabs`
291
+
292
+ ```tsx
293
+ import { GlassTabs, GlassTabList, GlassTab, GlassTabPanel } from '@sofyan/glass/react';
294
+
295
+ <GlassTabs value={tab} onChange={setTab} label="Sections">
296
+ <GlassTabList>
297
+ <GlassTab value="chats">Chats</GlassTab>
298
+ <GlassTab value="calls">Calls</GlassTab>
299
+ <GlassTab value="files" disabled>
300
+ Files
301
+ </GlassTab>
302
+ </GlassTabList>
303
+ <GlassTabPanel value="chats">…</GlassTabPanel>
304
+ <GlassTabPanel value="calls">…</GlassTabPanel>
305
+ <GlassTabPanel value="files">…</GlassTabPanel>
306
+ </GlassTabs>;
307
+ ```
308
+
309
+ The arrow keys move between tabs and select the one they land on (Left and Right swap in
310
+ right-to-left layouts; `orientation="vertical"` uses Up and Down). They wrap at the ends and
311
+ skip disabled tabs; Home and End go to the first and last. Only the selected tab is in the
312
+ Tab order. Hidden panels stay mounted, so they keep their state. The list is a glass pill
313
+ that scrolls sideways when the tabs don't fit, and is marked `data-glass-no-drag`, so inside
314
+ a sheet a swipe along it never pulls the sheet. `<GlassTabs>` renders only its children, so
315
+ the layout around the list and panels is yours.
316
+
317
+ ### Fields
318
+
319
+ `GlassInput`, `GlassTextarea` and `GlassSelect` take every prop of their element, plus:
320
+
321
+ ```tsx
322
+ import { GlassInput, GlassSelect } from '@sofyan/glass/react';
323
+
324
+ <GlassInput
325
+ label="Email"
326
+ type="email"
327
+ required
328
+ hint="We never share it."
329
+ error={errors.email} // set it once there is an error
330
+ />;
331
+
332
+ <GlassSelect label="Language" value={lang} onChange={(e) => setLang(e.target.value)}>
333
+ <option value="id">Bahasa Indonesia</option>
334
+ <option value="en">English</option>
335
+ </GlassSelect>;
336
+ ```
337
+
338
+ - The label is tied to the control; `hideLabel` hides it visually and keeps it as the name.
339
+ - `hint` and `error` are read with the control (`aria-describedby`, merged with one you pass).
340
+ The control is marked `aria-invalid` only while there is an error.
341
+ - `required` shows an asterisk that screen readers skip: the control's own `required` says it.
342
+ - Error text uses `--lg-danger-text` (4.5:1 on paper and canvas, light and dark). Text is
343
+ 16px on touch screens, so iOS doesn't zoom in.
344
+ - `className` goes on the wrapper, `inputClassName` on the control; refs reach the control.
345
+
169
346
  ### Refraction (Chromium only)
170
347
 
171
348
  Mount `<GlassFilter />` once near the root, then add `refract` (or `glass-refract`). Safari
172
349
  and Firefox keep the plain blur.
173
350
 
351
+ ## Using it in each kind of app
352
+
353
+ Pick one stylesheet, then the markup that fits your framework:
354
+
355
+ | App | Use |
356
+ | ------------------------------------------------------------ | --------------------------------------------------------- |
357
+ | Tailwind v4 (layered, as `@import 'tailwindcss'` loads it) | `tailwind.css` (+ `dark.css`) |
358
+ | No Tailwind | `glass.css` (+ `dark.css`) |
359
+ | Own CSS unlayered, or must support browsers without `@layer` | `glass.unlayered.css` (+ `dark.unlayered.css`) |
360
+ | React 18 or 19 | `@sofyan/glass/react` components, on top of that CSS file |
361
+ | Vue, Blade/Livewire or plain HTML | The classes directly, on top of that CSS file |
362
+
363
+ With the unlayered build, the `lg-*` Tailwind colours (`bg-lg-paper`, ...) come from
364
+ `tailwind.css`, which you don't import. Copy its block into your own `@theme`:
365
+
366
+ ```css
367
+ @theme inline {
368
+ --color-lg-canvas: rgb(var(--lg-c-canvas));
369
+ --color-lg-paper: rgb(var(--lg-c-paper));
370
+ --color-lg-raised: rgb(var(--lg-c-raised));
371
+ --color-lg-ink: rgb(var(--lg-c-ink));
372
+ --color-lg-ink-2: rgb(var(--lg-c-ink-2));
373
+ --color-lg-ink-3: rgb(var(--lg-c-ink-3));
374
+ --color-lg-line: rgb(var(--lg-c-line));
375
+ --color-lg-accent: var(--lg-accent, var(--color-primary, #0a84ff));
376
+ }
377
+ ```
378
+
379
+ Without React, use the [classes](#classes) in your templates:
380
+
381
+ ```html
382
+ <button type="button" class="glass-btn">Share</button>
383
+ <button type="button" class="glass-btn glass-tinted glass-btn-lg">Buy tickets</button>
384
+ <nav class="glass glass-dark glass-pill">…</nav>
385
+ ```
386
+
174
387
  ## Develop
175
388
 
176
389
  ```sh
package/dist/dark.css CHANGED
@@ -9,8 +9,8 @@
9
9
  * Then add class="dark" or data-theme="dark" to any element: <html> for the whole page,
10
10
  * or a wrapper for one part of it. Everything inside follows.
11
11
  *
12
- * It only retunes tokens (neutrals, the light glass, shadows) and the denser fills of
13
- * menus and sheets. Tones you set yourself (glass-dark, glass-clear, glass-tinted, ...)
12
+ * It only retunes tokens (neutrals, danger text, the light glass, shadows) and the denser
13
+ * fills of menus and sheets. Tones you set yourself (glass-dark, glass-clear, glass-tinted, ...)
14
14
  * stay as they are.
15
15
  */
16
16
 
@@ -32,6 +32,9 @@
32
32
  --lg-c-ink-3: 142 142 147;
33
33
  --lg-c-line: 255 255 255;
34
34
 
35
+ /* 4.5:1 or more on the dark paper, raised and canvas neutrals. */
36
+ --lg-danger-text: #ff6961;
37
+
35
38
  --lg-light-fill: rgba(30, 30, 32, 0.66);
36
39
  --lg-light-fill-hover: rgba(44, 44, 46, 0.76);
37
40
  --lg-light-text: #f5f5f7;
@@ -0,0 +1,102 @@
1
+ /*
2
+ * @sofyan/glass: opt-in dark theme, unlayered build
3
+ * ---------------------------------------------------------------------------
4
+ * Generated from src/dark.css by the build: the same rules in the same order, with the
5
+ * cascade layers taken out. Edit the source, not this file.
6
+ *
7
+ * The dark theme for glass.unlayered.css. Import it after that file:
8
+ *
9
+ * @import '@sofyan/glass/glass.unlayered.css';
10
+ * @import '@sofyan/glass/dark.unlayered.css';
11
+ *
12
+ * Then add class="dark" or data-theme="dark" to any element: <html> for the whole page,
13
+ * or a wrapper for one part of it. Everything inside follows.
14
+ *
15
+ * It only retunes tokens (neutrals, danger text, the light glass, shadows) and the denser
16
+ * fills of menus and sheets. Tones you set yourself (glass-dark, glass-clear, glass-tinted, ...)
17
+ * stay as they are.
18
+ *
19
+ * Cascade: no cascade layers here, so normal specificity and source order decide.
20
+ * Tailwind utilities no longer win automatically: an unlayered utility overrides a glass
21
+ * rule only when it is more specific, or as specific and loaded later, and a layered one
22
+ * never does. So load the glass files before your own CSS.
23
+ */
24
+
25
+ /* ── Tokens ───────────────────────────────────────────────────────────── */
26
+ /* :is(), not :where(): glass.css sets these tokens on :root, and on <html class="dark">
27
+ a zero-specificity rule would lose to it. :is() ties with :root, so the later rule
28
+ (this file) wins. */
29
+ :is(.dark, [data-theme='dark']) {
30
+ color-scheme: dark;
31
+
32
+ --lg-c-canvas: 11 11 12;
33
+ --lg-c-paper: 28 28 30;
34
+ --lg-c-raised: 44 44 46;
35
+ --lg-c-ink: 245 245 247;
36
+ --lg-c-ink-2: 152 152 157;
37
+ --lg-c-ink-3: 142 142 147;
38
+ --lg-c-line: 255 255 255;
39
+
40
+ /* 4.5:1 or more on the dark paper, raised and canvas neutrals. */
41
+ --lg-danger-text: #ff6961;
42
+
43
+ --lg-light-fill: rgba(30, 30, 32, 0.66);
44
+ --lg-light-fill-hover: rgba(44, 44, 46, 0.76);
45
+ --lg-light-text: #f5f5f7;
46
+ --lg-light-rim: rgba(255, 255, 255, 0.14);
47
+ --lg-light-edge: rgba(255, 255, 255, 0.08);
48
+
49
+ --lg-sheen: none;
50
+
51
+ --lg-shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.3);
52
+ --lg-shadow-md: 0 8px 32px rgba(0, 0, 0, 0.4);
53
+ --lg-shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.5);
54
+ }
55
+
56
+ /* The same fallbacks as glass.css, for the dark fills. They come after the rule above so
57
+ they win over it. */
58
+ @supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
59
+ :is(.dark, [data-theme='dark']) {
60
+ --lg-light-fill: rgba(30, 30, 32, 0.92);
61
+ --lg-light-fill-hover: rgba(44, 44, 46, 0.96);
62
+ }
63
+ }
64
+
65
+ @media (prefers-reduced-transparency: reduce) {
66
+ :is(.dark, [data-theme='dark']) {
67
+ --lg-light-fill: rgba(30, 30, 32, 0.97);
68
+ --lg-light-fill-hover: #2c2c2e;
69
+ }
70
+ }
71
+
72
+ @media (prefers-contrast: more) {
73
+ :is(.dark, [data-theme='dark']) {
74
+ --lg-light-edge: rgba(255, 255, 255, 0.45);
75
+ }
76
+ }
77
+
78
+ /* ── Composites ───────────────────────────────────────────────────────── */
79
+ /* Denser fills for floating overlays. The :not() skips surfaces with a tone of their own,
80
+ which glass.css lets win by source order; :where() keeps it out of the specificity. */
81
+ :where(.dark, [data-theme='dark'])
82
+ .glass-menu:where(
83
+ :not(.glass-dark, .glass-clear, .glass-tinted, .glass-tinted-secondary, .glass-danger)
84
+ ) {
85
+ --lg-fill: rgba(36, 36, 40, 0.88);
86
+ }
87
+
88
+ :where(.dark, [data-theme='dark'])
89
+ .glass-sheet:where(
90
+ :not(.glass-dark, .glass-clear, .glass-tinted, .glass-tinted-secondary, .glass-danger)
91
+ ) {
92
+ --lg-fill: rgba(30, 30, 34, 0.92);
93
+ }
94
+
95
+ :where(.dark, [data-theme='dark']) .glass-menu-item:hover,
96
+ :where(.dark, [data-theme='dark']) .glass-menu-item:focus-visible {
97
+ background-color: rgba(255, 255, 255, 0.1);
98
+ }
99
+
100
+ :where(.dark, [data-theme='dark']) .glass-menu-separator {
101
+ background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.14), transparent);
102
+ }