@glasshome/ui 1.10.0 → 1.10.1
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/package.json +5 -2
- package/scripts/check-pack.ts +32 -0
- package/src/index.ts +111 -0
- package/src/solid/accordion.tsx +60 -0
- package/src/solid/alert-dialog.tsx +148 -0
- package/src/solid/alert.tsx +118 -0
- package/src/solid/area-picker.tsx +225 -0
- package/src/solid/aspect-ratio.tsx +21 -0
- package/src/solid/avatar.tsx +41 -0
- package/src/solid/badge.tsx +74 -0
- package/src/solid/bottom-sheet/arbitration.ts +40 -0
- package/src/solid/bottom-sheet/bottom-sheet.tsx +457 -0
- package/src/solid/bottom-sheet/constants.ts +8 -0
- package/src/solid/bottom-sheet/drag-controller.ts +221 -0
- package/src/solid/bottom-sheet/focus-trap.ts +56 -0
- package/src/solid/bottom-sheet/index.ts +14 -0
- package/src/solid/bottom-sheet/keyboard.ts +35 -0
- package/src/solid/bottom-sheet/scroll-lock.ts +64 -0
- package/src/solid/bottom-sheet/state-machine.ts +40 -0
- package/src/solid/bottom-sheet/velocity.ts +42 -0
- package/src/solid/breadcrumb.tsx +105 -0
- package/src/solid/button-group.tsx +66 -0
- package/src/solid/button.tsx +20 -0
- package/src/solid/card.tsx +101 -0
- package/src/solid/carousel.tsx +302 -0
- package/src/solid/charts.tsx +338 -0
- package/src/solid/checkbox.tsx +70 -0
- package/src/solid/collapsible.tsx +38 -0
- package/src/solid/color-slider.tsx +98 -0
- package/src/solid/color-wheel.tsx +77 -0
- package/src/solid/context-menu.tsx +109 -0
- package/src/solid/copy-button.tsx +46 -0
- package/src/solid/count-pill.tsx +24 -0
- package/src/solid/data-table.tsx +248 -0
- package/src/solid/dialog-parts.tsx +242 -0
- package/src/solid/dialog.tsx +121 -0
- package/src/solid/dock.tsx +186 -0
- package/src/solid/dropdown-menu.tsx +130 -0
- package/src/solid/empty.tsx +99 -0
- package/src/solid/entity-data.ts +78 -0
- package/src/solid/entity-selector.tsx +697 -0
- package/src/solid/field.tsx +253 -0
- package/src/solid/form.tsx +231 -0
- package/src/solid/hero-action.tsx +117 -0
- package/src/solid/hover-card.tsx +26 -0
- package/src/solid/icon-picker.tsx +432 -0
- package/src/solid/icon.tsx +20 -0
- package/src/solid/image-picker.tsx +380 -0
- package/src/solid/index.ts +417 -0
- package/src/solid/input-group.tsx +170 -0
- package/src/solid/input-otp.tsx +175 -0
- package/src/solid/input.tsx +12 -0
- package/src/solid/item.tsx +179 -0
- package/src/solid/kbd.tsx +30 -0
- package/src/solid/label.tsx +19 -0
- package/src/solid/list-reorder.ts +148 -0
- package/src/solid/logo.tsx +79 -0
- package/src/solid/media-copy.ts +37 -0
- package/src/solid/media-store.ts +110 -0
- package/src/solid/media-tile.tsx +100 -0
- package/src/solid/menu-parts.tsx +248 -0
- package/src/solid/number-field.tsx +72 -0
- package/src/solid/option-card.tsx +218 -0
- package/src/solid/ornament.tsx +53 -0
- package/src/solid/overlay.tsx +16 -0
- package/src/solid/page-header.tsx +102 -0
- package/src/solid/pagination.tsx +111 -0
- package/src/solid/password-input.tsx +69 -0
- package/src/solid/picker-row.tsx +102 -0
- package/src/solid/picker-search.tsx +78 -0
- package/src/solid/popover.tsx +103 -0
- package/src/solid/progress.tsx +31 -0
- package/src/solid/radio-group.tsx +60 -0
- package/src/solid/resizable.tsx +149 -0
- package/src/solid/responsive-dialog.tsx +180 -0
- package/src/solid/schema-form.tsx +830 -0
- package/src/solid/scope-indicator.tsx +13 -0
- package/src/solid/scroll-area.tsx +27 -0
- package/src/solid/section-card.tsx +358 -0
- package/src/solid/select.tsx +139 -0
- package/src/solid/separator.tsx +35 -0
- package/src/solid/settings-row.tsx +159 -0
- package/src/solid/sheet.tsx +157 -0
- package/src/solid/skeleton.tsx +15 -0
- package/src/solid/slider.tsx +148 -0
- package/src/solid/sliding-indicator.tsx +294 -0
- package/src/solid/sonner.tsx +219 -0
- package/src/solid/spinner.tsx +20 -0
- package/src/solid/step-indicator.tsx +30 -0
- package/src/solid/switch.tsx +62 -0
- package/src/solid/table.tsx +98 -0
- package/src/solid/tabs.tsx +86 -0
- package/src/solid/textarea.tsx +16 -0
- package/src/solid/toggle-group.tsx +102 -0
- package/src/solid/toggle.tsx +57 -0
- package/src/solid/tooltip.tsx +38 -0
- package/src/solid/widget-card.tsx +117 -0
- package/src/solid/widget-identity.tsx +135 -0
- package/src/solid/widget-trust-badge.tsx +37 -0
- package/src/tokens/hex.ts +50 -0
- package/src/tokens/index.ts +8 -0
- package/src/tokens/presets.ts +192 -0
- package/src/tokens/theme-colors.ts +147 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type ComponentProps,
|
|
3
|
+
createEffect,
|
|
4
|
+
createSignal,
|
|
5
|
+
type JSX,
|
|
6
|
+
on,
|
|
7
|
+
onCleanup,
|
|
8
|
+
onMount,
|
|
9
|
+
Show,
|
|
10
|
+
splitProps,
|
|
11
|
+
} from "solid-js";
|
|
12
|
+
import { cn } from "../lib/utils.js";
|
|
13
|
+
|
|
14
|
+
function readTranslate(el: HTMLElement): [number, number] {
|
|
15
|
+
const t = getComputedStyle(el).translate;
|
|
16
|
+
if (!t || t === "none") return [0, 0];
|
|
17
|
+
const [x = "0", y = "0"] = t.split(" ");
|
|
18
|
+
return [Number.parseFloat(x) || 0, Number.parseFloat(y) || 0];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The sliding "moving background": a tinted indicator that measures the active item
|
|
23
|
+
* among its children and animates its position/size behind it. Reusable across
|
|
24
|
+
* any component with a selected item among siblings (Dock, Tabs, ToggleGroup,
|
|
25
|
+
* segmented controls…). Presentational + controlled — pass the active index.
|
|
26
|
+
*
|
|
27
|
+
* Wrap the items directly; by default it measures each direct child (excluding
|
|
28
|
+
* its own indicator). Give it `indicatorClass` for the look and `orientation` for the axis.
|
|
29
|
+
*/
|
|
30
|
+
interface SlidingIndicatorProps extends ComponentProps<"div"> {
|
|
31
|
+
/** Index-based active item among the measured children, or null to hide.
|
|
32
|
+
* Use this when you own the selection (e.g. Dock). */
|
|
33
|
+
active?: number | null;
|
|
34
|
+
/** Attribute-based active item: a selector for the currently-active child
|
|
35
|
+
* (e.g. "[data-selected]"). Use this with libraries that mark the active item
|
|
36
|
+
* in the DOM (Kobalte Tabs/ToggleGroup); a MutationObserver re-slides on change. */
|
|
37
|
+
activeSelector?: string;
|
|
38
|
+
orientation?: "horizontal" | "vertical";
|
|
39
|
+
/** Extra classes for the sliding indicator — radius and any per-surface `--glass-*`
|
|
40
|
+
* knob overrides. The trusted `.glass` material is ALWAYS applied underneath, so
|
|
41
|
+
* this can't flatten the indicator; it only tunes it. Default radius: `rounded-lg`. */
|
|
42
|
+
indicatorClass?: string;
|
|
43
|
+
/** Glass tone that drives the `.glass` material (`--glass-tone`). Defaults to
|
|
44
|
+
* `var(--primary)` — the one trusted look. Pass another CSS color to re-tint. */
|
|
45
|
+
indicatorTone?: string;
|
|
46
|
+
/** Selector for the measurable items. Default: direct children (minus the indicator). */
|
|
47
|
+
itemSelector?: string;
|
|
48
|
+
children: JSX.Element;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
type Pos = { offset: number; size: number; cross: number };
|
|
52
|
+
|
|
53
|
+
// One duration for both the slide (transform transition) and the scale dip, so
|
|
54
|
+
// they always overlap exactly. Shorter = snappier.
|
|
55
|
+
const SLIDE_MS = 220;
|
|
56
|
+
|
|
57
|
+
// Squash-and-stretch: at mid-flight the indicator stretches ALONG the travel axis
|
|
58
|
+
// and pinches perpendicular, which reads as momentum/speed. Both deforms are a
|
|
59
|
+
// fixed PIXEL budget on their own axis (not a percentage), so a 34px dropdown row
|
|
60
|
+
// and a 300px-wide one deform by the same visible pixels — a big element no longer
|
|
61
|
+
// animates more just because it's big. Each axis measures its own size (`size` =
|
|
62
|
+
// travel, `cross` = perpendicular) to turn the pixel budget into a scale factor.
|
|
63
|
+
// DEFORM_MAX caps the fraction so a very thin item can't collapse.
|
|
64
|
+
const STRETCH_PX = 6; // grow along travel
|
|
65
|
+
const SQUASH_PX = 6; // pinch perpendicular
|
|
66
|
+
const DEFORM_MAX = 0.14;
|
|
67
|
+
|
|
68
|
+
export function SlidingIndicator(props: SlidingIndicatorProps) {
|
|
69
|
+
const [local, rest] = splitProps(props, [
|
|
70
|
+
"active",
|
|
71
|
+
"activeSelector",
|
|
72
|
+
"orientation",
|
|
73
|
+
"indicatorClass",
|
|
74
|
+
"indicatorTone",
|
|
75
|
+
"itemSelector",
|
|
76
|
+
"class",
|
|
77
|
+
"children",
|
|
78
|
+
"ref",
|
|
79
|
+
]);
|
|
80
|
+
const horizontal = () => (local.orientation ?? "horizontal") === "horizontal";
|
|
81
|
+
let containerRef: HTMLDivElement | undefined;
|
|
82
|
+
// A caller's ref goes through here, not through the spread: `spread()` would
|
|
83
|
+
// hand its own ref to the element and leave containerRef unset, so nothing
|
|
84
|
+
// would ever measure.
|
|
85
|
+
const setContainerRef = (el: HTMLDivElement) => {
|
|
86
|
+
containerRef = el;
|
|
87
|
+
const forwarded = local.ref;
|
|
88
|
+
if (typeof forwarded === "function") forwarded(el);
|
|
89
|
+
};
|
|
90
|
+
const [pos, setPos] = createSignal<Pos | null>(null);
|
|
91
|
+
|
|
92
|
+
// On a real move, play a symmetric squash-and-stretch (1 → peak → 1) over the
|
|
93
|
+
// SAME duration as the slide, so the deform and the travel fully overlap: fastest
|
|
94
|
+
// and most stretched at mid-flight, back to a square at both ends (accelerate in,
|
|
95
|
+
// settle out). At the peak the indicator STRETCHES along the travel axis and
|
|
96
|
+
// PINCHES perpendicular (the reciprocal, so volume is preserved) — the classic
|
|
97
|
+
// speed cue. WAAPI on `scale` composes with the translate transition; it releases
|
|
98
|
+
// back to the base (1) on its own, so nothing can get stuck deformed.
|
|
99
|
+
let indicatorEl: HTMLDivElement | undefined;
|
|
100
|
+
createEffect(
|
|
101
|
+
on(
|
|
102
|
+
pos,
|
|
103
|
+
(p, prev) => {
|
|
104
|
+
// pos is a fresh object on every re-measure (the Select's MutationObserver
|
|
105
|
+
// fires often), so only animate on an actual travel.
|
|
106
|
+
if (!p || !prev || Math.abs(p.offset - prev.offset) < 1 || !indicatorEl) return;
|
|
107
|
+
const stretch = 1 + Math.min(DEFORM_MAX, STRETCH_PX / (p.size || 1));
|
|
108
|
+
const squash = 1 - Math.min(DEFORM_MAX, SQUASH_PX / (p.cross || 1));
|
|
109
|
+
const peak = horizontal() ? `${stretch} ${squash}` : `${squash} ${stretch}`;
|
|
110
|
+
indicatorEl.animate([{ scale: "1 1" }, { scale: peak }, { scale: "1 1" }], {
|
|
111
|
+
duration: SLIDE_MS,
|
|
112
|
+
easing: "ease-in-out",
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
{ defer: true },
|
|
116
|
+
),
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
// A zero-size pass is not a resting state, and nothing is guaranteed to fire
|
|
120
|
+
// when it ends: an ancestor dialog's own open animation neither resizes this
|
|
121
|
+
// container nor bubbles its animationend down here.
|
|
122
|
+
const RETRY_FRAMES = 90;
|
|
123
|
+
let retries = 0;
|
|
124
|
+
let retryFrame: number | undefined;
|
|
125
|
+
const remeasureNextFrame = () => {
|
|
126
|
+
if (disposed || retryFrame !== undefined || retries >= RETRY_FRAMES) return;
|
|
127
|
+
retries += 1;
|
|
128
|
+
retryFrame = requestAnimationFrame(() => {
|
|
129
|
+
retryFrame = undefined;
|
|
130
|
+
if (!disposed) measure();
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const measure = () => {
|
|
135
|
+
if (!containerRef) return;
|
|
136
|
+
syncObservedItems();
|
|
137
|
+
let el: HTMLElement | null | undefined;
|
|
138
|
+
if (local.activeSelector) {
|
|
139
|
+
el = containerRef.querySelector<HTMLElement>(local.activeSelector);
|
|
140
|
+
} else if (local.active != null && local.active >= 0) {
|
|
141
|
+
const sel = local.itemSelector ?? ":scope > :not([data-sliding-indicator])";
|
|
142
|
+
el = containerRef.querySelectorAll<HTMLElement>(sel)[local.active];
|
|
143
|
+
}
|
|
144
|
+
if (!el) {
|
|
145
|
+
setPos(null);
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
// Not laid out yet: a hidden/collapsing popover, a display:none tab panel, a
|
|
149
|
+
// portal measured before Kobalte positions it, or a dialog still running its
|
|
150
|
+
// open transform. Measuring then would fling the indicator to a bogus offset
|
|
151
|
+
// (a stray tinted blob a screenshot catches mid-open) or scale it by the
|
|
152
|
+
// ancestor's in-flight transform, so hide until there is real geometry.
|
|
153
|
+
// Rects, not clientWidth: a transformed ancestor leaves layout size intact.
|
|
154
|
+
const er = el.getBoundingClientRect();
|
|
155
|
+
const cr = containerRef.getBoundingClientRect();
|
|
156
|
+
if (er.width === 0 || er.height === 0 || cr.width === 0 || cr.height === 0) {
|
|
157
|
+
setPos(null);
|
|
158
|
+
remeasureNextFrame();
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
retries = 0;
|
|
162
|
+
// Measure relative to the container via bounding rects (not offsetLeft),
|
|
163
|
+
// so the active item can be a deep descendant (Kobalte menu/listbox items),
|
|
164
|
+
// not just a direct child. Add the container's own scroll so the indicator,
|
|
165
|
+
// which scrolls with the content, lands at the content offset; subtract the
|
|
166
|
+
// border so it aligns to the padding box (where the absolute indicator anchors).
|
|
167
|
+
// A row arriving through gh-stagger is still translated: bounding rects
|
|
168
|
+
// include that travel, its resting place does not.
|
|
169
|
+
const [tx, ty] = readTranslate(el);
|
|
170
|
+
setPos(
|
|
171
|
+
horizontal()
|
|
172
|
+
? {
|
|
173
|
+
offset: er.left - tx - cr.left - containerRef.clientLeft + containerRef.scrollLeft,
|
|
174
|
+
size: er.width,
|
|
175
|
+
cross: er.height,
|
|
176
|
+
}
|
|
177
|
+
: {
|
|
178
|
+
offset: er.top - ty - cr.top - containerRef.clientTop + containerRef.scrollTop,
|
|
179
|
+
size: er.height,
|
|
180
|
+
cross: er.width,
|
|
181
|
+
},
|
|
182
|
+
);
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
// Re-measure whenever the (index) active changes; queueMicrotask lets DOM settle.
|
|
186
|
+
createEffect(() => {
|
|
187
|
+
void local.active;
|
|
188
|
+
queueMicrotask(measure);
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
// The items actually under the ResizeObserver, so entries can be diffed
|
|
192
|
+
// against the freshly-resolved set on every measure() pass.
|
|
193
|
+
let ro: ResizeObserver | undefined;
|
|
194
|
+
const observedItems = new Set<Element>();
|
|
195
|
+
let disposed = false;
|
|
196
|
+
|
|
197
|
+
// Observing only the container missed the case that broke the admin tabs
|
|
198
|
+
// pill: an <iconify-icon> or a webfont finishes loading AFTER first paint and
|
|
199
|
+
// grows the trigger, but a w-full container never itself resizes, so the
|
|
200
|
+
// indicator kept the stale width. Observing the resolved item(s) directly
|
|
201
|
+
// catches that growth.
|
|
202
|
+
function syncObservedItems() {
|
|
203
|
+
if (!containerRef || !ro) return;
|
|
204
|
+
const next = new Set<Element>();
|
|
205
|
+
if (local.activeSelector) {
|
|
206
|
+
const el = containerRef.querySelector(local.activeSelector);
|
|
207
|
+
if (el) next.add(el);
|
|
208
|
+
} else if (local.active != null && local.active >= 0) {
|
|
209
|
+
const sel = local.itemSelector ?? ":scope > :not([data-sliding-indicator])";
|
|
210
|
+
for (const el of containerRef.querySelectorAll(sel)) next.add(el);
|
|
211
|
+
}
|
|
212
|
+
for (const el of observedItems) {
|
|
213
|
+
if (!next.has(el)) {
|
|
214
|
+
ro.unobserve(el);
|
|
215
|
+
observedItems.delete(el);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
for (const el of next) {
|
|
219
|
+
if (!observedItems.has(el)) {
|
|
220
|
+
ro.observe(el);
|
|
221
|
+
observedItems.add(el);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
onMount(() => {
|
|
227
|
+
if (!containerRef) return;
|
|
228
|
+
ro = new ResizeObserver(() => measure());
|
|
229
|
+
containerRef?.addEventListener("animationend", measure);
|
|
230
|
+
ro.observe(containerRef);
|
|
231
|
+
// Attribute-based selection (e.g. Kobalte's [data-selected]/[data-highlighted]):
|
|
232
|
+
// re-slide when the active child's marker attribute moves, or when items are
|
|
233
|
+
// added/removed, so newly-mounted items land under the same observer.
|
|
234
|
+
const mo = new MutationObserver(() => {
|
|
235
|
+
syncObservedItems();
|
|
236
|
+
queueMicrotask(measure);
|
|
237
|
+
});
|
|
238
|
+
mo.observe(containerRef, {
|
|
239
|
+
childList: true,
|
|
240
|
+
subtree: !!local.activeSelector,
|
|
241
|
+
attributes: !!local.activeSelector,
|
|
242
|
+
});
|
|
243
|
+
// Focus-based selection (e.g. Kobalte menus mark the highlighted item with
|
|
244
|
+
// roving focus, which is not an attribute mutation).
|
|
245
|
+
const onFocusIn = () => queueMicrotask(measure);
|
|
246
|
+
if (local.activeSelector) containerRef.addEventListener("focusin", onFocusIn);
|
|
247
|
+
syncObservedItems();
|
|
248
|
+
queueMicrotask(measure);
|
|
249
|
+
// Web fonts can finish loading after the first measure and grow glyphs
|
|
250
|
+
// (the same class of bug the item-level ResizeObserver above fixes for
|
|
251
|
+
// icons); one extra pass once fonts settle catches that too.
|
|
252
|
+
if (typeof document !== "undefined" && document.fonts) {
|
|
253
|
+
document.fonts.ready.then(() => {
|
|
254
|
+
if (!disposed) measure();
|
|
255
|
+
});
|
|
256
|
+
}
|
|
257
|
+
onCleanup(() => {
|
|
258
|
+
disposed = true;
|
|
259
|
+
if (retryFrame !== undefined) cancelAnimationFrame(retryFrame);
|
|
260
|
+
ro?.disconnect();
|
|
261
|
+
mo.disconnect();
|
|
262
|
+
containerRef?.removeEventListener("focusin", onFocusIn);
|
|
263
|
+
containerRef?.removeEventListener("animationend", measure);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
return (
|
|
268
|
+
<div ref={setContainerRef} class={cn("relative isolate", local.class)} {...rest}>
|
|
269
|
+
<Show when={pos()}>
|
|
270
|
+
{(p) => (
|
|
271
|
+
<div
|
|
272
|
+
ref={indicatorEl}
|
|
273
|
+
data-sliding-indicator
|
|
274
|
+
aria-hidden="true"
|
|
275
|
+
class={cn(
|
|
276
|
+
"pointer-events-none absolute -z-10",
|
|
277
|
+
horizontal() ? "inset-y-0 left-0" : "inset-x-0 top-0",
|
|
278
|
+
"glass glass-tint",
|
|
279
|
+
local.indicatorClass ?? "rounded-lg",
|
|
280
|
+
)}
|
|
281
|
+
style={{
|
|
282
|
+
transition: `transform ${SLIDE_MS}ms ease-in-out, width ${SLIDE_MS}ms ease-in-out, height ${SLIDE_MS}ms ease-in-out`,
|
|
283
|
+
"--glass-tone": local.indicatorTone ?? "var(--primary)",
|
|
284
|
+
...(horizontal()
|
|
285
|
+
? { transform: `translateX(${p().offset}px)`, width: `${p().size}px` }
|
|
286
|
+
: { transform: `translateY(${p().offset}px)`, height: `${p().size}px` }),
|
|
287
|
+
}}
|
|
288
|
+
/>
|
|
289
|
+
)}
|
|
290
|
+
</Show>
|
|
291
|
+
{local.children}
|
|
292
|
+
</div>
|
|
293
|
+
);
|
|
294
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
import { type Component, type ComponentProps, type JSX, mergeProps, Show } from "solid-js";
|
|
2
|
+
import { type ExternalToast, Toaster as SolidSonner, toast as sonnerToast } from "solid-sonner";
|
|
3
|
+
import {
|
|
4
|
+
ALERT_CLASS,
|
|
5
|
+
ALERT_CONTENT_CLASS,
|
|
6
|
+
ALERT_DESCRIPTION_CLASS,
|
|
7
|
+
ALERT_ICON_BG_CLASS,
|
|
8
|
+
ALERT_ICON_CLASS,
|
|
9
|
+
ALERT_TITLE_CLASS,
|
|
10
|
+
alertIconBgStyle,
|
|
11
|
+
} from "../lib/alert-tones.js";
|
|
12
|
+
import { glassToneText } from "../lib/glass-tone.js";
|
|
13
|
+
import { createIsMobile } from "../lib/use-is-mobile.js";
|
|
14
|
+
import { cn } from "../lib/utils.js";
|
|
15
|
+
import { Icon } from "./icon.js";
|
|
16
|
+
import { Spinner } from "./spinner.js";
|
|
17
|
+
|
|
18
|
+
type Position = ComponentProps<typeof SolidSonner>["position"];
|
|
19
|
+
type ToastMessage = JSX.Element | string;
|
|
20
|
+
|
|
21
|
+
type ToastKind = "success" | "error" | "warning" | "info" | "loading" | "message";
|
|
22
|
+
|
|
23
|
+
const KIND_TONE: Record<ToastKind, string | null> = {
|
|
24
|
+
success: "var(--success)",
|
|
25
|
+
error: "var(--destructive)",
|
|
26
|
+
warning: "var(--warning)",
|
|
27
|
+
info: "var(--primary)",
|
|
28
|
+
loading: null,
|
|
29
|
+
message: null,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const KIND_ICON: Record<ToastKind, string | null> = {
|
|
33
|
+
success: "lucide:circle-check",
|
|
34
|
+
error: "lucide:octagon-alert",
|
|
35
|
+
warning: "lucide:triangle-alert",
|
|
36
|
+
info: "lucide:info",
|
|
37
|
+
loading: null,
|
|
38
|
+
message: null,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/* No backdrop-blur on toasts: sonner animates transform, and backdrop-filter on
|
|
42
|
+
* a transform-animated element renders solid black mid-animation in Chromium.
|
|
43
|
+
* The opaque --card base gives the glass look with nothing to blur. Toned and
|
|
44
|
+
* neutral toasts are the same .glass material with a stronger lift. */
|
|
45
|
+
const TONE_TOAST_CLASS = "glass glass-tint [--glass-lift:0.5]";
|
|
46
|
+
const NEUTRAL_TOAST_CLASS =
|
|
47
|
+
"glass [--glass-tone:var(--foreground)] [--glass-wash:6%] [--glass-glow:0%] [--glass-drop:0%] [--glass-edge:color-mix(in_srgb,var(--foreground)_16%,transparent)] [--glass-lift:0.5]";
|
|
48
|
+
|
|
49
|
+
type GlassToastProps = {
|
|
50
|
+
kind: ToastKind;
|
|
51
|
+
title: ToastMessage;
|
|
52
|
+
description?: ToastMessage;
|
|
53
|
+
icon?: JSX.Element;
|
|
54
|
+
action?: JSX.Element;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const GlassToast: Component<GlassToastProps> = (props) => {
|
|
58
|
+
const tone = KIND_TONE[props.kind];
|
|
59
|
+
const toneIcon = KIND_ICON[props.kind];
|
|
60
|
+
// Explicit 112px (= the old [&>svg]:size-28) since host CSS can't reach the
|
|
61
|
+
// iconify shadow-DOM svg.
|
|
62
|
+
const watermarkGlyph: JSX.Element | null =
|
|
63
|
+
props.icon ?? (toneIcon ? <Icon icon={toneIcon} width={112} height={112} /> : null);
|
|
64
|
+
|
|
65
|
+
return (
|
|
66
|
+
<div
|
|
67
|
+
data-slot="toast"
|
|
68
|
+
role={props.kind === "error" ? "alert" : "status"}
|
|
69
|
+
class={cn(
|
|
70
|
+
ALERT_CLASS,
|
|
71
|
+
tone ? TONE_TOAST_CLASS : NEUTRAL_TOAST_CLASS,
|
|
72
|
+
"w-[356px] max-w-[calc(100vw-2rem)]",
|
|
73
|
+
)}
|
|
74
|
+
style={tone ? { "--glass-tone": tone, color: "var(--foreground)" } : undefined}
|
|
75
|
+
>
|
|
76
|
+
<Show when={tone && watermarkGlyph}>
|
|
77
|
+
<span
|
|
78
|
+
class={ALERT_ICON_BG_CLASS}
|
|
79
|
+
style={alertIconBgStyle(tone as string)}
|
|
80
|
+
aria-hidden="true"
|
|
81
|
+
>
|
|
82
|
+
{watermarkGlyph}
|
|
83
|
+
</span>
|
|
84
|
+
</Show>
|
|
85
|
+
<Show when={props.kind === "loading"}>
|
|
86
|
+
<span
|
|
87
|
+
class={ALERT_ICON_CLASS}
|
|
88
|
+
style={{
|
|
89
|
+
"background-color": "color-mix(in srgb, var(--foreground) 8%, transparent)",
|
|
90
|
+
color: "var(--foreground)",
|
|
91
|
+
}}
|
|
92
|
+
>
|
|
93
|
+
<Spinner class="size-[18px]" />
|
|
94
|
+
</span>
|
|
95
|
+
</Show>
|
|
96
|
+
<div class={ALERT_CONTENT_CLASS}>
|
|
97
|
+
<Show when={props.title}>
|
|
98
|
+
<p class={ALERT_TITLE_CLASS} style={tone ? { color: glassToneText(tone) } : undefined}>
|
|
99
|
+
{props.title}
|
|
100
|
+
</p>
|
|
101
|
+
</Show>
|
|
102
|
+
<Show when={props.description}>
|
|
103
|
+
<div class={ALERT_DESCRIPTION_CLASS}>{props.description}</div>
|
|
104
|
+
</Show>
|
|
105
|
+
</div>
|
|
106
|
+
<Show when={props.action}>
|
|
107
|
+
<div class="flex shrink-0 items-center">{props.action}</div>
|
|
108
|
+
</Show>
|
|
109
|
+
</div>
|
|
110
|
+
);
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const show =
|
|
114
|
+
(kind: ToastKind) =>
|
|
115
|
+
(message: ToastMessage, data: ExternalToast = {}): string | number => {
|
|
116
|
+
/* Pull rendered fields out of the sonner options, else sonner renders
|
|
117
|
+
* `description` a second time as loose text under the toast. */
|
|
118
|
+
const { description, icon, action, ...rest } = data;
|
|
119
|
+
return sonnerToast.custom(
|
|
120
|
+
() => (
|
|
121
|
+
<GlassToast
|
|
122
|
+
kind={kind}
|
|
123
|
+
title={message}
|
|
124
|
+
description={description as ToastMessage | undefined}
|
|
125
|
+
icon={icon ?? undefined}
|
|
126
|
+
action={action as JSX.Element | undefined}
|
|
127
|
+
/>
|
|
128
|
+
),
|
|
129
|
+
{ unstyled: true, ...rest },
|
|
130
|
+
);
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
type PromiseData<T> = Omit<ExternalToast, "description"> & {
|
|
134
|
+
loading?: ToastMessage;
|
|
135
|
+
success?: ToastMessage | ((data: T) => ToastMessage);
|
|
136
|
+
error?: ToastMessage | ((err: unknown) => ToastMessage);
|
|
137
|
+
finally?: () => void | Promise<void>;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const resolveMsg = <T,>(
|
|
141
|
+
m: ToastMessage | ((v: T) => ToastMessage) | undefined,
|
|
142
|
+
v: T,
|
|
143
|
+
): ToastMessage => (typeof m === "function" ? (m as (v: T) => ToastMessage)(v) : (m ?? ""));
|
|
144
|
+
|
|
145
|
+
function glassPromise<T>(
|
|
146
|
+
promise: Promise<T> | (() => Promise<T>),
|
|
147
|
+
data: PromiseData<T> = {},
|
|
148
|
+
): { unwrap: () => Promise<T> } {
|
|
149
|
+
const id = sonnerToast.custom(
|
|
150
|
+
() => <GlassToast kind="loading" title={data.loading ?? "Loading"} />,
|
|
151
|
+
{
|
|
152
|
+
unstyled: true,
|
|
153
|
+
duration: Number.POSITIVE_INFINITY,
|
|
154
|
+
},
|
|
155
|
+
);
|
|
156
|
+
const p = typeof promise === "function" ? promise() : promise;
|
|
157
|
+
p.then((v) => {
|
|
158
|
+
sonnerToast.custom(() => <GlassToast kind="success" title={resolveMsg(data.success, v)} />, {
|
|
159
|
+
id,
|
|
160
|
+
unstyled: true,
|
|
161
|
+
});
|
|
162
|
+
})
|
|
163
|
+
.catch((e: unknown) => {
|
|
164
|
+
sonnerToast.custom(() => <GlassToast kind="error" title={resolveMsg(data.error, e)} />, {
|
|
165
|
+
id,
|
|
166
|
+
unstyled: true,
|
|
167
|
+
});
|
|
168
|
+
})
|
|
169
|
+
.finally(() => data.finally?.());
|
|
170
|
+
return { unwrap: () => p };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const toast = Object.assign(
|
|
174
|
+
(message: ToastMessage, data?: ExternalToast) => show("message")(message, data),
|
|
175
|
+
{
|
|
176
|
+
success: show("success"),
|
|
177
|
+
error: show("error"),
|
|
178
|
+
warning: show("warning"),
|
|
179
|
+
info: show("info"),
|
|
180
|
+
message: show("message"),
|
|
181
|
+
loading: (message: ToastMessage, data: ExternalToast = {}) => {
|
|
182
|
+
const { description, icon, action, ...rest } = data;
|
|
183
|
+
return sonnerToast.custom(
|
|
184
|
+
() => (
|
|
185
|
+
<GlassToast
|
|
186
|
+
kind="loading"
|
|
187
|
+
title={message}
|
|
188
|
+
description={description as ToastMessage | undefined}
|
|
189
|
+
icon={icon ?? undefined}
|
|
190
|
+
action={action as JSX.Element | undefined}
|
|
191
|
+
/>
|
|
192
|
+
),
|
|
193
|
+
{ unstyled: true, duration: Number.POSITIVE_INFINITY, ...rest },
|
|
194
|
+
);
|
|
195
|
+
},
|
|
196
|
+
custom: sonnerToast.custom,
|
|
197
|
+
dismiss: sonnerToast.dismiss,
|
|
198
|
+
promise: glassPromise,
|
|
199
|
+
getHistory: sonnerToast.getHistory,
|
|
200
|
+
getToasts: sonnerToast.getToasts,
|
|
201
|
+
},
|
|
202
|
+
);
|
|
203
|
+
|
|
204
|
+
const Toaster: Component<ComponentProps<typeof SolidSonner>> = (rawProps) => {
|
|
205
|
+
const isMobile = createIsMobile();
|
|
206
|
+
const props = mergeProps({ duration: 3000, gap: 8, visibleToasts: 3 }, rawProps);
|
|
207
|
+
const position = (): Position => props.position ?? (isMobile() ? "top-center" : "bottom-right");
|
|
208
|
+
|
|
209
|
+
return (
|
|
210
|
+
<SolidSonner
|
|
211
|
+
{...props}
|
|
212
|
+
position={position()}
|
|
213
|
+
class="toaster group"
|
|
214
|
+
toastOptions={{ unstyled: true, ...props.toastOptions }}
|
|
215
|
+
/>
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export { GlassToast, Toaster, toast };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { type Component, type ComponentProps, splitProps } from "solid-js";
|
|
2
|
+
import { cn } from "../lib/utils.js";
|
|
3
|
+
import { Icon } from "./icon.js";
|
|
4
|
+
|
|
5
|
+
const Spinner: Component<Omit<ComponentProps<typeof Icon>, "icon">> = (props) => {
|
|
6
|
+
const [local, rest] = splitProps(props, ["class"]);
|
|
7
|
+
return (
|
|
8
|
+
<Icon
|
|
9
|
+
icon="lucide:loader-circle"
|
|
10
|
+
// Sizes the inner svg via the host's CSS box, so `class="size-*"` keeps working.
|
|
11
|
+
height="none"
|
|
12
|
+
role="status"
|
|
13
|
+
aria-label="Loading"
|
|
14
|
+
class={cn("size-4 animate-spin", local.class)}
|
|
15
|
+
{...rest}
|
|
16
|
+
/>
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export { Spinner };
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { For } from "solid-js";
|
|
2
|
+
import { POSITION_BAR, POSITION_BAR_LIT } from "../lib/position-bar-classes.js";
|
|
3
|
+
import { cn } from "../lib/utils.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Position inside a multi-step flow: lit bars for the eye, one spoken line for
|
|
7
|
+
* screen readers. The bars carry no text of their own, so the sr-only line is
|
|
8
|
+
* the only place the position is readable.
|
|
9
|
+
*/
|
|
10
|
+
export function StepIndicator(props: { count: number; index: number; class?: string }) {
|
|
11
|
+
const steps = () => Array.from({ length: Math.max(props.count, 0) }, (_, step) => step);
|
|
12
|
+
|
|
13
|
+
return (
|
|
14
|
+
<div
|
|
15
|
+
data-slot="step-indicator"
|
|
16
|
+
class={cn("flex items-center justify-center gap-1", props.class)}
|
|
17
|
+
>
|
|
18
|
+
<span class="sr-only">{`Step ${props.index + 1} of ${props.count}`}</span>
|
|
19
|
+
<For each={steps()}>
|
|
20
|
+
{(step) => (
|
|
21
|
+
<span
|
|
22
|
+
data-slot="step-indicator-segment"
|
|
23
|
+
aria-current={step === props.index ? "step" : undefined}
|
|
24
|
+
class={cn(POSITION_BAR, "w-6", step <= props.index && POSITION_BAR_LIT)}
|
|
25
|
+
/>
|
|
26
|
+
)}
|
|
27
|
+
</For>
|
|
28
|
+
</div>
|
|
29
|
+
);
|
|
30
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type Component, type ComponentProps, createSignal, splitProps } from "solid-js";
|
|
2
|
+
import { FIELD_CHROME, FOCUS_RING } from "../lib/input-classes.js";
|
|
3
|
+
import { THUMB_CLASS } from "../lib/thumb-classes.js";
|
|
4
|
+
import { cn } from "../lib/utils.js";
|
|
5
|
+
|
|
6
|
+
type SwitchProps = Omit<ComponentProps<"button">, "onChange" | "children"> & {
|
|
7
|
+
checked?: boolean;
|
|
8
|
+
defaultChecked?: boolean;
|
|
9
|
+
onChange?: (checked: boolean) => void;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const Switch: Component<SwitchProps> = (props) => {
|
|
13
|
+
const [local, rest] = splitProps(props, [
|
|
14
|
+
"class",
|
|
15
|
+
"checked",
|
|
16
|
+
"defaultChecked",
|
|
17
|
+
"onChange",
|
|
18
|
+
"disabled",
|
|
19
|
+
"name",
|
|
20
|
+
]);
|
|
21
|
+
const [uncontrolled, setUncontrolled] = createSignal(props.defaultChecked ?? false);
|
|
22
|
+
const checked = () => local.checked ?? uncontrolled();
|
|
23
|
+
|
|
24
|
+
return (
|
|
25
|
+
<button
|
|
26
|
+
{...rest}
|
|
27
|
+
type="button"
|
|
28
|
+
role="switch"
|
|
29
|
+
aria-checked={checked()}
|
|
30
|
+
data-slot="switch"
|
|
31
|
+
data-checked={checked() ? "" : undefined}
|
|
32
|
+
disabled={local.disabled}
|
|
33
|
+
class={cn(
|
|
34
|
+
// Unchecked wears the recessed input surface, the same dug-out glass as
|
|
35
|
+
// the slider rail; checked matches the slider fill. A `border-input`
|
|
36
|
+
// utility here would beat `:where(.glass)` and flatten the rim.
|
|
37
|
+
`group/switch peer relative inline-flex h-7 w-14 shrink-0 cursor-pointer items-center rounded-xl transition-glass duration-200 ease-out disabled:cursor-not-allowed disabled:opacity-50 ${FOCUS_RING}`,
|
|
38
|
+
checked() ? "glass glass-tint [--glass-tone:var(--primary)]" : FIELD_CHROME,
|
|
39
|
+
local.class,
|
|
40
|
+
)}
|
|
41
|
+
onClick={() => {
|
|
42
|
+
const next = !checked();
|
|
43
|
+
setUncontrolled(next);
|
|
44
|
+
local.onChange?.(next);
|
|
45
|
+
}}
|
|
46
|
+
>
|
|
47
|
+
<span
|
|
48
|
+
data-slot="switch-thumb"
|
|
49
|
+
class={cn(
|
|
50
|
+
THUMB_CLASS,
|
|
51
|
+
"pointer-events-none transition-transform duration-300 [transition-timing-function:cubic-bezier(0.34,1.56,0.64,1)] group-data-[checked]/switch:translate-x-full",
|
|
52
|
+
)}
|
|
53
|
+
// Off must read quieter than on: the knob dims with the track rather
|
|
54
|
+
// than staying one bright material in both states.
|
|
55
|
+
style={{ background: checked() ? "var(--thumb-face-on)" : "var(--thumb-face-off)" }}
|
|
56
|
+
/>
|
|
57
|
+
{local.name && <input type="hidden" name={local.name} value={checked() ? "on" : "off"} />}
|
|
58
|
+
</button>
|
|
59
|
+
);
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export { Switch };
|