@sofyan/glass 0.1.5 → 0.2.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 +103 -12
- package/dist/dark.unlayered.css +99 -0
- package/dist/glass.css +13 -0
- package/dist/glass.unlayered.css +727 -0
- package/dist/react/index.d.ts +26 -6
- package/dist/react/index.js +42 -8
- package/dist/react/index.js.map +1 -1
- package/package.json +5 -2
package/README.md
CHANGED
|
@@ -4,10 +4,11 @@ Liquid Glass for the web: a translucent material (CSS), React components, and a
|
|
|
4
4
|
`<dialog>` sheet that closes with a swipe down on phones.
|
|
5
5
|
|
|
6
6
|
- **CSS first.** Plain CSS in cascade layers. Works with Tailwind v4 (its utilities always
|
|
7
|
-
win) and without Tailwind.
|
|
7
|
+
win) and without Tailwind. An unlayered build is there too, for apps whose own CSS is
|
|
8
|
+
unlayered or that support browsers without cascade layers.
|
|
8
9
|
- **Accessible.** Fallbacks for no `backdrop-filter`, reduced transparency, more contrast
|
|
9
|
-
and reduced motion.
|
|
10
|
-
(WCAG 2.5.1, 2.5.7).
|
|
10
|
+
and reduced motion. A tap outside the sheet is the single-tap alternative to the swipe
|
|
11
|
+
(WCAG 2.5.1, 2.5.7); the close button stays for screen readers and keyboards.
|
|
11
12
|
- **Safari first.** No effect depends on Chromium-only features; refraction is an opt-in
|
|
12
13
|
extra that only Chromium draws.
|
|
13
14
|
|
|
@@ -39,6 +40,21 @@ Optional dark theme, after either of the above (see [Dark mode](#dark-mode)):
|
|
|
39
40
|
@import '@sofyan/glass/dark.css';
|
|
40
41
|
```
|
|
41
42
|
|
|
43
|
+
Unlayered, when your app's own CSS is unlayered, or when it must support browsers without
|
|
44
|
+
cascade layers (before Safari 15.4, Chrome 99, Firefox 97), which drop layered CSS
|
|
45
|
+
entirely. The dark theme is optional here too:
|
|
46
|
+
|
|
47
|
+
```css
|
|
48
|
+
@import '@sofyan/glass/glass.unlayered.css';
|
|
49
|
+
@import '@sofyan/glass/dark.unlayered.css';
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
They hold the same rules in the same order, without the layers, so normal specificity and
|
|
53
|
+
source order decide: Tailwind utilities no longer win automatically. Load them before your
|
|
54
|
+
own CSS. There is no unlayered `tailwind.css`, since it only makes sense with layered
|
|
55
|
+
Tailwind; see [Using it in each kind of app](#using-it-in-each-kind-of-app) for the `lg-*`
|
|
56
|
+
colours.
|
|
57
|
+
|
|
42
58
|
### Classes
|
|
43
59
|
|
|
44
60
|
| Class | What |
|
|
@@ -76,12 +92,13 @@ See the top of [`src/glass.css`](src/glass.css) for the full list.
|
|
|
76
92
|
|
|
77
93
|
#### Dark mode
|
|
78
94
|
|
|
79
|
-
Import `dark.css` after `glass.css` or `tailwind.css
|
|
80
|
-
`data-theme="dark"` to any element:
|
|
81
|
-
of it. It sets `color-scheme: dark`,
|
|
82
|
-
|
|
83
|
-
(`glass-dark`, `glass-clear`, `glass-tinted`,
|
|
84
|
-
contrast and no `backdrop-filter` fallbacks
|
|
95
|
+
Import `dark.css` after `glass.css` or `tailwind.css` (or `dark.unlayered.css` after
|
|
96
|
+
`glass.unlayered.css`), then add `class="dark"` or `data-theme="dark"` to any element:
|
|
97
|
+
`<html>` for the whole page, or a wrapper for one part of it. It sets `color-scheme: dark`,
|
|
98
|
+
retunes the neutrals, the light glass and the shadows, and gives menus and sheets denser
|
|
99
|
+
dark fills. Surfaces with a tone of their own (`glass-dark`, `glass-clear`, `glass-tinted`,
|
|
100
|
+
...) keep it. The reduced transparency, more contrast and no `backdrop-filter` fallbacks
|
|
101
|
+
still apply.
|
|
85
102
|
|
|
86
103
|
```html
|
|
87
104
|
<html class="dark">
|
|
@@ -128,21 +145,56 @@ focus returns to the opener. A tap outside closes it too.
|
|
|
128
145
|
|
|
129
146
|
- **Phones (< 640px):** a bottom sheet with a grabber. Pull it down to close: past a quarter
|
|
130
147
|
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
|
|
148
|
+
the body, and any scrolling list inside it, drags only when scrolled to the top, so
|
|
149
|
+
scrolling never closes it. The scrolling areas under the finger are found by themselves.
|
|
132
150
|
- After a swipe, `onClose` runs once the sheet has slid away. If `open` is still true a
|
|
133
151
|
moment later (say you ask "discard changes?" first), the sheet slides back.
|
|
152
|
+
- On phones there is no visible ✕, like a native sheet: the grabber, a pull down or a tap
|
|
153
|
+
outside closes it. The close button is still there for screen readers and shows when the
|
|
154
|
+
keyboard focuses it.
|
|
155
|
+
- While a finger is on the sheet, the page behind stays still: the hook cancels the touch
|
|
156
|
+
moves that have nothing to scroll (iOS Safari would pan the page otherwise). Mark a
|
|
157
|
+
sideways scroller outside a scrolling area (in the header or footer, say) with
|
|
158
|
+
`data-glass-no-drag`.
|
|
134
159
|
- **640px and up:** a centred card. No swipe.
|
|
135
160
|
- Opening focuses the dialog itself, so a screen reader starts at the title and no focus
|
|
136
161
|
ring shows on the close button.
|
|
137
162
|
- `swipeToClose={false}` turns the gesture off. Add `data-glass-no-drag` to an element to
|
|
138
163
|
stop a drag starting on it (e.g. a horizontal carousel).
|
|
139
164
|
|
|
140
|
-
|
|
165
|
+
### Use it on your own sheet
|
|
166
|
+
|
|
167
|
+
`useSwipeToClose` gives any sheet the same gesture. All it needs is a ref on the panel
|
|
168
|
+
that moves:
|
|
141
169
|
|
|
142
170
|
```tsx
|
|
143
|
-
|
|
171
|
+
import { useRef, type ReactNode } from 'react';
|
|
172
|
+
import { useSwipeToClose } from '@sofyan/glass/react';
|
|
173
|
+
|
|
174
|
+
type SheetProps = { open: boolean; onClose: () => void; children: ReactNode };
|
|
175
|
+
|
|
176
|
+
function Sheet({ open, onClose, children }: SheetProps) {
|
|
177
|
+
const panelRef = useRef<HTMLDivElement>(null);
|
|
178
|
+
useSwipeToClose(panelRef, { enabled: open, onClose });
|
|
179
|
+
|
|
180
|
+
if (!open) return null;
|
|
181
|
+
return (
|
|
182
|
+
<div className="my-backdrop">
|
|
183
|
+
<div ref={panelRef} className="my-panel">
|
|
184
|
+
{children}
|
|
185
|
+
</div>
|
|
186
|
+
</div>
|
|
187
|
+
);
|
|
188
|
+
}
|
|
144
189
|
```
|
|
145
190
|
|
|
191
|
+
There is no need to say which part scrolls, however deep it sits. At each press the hook
|
|
192
|
+
looks at the elements from the finger up to the panel (the panel too): one with
|
|
193
|
+
`overflow-y` auto or scroll and more content than fits is a scrolling area, and while any
|
|
194
|
+
of them is scrolled down, a pull scrolls it instead of moving the sheet. `scrollRef` is
|
|
195
|
+
optional, for a scroller it cannot see that way (say, `overflow-y: hidden` scrolled by a
|
|
196
|
+
script). The gesture applies below 640px; pass `media` to change that.
|
|
197
|
+
|
|
146
198
|
Pass `enabled: false` once the sheet is closed. After `onClose` the hook keeps the sheet
|
|
147
199
|
off-screen and clears its inline styles when `enabled` turns false; if that has not
|
|
148
200
|
happened a moment later, it slides the sheet back.
|
|
@@ -160,11 +212,50 @@ dialog.my-sheet[data-closing]::backdrop {
|
|
|
160
212
|
}
|
|
161
213
|
```
|
|
162
214
|
|
|
215
|
+
When the backdrop is a parent element instead, select it with `:has()`:
|
|
216
|
+
`.my-backdrop:has(> [data-closing]) { background-color: transparent; }`.
|
|
217
|
+
|
|
163
218
|
### Refraction (Chromium only)
|
|
164
219
|
|
|
165
220
|
Mount `<GlassFilter />` once near the root, then add `refract` (or `glass-refract`). Safari
|
|
166
221
|
and Firefox keep the plain blur.
|
|
167
222
|
|
|
223
|
+
## Using it in each kind of app
|
|
224
|
+
|
|
225
|
+
Pick one stylesheet, then the markup that fits your framework:
|
|
226
|
+
|
|
227
|
+
| App | Use |
|
|
228
|
+
| ------------------------------------------------------------ | --------------------------------------------------------- |
|
|
229
|
+
| Tailwind v4 (layered, as `@import 'tailwindcss'` loads it) | `tailwind.css` (+ `dark.css`) |
|
|
230
|
+
| No Tailwind | `glass.css` (+ `dark.css`) |
|
|
231
|
+
| Own CSS unlayered, or must support browsers without `@layer` | `glass.unlayered.css` (+ `dark.unlayered.css`) |
|
|
232
|
+
| React 18 or 19 | `@sofyan/glass/react` components, on top of that CSS file |
|
|
233
|
+
| Vue, Blade/Livewire or plain HTML | The classes directly, on top of that CSS file |
|
|
234
|
+
|
|
235
|
+
With the unlayered build, the `lg-*` Tailwind colours (`bg-lg-paper`, ...) come from
|
|
236
|
+
`tailwind.css`, which you don't import. Copy its block into your own `@theme`:
|
|
237
|
+
|
|
238
|
+
```css
|
|
239
|
+
@theme inline {
|
|
240
|
+
--color-lg-canvas: rgb(var(--lg-c-canvas));
|
|
241
|
+
--color-lg-paper: rgb(var(--lg-c-paper));
|
|
242
|
+
--color-lg-raised: rgb(var(--lg-c-raised));
|
|
243
|
+
--color-lg-ink: rgb(var(--lg-c-ink));
|
|
244
|
+
--color-lg-ink-2: rgb(var(--lg-c-ink-2));
|
|
245
|
+
--color-lg-ink-3: rgb(var(--lg-c-ink-3));
|
|
246
|
+
--color-lg-line: rgb(var(--lg-c-line));
|
|
247
|
+
--color-lg-accent: var(--lg-accent, var(--color-primary, #0a84ff));
|
|
248
|
+
}
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
Without React, use the [classes](#classes) in your templates:
|
|
252
|
+
|
|
253
|
+
```html
|
|
254
|
+
<button type="button" class="glass-btn">Share</button>
|
|
255
|
+
<button type="button" class="glass-btn glass-tinted glass-btn-lg">Buy tickets</button>
|
|
256
|
+
<nav class="glass glass-dark glass-pill">…</nav>
|
|
257
|
+
```
|
|
258
|
+
|
|
168
259
|
## Develop
|
|
169
260
|
|
|
170
261
|
```sh
|
|
@@ -0,0 +1,99 @@
|
|
|
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, the light glass, shadows) and the denser fills of
|
|
16
|
+
* 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
|
+
--lg-light-fill: rgba(30, 30, 32, 0.66);
|
|
41
|
+
--lg-light-fill-hover: rgba(44, 44, 46, 0.76);
|
|
42
|
+
--lg-light-text: #f5f5f7;
|
|
43
|
+
--lg-light-rim: rgba(255, 255, 255, 0.14);
|
|
44
|
+
--lg-light-edge: rgba(255, 255, 255, 0.08);
|
|
45
|
+
|
|
46
|
+
--lg-sheen: none;
|
|
47
|
+
|
|
48
|
+
--lg-shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.3);
|
|
49
|
+
--lg-shadow-md: 0 8px 32px rgba(0, 0, 0, 0.4);
|
|
50
|
+
--lg-shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.5);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/* The same fallbacks as glass.css, for the dark fills. They come after the rule above so
|
|
54
|
+
they win over it. */
|
|
55
|
+
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
|
|
56
|
+
:is(.dark, [data-theme='dark']) {
|
|
57
|
+
--lg-light-fill: rgba(30, 30, 32, 0.92);
|
|
58
|
+
--lg-light-fill-hover: rgba(44, 44, 46, 0.96);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
@media (prefers-reduced-transparency: reduce) {
|
|
63
|
+
:is(.dark, [data-theme='dark']) {
|
|
64
|
+
--lg-light-fill: rgba(30, 30, 32, 0.97);
|
|
65
|
+
--lg-light-fill-hover: #2c2c2e;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@media (prefers-contrast: more) {
|
|
70
|
+
:is(.dark, [data-theme='dark']) {
|
|
71
|
+
--lg-light-edge: rgba(255, 255, 255, 0.45);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/* ── Composites ───────────────────────────────────────────────────────── */
|
|
76
|
+
/* Denser fills for floating overlays. The :not() skips surfaces with a tone of their own,
|
|
77
|
+
which glass.css lets win by source order; :where() keeps it out of the specificity. */
|
|
78
|
+
:where(.dark, [data-theme='dark'])
|
|
79
|
+
.glass-menu:where(
|
|
80
|
+
:not(.glass-dark, .glass-clear, .glass-tinted, .glass-tinted-secondary, .glass-danger)
|
|
81
|
+
) {
|
|
82
|
+
--lg-fill: rgba(36, 36, 40, 0.88);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
:where(.dark, [data-theme='dark'])
|
|
86
|
+
.glass-sheet:where(
|
|
87
|
+
:not(.glass-dark, .glass-clear, .glass-tinted, .glass-tinted-secondary, .glass-danger)
|
|
88
|
+
) {
|
|
89
|
+
--lg-fill: rgba(30, 30, 34, 0.92);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
:where(.dark, [data-theme='dark']) .glass-menu-item:hover,
|
|
93
|
+
:where(.dark, [data-theme='dark']) .glass-menu-item:focus-visible {
|
|
94
|
+
background-color: rgba(255, 255, 255, 0.1);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
:where(.dark, [data-theme='dark']) .glass-menu-separator {
|
|
98
|
+
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.14), transparent);
|
|
99
|
+
}
|
package/dist/glass.css
CHANGED
|
@@ -642,6 +642,19 @@
|
|
|
642
642
|
padding-top: 0.5rem;
|
|
643
643
|
}
|
|
644
644
|
|
|
645
|
+
/* Like a native sheet: no ✕ on phones. A pull down or a tap outside closes it. The
|
|
646
|
+
button stays for screen readers, and shows when the keyboard focuses it. */
|
|
647
|
+
.glass-dialog-close:not(:focus-visible) {
|
|
648
|
+
position: absolute;
|
|
649
|
+
width: 1px;
|
|
650
|
+
height: 1px;
|
|
651
|
+
margin: -1px;
|
|
652
|
+
padding: 0;
|
|
653
|
+
overflow: hidden;
|
|
654
|
+
clip-path: inset(50%);
|
|
655
|
+
white-space: nowrap;
|
|
656
|
+
}
|
|
657
|
+
|
|
645
658
|
/* Clear the home indicator under the last line. */
|
|
646
659
|
.glass-dialog-body:last-child,
|
|
647
660
|
.glass-dialog-footer {
|