velvetkit 0.1.0 → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/velvetkit/version.rb +1 -1
- data/registry/app/components/ui/file_field.html.erb +4 -0
- data/registry/app/components/ui/file_field.rb +73 -0
- data/registry/app/components/ui/navigation_menu/item.html.erb +16 -0
- data/registry/app/components/ui/navigation_menu/item.rb +102 -0
- data/registry/app/components/ui/navigation_menu/link.html.erb +9 -0
- data/registry/app/components/ui/navigation_menu/link.rb +41 -0
- data/registry/app/components/ui/navigation_menu.html.erb +3 -0
- data/registry/app/components/ui/navigation_menu.rb +64 -0
- data/registry/app/components/ui/session_tabs/add.html.erb +1 -0
- data/registry/app/components/ui/session_tabs/add.rb +52 -0
- data/registry/app/components/ui/session_tabs/tab.html.erb +37 -0
- data/registry/app/components/ui/session_tabs/tab.rb +243 -0
- data/registry/app/components/ui/session_tabs.html.erb +15 -0
- data/registry/app/components/ui/session_tabs.rb +101 -0
- data/registry/app/components/ui/skeleton.html.erb +1 -0
- data/registry/app/components/ui/skeleton.rb +30 -0
- data/registry/app/components/ui/toast/viewport.html.erb +4 -0
- data/registry/app/components/ui/toast/viewport.rb +41 -0
- data/registry/app/components/ui/toast.html.erb +11 -0
- data/registry/app/components/ui/toast.rb +89 -0
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +49 -0
- data/registry/app/javascript/controllers/ui/menu_controller.ts +3 -0
- data/registry/app/javascript/controllers/ui/navigation_menu_controller.ts +265 -0
- data/registry/app/javascript/controllers/ui/session_tabs_controller.ts +732 -0
- data/registry/app/javascript/controllers/ui/toast_controller.ts +62 -0
- data/registry/app/javascript/controllers/ui/toast_viewport_controller.ts +82 -0
- data/registry/app/javascript/velvetkit_icons.ts +21 -9
- data/registry/components.yml +10 -0
- data/registry/config/locales/velvetkit.en.yml +21 -0
- data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +3 -0
- data/registry/test/components/previews/ui/file_field_preview/with_error.html.erb +3 -0
- data/registry/test/components/previews/ui/file_field_preview.rb +18 -0
- data/registry/test/components/previews/ui/navigation_menu_preview/custom_panel.html.erb +20 -0
- data/registry/test/components/previews/ui/navigation_menu_preview/playground.html.erb +73 -0
- data/registry/test/components/previews/ui/navigation_menu_preview/simple.html.erb +23 -0
- data/registry/test/components/previews/ui/navigation_menu_preview.rb +23 -0
- data/registry/test/components/previews/ui/session_tabs_preview/handling_events.html.erb +46 -0
- data/registry/test/components/previews/ui/session_tabs_preview/saved_views.html.erb +16 -0
- data/registry/test/components/previews/ui/session_tabs_preview/touch_with_confirmation.html.erb +20 -0
- data/registry/test/components/previews/ui/session_tabs_preview.rb +35 -0
- data/registry/test/components/previews/ui/skeleton_preview/card_placeholder.html.erb +10 -0
- data/registry/test/components/previews/ui/skeleton_preview/shapes.html.erb +9 -0
- data/registry/test/components/previews/ui/skeleton_preview.rb +15 -0
- data/registry/test/components/previews/ui/toast_preview/live_viewport.html.erb +21 -0
- data/registry/test/components/previews/ui/toast_preview/variants.html.erb +7 -0
- data/registry/test/components/previews/ui/toast_preview/with_action.html.erb +7 -0
- data/registry/test/components/previews/ui/toast_preview.rb +29 -0
- data/registry/test/components/ui/file_field_test.rb +101 -0
- data/registry/test/components/ui/navigation_menu_test.rb +130 -0
- data/registry/test/components/ui/session_tabs/add_test.rb +52 -0
- data/registry/test/components/ui/session_tabs_test.rb +281 -0
- data/registry/test/components/ui/skeleton_test.rb +65 -0
- data/registry/test/components/ui/toast/viewport_test.rb +72 -0
- data/registry/test/components/ui/toast_test.rb +183 -0
- metadata +51 -1
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
import { useFloating } from './_internal/use_floating.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* UI::NavigationMenu controller — disclosure navigation: triggers open a
|
|
6
|
+
* panel of links below them. One panel is open at a time.
|
|
7
|
+
*
|
|
8
|
+
* Mouse: hover a trigger to open its panel after openDelay; once a panel is
|
|
9
|
+
* open, hovering another trigger switches at once. A panel opened by hover
|
|
10
|
+
* closes closeDelay after the pointer leaves trigger and panel (long enough
|
|
11
|
+
* to cross the gap). One opened by click or keyboard stays until Escape, a
|
|
12
|
+
* click outside or focus leaving — so moving the mouse away to scroll
|
|
13
|
+
* doesn't close it. Click toggles (the only way in on touch).
|
|
14
|
+
*
|
|
15
|
+
* The panel opens below its trigger and flips above when there isn't room
|
|
16
|
+
* below (a menu in a footer, or a header scrolled near the bottom), and
|
|
17
|
+
* follows the trigger as the page or any container scrolls.
|
|
18
|
+
*
|
|
19
|
+
* Keyboard: Left/Right/Home/End move along the top row; Enter, Space or
|
|
20
|
+
* ArrowDown on a trigger opens its panel (ArrowDown also focuses its first
|
|
21
|
+
* link); Up/Down move between links in the panel; Escape closes and returns
|
|
22
|
+
* focus to the trigger. Tabbing out of the nav, or clicking outside, closes.
|
|
23
|
+
*/
|
|
24
|
+
export default class extends Controller<HTMLElement> {
|
|
25
|
+
static targets = ['top', 'trigger', 'panel', 'link'];
|
|
26
|
+
static values = {
|
|
27
|
+
openDelay: { type: Number, default: 150 },
|
|
28
|
+
closeDelay: { type: Number, default: 200 },
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
declare readonly topTargets: HTMLElement[];
|
|
32
|
+
declare readonly triggerTargets: HTMLButtonElement[];
|
|
33
|
+
declare readonly panelTargets: HTMLElement[];
|
|
34
|
+
declare openDelayValue: number;
|
|
35
|
+
declare closeDelayValue: number;
|
|
36
|
+
|
|
37
|
+
private openTrigger: HTMLButtonElement | null = null;
|
|
38
|
+
private openedBy: 'hover' | 'click' | 'keyboard' | null = null;
|
|
39
|
+
private openedByHoverAt = 0;
|
|
40
|
+
private openTimer: number | null = null;
|
|
41
|
+
private closeTimer: number | null = null;
|
|
42
|
+
private floatingCleanup: (() => void) | null = null;
|
|
43
|
+
|
|
44
|
+
disconnect() {
|
|
45
|
+
this.close({ restoreFocus: false });
|
|
46
|
+
this.clearTimers();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// ── Click ──────────────────────────────────────────────────────────────
|
|
50
|
+
|
|
51
|
+
toggle(event: MouseEvent) {
|
|
52
|
+
const trigger = event.currentTarget as HTMLButtonElement;
|
|
53
|
+
this.clearTimers();
|
|
54
|
+
if (this.openTrigger === trigger) {
|
|
55
|
+
// A click that lands just after hover opened the panel is the user
|
|
56
|
+
// reaching for it, not asking to close it.
|
|
57
|
+
if (this.openedBy === 'hover' && Date.now() - this.openedByHoverAt < 400) {
|
|
58
|
+
this.openedBy = 'click'; // the click claims it: no longer closes on leave
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.close({ restoreFocus: false });
|
|
62
|
+
} else {
|
|
63
|
+
// Enter/Space on a focused button also arrive as a click (detail 0).
|
|
64
|
+
this.open(trigger, event.detail === 0 ? 'keyboard' : 'click');
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ── Hover (mouse only) ─────────────────────────────────────────────────
|
|
69
|
+
|
|
70
|
+
hoverOpen(event: PointerEvent) {
|
|
71
|
+
if (event.pointerType !== 'mouse') return;
|
|
72
|
+
const trigger = event.currentTarget as HTMLButtonElement;
|
|
73
|
+
this.clearTimers();
|
|
74
|
+
if (this.openTrigger === trigger) return;
|
|
75
|
+
|
|
76
|
+
const show = () => {
|
|
77
|
+
this.open(trigger, 'hover');
|
|
78
|
+
this.openedByHoverAt = Date.now();
|
|
79
|
+
};
|
|
80
|
+
if (this.openTrigger) show(); // already browsing panels: switch at once
|
|
81
|
+
else this.openTimer = window.setTimeout(show, this.openDelayValue);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
hoverClose(event: PointerEvent) {
|
|
85
|
+
if (event.pointerType !== 'mouse') return;
|
|
86
|
+
this.clearOpenTimer();
|
|
87
|
+
if (!this.openTrigger || this.openedBy !== 'hover') return;
|
|
88
|
+
this.clearCloseTimer();
|
|
89
|
+
this.closeTimer = window.setTimeout(() => this.close({ restoreFocus: false }), this.closeDelayValue);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
cancelClose() {
|
|
93
|
+
this.clearCloseTimer();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── Keyboard ───────────────────────────────────────────────────────────
|
|
97
|
+
|
|
98
|
+
topKeydown(event: KeyboardEvent) {
|
|
99
|
+
const item = event.currentTarget as HTMLElement;
|
|
100
|
+
const tops = this.topTargets;
|
|
101
|
+
const index = tops.indexOf(item);
|
|
102
|
+
const isTrigger = this.triggerTargets.includes(item as HTMLButtonElement);
|
|
103
|
+
|
|
104
|
+
switch (event.key) {
|
|
105
|
+
case 'ArrowRight':
|
|
106
|
+
this.focusTop(tops[(index + 1) % tops.length]);
|
|
107
|
+
break;
|
|
108
|
+
case 'ArrowLeft':
|
|
109
|
+
this.focusTop(tops[(index - 1 + tops.length) % tops.length]);
|
|
110
|
+
break;
|
|
111
|
+
case 'Home':
|
|
112
|
+
this.focusTop(tops[0]);
|
|
113
|
+
break;
|
|
114
|
+
case 'End':
|
|
115
|
+
this.focusTop(tops[tops.length - 1]);
|
|
116
|
+
break;
|
|
117
|
+
case 'ArrowDown':
|
|
118
|
+
if (!isTrigger) return;
|
|
119
|
+
this.open(item as HTMLButtonElement, 'keyboard');
|
|
120
|
+
this.linksOf(this.panelFor(item as HTMLButtonElement))[0]?.focus();
|
|
121
|
+
break;
|
|
122
|
+
case 'Escape':
|
|
123
|
+
if (!this.openTrigger) return;
|
|
124
|
+
this.close({ restoreFocus: true });
|
|
125
|
+
break;
|
|
126
|
+
default:
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
event.preventDefault();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
panelKeydown(event: KeyboardEvent) {
|
|
133
|
+
const panel = event.currentTarget as HTMLElement;
|
|
134
|
+
const links = this.linksOf(panel);
|
|
135
|
+
const index = links.indexOf(document.activeElement as HTMLElement);
|
|
136
|
+
|
|
137
|
+
switch (event.key) {
|
|
138
|
+
case 'ArrowDown':
|
|
139
|
+
links[(index + 1) % links.length]?.focus();
|
|
140
|
+
break;
|
|
141
|
+
case 'ArrowUp':
|
|
142
|
+
if (index <= 0) this.openTrigger?.focus();
|
|
143
|
+
else links[index - 1]?.focus();
|
|
144
|
+
break;
|
|
145
|
+
case 'Home':
|
|
146
|
+
links[0]?.focus();
|
|
147
|
+
break;
|
|
148
|
+
case 'End':
|
|
149
|
+
links[links.length - 1]?.focus();
|
|
150
|
+
break;
|
|
151
|
+
case 'Escape':
|
|
152
|
+
this.close({ restoreFocus: true });
|
|
153
|
+
break;
|
|
154
|
+
default:
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
event.preventDefault();
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Focus landed somewhere that isn't the open panel or its trigger — the
|
|
161
|
+
// next top-level item after Tabbing past the last link, or anything else
|
|
162
|
+
// on the page — so the panel no longer belongs to where the user is.
|
|
163
|
+
focusIn(event: FocusEvent) {
|
|
164
|
+
this.closeUnlessWithinOpen(event.target as Node);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
focusOut(event: FocusEvent) {
|
|
168
|
+
const next = event.relatedTarget as Node | null;
|
|
169
|
+
// Focus went nowhere (a click on plain page): the outside-pointer
|
|
170
|
+
// handler decides. Moving within the nav: focusIn decides.
|
|
171
|
+
if (next === null || this.element.contains(next)) return;
|
|
172
|
+
this.close({ restoreFocus: false });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private closeUnlessWithinOpen(node: Node) {
|
|
176
|
+
const trigger = this.openTrigger;
|
|
177
|
+
if (!trigger) return;
|
|
178
|
+
if (trigger.contains(node) || this.panelFor(trigger)?.contains(node)) return;
|
|
179
|
+
this.close({ restoreFocus: false });
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ── Open / close ───────────────────────────────────────────────────────
|
|
183
|
+
|
|
184
|
+
private open(trigger: HTMLButtonElement, by: 'hover' | 'click' | 'keyboard') {
|
|
185
|
+
if (this.openTrigger === trigger) return;
|
|
186
|
+
this.close({ restoreFocus: false });
|
|
187
|
+
|
|
188
|
+
const panel = this.panelFor(trigger);
|
|
189
|
+
if (!panel) return;
|
|
190
|
+
|
|
191
|
+
panel.hidden = false;
|
|
192
|
+
panel.dataset.state = 'open';
|
|
193
|
+
trigger.setAttribute('aria-expanded', 'true');
|
|
194
|
+
this.floatingCleanup = useFloating(trigger, panel, {
|
|
195
|
+
placement: 'bottom-start',
|
|
196
|
+
offset: 6,
|
|
197
|
+
flip: true,
|
|
198
|
+
shift: true,
|
|
199
|
+
strategy: 'fixed',
|
|
200
|
+
}).destroy;
|
|
201
|
+
|
|
202
|
+
this.openTrigger = trigger;
|
|
203
|
+
this.openedBy = by;
|
|
204
|
+
document.addEventListener('pointerdown', this.onOutsidePointer, true);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private close({ restoreFocus }: { restoreFocus: boolean }) {
|
|
208
|
+
const trigger = this.openTrigger;
|
|
209
|
+
if (!trigger) return;
|
|
210
|
+
const panel = this.panelFor(trigger);
|
|
211
|
+
|
|
212
|
+
this.floatingCleanup?.();
|
|
213
|
+
this.floatingCleanup = null;
|
|
214
|
+
if (panel) {
|
|
215
|
+
panel.hidden = true;
|
|
216
|
+
panel.dataset.state = 'closed';
|
|
217
|
+
}
|
|
218
|
+
trigger.setAttribute('aria-expanded', 'false');
|
|
219
|
+
this.openTrigger = null;
|
|
220
|
+
this.openedBy = null;
|
|
221
|
+
this.openedByHoverAt = 0;
|
|
222
|
+
document.removeEventListener('pointerdown', this.onOutsidePointer, true);
|
|
223
|
+
if (restoreFocus) trigger.focus();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
private onOutsidePointer = (event: PointerEvent) => {
|
|
227
|
+
if (this.element.contains(event.target as Node)) return;
|
|
228
|
+
this.close({ restoreFocus: false });
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// ── Helpers ────────────────────────────────────────────────────────────
|
|
232
|
+
|
|
233
|
+
private panelFor(trigger: HTMLButtonElement): HTMLElement | null {
|
|
234
|
+
const id = trigger.getAttribute('aria-controls');
|
|
235
|
+
return this.panelTargets.find((p) => p.id === id) ?? null;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
private linksOf(panel: HTMLElement | null): HTMLElement[] {
|
|
239
|
+
if (!panel) return [];
|
|
240
|
+
return Array.from(panel.querySelectorAll<HTMLElement>('a[href], button:not([disabled])'));
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// Moving along the top row closes a panel left behind, so an open panel
|
|
244
|
+
// always belongs to the item with focus.
|
|
245
|
+
private focusTop(item: HTMLElement | undefined) {
|
|
246
|
+
if (!item) return;
|
|
247
|
+
if (this.openTrigger && item !== this.openTrigger) this.close({ restoreFocus: false });
|
|
248
|
+
item.focus();
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
private clearOpenTimer() {
|
|
252
|
+
if (this.openTimer !== null) window.clearTimeout(this.openTimer);
|
|
253
|
+
this.openTimer = null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
private clearCloseTimer() {
|
|
257
|
+
if (this.closeTimer !== null) window.clearTimeout(this.closeTimer);
|
|
258
|
+
this.closeTimer = null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
private clearTimers() {
|
|
262
|
+
this.clearOpenTimer();
|
|
263
|
+
this.clearCloseTimer();
|
|
264
|
+
}
|
|
265
|
+
}
|