@glasshome/ui 1.36.0 → 1.37.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 +11 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -3
- package/dist/lib/dock-slot-classes.d.ts +1 -0
- package/dist/lib/hold.d.ts +33 -1
- package/dist/lib/motion-classes.d.ts +2 -0
- package/dist/solid/dock-edit.d.ts +28 -0
- package/dist/solid/dock-paging.d.ts +13 -0
- package/dist/solid/dock.d.ts +18 -1
- package/dist/solid/face-toggle.d.ts +17 -0
- package/dist/solid/glyph-tile.d.ts +18 -0
- package/dist/solid/icon-picker.d.ts +4 -0
- package/dist/solid/index.d.ts +7 -3
- package/dist/solid/index.js +5813 -5197
- package/dist/solid/preview-tile.d.ts +14 -0
- package/dist/solid/slot-button.d.ts +9 -0
- package/dist/{utils-BhU5YZKh.js → utils-Duqv3Sgo.js} +680 -624
- package/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/lib/dock-slot-classes.ts +4 -0
- package/src/lib/hold.ts +154 -3
- package/src/lib/motion-classes.ts +11 -0
- package/src/solid/dashboard-preview.tsx +21 -4
- package/src/solid/dock-edit.tsx +267 -0
- package/src/solid/dock-paging.ts +37 -0
- package/src/solid/dock.tsx +254 -73
- package/src/solid/face-toggle.tsx +93 -0
- package/src/solid/glyph-tile.tsx +77 -0
- package/src/solid/icon-picker.tsx +40 -21
- package/src/solid/index.ts +7 -3
- package/src/solid/preview-tile.tsx +75 -4
- package/src/solid/slot-button.tsx +45 -0
- package/src/styles/theme.css +31 -0
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -84,7 +84,8 @@ export {
|
|
|
84
84
|
SETTLE_MOTION,
|
|
85
85
|
STAGGER,
|
|
86
86
|
} from "./lib/motion-classes.js";
|
|
87
|
-
export { HOLD_GRACE_MS, HOLD_MS, spendLongPress } from "./lib/hold.js";
|
|
87
|
+
export { createHold, HOLD_GRACE_MS, HOLD_MS, spendLongPress } from "./lib/hold.js";
|
|
88
|
+
export type { Hold, HoldOptions, HoldPress } from "./lib/hold.js";
|
|
88
89
|
export { MOTION_WINDOW_MS, startMotionWindow } from "./lib/motion-window.js";
|
|
89
90
|
export {
|
|
90
91
|
anchorToTriggerTop,
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/* The footprint of a dock slot: the dock's items, its add slot, and the slot buttons
|
|
2
|
+
* that stand for them in a form, so a pick looks like what lands in the dock. */
|
|
3
|
+
export const DOCK_SLOT_SHAPE =
|
|
4
|
+
"flex size-11 touch-manipulation items-center justify-center rounded-lg sm:size-12";
|
package/src/lib/hold.ts
CHANGED
|
@@ -6,13 +6,12 @@ export const HOLD_MS = 350;
|
|
|
6
6
|
* --duration-hold, which is HOLD_MS - HOLD_GRACE_MS. */
|
|
7
7
|
export const HOLD_GRACE_MS = 100;
|
|
8
8
|
|
|
9
|
+
const HOLD_SLOP_PX = 10;
|
|
9
10
|
const NATIVE_LONG_PRESS = ["contextmenu", "selectstart"] as const;
|
|
10
11
|
// Not pointercancel: Android may cancel the pointer before its own long press fires.
|
|
11
12
|
const RELEASE = ["pointerup", "touchend"] as const;
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
* must not land on what the hold opened under the finger. Lasts until the release. */
|
|
15
|
-
export function spendLongPress(): void {
|
|
14
|
+
function spendPress(): void {
|
|
16
15
|
if (typeof document === "undefined") return;
|
|
17
16
|
const cancel = (e: Event) => e.preventDefault();
|
|
18
17
|
for (const type of NATIVE_LONG_PRESS) document.addEventListener(type, cancel, true);
|
|
@@ -26,3 +25,155 @@ export function spendLongPress(): void {
|
|
|
26
25
|
const fallback = setTimeout(release, 5000);
|
|
27
26
|
for (const type of RELEASE) window.addEventListener(type, onEnd, true);
|
|
28
27
|
}
|
|
28
|
+
|
|
29
|
+
/** A fired hold spends the press: the browser's own long press (text selection, context menu)
|
|
30
|
+
* must not land on what the hold opened under the finger. Lasts until the release.
|
|
31
|
+
* @deprecated use createHold, which also spends the release. */
|
|
32
|
+
export function spendLongPress(): void {
|
|
33
|
+
spendPress();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** The release of a fired hold belongs to the hold: a finger's compat mousedown and click, or a
|
|
37
|
+
* mouse's click, never reach what the hold opened under the pointer. */
|
|
38
|
+
function spendRelease(pressed: EventTarget | null | undefined): () => void {
|
|
39
|
+
if (typeof window === "undefined") return () => {};
|
|
40
|
+
const stopTouch = (e: Event) => {
|
|
41
|
+
if (e.cancelable) e.preventDefault();
|
|
42
|
+
untouch();
|
|
43
|
+
};
|
|
44
|
+
// A pressed node the hold took out of the page still gets its touchend; the window does not.
|
|
45
|
+
const untouch = () => {
|
|
46
|
+
window.removeEventListener("touchend", stopTouch, true);
|
|
47
|
+
pressed?.removeEventListener("touchend", stopTouch);
|
|
48
|
+
};
|
|
49
|
+
const swallow = (e: Event) => {
|
|
50
|
+
e.preventDefault();
|
|
51
|
+
e.stopPropagation();
|
|
52
|
+
off();
|
|
53
|
+
};
|
|
54
|
+
const afterUp = () => setTimeout(off, 0);
|
|
55
|
+
const off = () => {
|
|
56
|
+
untouch();
|
|
57
|
+
window.removeEventListener("click", swallow, true);
|
|
58
|
+
window.removeEventListener("pointerup", afterUp, true);
|
|
59
|
+
window.removeEventListener("pointercancel", off, true);
|
|
60
|
+
window.removeEventListener("pointerdown", off, true);
|
|
61
|
+
};
|
|
62
|
+
window.addEventListener("touchend", stopTouch, { capture: true, passive: false });
|
|
63
|
+
pressed?.addEventListener("touchend", stopTouch, { passive: false });
|
|
64
|
+
window.addEventListener("click", swallow, true);
|
|
65
|
+
window.addEventListener("pointerup", afterUp, true);
|
|
66
|
+
window.addEventListener("pointercancel", off, true);
|
|
67
|
+
window.addEventListener("pointerdown", off, true);
|
|
68
|
+
return off;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** What a hold reads from a press: a PointerEvent fits, and so does a touch or a recorded point. */
|
|
72
|
+
export interface HoldPress {
|
|
73
|
+
clientX: number;
|
|
74
|
+
clientY: number;
|
|
75
|
+
pointerId?: number;
|
|
76
|
+
target?: EventTarget | null;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface HoldOptions {
|
|
80
|
+
onHold: () => void;
|
|
81
|
+
delayMs?: number;
|
|
82
|
+
slopPx?: number;
|
|
83
|
+
/** The press outlasted a tap: show the hold fill from where it started. */
|
|
84
|
+
onArm?: (at: { x: number; y: number }) => void;
|
|
85
|
+
/** The armed press ended, fired or not, or moved away. */
|
|
86
|
+
onDisarm?: () => void;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface Hold {
|
|
90
|
+
down: (press: HoldPress) => void;
|
|
91
|
+
move: (press: HoldPress) => void;
|
|
92
|
+
up: () => void;
|
|
93
|
+
cancel: () => void;
|
|
94
|
+
/** The current press, or the last one, became a hold. */
|
|
95
|
+
fired: () => boolean;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** The one hold door: a press held still for `delayMs` calls `onHold`, after it spends the
|
|
99
|
+
* press and its release so neither lands on what the hold opened. One press at a time; the
|
|
100
|
+
* release is watched on the window too, so a release off the pressed node still ends it. */
|
|
101
|
+
export function createHold(opts: HoldOptions): Hold {
|
|
102
|
+
let grace: ReturnType<typeof setTimeout> | undefined;
|
|
103
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
104
|
+
let start = { x: 0, y: 0 };
|
|
105
|
+
let pointer: number | undefined;
|
|
106
|
+
let pressing = false;
|
|
107
|
+
let armed = false;
|
|
108
|
+
let didFire = false;
|
|
109
|
+
let unspend: (() => void) | undefined;
|
|
110
|
+
|
|
111
|
+
const other = (press: { pointerId?: number }) =>
|
|
112
|
+
pointer !== undefined && press.pointerId !== undefined && press.pointerId !== pointer;
|
|
113
|
+
|
|
114
|
+
const onWindowEnd = (e: PointerEvent) => {
|
|
115
|
+
if (other(e)) return;
|
|
116
|
+
if (e.type === "pointercancel") cancel();
|
|
117
|
+
else up();
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const stop = () => {
|
|
121
|
+
clearTimeout(grace);
|
|
122
|
+
clearTimeout(timer);
|
|
123
|
+
grace = timer = undefined;
|
|
124
|
+
if (armed) opts.onDisarm?.();
|
|
125
|
+
armed = false;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const up = () => {
|
|
129
|
+
stop();
|
|
130
|
+
pressing = false;
|
|
131
|
+
if (typeof window === "undefined") return;
|
|
132
|
+
window.removeEventListener("pointerup", onWindowEnd, true);
|
|
133
|
+
window.removeEventListener("pointercancel", onWindowEnd, true);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const cancel = () => {
|
|
137
|
+
up();
|
|
138
|
+
unspend?.();
|
|
139
|
+
unspend = undefined;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
return {
|
|
143
|
+
down(press) {
|
|
144
|
+
if (pressing && other(press)) return;
|
|
145
|
+
cancel();
|
|
146
|
+
pressing = true;
|
|
147
|
+
pointer = press.pointerId;
|
|
148
|
+
didFire = false;
|
|
149
|
+
start = { x: press.clientX, y: press.clientY };
|
|
150
|
+
if (typeof window !== "undefined") {
|
|
151
|
+
window.addEventListener("pointerup", onWindowEnd, true);
|
|
152
|
+
window.addEventListener("pointercancel", onWindowEnd, true);
|
|
153
|
+
}
|
|
154
|
+
const delay = opts.delayMs ?? HOLD_MS;
|
|
155
|
+
grace = setTimeout(
|
|
156
|
+
() => {
|
|
157
|
+
armed = true;
|
|
158
|
+
opts.onArm?.(start);
|
|
159
|
+
},
|
|
160
|
+
Math.min(HOLD_GRACE_MS, delay),
|
|
161
|
+
);
|
|
162
|
+
timer = setTimeout(() => {
|
|
163
|
+
timer = undefined;
|
|
164
|
+
didFire = true;
|
|
165
|
+
spendPress();
|
|
166
|
+
unspend = spendRelease(press.target);
|
|
167
|
+
opts.onHold();
|
|
168
|
+
}, delay);
|
|
169
|
+
},
|
|
170
|
+
move(press) {
|
|
171
|
+
if (timer === undefined || other(press)) return;
|
|
172
|
+
const slop = opts.slopPx ?? HOLD_SLOP_PX;
|
|
173
|
+
if (Math.hypot(press.clientX - start.x, press.clientY - start.y) > slop) stop();
|
|
174
|
+
},
|
|
175
|
+
up,
|
|
176
|
+
cancel,
|
|
177
|
+
fired: () => didFire,
|
|
178
|
+
};
|
|
179
|
+
}
|
|
@@ -49,6 +49,17 @@ export const PRESS_DIP = "active:scale-[0.97]";
|
|
|
49
49
|
export const HOLD_MOTION =
|
|
50
50
|
"scale-0 opacity-0 transition-[scale,opacity] duration-(--duration-state) ease-(--ease-contract) data-[hold]:scale-100 data-[hold]:opacity-100 data-[hold]:duration-(--duration-hold) data-[hold]:ease-linear data-[fired]:scale-100 data-[fired]:opacity-0";
|
|
51
51
|
|
|
52
|
+
/* A newcomer in the dock: a ring of the accent that fades once and leaves
|
|
53
|
+
* nothing behind. The host sets data-arrived on the item. */
|
|
54
|
+
export const DOCK_ARRIVE_MOTION =
|
|
55
|
+
"pointer-events-none absolute inset-0 rounded-[inherit] opacity-0 ring-2 ring-primary shadow-[0_0_14px_var(--primary)] group-data-[arrived]:animate-dock-arrive";
|
|
56
|
+
|
|
57
|
+
/* Edit mode on the dock: every item sways a hair, neighbours out of step. The
|
|
58
|
+
* host sets data-editing; the item under a finger stands still (data-dragging).
|
|
59
|
+
* Rotate only, so it stays on the compositor. */
|
|
60
|
+
export const DOCK_EDIT_MOTION =
|
|
61
|
+
"data-[editing]:animate-dock-jiggle data-[editing]:nth-even:[animation-direction:alternate-reverse] data-[dragging]:animate-none";
|
|
62
|
+
|
|
52
63
|
/* MODAL_MOTION's twin for a surface that stays mounted: it rises out of what
|
|
53
64
|
* opened it, and its closed state is a resting style, since a mount door's exit
|
|
54
65
|
* ends and leaves the element showing. */
|
|
@@ -94,8 +94,16 @@ export function DashboardPreview(props: {
|
|
|
94
94
|
|
|
95
95
|
const u = (n: number) => `calc(var(--dp-unit) * ${n})`;
|
|
96
96
|
|
|
97
|
+
/** The share of a small icon tile's shorter side its glyph fills. */
|
|
98
|
+
const GLYPH_SHARE = 0.42;
|
|
99
|
+
|
|
100
|
+
/** Below this board width a name would be a smudge, so the glyph takes the tile instead. */
|
|
101
|
+
const NAMED_FROM = "@max-[520px]:hidden";
|
|
102
|
+
|
|
97
103
|
function IconTile(props: { tile: DashboardPreviewTile }) {
|
|
98
104
|
const roomy = () => props.tile.w >= 2 && props.tile.h >= 2;
|
|
105
|
+
const fitted = () =>
|
|
106
|
+
Math.min(span(props.tile.w, CELL.w), span(props.tile.h, CELL.h)) * GLYPH_SHARE;
|
|
99
107
|
return (
|
|
100
108
|
<div
|
|
101
109
|
data-slot="dashboard-preview-icon-tile"
|
|
@@ -104,16 +112,25 @@ function IconTile(props: { tile: DashboardPreviewTile }) {
|
|
|
104
112
|
"text-muted-foreground flex h-full w-full items-center overflow-hidden",
|
|
105
113
|
roomy() ? "flex-col justify-center text-center" : "justify-center",
|
|
106
114
|
)}
|
|
107
|
-
style={{
|
|
115
|
+
style={{
|
|
116
|
+
"border-radius": u(20),
|
|
117
|
+
gap: u(roomy() ? 8 : 10),
|
|
118
|
+
padding: u(12),
|
|
119
|
+
"--dp-glyph": u(roomy() ? 32 : 22),
|
|
120
|
+
"--dp-glyph-fit": u(fitted()),
|
|
121
|
+
}}
|
|
108
122
|
>
|
|
109
123
|
<Icon
|
|
110
124
|
icon={props.tile.icon}
|
|
111
125
|
width="1em"
|
|
112
126
|
height="1em"
|
|
113
|
-
|
|
114
|
-
class="text-foreground/80 shrink-0"
|
|
127
|
+
class="text-foreground/80 shrink-0 text-[length:var(--dp-glyph)] @max-[520px]:text-[length:var(--dp-glyph-fit)]"
|
|
115
128
|
/>
|
|
116
|
-
<div
|
|
129
|
+
<div
|
|
130
|
+
data-slot="dashboard-preview-icon-tile-label"
|
|
131
|
+
class={cn("flex min-w-0 flex-col", NAMED_FROM)}
|
|
132
|
+
style={{ gap: u(4) }}
|
|
133
|
+
>
|
|
117
134
|
<span class="text-foreground/90 truncate font-medium" style={{ "font-size": u(13) }}>
|
|
118
135
|
{props.tile.label}
|
|
119
136
|
</span>
|
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { createSignal, For, onCleanup, Show } from "solid-js";
|
|
2
|
+
import { Portal } from "solid-js/web";
|
|
3
|
+
import { Z_CLASS } from "../lib/layers.js";
|
|
4
|
+
import { cn } from "../lib/utils.js";
|
|
5
|
+
import { type EdgeZone, edgeZone, moveId, slotAt } from "./dock-paging.js";
|
|
6
|
+
import { usePortalMount } from "./portal-mount.js";
|
|
7
|
+
|
|
8
|
+
const DRAG_SLOP = 6;
|
|
9
|
+
|
|
10
|
+
interface ReorderOptions {
|
|
11
|
+
bar: () => HTMLElement | undefined;
|
|
12
|
+
vertical: () => boolean;
|
|
13
|
+
ids: () => string[];
|
|
14
|
+
enabled: () => boolean;
|
|
15
|
+
onReorder: (ids: string[]) => void;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Pointer reorder for the dock strip: the dragged cell follows the pointer along the axis, the others take their preview slots through flex order, and the new order is reported once on release. */
|
|
19
|
+
export function createDockReorder(options: ReorderOptions) {
|
|
20
|
+
const [dragId, setDragId] = createSignal<string | null>(null);
|
|
21
|
+
const [preview, setPreview] = createSignal<string[] | null>(null);
|
|
22
|
+
const [lift, setLift] = createSignal(0);
|
|
23
|
+
const [point, setPoint] = createSignal<{ x: number; y: number } | null>(null);
|
|
24
|
+
let swallowClick = false;
|
|
25
|
+
let stopDrag: (() => void) | undefined;
|
|
26
|
+
onCleanup(() => stopDrag?.());
|
|
27
|
+
|
|
28
|
+
const along = (x: number, y: number) => (options.vertical() ? y : x);
|
|
29
|
+
const startOf = (rect: DOMRect) => (options.vertical() ? rect.top : rect.left);
|
|
30
|
+
const sizeOf = (rect: DOMRect) => (options.vertical() ? rect.height : rect.width);
|
|
31
|
+
|
|
32
|
+
function begin(id: string, down: PointerEvent) {
|
|
33
|
+
if (!options.enabled() || down.button !== 0) return;
|
|
34
|
+
if ((down.target as Element).closest('[data-slot="dock-item-remove"]')) return;
|
|
35
|
+
const bar = options.bar();
|
|
36
|
+
if (!bar) return;
|
|
37
|
+
swallowClick = false;
|
|
38
|
+
stopDrag?.();
|
|
39
|
+
|
|
40
|
+
const ids = options.ids();
|
|
41
|
+
let dragging = false;
|
|
42
|
+
let contentMids: number[] = [];
|
|
43
|
+
let grab = 0;
|
|
44
|
+
let half = 0;
|
|
45
|
+
let last = { x: down.clientX, y: down.clientY };
|
|
46
|
+
|
|
47
|
+
const slotMids = () => {
|
|
48
|
+
const barRect = bar.getBoundingClientRect();
|
|
49
|
+
const scrolled = options.vertical() ? bar.scrollTop : bar.scrollLeft;
|
|
50
|
+
return contentMids.map((mid) => mid + startOf(barRect) - scrolled);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const measure = () => {
|
|
54
|
+
const barRect = bar.getBoundingClientRect();
|
|
55
|
+
const scrolled = options.vertical() ? bar.scrollTop : bar.scrollLeft;
|
|
56
|
+
const rects = [...bar.querySelectorAll('[data-slot="dock-cell"]')].map((cell) =>
|
|
57
|
+
cell.getBoundingClientRect(),
|
|
58
|
+
);
|
|
59
|
+
contentMids = rects.map(
|
|
60
|
+
(rect) => startOf(rect) + sizeOf(rect) / 2 - startOf(barRect) + scrolled,
|
|
61
|
+
);
|
|
62
|
+
const origin = rects[ids.indexOf(id)];
|
|
63
|
+
half = origin ? sizeOf(origin) / 2 : 0;
|
|
64
|
+
grab = along(down.clientX, down.clientY) - (slotMids()[ids.indexOf(id)] ?? 0);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const track = () => {
|
|
68
|
+
const mids = slotMids();
|
|
69
|
+
const center = along(last.x, last.y) - grab;
|
|
70
|
+
const to = slotAt(mids, center);
|
|
71
|
+
const barRect = bar.getBoundingClientRect();
|
|
72
|
+
const lo = startOf(barRect) + half;
|
|
73
|
+
const hi = startOf(barRect) + sizeOf(barRect) - half;
|
|
74
|
+
setPreview(moveId(ids, id, to));
|
|
75
|
+
setLift(Math.min(hi, Math.max(lo, center)) - (mids[to] ?? center));
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const onMove = (move: PointerEvent) => {
|
|
79
|
+
if (move.pointerId !== down.pointerId) return;
|
|
80
|
+
last = { x: move.clientX, y: move.clientY };
|
|
81
|
+
if (!dragging) {
|
|
82
|
+
if (Math.hypot(last.x - down.clientX, last.y - down.clientY) < DRAG_SLOP) return;
|
|
83
|
+
dragging = true;
|
|
84
|
+
measure();
|
|
85
|
+
setDragId(id);
|
|
86
|
+
}
|
|
87
|
+
track();
|
|
88
|
+
setPoint({ ...last });
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
const finish = (commit: boolean) => {
|
|
92
|
+
const next = preview();
|
|
93
|
+
const moved = dragging;
|
|
94
|
+
stopDrag?.();
|
|
95
|
+
if (moved) {
|
|
96
|
+
swallowClick = true;
|
|
97
|
+
// The release click lands on the dock bar when the item moved under the pointer, so no item ever sees it; the next press ends the claim.
|
|
98
|
+
window.addEventListener("pointerdown", () => (swallowClick = false), {
|
|
99
|
+
once: true,
|
|
100
|
+
capture: true,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
if (commit && moved && next && next.join("\n") !== ids.join("\n")) options.onReorder(next);
|
|
104
|
+
};
|
|
105
|
+
const onUp = (up: PointerEvent) => {
|
|
106
|
+
if (up.pointerId === down.pointerId) finish(true);
|
|
107
|
+
};
|
|
108
|
+
const onCancel = (cancel: PointerEvent) => {
|
|
109
|
+
if (cancel.pointerId === down.pointerId) finish(false);
|
|
110
|
+
};
|
|
111
|
+
const onScroll = () => {
|
|
112
|
+
if (dragging) track();
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
window.addEventListener("pointermove", onMove);
|
|
116
|
+
window.addEventListener("pointerup", onUp);
|
|
117
|
+
window.addEventListener("pointercancel", onCancel);
|
|
118
|
+
bar.addEventListener("scroll", onScroll);
|
|
119
|
+
stopDrag = () => {
|
|
120
|
+
window.removeEventListener("pointermove", onMove);
|
|
121
|
+
window.removeEventListener("pointerup", onUp);
|
|
122
|
+
window.removeEventListener("pointercancel", onCancel);
|
|
123
|
+
bar.removeEventListener("scroll", onScroll);
|
|
124
|
+
stopDrag = undefined;
|
|
125
|
+
setDragId(null);
|
|
126
|
+
setPreview(null);
|
|
127
|
+
setLift(0);
|
|
128
|
+
setPoint(null);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function nudge(id: string, step: -1 | 1) {
|
|
133
|
+
const ids = options.ids();
|
|
134
|
+
const to = ids.indexOf(id) + step;
|
|
135
|
+
if (ids.indexOf(id) < 0 || to < 0 || to >= ids.length) return;
|
|
136
|
+
options.onReorder(moveId(ids, id, to));
|
|
137
|
+
requestAnimationFrame(() =>
|
|
138
|
+
options
|
|
139
|
+
.bar()
|
|
140
|
+
?.querySelector<HTMLElement>(`[data-dock-id="${CSS.escape(id)}"]`)
|
|
141
|
+
?.focus(),
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** A held press on a strip that scrolls starts the browser's own autoscroll once the pointer leaves it; cancelling the default press keeps the strip still. */
|
|
146
|
+
function onMouseDown(e: MouseEvent) {
|
|
147
|
+
if (options.enabled() && !(e.target as Element).closest('[data-slot="dock-item-remove"]')) {
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** A drag ends in a click on the item under the pointer; that click belongs to no one. */
|
|
153
|
+
function onClickCapture(e: MouseEvent) {
|
|
154
|
+
if (!swallowClick) return;
|
|
155
|
+
swallowClick = false;
|
|
156
|
+
e.preventDefault();
|
|
157
|
+
e.stopPropagation();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
return { dragId, preview, lift, point, begin, nudge, onMouseDown, onClickCapture };
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const ZONES: EdgeZone[] = ["left", "bottom", "right"];
|
|
164
|
+
|
|
165
|
+
const ZONE_BOX: Record<EdgeZone, string> = {
|
|
166
|
+
left: "inset-y-0 left-0 w-1/4 border-r-2",
|
|
167
|
+
right: "inset-y-0 right-0 w-1/4 border-l-2",
|
|
168
|
+
bottom: "inset-x-1/4 bottom-0 h-2/5 border-t-2",
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
const GRIP_BOX: Record<EdgeZone, string> = {
|
|
172
|
+
bottom: "top-0 left-1/2 h-3 w-12 -translate-x-1/2",
|
|
173
|
+
left: "top-1/2 left-0 h-12 w-3 -translate-y-1/2",
|
|
174
|
+
right: "top-1/2 right-0 h-12 w-3 -translate-y-1/2",
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const GRIP_PILL: Record<EdgeZone, string> = {
|
|
178
|
+
bottom: "h-1 w-8",
|
|
179
|
+
left: "h-8 w-1",
|
|
180
|
+
right: "h-8 w-1",
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
/** The grip on a dock's outer edge: carry it toward a screen edge and the three landing zones light up. */
|
|
184
|
+
export function DockMoveGrip(props: {
|
|
185
|
+
placement: EdgeZone;
|
|
186
|
+
onMove: (placement: EdgeZone) => void;
|
|
187
|
+
}) {
|
|
188
|
+
const [carrying, setCarrying] = createSignal(false);
|
|
189
|
+
const [zone, setZone] = createSignal<EdgeZone | null>(null);
|
|
190
|
+
let stop: (() => void) | undefined;
|
|
191
|
+
onCleanup(() => stop?.());
|
|
192
|
+
|
|
193
|
+
const zoneAt = (e: PointerEvent) =>
|
|
194
|
+
edgeZone(
|
|
195
|
+
{ x: e.clientX, y: e.clientY },
|
|
196
|
+
{ width: window.innerWidth, height: window.innerHeight },
|
|
197
|
+
);
|
|
198
|
+
|
|
199
|
+
function onPointerDown(down: PointerEvent) {
|
|
200
|
+
if (down.button !== 0) return;
|
|
201
|
+
stop?.();
|
|
202
|
+
setCarrying(true);
|
|
203
|
+
setZone(zoneAt(down));
|
|
204
|
+
const onMove = (move: PointerEvent) => {
|
|
205
|
+
if (move.pointerId === down.pointerId) setZone(zoneAt(move));
|
|
206
|
+
};
|
|
207
|
+
const onUp = (up: PointerEvent) => {
|
|
208
|
+
if (up.pointerId !== down.pointerId) return;
|
|
209
|
+
const landed = zoneAt(up);
|
|
210
|
+
stop?.();
|
|
211
|
+
if (landed && landed !== props.placement) props.onMove(landed);
|
|
212
|
+
};
|
|
213
|
+
const onCancel = (cancel: PointerEvent) => {
|
|
214
|
+
if (cancel.pointerId === down.pointerId) stop?.();
|
|
215
|
+
};
|
|
216
|
+
window.addEventListener("pointermove", onMove);
|
|
217
|
+
window.addEventListener("pointerup", onUp);
|
|
218
|
+
window.addEventListener("pointercancel", onCancel);
|
|
219
|
+
stop = () => {
|
|
220
|
+
window.removeEventListener("pointermove", onMove);
|
|
221
|
+
window.removeEventListener("pointerup", onUp);
|
|
222
|
+
window.removeEventListener("pointercancel", onCancel);
|
|
223
|
+
stop = undefined;
|
|
224
|
+
setCarrying(false);
|
|
225
|
+
setZone(null);
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return (
|
|
230
|
+
<>
|
|
231
|
+
<div
|
|
232
|
+
data-slot="dock-grip"
|
|
233
|
+
aria-hidden="true"
|
|
234
|
+
onPointerDown={onPointerDown}
|
|
235
|
+
class={cn(
|
|
236
|
+
"absolute z-10 flex cursor-grab touch-none items-center justify-center",
|
|
237
|
+
GRIP_BOX[props.placement],
|
|
238
|
+
)}
|
|
239
|
+
>
|
|
240
|
+
<span class={cn("bg-foreground/35 rounded-full", GRIP_PILL[props.placement])} />
|
|
241
|
+
</div>
|
|
242
|
+
<Show when={carrying()}>
|
|
243
|
+
<Portal mount={usePortalMount()}>
|
|
244
|
+
<div
|
|
245
|
+
data-slot="dock-edge-zones"
|
|
246
|
+
aria-hidden="true"
|
|
247
|
+
class={cn("pointer-events-none fixed inset-0", Z_CLASS.overlay)}
|
|
248
|
+
>
|
|
249
|
+
<For each={ZONES}>
|
|
250
|
+
{(name) => (
|
|
251
|
+
<div
|
|
252
|
+
data-zone={name}
|
|
253
|
+
data-active={zone() === name ? "" : undefined}
|
|
254
|
+
class={cn(
|
|
255
|
+
"bg-primary/10 border-primary/30 absolute transition-colors duration-(--duration-state)",
|
|
256
|
+
"data-[active]:bg-primary/25 data-[active]:border-primary",
|
|
257
|
+
ZONE_BOX[name],
|
|
258
|
+
)}
|
|
259
|
+
/>
|
|
260
|
+
)}
|
|
261
|
+
</For>
|
|
262
|
+
</div>
|
|
263
|
+
</Portal>
|
|
264
|
+
</Show>
|
|
265
|
+
</>
|
|
266
|
+
);
|
|
267
|
+
}
|
package/src/solid/dock-paging.ts
CHANGED
|
@@ -24,3 +24,40 @@ export function stripWidth(available: number, itemWidth: number, gap: number): n
|
|
|
24
24
|
const count = Math.max(1, Math.floor((available + gap) / (itemWidth + gap)));
|
|
25
25
|
return Math.min(available, count * itemWidth + (count - 1) * gap);
|
|
26
26
|
}
|
|
27
|
+
|
|
28
|
+
/** Where a dragged item lands: the slot whose midpoint is nearest the pointer. */
|
|
29
|
+
export function slotAt(mids: readonly number[], pointer: number): number {
|
|
30
|
+
let best = 0;
|
|
31
|
+
let bestGap = Number.POSITIVE_INFINITY;
|
|
32
|
+
mids.forEach((mid, i) => {
|
|
33
|
+
const gap = Math.abs(mid - pointer);
|
|
34
|
+
if (gap < bestGap) {
|
|
35
|
+
best = i;
|
|
36
|
+
bestGap = gap;
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return best;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The ids with one moved to a slot; the others keep their order. */
|
|
43
|
+
export function moveId(ids: readonly string[], id: string, to: number): string[] {
|
|
44
|
+
const from = ids.indexOf(id);
|
|
45
|
+
if (from < 0) return [...ids];
|
|
46
|
+
const rest = ids.filter((_, i) => i !== from);
|
|
47
|
+
rest.splice(Math.max(0, Math.min(rest.length, to)), 0, id);
|
|
48
|
+
return rest;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type EdgeZone = "left" | "right" | "bottom";
|
|
52
|
+
|
|
53
|
+
/** The viewport edge a pointer is closest to for a dock being carried: outer quarters are the sides, the lower part of the middle is the bottom, the rest is no edge. */
|
|
54
|
+
export function edgeZone(
|
|
55
|
+
point: { x: number; y: number },
|
|
56
|
+
viewport: { width: number; height: number },
|
|
57
|
+
): EdgeZone | null {
|
|
58
|
+
if (viewport.width <= 0 || viewport.height <= 0) return null;
|
|
59
|
+
if (point.x < viewport.width * 0.25) return "left";
|
|
60
|
+
if (point.x > viewport.width * 0.75) return "right";
|
|
61
|
+
if (point.y > viewport.height * 0.6) return "bottom";
|
|
62
|
+
return null;
|
|
63
|
+
}
|