@alulore/kumiko 0.0.0-stage → 0.14.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/dist/badge/badge.css +287 -0
- package/dist/badge/badge.d.ts +40 -0
- package/dist/badge/badge.js +51 -0
- package/dist/badge/index.d.ts +1 -0
- package/dist/badge/index.js +1 -0
- package/dist/board/board-api.d.ts +147 -0
- package/dist/board/board-api.js +139 -0
- package/dist/board/board-card-menu.d.ts +17 -0
- package/dist/board/board-card-menu.js +53 -0
- package/dist/board/board-drag-events.d.ts +12 -0
- package/dist/board/board-drag-events.js +86 -0
- package/dist/board/board-fold.d.ts +10 -0
- package/dist/board/board-fold.js +30 -0
- package/dist/board/board-group.d.ts +10 -0
- package/dist/board/board-group.js +28 -0
- package/dist/board/board-keys.d.ts +16 -0
- package/dist/board/board-keys.js +65 -0
- package/dist/board/board-locked-rows.d.ts +23 -0
- package/dist/board/board-locked-rows.js +77 -0
- package/dist/board/board-motion.d.ts +7 -0
- package/dist/board/board-motion.js +102 -0
- package/dist/board/board-primitive.d.ts +53 -0
- package/dist/board/board-primitive.js +66 -0
- package/dist/board/board-pull.d.ts +6 -0
- package/dist/board/board-pull.js +94 -0
- package/dist/board/board-selection.d.ts +42 -0
- package/dist/board/board-selection.js +130 -0
- package/dist/board/board-slots.d.ts +1003 -0
- package/dist/board/board-slots.js +250 -0
- package/dist/board/board-types.d.ts +99 -0
- package/dist/board/board-types.js +1 -0
- package/dist/board/board.css +343 -0
- package/dist/board/board.d.ts +290 -0
- package/dist/board/board.js +23 -0
- package/dist/board/card-menu.d.ts +21 -0
- package/dist/board/card-menu.js +79 -0
- package/dist/board/cell-key.d.ts +6 -0
- package/dist/board/cell-key.js +16 -0
- package/dist/board/index.d.ts +6 -0
- package/dist/board/index.js +5 -0
- package/dist/board/interactive.d.ts +18 -0
- package/dist/board/interactive.js +51 -0
- package/dist/board/keep-capture.d.ts +16 -0
- package/dist/board/keep-capture.js +57 -0
- package/dist/board/landing.d.ts +5 -0
- package/dist/board/landing.js +10 -0
- package/dist/board/press-to-drop.d.ts +9 -0
- package/dist/board/press-to-drop.js +28 -0
- package/dist/board/refused-home.d.ts +16 -0
- package/dist/board/refused-home.js +50 -0
- package/dist/board/repeat-gate.d.ts +4 -0
- package/dist/board/repeat-gate.js +12 -0
- package/dist/board/scrollbar-gutter.d.ts +1 -0
- package/dist/board/scrollbar-gutter.js +27 -0
- package/dist/board/touch-hold.d.ts +19 -0
- package/dist/board/touch-hold.js +103 -0
- package/dist/button/button.css +576 -0
- package/dist/button/button.d.ts +50 -0
- package/dist/button/button.js +70 -0
- package/dist/button/index.d.ts +2 -0
- package/dist/button/index.js +1 -0
- package/dist/checkbox/checkbox-mark.d.ts +8 -0
- package/dist/checkbox/checkbox-mark.js +17 -0
- package/dist/checkbox/checkbox-marks.d.ts +33 -0
- package/dist/checkbox/checkbox-marks.js +30 -0
- package/dist/checkbox/checkbox.css +201 -0
- package/dist/checkbox/checkbox.d.ts +26 -0
- package/dist/checkbox/checkbox.js +11 -0
- package/dist/checkbox/index.d.ts +3 -0
- package/dist/checkbox/index.js +3 -0
- package/dist/chip/chip.css +251 -0
- package/dist/chip/chip.d.ts +39 -0
- package/dist/chip/chip.js +25 -0
- package/dist/chip/index.d.ts +1 -0
- package/dist/chip/index.js +1 -0
- package/dist/dialog/dialog.css +247 -0
- package/dist/dialog/index.d.ts +3 -0
- package/dist/dialog/index.js +3 -0
- package/dist/hover-card/hover-card.css +149 -0
- package/dist/hover-card/hover-card.d.ts +3 -0
- package/dist/hover-card/hover-card.js +8 -0
- package/dist/hover-card/index.d.ts +2 -0
- package/dist/hover-card/index.js +2 -0
- package/dist/input/index.d.ts +5 -0
- package/dist/input/index.js +5 -0
- package/dist/input/input-attributes.d.ts +12 -0
- package/dist/input/input-attributes.js +9 -0
- package/dist/input/input.css +752 -0
- package/dist/input/input.d.ts +28 -0
- package/dist/input/input.js +9 -0
- package/dist/input/search-input.d.ts +41 -0
- package/dist/input/search-input.js +47 -0
- package/dist/kbd/index.d.ts +1 -0
- package/dist/kbd/index.js +1 -0
- package/dist/kbd/kbd.css +77 -0
- package/dist/kbd/kbd.d.ts +21 -0
- package/dist/kbd/kbd.js +37 -0
- package/dist/label/index.d.ts +1 -0
- package/dist/label/index.js +1 -0
- package/dist/label/label.css +37 -0
- package/dist/label/label.d.ts +13 -0
- package/dist/label/label.js +10 -0
- package/dist/menu/index.d.ts +3 -0
- package/dist/menu/index.js +3 -0
- package/dist/menu/menu.css +349 -0
- package/dist/menu/menu.d.ts +11 -0
- package/dist/menu/menu.js +86 -0
- package/dist/menu/point-menu.d.ts +26 -0
- package/dist/menu/point-menu.js +51 -0
- package/dist/motion/flip-math.d.ts +19 -0
- package/dist/motion/flip-math.js +33 -0
- package/dist/motion/flip.d.ts +40 -0
- package/dist/motion/flip.js +203 -0
- package/dist/motion/index.d.ts +2 -0
- package/dist/motion/index.js +2 -0
- package/dist/motion/motion.css +169 -0
- package/dist/popover/index.d.ts +1 -0
- package/dist/popover/index.js +1 -0
- package/dist/popover/popover.css +208 -0
- package/dist/progress/index.d.ts +2 -0
- package/dist/progress/index.js +2 -0
- package/dist/progress/progress.css +390 -0
- package/dist/progress/progress.d.ts +36 -0
- package/dist/progress/progress.js +107 -0
- package/dist/select/index.d.ts +3 -0
- package/dist/select/index.js +2 -0
- package/dist/select/select-labels.d.ts +6 -0
- package/dist/select/select-labels.js +64 -0
- package/dist/select/select-search.d.ts +18 -0
- package/dist/select/select-search.js +10 -0
- package/dist/select/select.css +470 -0
- package/dist/select/select.d.ts +44 -0
- package/dist/select/select.js +92 -0
- package/dist/separator/index.d.ts +2 -0
- package/dist/separator/index.js +1 -0
- package/dist/separator/separator.css +32 -0
- package/dist/separator/separator.d.ts +17 -0
- package/dist/separator/separator.js +10 -0
- package/dist/slider/index.d.ts +2 -0
- package/dist/slider/index.js +2 -0
- package/dist/slider/slider-bubble.css +203 -0
- package/dist/slider/slider-feel.d.ts +33 -0
- package/dist/slider/slider-feel.js +153 -0
- package/dist/slider/slider-format.d.ts +13 -0
- package/dist/slider/slider-format.js +15 -0
- package/dist/slider/slider-marks.d.ts +16 -0
- package/dist/slider/slider-marks.js +32 -0
- package/dist/slider/slider.css +335 -0
- package/dist/slider/slider.d.ts +34 -0
- package/dist/slider/slider.js +94 -0
- package/dist/smooth-caret/caret-box.d.ts +7 -0
- package/dist/smooth-caret/caret-box.js +8 -0
- package/dist/smooth-caret/caret-controller.d.ts +7 -0
- package/dist/smooth-caret/caret-controller.js +222 -0
- package/dist/smooth-caret/caret-geometry.d.ts +11 -0
- package/dist/smooth-caret/caret-geometry.js +230 -0
- package/dist/smooth-caret/caret-glide.d.ts +13 -0
- package/dist/smooth-caret/caret-glide.js +76 -0
- package/dist/smooth-caret/caret-host.d.ts +5 -0
- package/dist/smooth-caret/caret-host.js +40 -0
- package/dist/smooth-caret/index.d.ts +7 -0
- package/dist/smooth-caret/index.js +7 -0
- package/dist/smooth-caret/remote-carets.d.ts +40 -0
- package/dist/smooth-caret/remote-carets.js +130 -0
- package/dist/smooth-caret/smooth-caret.css +157 -0
- package/dist/smooth-caret/smooth-caret.d.ts +5 -0
- package/dist/smooth-caret/smooth-caret.js +9 -0
- package/dist/smooth-caret/use-smooth-caret.d.ts +5 -0
- package/dist/smooth-caret/use-smooth-caret.js +9 -0
- package/dist/switch/index.d.ts +2 -0
- package/dist/switch/index.js +2 -0
- package/dist/switch/switch-drag.d.ts +21 -0
- package/dist/switch/switch-drag.js +23 -0
- package/dist/switch/switch-gesture.d.ts +28 -0
- package/dist/switch/switch-gesture.js +131 -0
- package/dist/switch/switch.css +189 -0
- package/dist/switch/switch.d.ts +15 -0
- package/dist/switch/switch.js +24 -0
- package/dist/textarea/index.d.ts +2 -0
- package/dist/textarea/index.js +2 -0
- package/dist/textarea/textarea.css +37 -0
- package/dist/textarea/textarea.d.ts +35 -0
- package/dist/textarea/textarea.js +11 -0
- package/dist/toast/index.d.ts +2 -0
- package/dist/toast/index.js +2 -0
- package/dist/toast/toast.css +293 -0
- package/dist/tooltip/index.d.ts +2 -0
- package/dist/tooltip/index.js +2 -0
- package/dist/tooltip/tooltip.css +143 -0
- package/dist/tooltip/tooltip.d.ts +14 -0
- package/dist/tooltip/tooltip.js +13 -0
- package/package.json +246 -5
- package/README.md +0 -3
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { createGlider, prefersReducedMotion } from "./caret-glide";
|
|
2
|
+
/* A nudge under this many pixels is not a move. */
|
|
3
|
+
const MOVED_BY = 0.5;
|
|
4
|
+
/* A jump longer than this many pixels snaps instead of gliding. */
|
|
5
|
+
export const GLIDE_LIMIT = 400;
|
|
6
|
+
/* How long a person who left takes to fade out before they are removed. */
|
|
7
|
+
const LEAVE_MS = 220;
|
|
8
|
+
/* How long a caret stays still before its name tag fades. */
|
|
9
|
+
export const TAG_STILL_MS = 1800;
|
|
10
|
+
export function spotMoved(before, after) {
|
|
11
|
+
return (before === null ||
|
|
12
|
+
Math.abs(before.x - after.x) >= MOVED_BY ||
|
|
13
|
+
Math.abs(before.y - after.y) >= MOVED_BY ||
|
|
14
|
+
Math.abs(before.height - after.height) >= MOVED_BY);
|
|
15
|
+
}
|
|
16
|
+
export function spotJumped(before, after) {
|
|
17
|
+
return before === null || Math.hypot(after.x - before.x, after.y - before.y) > GLIDE_LIMIT;
|
|
18
|
+
}
|
|
19
|
+
function createEntry(layer) {
|
|
20
|
+
const root = document.createElement("div");
|
|
21
|
+
root.dataset.slot = "remote-caret";
|
|
22
|
+
const bar = document.createElement("div");
|
|
23
|
+
bar.dataset.slot = "remote-caret-bar";
|
|
24
|
+
const tag = document.createElement("div");
|
|
25
|
+
tag.dataset.slot = "remote-caret-tag";
|
|
26
|
+
root.append(bar, tag);
|
|
27
|
+
layer.append(root);
|
|
28
|
+
const glider = createGlider((x, y, height) => {
|
|
29
|
+
root.style.transform = `translate3d(${x}px, ${y}px, 0)`;
|
|
30
|
+
root.style.height = `${height}px`;
|
|
31
|
+
});
|
|
32
|
+
return { root, tag, glider, spot: null, shown: false, stillTimer: 0, leaveTimer: 0 };
|
|
33
|
+
}
|
|
34
|
+
function hide(entry) {
|
|
35
|
+
delete entry.root.dataset.visible;
|
|
36
|
+
entry.shown = false;
|
|
37
|
+
entry.spot = null;
|
|
38
|
+
}
|
|
39
|
+
/** Shows the tag, and starts the countdown to fading it. */
|
|
40
|
+
function settleTag(entry, stillMs) {
|
|
41
|
+
delete entry.root.dataset.still;
|
|
42
|
+
window.clearTimeout(entry.stillTimer);
|
|
43
|
+
entry.stillTimer = window.setTimeout(() => {
|
|
44
|
+
entry.root.dataset.still = "true";
|
|
45
|
+
}, stillMs);
|
|
46
|
+
}
|
|
47
|
+
function dress(entry, caret) {
|
|
48
|
+
entry.root.style.setProperty("--kumiko-remote-caret-color", caret.colour);
|
|
49
|
+
if (entry.tag.textContent !== caret.name) {
|
|
50
|
+
entry.tag.textContent = caret.name;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
function show(overlay, entry, spot, snap) {
|
|
54
|
+
const moved = spotMoved(entry.spot, spot);
|
|
55
|
+
const jump = snap || !entry.shown || spotJumped(entry.spot, spot) || prefersReducedMotion(overlay.host);
|
|
56
|
+
entry.glider.to(spot.x, spot.y, spot.height, jump);
|
|
57
|
+
entry.root.dataset.visible = "true";
|
|
58
|
+
entry.shown = true;
|
|
59
|
+
entry.spot = spot;
|
|
60
|
+
if (moved) {
|
|
61
|
+
settleTag(entry, overlay.stillMs);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function drop(overlay, id, entry) {
|
|
65
|
+
entry.glider.stop();
|
|
66
|
+
window.clearTimeout(entry.stillTimer);
|
|
67
|
+
window.clearTimeout(entry.leaveTimer);
|
|
68
|
+
entry.root.remove();
|
|
69
|
+
overlay.entries.delete(id);
|
|
70
|
+
}
|
|
71
|
+
function retire(overlay, id, entry) {
|
|
72
|
+
if (entry.leaveTimer !== 0) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
hide(entry);
|
|
76
|
+
entry.leaveTimer = window.setTimeout(() => drop(overlay, id, entry), LEAVE_MS);
|
|
77
|
+
}
|
|
78
|
+
function placeOne(overlay, caret, snap) {
|
|
79
|
+
let entry = overlay.entries.get(caret.id);
|
|
80
|
+
if (entry === undefined) {
|
|
81
|
+
entry = createEntry(overlay.layer);
|
|
82
|
+
overlay.entries.set(caret.id, entry);
|
|
83
|
+
}
|
|
84
|
+
window.clearTimeout(entry.leaveTimer);
|
|
85
|
+
entry.leaveTimer = 0;
|
|
86
|
+
dress(entry, caret);
|
|
87
|
+
if (caret.spot === null) {
|
|
88
|
+
hide(entry);
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
show(overlay, entry, caret.spot, snap);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* The overlay for remote collaborators: one gliding caret bar and name tag per person, in a fixed layer over
|
|
96
|
+
* the page. Framework-free, so any editor can drive it from its own awareness data.
|
|
97
|
+
*/
|
|
98
|
+
export function createRemoteCarets(options = {}) {
|
|
99
|
+
const host = options.host ?? document.body;
|
|
100
|
+
const layer = document.createElement("div");
|
|
101
|
+
layer.dataset.slot = "remote-carets";
|
|
102
|
+
layer.setAttribute("aria-hidden", "true");
|
|
103
|
+
host.append(layer);
|
|
104
|
+
const overlay = {
|
|
105
|
+
host,
|
|
106
|
+
layer,
|
|
107
|
+
stillMs: options.stillMs ?? TAG_STILL_MS,
|
|
108
|
+
entries: new Map()
|
|
109
|
+
};
|
|
110
|
+
return {
|
|
111
|
+
place(carets, snap) {
|
|
112
|
+
const present = new Set();
|
|
113
|
+
for (const caret of carets) {
|
|
114
|
+
present.add(caret.id);
|
|
115
|
+
placeOne(overlay, caret, snap);
|
|
116
|
+
}
|
|
117
|
+
for (const [id, entry] of overlay.entries) {
|
|
118
|
+
if (!present.has(id)) {
|
|
119
|
+
retire(overlay, id, entry);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
destroy() {
|
|
124
|
+
for (const [id, entry] of overlay.entries) {
|
|
125
|
+
drop(overlay, id, entry);
|
|
126
|
+
}
|
|
127
|
+
layer.remove();
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
@import "../motion/motion.css";
|
|
2
|
+
|
|
3
|
+
@layer kumiko {
|
|
4
|
+
:where(:root) {
|
|
5
|
+
--kumiko-smooth-caret-width: 2px;
|
|
6
|
+
--kumiko-smooth-caret-radius: 2px;
|
|
7
|
+
--kumiko-smooth-caret-blink-duration: 1.1s;
|
|
8
|
+
--kumiko-smooth-caret-z: 2147483646;
|
|
9
|
+
--kumiko-input-remote-tag-font-family: inherit;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/* The glide takes over from the native caret in the field it follows. */
|
|
13
|
+
[data-smooth-caret] {
|
|
14
|
+
caret-color: transparent !important;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
[data-slot="smooth-caret"] {
|
|
18
|
+
position: fixed;
|
|
19
|
+
inset-block-start: 0;
|
|
20
|
+
inset-inline-start: 0;
|
|
21
|
+
z-index: var(--kumiko-smooth-caret-z);
|
|
22
|
+
inline-size: var(--kumiko-smooth-caret-width);
|
|
23
|
+
block-size: 1rem;
|
|
24
|
+
pointer-events: none;
|
|
25
|
+
opacity: 0;
|
|
26
|
+
will-change: transform, height;
|
|
27
|
+
transition: opacity var(--kumiko-fade-duration) var(--kumiko-fade-ease);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
[data-slot="smooth-caret"][data-visible="true"] {
|
|
31
|
+
opacity: 1;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
[data-slot="smooth-caret-bar"] {
|
|
35
|
+
inline-size: 100%;
|
|
36
|
+
block-size: 100%;
|
|
37
|
+
border-radius: var(--kumiko-smooth-caret-radius);
|
|
38
|
+
background: var(--_caret-color, Highlight);
|
|
39
|
+
transform-origin: center;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
[data-slot="smooth-caret"][data-idle="true"] [data-slot="smooth-caret-bar"] {
|
|
43
|
+
animation: kumiko-smooth-caret-blink var(--kumiko-smooth-caret-blink-duration) ease-in-out
|
|
44
|
+
infinite;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@keyframes kumiko-smooth-caret-blink {
|
|
48
|
+
0%,
|
|
49
|
+
30% {
|
|
50
|
+
opacity: 1;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
65% {
|
|
54
|
+
opacity: 0.12;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
100% {
|
|
58
|
+
opacity: 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/* Remote collaborators: a fixed layer of coloured carets with name tags. Positions are viewport pixels, so they are physical. */
|
|
63
|
+
[data-slot="remote-carets"] {
|
|
64
|
+
position: fixed;
|
|
65
|
+
inset: 0;
|
|
66
|
+
z-index: calc(var(--kumiko-smooth-caret-z) - 1);
|
|
67
|
+
overflow: hidden;
|
|
68
|
+
pointer-events: none;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
[data-slot="remote-caret"] {
|
|
72
|
+
--_remote: var(--kumiko-remote-caret-color, var(--kumiko-input-remote-color, Highlight));
|
|
73
|
+
|
|
74
|
+
position: absolute;
|
|
75
|
+
top: 0;
|
|
76
|
+
left: 0;
|
|
77
|
+
inline-size: var(--kumiko-input-remote-caret-width, 2px);
|
|
78
|
+
block-size: 1rem;
|
|
79
|
+
opacity: 0;
|
|
80
|
+
pointer-events: none;
|
|
81
|
+
transition: opacity var(--kumiko-fade-duration) var(--kumiko-fade-ease);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
[data-slot="remote-caret"][data-visible="true"] {
|
|
85
|
+
opacity: 1;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
[data-slot="remote-caret-bar"] {
|
|
89
|
+
inline-size: 100%;
|
|
90
|
+
block-size: 100%;
|
|
91
|
+
margin-inline-start: -1px;
|
|
92
|
+
border-radius: var(--kumiko-smooth-caret-radius);
|
|
93
|
+
background: var(--_remote);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
[data-slot="remote-caret-tag"] {
|
|
97
|
+
position: absolute;
|
|
98
|
+
inset-block-end: 100%;
|
|
99
|
+
inset-inline-start: -1px;
|
|
100
|
+
padding: var(--kumiko-input-remote-tag-padding, 0.1rem 0.35rem);
|
|
101
|
+
border-radius: var(--kumiko-input-remote-tag-radius, 4px)
|
|
102
|
+
var(--kumiko-input-remote-tag-radius, 4px) var(--kumiko-input-remote-tag-radius, 4px) 0;
|
|
103
|
+
background: var(--_remote);
|
|
104
|
+
color: var(--kumiko-input-remote-tag-fg, oklch(0.99 0 0));
|
|
105
|
+
font-family: var(--kumiko-input-remote-tag-font-family);
|
|
106
|
+
font-size: var(--kumiko-input-remote-tag-font-size, 0.6875rem);
|
|
107
|
+
font-weight: var(--kumiko-input-remote-tag-weight, 600);
|
|
108
|
+
line-height: 1.2;
|
|
109
|
+
white-space: nowrap;
|
|
110
|
+
transition: opacity calc(var(--kumiko-fade-duration) * 2.25) var(--kumiko-fade-ease);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
[data-slot="remote-caret"][data-still="true"] [data-slot="remote-caret-tag"] {
|
|
114
|
+
opacity: 0;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/* The still preset and reduced motion show and hide at once. */
|
|
118
|
+
:where([data-kumiko-motion="still"])
|
|
119
|
+
:is([data-slot="remote-caret"], [data-slot="remote-caret-tag"]) {
|
|
120
|
+
transition: none;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/* Reduced motion keeps the caret still and steady. */
|
|
124
|
+
@media (prefers-reduced-motion: reduce) {
|
|
125
|
+
[data-slot="smooth-caret"] {
|
|
126
|
+
transition: none;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
[data-slot="smooth-caret"][data-idle="true"] [data-slot="smooth-caret-bar"] {
|
|
130
|
+
animation: none;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
:is([data-slot="remote-caret"], [data-slot="remote-caret-tag"]) {
|
|
134
|
+
transition: none;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/* Forced colours use the browser's own caret: the drawn one is hidden, and the browser colours the native one. */
|
|
139
|
+
@media (forced-colors: active) {
|
|
140
|
+
[data-slot="smooth-caret"] {
|
|
141
|
+
display: none;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
[data-slot="remote-caret"] {
|
|
145
|
+
forced-color-adjust: none;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
[data-slot="remote-caret-bar"] {
|
|
149
|
+
background: CanvasText;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
[data-slot="remote-caret-tag"] {
|
|
153
|
+
background: Highlight;
|
|
154
|
+
color: HighlightText;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts the gliding caret for the whole app. Kumiko's fields already do, so this is only for an app with
|
|
3
|
+
* contenteditable fields of its own that wants it before any Kumiko field mounts. It never makes a second caret.
|
|
4
|
+
*/
|
|
5
|
+
export declare function SmoothCaret(): null;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useSmoothCaret } from "./use-smooth-caret";
|
|
2
|
+
/**
|
|
3
|
+
* Starts the gliding caret for the whole app. Kumiko's fields already do, so this is only for an app with
|
|
4
|
+
* contenteditable fields of its own that wants it before any Kumiko field mounts. It never makes a second caret.
|
|
5
|
+
*/
|
|
6
|
+
export function SmoothCaret() {
|
|
7
|
+
useSmoothCaret();
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useEffect } from "react";
|
|
2
|
+
import { acquireSmoothCaret } from "./caret-host";
|
|
3
|
+
/**
|
|
4
|
+
* Keeps the gliding caret running while the component is mounted. Kumiko's fields call it themselves; a
|
|
5
|
+
* contenteditable field that is not a Kumiko component calls it to opt in.
|
|
6
|
+
*/
|
|
7
|
+
export function useSmoothCaret(enabled = true) {
|
|
8
|
+
useEffect(() => (enabled ? acquireSmoothCaret() : undefined), [enabled]);
|
|
9
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { Switch, type SwitchProps } from "./switch";
|
|
2
|
+
export { createSwitchController, SwitchThumb, type SwitchApi, type SwitchCheckedChangeReason, type SwitchController, type SwitchOptions, type SwitchPrimitive, type SwitchState, type SwitchThumbState } from "@ui-sdk/react/switch";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** How far a press must move before it means something, in pixels. */
|
|
2
|
+
export declare const dragThreshold = 4;
|
|
3
|
+
/** What a press that has moved by `dx` and `dy` is turning into. */
|
|
4
|
+
export type Intent = "pending" | "drag" | "scroll";
|
|
5
|
+
/**
|
|
6
|
+
* Reads a moving press. Horizontal movement past the threshold is a drag; vertical movement past it is
|
|
7
|
+
* the page being scrolled, which a switch leaves alone. Until either, the press is still a tap.
|
|
8
|
+
*/
|
|
9
|
+
export declare function intentOf(dx: number, dy: number, threshold?: number): Intent;
|
|
10
|
+
export interface DragStart {
|
|
11
|
+
/** Whether the switch is on when the press begins. */
|
|
12
|
+
checked: boolean;
|
|
13
|
+
/** How far the thumb can travel from its start side to its end side, in pixels. */
|
|
14
|
+
travel: number;
|
|
15
|
+
/** 1 in left-to-right text, -1 in right-to-left. */
|
|
16
|
+
direction: 1 | -1;
|
|
17
|
+
}
|
|
18
|
+
/** How far the thumb sits from its start side after the pointer moved by `dx`, kept inside the track. */
|
|
19
|
+
export declare function thumbOffset({ checked, travel, direction }: DragStart, dx: number): number;
|
|
20
|
+
/** Whether the switch ends on: the thumb's centre is past the middle of the track. */
|
|
21
|
+
export declare function endsOn(offset: number, travel: number): boolean;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** How far a press must move before it means something, in pixels. */
|
|
2
|
+
export const dragThreshold = 4;
|
|
3
|
+
/**
|
|
4
|
+
* Reads a moving press. Horizontal movement past the threshold is a drag; vertical movement past it is
|
|
5
|
+
* the page being scrolled, which a switch leaves alone. Until either, the press is still a tap.
|
|
6
|
+
*/
|
|
7
|
+
export function intentOf(dx, dy, threshold = dragThreshold) {
|
|
8
|
+
const across = Math.abs(dx);
|
|
9
|
+
const down = Math.abs(dy);
|
|
10
|
+
if (Math.max(across, down) < threshold) {
|
|
11
|
+
return "pending";
|
|
12
|
+
}
|
|
13
|
+
return across > down ? "drag" : "scroll";
|
|
14
|
+
}
|
|
15
|
+
/** How far the thumb sits from its start side after the pointer moved by `dx`, kept inside the track. */
|
|
16
|
+
export function thumbOffset({ checked, travel, direction }, dx) {
|
|
17
|
+
const start = checked ? travel : 0;
|
|
18
|
+
return Math.min(travel, Math.max(0, start + dx * direction));
|
|
19
|
+
}
|
|
20
|
+
/** Whether the switch ends on: the thumb's centre is past the middle of the track. */
|
|
21
|
+
export function endsOn(offset, travel) {
|
|
22
|
+
return offset * 2 > travel;
|
|
23
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { SwitchController } from "@ui-sdk/react/switch";
|
|
2
|
+
import { type PointerEvent, type MouseEvent } from "react";
|
|
3
|
+
/**
|
|
4
|
+
* The one click that follows a drag. A mouse sends it to the switch at once; a finger sends none, or a tap a
|
|
5
|
+
* moment later, so no timer can say when it is over. It is held until the switch's own click takes it, or until
|
|
6
|
+
* the next press or key anywhere in the page, which no click of that drag can come after.
|
|
7
|
+
*/
|
|
8
|
+
export interface Swallow {
|
|
9
|
+
/** Holds the next click. */
|
|
10
|
+
arm: () => void;
|
|
11
|
+
/** Whether a click was being held; it is let go either way. */
|
|
12
|
+
take: () => boolean;
|
|
13
|
+
clear: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function createSwallow(): Swallow;
|
|
16
|
+
/**
|
|
17
|
+
* The drag of a switch's thumb as state a switch can render: where the thumb is while it is dragged (or null),
|
|
18
|
+
* and the pointer handlers that move it. A drag ends on the side where the thumb's centre is, through the
|
|
19
|
+
* controller, and swallows the click that follows it so the switch does not flip twice.
|
|
20
|
+
*/
|
|
21
|
+
export declare function useSwitchDrag(controller: SwitchController): {
|
|
22
|
+
offset: number | null;
|
|
23
|
+
down: (event: PointerEvent<HTMLElement>) => void;
|
|
24
|
+
move: (event: PointerEvent<HTMLElement>) => void;
|
|
25
|
+
up: (event: PointerEvent<HTMLElement>) => void;
|
|
26
|
+
cancel: () => void;
|
|
27
|
+
click: (event: MouseEvent<HTMLElement>) => void;
|
|
28
|
+
};
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
|
+
import { endsOn, intentOf, thumbOffset } from "./switch-drag";
|
|
3
|
+
const idle = { phase: "idle" };
|
|
4
|
+
const px = (value) => Number(value.replace("px", ""));
|
|
5
|
+
/** How far the thumb can travel along the track, in pixels, from layout so a hover or press scale cannot change it. */
|
|
6
|
+
function travelOf(root) {
|
|
7
|
+
const thumb = root.querySelector('[data-slot="switch-thumb"]');
|
|
8
|
+
const found = getComputedStyle(root);
|
|
9
|
+
const inset = px(found.paddingInlineStart) + px(found.paddingInlineEnd);
|
|
10
|
+
return Math.max(0, root.clientWidth - inset - (thumb?.offsetWidth ?? 0));
|
|
11
|
+
}
|
|
12
|
+
/** The gesture a press begins, or none when the switch cannot be changed or the press is not a primary one. */
|
|
13
|
+
function begin(event, checked) {
|
|
14
|
+
const root = event.currentTarget;
|
|
15
|
+
const blocked = Object.hasOwn(root.dataset, "disabled") || Object.hasOwn(root.dataset, "readonly");
|
|
16
|
+
if (blocked || event.button !== 0 || !event.isPrimary) {
|
|
17
|
+
return idle;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
phase: "pressed",
|
|
21
|
+
id: event.pointerId,
|
|
22
|
+
x: event.clientX,
|
|
23
|
+
y: event.clientY,
|
|
24
|
+
start: {
|
|
25
|
+
checked,
|
|
26
|
+
travel: travelOf(root),
|
|
27
|
+
direction: getComputedStyle(root).direction === "rtl" ? -1 : 1
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** What the gesture becomes as the pointer moves: a drag once it goes sideways, or ignored once it goes up or down. */
|
|
32
|
+
function advance(gesture, event) {
|
|
33
|
+
if (gesture.phase !== "pressed" || event.pointerId !== gesture.id) {
|
|
34
|
+
return gesture;
|
|
35
|
+
}
|
|
36
|
+
const intent = intentOf(event.clientX - gesture.x, event.clientY - gesture.y);
|
|
37
|
+
if (intent === "scroll") {
|
|
38
|
+
return { phase: "scrolling", id: gesture.id };
|
|
39
|
+
}
|
|
40
|
+
return intent === "drag" ? { ...gesture, phase: "dragging" } : gesture;
|
|
41
|
+
}
|
|
42
|
+
/** The gesture after a move and, once it is a drag, where the thumb is. The drag takes hold of the pointer as it starts. */
|
|
43
|
+
function follow(before, event) {
|
|
44
|
+
const gesture = advance(before, event);
|
|
45
|
+
if (gesture.phase !== "dragging" || event.pointerId !== gesture.id) {
|
|
46
|
+
return { gesture, offset: null };
|
|
47
|
+
}
|
|
48
|
+
if (before.phase === "pressed") {
|
|
49
|
+
event.currentTarget.setPointerCapture(event.pointerId);
|
|
50
|
+
}
|
|
51
|
+
return { gesture, offset: thumbOffset(gesture.start, event.clientX - gesture.x) };
|
|
52
|
+
}
|
|
53
|
+
export function createSwallow() {
|
|
54
|
+
let held = false;
|
|
55
|
+
let stop = undefined;
|
|
56
|
+
const clear = () => {
|
|
57
|
+
held = false;
|
|
58
|
+
stop?.();
|
|
59
|
+
stop = undefined;
|
|
60
|
+
};
|
|
61
|
+
return {
|
|
62
|
+
arm: () => {
|
|
63
|
+
clear();
|
|
64
|
+
held = true;
|
|
65
|
+
window.addEventListener("pointerdown", clear, { capture: true });
|
|
66
|
+
window.addEventListener("keydown", clear, { capture: true });
|
|
67
|
+
stop = () => {
|
|
68
|
+
window.removeEventListener("pointerdown", clear, { capture: true });
|
|
69
|
+
window.removeEventListener("keydown", clear, { capture: true });
|
|
70
|
+
};
|
|
71
|
+
},
|
|
72
|
+
take: () => {
|
|
73
|
+
const was = held;
|
|
74
|
+
clear();
|
|
75
|
+
return was;
|
|
76
|
+
},
|
|
77
|
+
clear
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** The side a drag ends on when the pointer lets go, or null when no drag was going on. */
|
|
81
|
+
function endedOn(gesture, event) {
|
|
82
|
+
if (gesture.phase !== "dragging" || event.pointerId !== gesture.id) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
return endsOn(thumbOffset(gesture.start, event.clientX - gesture.x), gesture.start.travel);
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* The drag of a switch's thumb as state a switch can render: where the thumb is while it is dragged (or null),
|
|
89
|
+
* and the pointer handlers that move it. A drag ends on the side where the thumb's centre is, through the
|
|
90
|
+
* controller, and swallows the click that follows it so the switch does not flip twice.
|
|
91
|
+
*/
|
|
92
|
+
export function useSwitchDrag(controller) {
|
|
93
|
+
const gesture = useRef(idle);
|
|
94
|
+
const [swallow] = useState(createSwallow);
|
|
95
|
+
const [offset, setOffset] = useState(null);
|
|
96
|
+
const finish = () => {
|
|
97
|
+
gesture.current = idle;
|
|
98
|
+
setOffset(null);
|
|
99
|
+
};
|
|
100
|
+
return {
|
|
101
|
+
offset,
|
|
102
|
+
down: (event) => {
|
|
103
|
+
swallow.clear();
|
|
104
|
+
gesture.current = begin(event, controller.getChecked());
|
|
105
|
+
},
|
|
106
|
+
move: (event) => {
|
|
107
|
+
const next = follow(gesture.current, event);
|
|
108
|
+
gesture.current = next.gesture;
|
|
109
|
+
if (next.offset !== null) {
|
|
110
|
+
setOffset(next.offset);
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
up: (event) => {
|
|
114
|
+
const current = gesture.current;
|
|
115
|
+
const end = endedOn(current, event);
|
|
116
|
+
if (end !== null && current.phase === "dragging") {
|
|
117
|
+
swallow.arm();
|
|
118
|
+
if (end !== current.start.checked) {
|
|
119
|
+
controller.setChecked(end, { reason: "press", event: event.nativeEvent });
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
finish();
|
|
123
|
+
},
|
|
124
|
+
cancel: finish,
|
|
125
|
+
click: (event) => {
|
|
126
|
+
if (swallow.take()) {
|
|
127
|
+
event.preventDefault();
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|