@glasshome/ui 1.41.0 → 1.43.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/SPEC.md +5 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +3 -3
- package/dist/lib/anchor-placement.d.ts +34 -0
- package/dist/lib/balance-rows.d.ts +10 -4
- package/dist/lib/behavior/anchored.d.ts +26 -0
- package/dist/lib/behavior/button-root.d.ts +7 -0
- package/dist/lib/behavior/collection.d.ts +32 -0
- package/dist/lib/behavior/controllable.d.ts +8 -0
- package/dist/lib/behavior/dismiss.d.ts +13 -0
- package/dist/lib/behavior/focus-scope.d.ts +24 -0
- package/dist/lib/behavior/hide-outside.d.ts +7 -0
- package/dist/lib/behavior/layer-stack.d.ts +34 -0
- package/dist/lib/behavior/menu.d.ts +59 -0
- package/dist/lib/behavior/modal-root.d.ts +68 -0
- package/dist/lib/behavior/polymorphic.d.ts +6 -0
- package/dist/lib/behavior/presence.d.ts +18 -0
- package/dist/lib/behavior/range.d.ts +44 -0
- package/dist/lib/behavior/solid.d.ts +13 -0
- package/dist/lib/color/color.d.ts +41 -0
- package/dist/lib/color/index.d.ts +2 -0
- package/dist/lib/color/intl.d.ts +40 -0
- package/dist/lib/dock-slot-classes.d.ts +2 -0
- package/dist/lib/hold.d.ts +1 -1
- package/dist/lib/locale.d.ts +3 -0
- package/dist/lib/menu-classes.d.ts +2 -0
- package/dist/lib/motion-classes.d.ts +5 -2
- package/dist/lib/overlay-classes.d.ts +4 -0
- package/dist/lib/page-cover.d.ts +8 -0
- package/dist/lib/scroll-track.d.ts +3 -2
- package/dist/lib/section-tokens.d.ts +1 -1
- package/dist/lib/segment-classes.d.ts +2 -0
- package/dist/lib/shadow-layer.d.ts +0 -43
- package/dist/lib/toggle-set.d.ts +44 -0
- package/dist/lib/visually-hidden.d.ts +3 -0
- package/dist/solid/accordion.d.ts +35 -11
- package/dist/solid/alert-dialog.d.ts +5 -8
- package/dist/solid/avatar.d.ts +8 -4
- package/dist/solid/bottom-sheet/bottom-sheet.d.ts +2 -2
- package/dist/solid/bottom-sheet/constants.d.ts +1 -1
- package/dist/solid/button.d.ts +5 -4
- package/dist/solid/charts.d.ts +1 -1
- package/dist/solid/checkbox.d.ts +14 -4
- package/dist/solid/collapsible.d.ts +33 -5
- package/dist/solid/color-disc.d.ts +1 -1
- package/dist/solid/color-slider.d.ts +1 -1
- package/dist/solid/color-wheel.d.ts +1 -1
- package/dist/solid/context-menu.d.ts +58 -24
- package/dist/solid/dialog-parts.d.ts +53 -16
- package/dist/solid/dialog.d.ts +7 -10
- package/dist/solid/dropdown-menu.d.ts +55 -24
- package/dist/solid/empty.d.ts +1 -1
- package/dist/solid/form.d.ts +5 -6
- package/dist/solid/hover-card.d.ts +65 -9
- package/dist/solid/index.d.ts +5 -2
- package/dist/solid/index.js +8671 -14317
- package/dist/solid/input-group.d.ts +6 -6
- package/dist/solid/item.d.ts +1 -1
- package/dist/solid/menu-parts.d.ts +175 -24
- package/dist/solid/option-card.d.ts +1 -4
- package/dist/solid/picker-row.d.ts +2 -0
- package/dist/solid/popover.d.ts +77 -12
- package/dist/solid/progress.d.ts +11 -3
- package/dist/solid/radio-group.d.ts +31 -6
- package/dist/solid/responsive-dialog.d.ts +3 -1
- package/dist/solid/scroll-area.d.ts +1 -1
- package/dist/solid/section-card.d.ts +2 -3
- package/dist/solid/select.d.ts +107 -10
- package/dist/solid/selection.d.ts +10 -1
- package/dist/solid/sheet.d.ts +6 -9
- package/dist/solid/sliding-indicator.d.ts +1 -1
- package/dist/solid/sonner.d.ts +1 -1
- package/dist/solid/stale-veil.d.ts +7 -0
- package/dist/solid/tabs.d.ts +28 -9
- package/dist/solid/toggle-group.d.ts +30 -5
- package/dist/solid/toggle.d.ts +11 -3
- package/dist/solid/tooltip.d.ts +71 -9
- package/dist/tokens/index.js +54 -51
- package/dist/{utils-CGN4X-2O.js → utils-3Codx-NZ.js} +848 -618
- package/package.json +6 -7
- package/src/index.ts +1 -0
- package/src/lib/anchor-placement.ts +77 -4
- package/src/lib/balance-rows.ts +115 -40
- package/src/lib/behavior/anchored.ts +106 -0
- package/src/lib/behavior/button-root.tsx +103 -0
- package/src/lib/behavior/collection.ts +218 -0
- package/src/lib/behavior/controllable.ts +22 -0
- package/src/lib/behavior/dismiss.ts +185 -0
- package/src/lib/behavior/focus-scope.ts +227 -0
- package/src/lib/behavior/hide-outside.ts +85 -0
- package/src/lib/behavior/layer-stack.ts +150 -0
- package/src/lib/behavior/menu.ts +470 -0
- package/src/lib/behavior/modal-root.tsx +452 -0
- package/src/lib/behavior/polymorphic.tsx +30 -0
- package/src/lib/behavior/presence.ts +89 -0
- package/src/lib/behavior/range.ts +212 -0
- package/src/lib/behavior/solid.ts +30 -0
- package/src/lib/clipboard.ts +4 -4
- package/src/lib/color/color.ts +746 -0
- package/src/lib/color/index.ts +2 -0
- package/src/lib/color/intl.ts +60 -0
- package/src/lib/dock-slot-classes.ts +3 -0
- package/src/lib/hold.ts +1 -1
- package/src/lib/locale.ts +62 -0
- package/src/lib/menu-classes.ts +3 -0
- package/src/lib/motion-classes.ts +33 -8
- package/src/lib/overlay-classes.ts +8 -3
- package/src/lib/page-cover.ts +47 -0
- package/src/lib/scroll-track.ts +11 -6
- package/src/lib/section-tokens.ts +1 -1
- package/src/lib/segment-classes.ts +3 -0
- package/src/lib/shadow-layer.ts +0 -136
- package/src/lib/toggle-set.ts +133 -0
- package/src/lib/visually-hidden.ts +15 -0
- package/src/solid/accordion.tsx +294 -20
- package/src/solid/alert-dialog.tsx +36 -85
- package/src/solid/area-picker.tsx +5 -3
- package/src/solid/avatar.tsx +98 -24
- package/src/solid/badge.tsx +5 -5
- package/src/solid/bottom-sheet/bottom-sheet.tsx +73 -129
- package/src/solid/bottom-sheet/constants.ts +3 -1
- package/src/solid/bottom-sheet/drag-controller.ts +11 -0
- package/src/solid/breadcrumb.tsx +4 -4
- package/src/solid/button-group.tsx +4 -4
- package/src/solid/button.tsx +12 -6
- package/src/solid/card.tsx +5 -4
- package/src/solid/charts.tsx +1 -1
- package/src/solid/checkbox.tsx +167 -44
- package/src/solid/collapsible.tsx +260 -12
- package/src/solid/color-disc.tsx +11 -12
- package/src/solid/color-slider.tsx +114 -47
- package/src/solid/color-wheel.tsx +137 -39
- package/src/solid/context-menu.tsx +287 -86
- package/src/solid/dialog-parts.tsx +135 -46
- package/src/solid/dialog.tsx +44 -76
- package/src/solid/dock-edit.tsx +5 -4
- package/src/solid/dock.tsx +6 -5
- package/src/solid/dropdown-menu.tsx +252 -107
- package/src/solid/empty.tsx +1 -1
- package/src/solid/entity-selector.tsx +5 -3
- package/src/solid/face-toggle.tsx +43 -18
- package/src/solid/form.tsx +9 -9
- package/src/solid/glyph-tile.tsx +48 -21
- package/src/solid/hover-card.tsx +288 -21
- package/src/solid/icon-picker.tsx +2 -1
- package/src/solid/image-picker.tsx +6 -5
- package/src/solid/index.ts +5 -4
- package/src/solid/input-group.tsx +7 -5
- package/src/solid/input-otp.tsx +2 -1
- package/src/solid/item.tsx +5 -5
- package/src/solid/logo.tsx +5 -4
- package/src/solid/menu-parts.tsx +619 -145
- package/src/solid/option-card.tsx +6 -10
- package/src/solid/picker-row.tsx +10 -5
- package/src/solid/picker-search.tsx +2 -1
- package/src/solid/popover.tsx +424 -71
- package/src/solid/preview-tile.tsx +1 -1
- package/src/solid/progress.tsx +84 -15
- package/src/solid/radio-group.tsx +271 -34
- package/src/solid/responsive-dialog.tsx +10 -10
- package/src/solid/schema-form.tsx +1 -6
- package/src/solid/scroll-area.tsx +1 -1
- package/src/solid/section-card.tsx +3 -4
- package/src/solid/select.tsx +950 -95
- package/src/solid/selection.tsx +142 -66
- package/src/solid/sheet.tsx +48 -80
- package/src/solid/slider.tsx +115 -65
- package/src/solid/sliding-indicator.tsx +1 -1
- package/src/solid/sonner.tsx +1 -1
- package/src/solid/spotlight.tsx +23 -11
- package/src/solid/stale-veil.tsx +20 -0
- package/src/solid/switch.tsx +8 -6
- package/src/solid/tabs.tsx +377 -52
- package/src/solid/term.tsx +3 -4
- package/src/solid/toggle-group.tsx +237 -95
- package/src/solid/toggle.tsx +43 -9
- package/src/solid/tooltip.tsx +285 -26
- package/src/styles/globals.css +64 -10
- package/src/styles/theme.css +12 -4
- package/src/tokens/material.ts +11 -2
- package/dist/lib/when-animations-settle.d.ts +0 -4
- package/dist/solid/bottom-sheet/focus-trap.d.ts +0 -6
- package/dist/solid/bottom-sheet/scroll-lock.d.ts +0 -2
- package/dist/solid/bottom-sheet/sheet-motion.d.ts +0 -1
- package/src/lib/when-animations-settle.ts +0 -12
- package/src/solid/bottom-sheet/focus-trap.ts +0 -63
- package/src/solid/bottom-sheet/scroll-lock.ts +0 -66
- package/src/solid/bottom-sheet/sheet-motion.ts +0 -4
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@glasshome/ui",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "SolidJS component library for GlassHome
|
|
3
|
+
"version": "1.43.0",
|
|
4
|
+
"description": "SolidJS component library for GlassHome",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"components",
|
|
7
7
|
"glasshome",
|
|
8
8
|
"home-assistant",
|
|
9
|
-
"kobalte",
|
|
10
9
|
"smart-home",
|
|
11
10
|
"solidjs",
|
|
12
11
|
"tailwindcss",
|
|
@@ -85,7 +84,9 @@
|
|
|
85
84
|
"check:publish": "publint && attw --pack . --profile esm-only --exclude-entrypoints ./styles ./styles/theme && bun scripts/check-pack.ts",
|
|
86
85
|
"dev": "tsc -b tsconfig.build.json --force && vite build --watch",
|
|
87
86
|
"lint": "oxlint --type-aware . && oxfmt --check .",
|
|
88
|
-
"test": "vitest run --project '!visual'",
|
|
87
|
+
"test": "vitest run --project '!visual' --project '!webkit'",
|
|
88
|
+
"test:webkit": "vitest run --project webkit",
|
|
89
|
+
"test:webkit:image": "sh dev/webkit-in-image.sh",
|
|
89
90
|
"test:visual": "vitest run --project visual",
|
|
90
91
|
"test:visual:image": "sh dev/visual-in-image.sh",
|
|
91
92
|
"test:visual:update": "sh dev/visual-in-image.sh --update",
|
|
@@ -98,7 +99,6 @@
|
|
|
98
99
|
"dependencies": {
|
|
99
100
|
"@iconify/types": "^2.0.0",
|
|
100
101
|
"@iconify/utils": "^3.1.4",
|
|
101
|
-
"@kobalte/core": "^0.13.13",
|
|
102
102
|
"@types/json-schema": "^7.0.15",
|
|
103
103
|
"clsx": "^2.1.1",
|
|
104
104
|
"cva": "^1.0.0-beta.4",
|
|
@@ -152,7 +152,6 @@
|
|
|
152
152
|
"node": ">=20"
|
|
153
153
|
},
|
|
154
154
|
"patchedDependencies": {
|
|
155
|
-
"@guidepup/virtual-screen-reader@0.33.0": "patches/@guidepup%2Fvirtual-screen-reader@0.33.0.patch"
|
|
156
|
-
"@kobalte/core@0.13.13": "patches/@kobalte%2Fcore@0.13.13.patch"
|
|
155
|
+
"@guidepup/virtual-screen-reader@0.33.0": "patches/@guidepup%2Fvirtual-screen-reader@0.33.0.patch"
|
|
157
156
|
}
|
|
158
157
|
}
|
package/src/index.ts
CHANGED
|
@@ -83,6 +83,7 @@ export {
|
|
|
83
83
|
SCRIM_MOTION,
|
|
84
84
|
SETTLE_MOTION,
|
|
85
85
|
STAGGER,
|
|
86
|
+
STALE_CONTENT,
|
|
86
87
|
} from "./lib/motion-classes.js";
|
|
87
88
|
export { createHold, HOLD_GRACE_MS, HOLD_MS, spendLongPress } from "./lib/hold.js";
|
|
88
89
|
export type { Hold, HoldOptions, HoldPress } from "./lib/hold.js";
|
|
@@ -2,13 +2,85 @@ import type { JSX } from "solid-js";
|
|
|
2
2
|
|
|
3
3
|
const EDGE = 16;
|
|
4
4
|
|
|
5
|
-
interface Box {
|
|
5
|
+
export interface Box {
|
|
6
6
|
left: number;
|
|
7
7
|
top: number;
|
|
8
8
|
width: number;
|
|
9
9
|
height: number;
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
+
export type Side = "top" | "bottom" | "left" | "right";
|
|
13
|
+
export type Align = "start" | "center" | "end";
|
|
14
|
+
export interface AnchorPlace {
|
|
15
|
+
side: Side;
|
|
16
|
+
align: Align;
|
|
17
|
+
offset: number;
|
|
18
|
+
matchAnchorWidth: boolean;
|
|
19
|
+
/** False keeps the chosen side even without room; the part only shifts to stay inside the viewport. */
|
|
20
|
+
flip?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface Placement {
|
|
23
|
+
left: number;
|
|
24
|
+
top: number;
|
|
25
|
+
side: Side;
|
|
26
|
+
maxWidth: number;
|
|
27
|
+
maxHeight: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(v, hi));
|
|
31
|
+
|
|
32
|
+
const OPPOSITE: Record<Side, Side> = { top: "bottom", bottom: "top", left: "right", right: "left" };
|
|
33
|
+
|
|
34
|
+
const crossStart = (align: Align, start: number, size: number, own: number): number =>
|
|
35
|
+
start + { start: 0, center: (size - own) / 2, end: size - own }[align];
|
|
36
|
+
|
|
37
|
+
/** Where a part of this size sits beside its anchor: on the chosen side, or the opposite when that
|
|
38
|
+
* side lacks room and the other has more, aligned along the edge and held 16px inside the viewport. */
|
|
39
|
+
export function placeBeside(
|
|
40
|
+
anchor: Box,
|
|
41
|
+
floating: { width: number; height: number },
|
|
42
|
+
viewport: { width: number; height: number },
|
|
43
|
+
place: AnchorPlace,
|
|
44
|
+
): Placement {
|
|
45
|
+
const { offset } = place;
|
|
46
|
+
const rooms: Record<Side, number> = {
|
|
47
|
+
bottom: viewport.height - EDGE - (anchor.top + anchor.height + offset),
|
|
48
|
+
top: anchor.top - offset - EDGE,
|
|
49
|
+
right: viewport.width - EDGE - (anchor.left + anchor.width + offset),
|
|
50
|
+
left: anchor.left - offset - EDGE,
|
|
51
|
+
};
|
|
52
|
+
const vertical = place.side === "top" || place.side === "bottom";
|
|
53
|
+
const need = vertical ? floating.height : floating.width;
|
|
54
|
+
const other = OPPOSITE[place.side];
|
|
55
|
+
const side =
|
|
56
|
+
place.flip === false || rooms[place.side] >= need || rooms[other] <= rooms[place.side]
|
|
57
|
+
? place.side
|
|
58
|
+
: other;
|
|
59
|
+
const maxWidth = viewport.width - 2 * EDGE;
|
|
60
|
+
const maxHeight = viewport.height - 2 * EDGE;
|
|
61
|
+
const width = Math.min(
|
|
62
|
+
Math.max(floating.width, place.matchAnchorWidth && vertical ? anchor.width : 0),
|
|
63
|
+
maxWidth,
|
|
64
|
+
);
|
|
65
|
+
const height = Math.min(floating.height, maxHeight);
|
|
66
|
+
const along: Record<Side, number> = {
|
|
67
|
+
bottom: anchor.top + anchor.height + offset,
|
|
68
|
+
top: anchor.top - offset - height,
|
|
69
|
+
right: anchor.left + anchor.width + offset,
|
|
70
|
+
left: anchor.left - offset - width,
|
|
71
|
+
};
|
|
72
|
+
const beside = side === "top" || side === "bottom";
|
|
73
|
+
const left = beside ? crossStart(place.align, anchor.left, anchor.width, width) : along[side];
|
|
74
|
+
const top = beside ? along[side] : crossStart(place.align, anchor.top, anchor.height, height);
|
|
75
|
+
return {
|
|
76
|
+
left: clamp(left, EDGE, viewport.width - EDGE - width),
|
|
77
|
+
top: clamp(top, EDGE, viewport.height - EDGE - height),
|
|
78
|
+
side,
|
|
79
|
+
maxWidth,
|
|
80
|
+
maxHeight,
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
12
84
|
/** Where a panel of this size sits over its anchor: centred on it, shifted only as far as the
|
|
13
85
|
* screen needs, as wide as the anchor at least. Before the panel is measured (`height`
|
|
14
86
|
* undefined) its top sits on the anchor's top. */
|
|
@@ -18,7 +90,6 @@ function anchoredBox(
|
|
|
18
90
|
width: number,
|
|
19
91
|
height?: number,
|
|
20
92
|
): Box {
|
|
21
|
-
const clamp = (v: number, lo: number, hi: number) => Math.max(lo, Math.min(v, hi));
|
|
22
93
|
const w = Math.min(Math.max(width, anchor.width), viewport.width - 2 * EDGE);
|
|
23
94
|
const x = clamp(anchor.left + (anchor.width - w) / 2, EDGE, viewport.width - EDGE - w);
|
|
24
95
|
const h = Math.min(Math.max(height ?? 0, anchor.height), viewport.height - 2 * EDGE);
|
|
@@ -29,13 +100,15 @@ function anchoredBox(
|
|
|
29
100
|
return { left: x, top: y, width: w, height: h };
|
|
30
101
|
}
|
|
31
102
|
|
|
103
|
+
/** The corner a control takes (rounded-lg is --radius), clamped to a capsule at its height; read from the theme so opening a panel asks the page for no style. */
|
|
104
|
+
export const controlRadius = (height: number): string => `min(var(--radius), ${height / 2}px)`;
|
|
105
|
+
|
|
32
106
|
/** A panel that grows out of the element that opened it. The --morph-* values start the panel
|
|
33
107
|
* clipped to the element's box (MORPH_MOTION's keyframes), so it unrolls out of it. */
|
|
34
108
|
export function overAnchor(anchor: HTMLElement, width: number, height?: number): JSX.CSSProperties {
|
|
35
109
|
const box = anchor.getBoundingClientRect();
|
|
36
110
|
const vh = window.innerHeight;
|
|
37
111
|
const placed = anchoredBox(box, { width: window.innerWidth, height: vh }, width, height);
|
|
38
|
-
const radius = getComputedStyle(anchor).borderTopLeftRadius || "0px";
|
|
39
112
|
return {
|
|
40
113
|
left: `${placed.left}px`,
|
|
41
114
|
top: `${placed.top}px`,
|
|
@@ -48,6 +121,6 @@ export function overAnchor(anchor: HTMLElement, width: number, height?: number):
|
|
|
48
121
|
"--morph-y": `${box.top - placed.top}px`,
|
|
49
122
|
"--morph-w": `${box.width}px`,
|
|
50
123
|
"--morph-h": `${box.height}px`,
|
|
51
|
-
"--morph-radius":
|
|
124
|
+
"--morph-radius": controlRadius(box.height),
|
|
52
125
|
} as JSX.CSSProperties;
|
|
53
126
|
}
|
package/src/lib/balance-rows.ts
CHANGED
|
@@ -1,62 +1,137 @@
|
|
|
1
|
+
import type { Accessor } from "solid-js";
|
|
2
|
+
import { effect } from "./behavior/solid.js";
|
|
3
|
+
|
|
1
4
|
/* A row of segments that runs out of width wraps into a grid whose rows hold an even count
|
|
2
5
|
* (four modes go two by two, five go three and two), never a full row and an orphan. */
|
|
3
6
|
|
|
4
|
-
/**
|
|
5
|
-
export const BALANCED_ROWS =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
7
|
+
/** Marks the element holding the items: a wrapping row until the column count is known, then a grid of that many equal columns (globals.css). */
|
|
8
|
+
export const BALANCED_ROWS = "data-balanced-rows";
|
|
9
|
+
|
|
10
|
+
const BALANCED = "data-balanced";
|
|
11
|
+
|
|
12
|
+
/** Marks the node inside each item that sizes to its content in either layout. */
|
|
13
|
+
export const BALANCE_CONTENT = "data-balance-content";
|
|
14
|
+
|
|
15
|
+
const SLACK_PX = 0.5;
|
|
16
|
+
|
|
17
|
+
const total = (widths: number[]) => widths.reduce((sum, w) => sum + w, 0);
|
|
18
|
+
|
|
19
|
+
/** Columns of the balanced grid, from each item's own width; every item when one row holds them all. */
|
|
20
|
+
function balancedColumns(room: number, widths: number[]): number {
|
|
21
|
+
const count = widths.length;
|
|
22
|
+
if (count < 2 || total(widths) <= room + SLACK_PX) return count;
|
|
19
23
|
let columns = count - 1;
|
|
20
24
|
for (; columns > 1; columns--) {
|
|
21
|
-
|
|
22
|
-
|
|
25
|
+
let span = 0;
|
|
26
|
+
for (let column = 0; column < columns; column++) {
|
|
27
|
+
let widest = 0;
|
|
28
|
+
for (let i = column; i < count; i += columns) widest = Math.max(widest, widths[i] ?? 0);
|
|
29
|
+
span += widest;
|
|
30
|
+
}
|
|
31
|
+
if (span <= room + SLACK_PX) break;
|
|
23
32
|
}
|
|
24
|
-
|
|
33
|
+
return Math.ceil(count / Math.ceil(count / Math.max(columns, 1)));
|
|
25
34
|
}
|
|
26
35
|
|
|
36
|
+
const inline = (sizes: readonly ResizeObserverSize[] | undefined, fallback: number) =>
|
|
37
|
+
sizes?.[0]?.inlineSize ?? fallback;
|
|
38
|
+
|
|
27
39
|
/**
|
|
28
40
|
* Lays `items` out in `BALANCED_ROWS`. `sizer` is the element that fills its line once wrapped
|
|
29
|
-
* (it takes `data-wrapped` too); `item` selects the items among `items`' children
|
|
41
|
+
* (it takes `data-wrapped` too); `item` selects the items among `items`' children, each holding
|
|
42
|
+
* one `BALANCE_CONTENT` node. Sizes come from a ResizeObserver only, never a layout read: an
|
|
43
|
+
* item's own width is its content node's width plus its padding and border, the same in either
|
|
44
|
+
* layout, so only the column count waits a frame, and the wrapping row never overflows meanwhile.
|
|
30
45
|
*/
|
|
31
|
-
export function balanceRows(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
46
|
+
export function balanceRows(
|
|
47
|
+
sizer: HTMLElement,
|
|
48
|
+
items: HTMLElement,
|
|
49
|
+
item: string,
|
|
50
|
+
members: Accessor<unknown>,
|
|
51
|
+
): () => void {
|
|
52
|
+
const content = `[${BALANCE_CONTENT}]`;
|
|
53
|
+
const each = () => [...items.querySelectorAll<HTMLElement>(`:scope > ${item}`)];
|
|
54
|
+
const own = new WeakMap<Element, number>();
|
|
55
|
+
const chrome = new WeakMap<Element, number>();
|
|
56
|
+
let columns = 0;
|
|
39
57
|
let frame = 0;
|
|
40
|
-
|
|
58
|
+
|
|
59
|
+
const widths = (): number[] | undefined => {
|
|
60
|
+
const out: number[] = [];
|
|
61
|
+
for (const el of each()) {
|
|
62
|
+
const inner = el.querySelector(content);
|
|
63
|
+
const width = inner ? own.get(inner) : undefined;
|
|
64
|
+
const edge = chrome.get(el);
|
|
65
|
+
if (width === undefined || edge === undefined) return undefined;
|
|
66
|
+
out.push(width + edge);
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const layout = (next: number, count: number) => {
|
|
72
|
+
columns = next;
|
|
73
|
+
const wrapped = next > 0 && next < count;
|
|
74
|
+
for (const el of [sizer, items]) {
|
|
75
|
+
if (wrapped) el.setAttribute("data-wrapped", "");
|
|
76
|
+
else el.removeAttribute("data-wrapped");
|
|
77
|
+
}
|
|
78
|
+
if (next > 0) {
|
|
79
|
+
items.setAttribute(BALANCED, "");
|
|
80
|
+
items.style.setProperty("--balance-columns", String(next));
|
|
81
|
+
} else {
|
|
82
|
+
items.removeAttribute(BALANCED);
|
|
83
|
+
items.style.removeProperty("--balance-columns");
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
// A layout change inside any ResizeObserver callback resizes what other observers watch and loops, so it waits a frame.
|
|
88
|
+
const decide = () => {
|
|
41
89
|
cancelAnimationFrame(frame);
|
|
42
|
-
frame = requestAnimationFrame(
|
|
90
|
+
frame = requestAnimationFrame(() => {
|
|
91
|
+
const known = widths();
|
|
92
|
+
if (!known) return;
|
|
93
|
+
const space = room();
|
|
94
|
+
const next = space > 0 ? balancedColumns(space, known) : 0;
|
|
95
|
+
if (next !== columns) layout(next, known.length);
|
|
96
|
+
});
|
|
43
97
|
};
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
98
|
+
|
|
99
|
+
const parent = sizer.parentElement;
|
|
100
|
+
let line = 0;
|
|
101
|
+
let sizerWidth = 0;
|
|
102
|
+
let itemsWidth = 0;
|
|
103
|
+
// The room is the line the sizer may fill, whatever width it fits to now.
|
|
104
|
+
const room = () => (parent ? line - (sizerWidth - itemsWidth) : itemsWidth);
|
|
105
|
+
|
|
106
|
+
const resize = new ResizeObserver((entries) => {
|
|
107
|
+
for (const entry of entries) {
|
|
108
|
+
const border = inline(entry.borderBoxSize, entry.contentRect.width);
|
|
109
|
+
if (entry.target === parent) line = entry.contentRect.width;
|
|
110
|
+
if (entry.target === sizer) sizerWidth = border;
|
|
111
|
+
if (entry.target === items) itemsWidth = entry.contentRect.width;
|
|
112
|
+
if (entry.target.hasAttribute(BALANCE_CONTENT)) own.set(entry.target, border);
|
|
113
|
+
else if (entry.target.parentElement === items)
|
|
114
|
+
chrome.set(entry.target, border - inline(entry.contentBoxSize, entry.contentRect.width));
|
|
115
|
+
}
|
|
116
|
+
decide();
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
const watch = () => {
|
|
47
120
|
resize.disconnect();
|
|
121
|
+
if (parent) resize.observe(parent);
|
|
48
122
|
resize.observe(sizer);
|
|
49
|
-
|
|
123
|
+
resize.observe(items);
|
|
124
|
+
for (const el of each()) {
|
|
125
|
+
resize.observe(el);
|
|
126
|
+
const inner = el.querySelector(content);
|
|
127
|
+
if (inner) resize.observe(inner);
|
|
128
|
+
}
|
|
50
129
|
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
later();
|
|
55
|
-
});
|
|
56
|
-
children.observe(items, { childList: true });
|
|
130
|
+
|
|
131
|
+
layout(0, 0);
|
|
132
|
+
effect(members, watch);
|
|
57
133
|
return () => {
|
|
58
134
|
cancelAnimationFrame(frame);
|
|
59
135
|
resize.disconnect();
|
|
60
|
-
children.disconnect();
|
|
61
136
|
};
|
|
62
137
|
}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import type { Accessor } from "solid-js";
|
|
2
|
+
import { type Align, placeBeside, type Side } from "../anchor-placement.js";
|
|
3
|
+
import { effect } from "./solid.js";
|
|
4
|
+
|
|
5
|
+
/** An element, or a point or box with no element of its own: a context menu opens at the pointer. */
|
|
6
|
+
export interface AnchorLike {
|
|
7
|
+
getBoundingClientRect(): { left: number; top: number; width: number; height: number };
|
|
8
|
+
getRootNode?(): Node;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface AnchoredOptions {
|
|
12
|
+
anchor: Accessor<AnchorLike | undefined>;
|
|
13
|
+
floating: Accessor<HTMLElement | undefined>;
|
|
14
|
+
open: Accessor<boolean>;
|
|
15
|
+
side?: Accessor<Side>;
|
|
16
|
+
align?: Accessor<Align>;
|
|
17
|
+
offset?: number;
|
|
18
|
+
matchAnchorWidth?: boolean;
|
|
19
|
+
/** The anchor counts as its top edge and the part never flips: placed below, it covers the anchor. */
|
|
20
|
+
coverAnchor?: boolean;
|
|
21
|
+
/** A custom property the part gets the anchor's width in, for styles sized by the anchor. */
|
|
22
|
+
widthVar?: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const SHADOW_ROOT_NODE = 11;
|
|
26
|
+
// The widget realm's tap guard reads this marker as a placed panel.
|
|
27
|
+
const ANCHORED = "data-anchored";
|
|
28
|
+
|
|
29
|
+
export function createAnchored(options: AnchoredOptions): void {
|
|
30
|
+
effect(
|
|
31
|
+
() =>
|
|
32
|
+
[
|
|
33
|
+
options.open(),
|
|
34
|
+
options.anchor(),
|
|
35
|
+
options.floating(),
|
|
36
|
+
options.side?.() ?? "bottom",
|
|
37
|
+
options.align?.() ?? "center",
|
|
38
|
+
] as const,
|
|
39
|
+
([open, anchor, floating, side, align]) => {
|
|
40
|
+
if (!open || !anchor || !floating) return;
|
|
41
|
+
floating.setAttribute(ANCHORED, "");
|
|
42
|
+
const offset = options.offset ?? 0;
|
|
43
|
+
const matchAnchorWidth = options.matchAnchorWidth ?? false;
|
|
44
|
+
const cover = options.coverAnchor ?? false;
|
|
45
|
+
const written = new Map<string, string>();
|
|
46
|
+
const write = (name: string, value: string) => {
|
|
47
|
+
if (written.get(name) === value) return;
|
|
48
|
+
written.set(name, value);
|
|
49
|
+
floating.style.setProperty(name, value);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
let size = { width: 0, height: 0 };
|
|
53
|
+
const place = (measure: boolean) => {
|
|
54
|
+
if (measure) {
|
|
55
|
+
write("position", "fixed");
|
|
56
|
+
write("margin", "0");
|
|
57
|
+
write("right", "auto");
|
|
58
|
+
write("bottom", "auto");
|
|
59
|
+
}
|
|
60
|
+
const box = anchor.getBoundingClientRect();
|
|
61
|
+
if (options.widthVar) write(options.widthVar, `${box.width}px`);
|
|
62
|
+
if (measure) size = { width: floating.offsetWidth, height: floating.offsetHeight };
|
|
63
|
+
const placed = placeBeside(
|
|
64
|
+
{ left: box.left, top: box.top, width: box.width, height: cover ? 0 : box.height },
|
|
65
|
+
size,
|
|
66
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
67
|
+
{ side, align, offset, matchAnchorWidth, flip: !cover },
|
|
68
|
+
);
|
|
69
|
+
// A covering part may sit as near the edge as its anchor already does.
|
|
70
|
+
const top = cover && box.top >= 0 ? Math.min(placed.top, box.top + offset) : placed.top;
|
|
71
|
+
write("top", `${top}px`);
|
|
72
|
+
write("left", `${placed.left}px`);
|
|
73
|
+
write("max-width", `${placed.maxWidth}px`);
|
|
74
|
+
write("max-height", `${placed.maxHeight}px`);
|
|
75
|
+
if (matchAnchorWidth) write("min-width", `${box.width}px`);
|
|
76
|
+
if (floating.getAttribute("data-side") !== placed.side)
|
|
77
|
+
floating.setAttribute("data-side", placed.side);
|
|
78
|
+
};
|
|
79
|
+
place(true);
|
|
80
|
+
|
|
81
|
+
let frame: number | undefined;
|
|
82
|
+
const onScroll = () => {
|
|
83
|
+
if (frame !== undefined) return;
|
|
84
|
+
frame = requestAnimationFrame(() => {
|
|
85
|
+
frame = undefined;
|
|
86
|
+
place(false);
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
const onResize = () => place(true);
|
|
90
|
+
|
|
91
|
+
const root = anchor.getRootNode?.() ?? document;
|
|
92
|
+
const scrollTargets: EventTarget[] =
|
|
93
|
+
root.nodeType === SHADOW_ROOT_NODE ? [document, root] : [document];
|
|
94
|
+
for (const target of scrollTargets)
|
|
95
|
+
target.addEventListener("scroll", onScroll, { capture: true, passive: true });
|
|
96
|
+
window.addEventListener("resize", onResize);
|
|
97
|
+
return () => {
|
|
98
|
+
for (const target of scrollTargets)
|
|
99
|
+
target.removeEventListener("scroll", onScroll, { capture: true });
|
|
100
|
+
window.removeEventListener("resize", onResize);
|
|
101
|
+
if (frame !== undefined) cancelAnimationFrame(frame);
|
|
102
|
+
floating.removeAttribute(ANCHORED);
|
|
103
|
+
};
|
|
104
|
+
},
|
|
105
|
+
);
|
|
106
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { type ComponentProps, createSignal, type JSX, type ValidComponent } from "solid-js";
|
|
2
|
+
import { Polymorphic } from "./polymorphic.js";
|
|
3
|
+
import { callEventHandler, effect, merge, omit } from "./solid.js";
|
|
4
|
+
|
|
5
|
+
export type ButtonRootProps<T extends ValidComponent = "button"> = Omit<
|
|
6
|
+
ComponentProps<T>,
|
|
7
|
+
"as" | "disabled"
|
|
8
|
+
> & {
|
|
9
|
+
as?: T;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type Passed = {
|
|
14
|
+
onClick?: JSX.EventHandlerUnion<HTMLElement, MouseEvent>;
|
|
15
|
+
ref?: unknown;
|
|
16
|
+
href?: unknown;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const NATIVE = new Set(["button", "input"]);
|
|
20
|
+
|
|
21
|
+
/** A press target: a native button, a link, or any other element made one (role, Tab stop, Enter and Space). */
|
|
22
|
+
export function ButtonRoot<T extends ValidComponent = "button">(props: ButtonRootProps<T>) {
|
|
23
|
+
const own = props as ButtonRootProps<ValidComponent> & Passed;
|
|
24
|
+
const [el, setEl] = createSignal<HTMLElement>();
|
|
25
|
+
const tag = () =>
|
|
26
|
+
typeof own.as === "string" ? own.as : own.as === undefined ? "button" : el()?.localName;
|
|
27
|
+
const native = () => NATIVE.has(tag() ?? "");
|
|
28
|
+
const made = () => !native() && !(tag() === "a" && own.href !== undefined);
|
|
29
|
+
|
|
30
|
+
// Bound on the element, since Solid's delegated handlers never reach inside a closed shadow root.
|
|
31
|
+
effect(el, (node) => {
|
|
32
|
+
if (!node) return;
|
|
33
|
+
const click = (event: MouseEvent) => {
|
|
34
|
+
if (own.disabled && !native()) {
|
|
35
|
+
event.preventDefault();
|
|
36
|
+
event.stopImmediatePropagation();
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
callEventHandler(
|
|
40
|
+
event as MouseEvent & { currentTarget: HTMLElement; target: Element },
|
|
41
|
+
own.onClick,
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
const keydown = (event: KeyboardEvent) => {
|
|
45
|
+
if (!made() || own.disabled || event.target !== node) return;
|
|
46
|
+
if (event.key === "Enter") {
|
|
47
|
+
event.preventDefault();
|
|
48
|
+
node.click();
|
|
49
|
+
} else if (event.key === " ") event.preventDefault();
|
|
50
|
+
};
|
|
51
|
+
const keyup = (event: KeyboardEvent) => {
|
|
52
|
+
if (!made() || own.disabled || event.target !== node || event.key !== " ") return;
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
node.click();
|
|
55
|
+
};
|
|
56
|
+
node.addEventListener("click", click);
|
|
57
|
+
node.addEventListener("keydown", keydown);
|
|
58
|
+
node.addEventListener("keyup", keyup);
|
|
59
|
+
return () => {
|
|
60
|
+
node.removeEventListener("click", click);
|
|
61
|
+
node.removeEventListener("keydown", keydown);
|
|
62
|
+
node.removeEventListener("keyup", keyup);
|
|
63
|
+
};
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<Polymorphic
|
|
68
|
+
{...merge(
|
|
69
|
+
{
|
|
70
|
+
get type() {
|
|
71
|
+
return native() ? "button" : undefined;
|
|
72
|
+
},
|
|
73
|
+
get role() {
|
|
74
|
+
return made() ? "button" : undefined;
|
|
75
|
+
},
|
|
76
|
+
get tabIndex() {
|
|
77
|
+
return made() && !own.disabled ? 0 : undefined;
|
|
78
|
+
},
|
|
79
|
+
get disabled() {
|
|
80
|
+
return native() ? own.disabled : undefined;
|
|
81
|
+
},
|
|
82
|
+
get "aria-disabled"() {
|
|
83
|
+
return !native() && own.disabled ? true : undefined;
|
|
84
|
+
},
|
|
85
|
+
get "data-disabled"() {
|
|
86
|
+
return own.disabled ? "" : undefined;
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
omit(own, "as", "disabled", "onClick", "ref"),
|
|
90
|
+
{
|
|
91
|
+
get as() {
|
|
92
|
+
return own.as;
|
|
93
|
+
},
|
|
94
|
+
defaultAs: "button" as ValidComponent,
|
|
95
|
+
ref: (node: HTMLElement) => {
|
|
96
|
+
setEl(node);
|
|
97
|
+
if (typeof own.ref === "function") own.ref(node);
|
|
98
|
+
},
|
|
99
|
+
},
|
|
100
|
+
)}
|
|
101
|
+
/>
|
|
102
|
+
);
|
|
103
|
+
}
|