velvetkit 0.1.0.beta → 0.2.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.
- checksums.yaml +4 -4
- data/README.md +143 -75
- data/lib/velvetkit/version.rb +1 -1
- data/registry/app/components/ui/checkbox.html.erb +2 -0
- data/registry/app/components/ui/checkbox.rb +11 -5
- data/registry/app/components/ui/collapsible.html.erb +7 -0
- data/registry/app/components/ui/collapsible.rb +67 -0
- data/registry/app/components/ui/date_picker.html.erb +4 -1
- data/registry/app/components/ui/date_picker.rb +10 -2
- 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/form_field/messages.html.erb +6 -0
- data/registry/app/components/ui/form_field/messages.rb +11 -0
- data/registry/app/components/ui/form_field.html.erb +9 -0
- data/registry/app/components/ui/form_field.rb +105 -0
- data/registry/app/components/ui/input.html.erb +1 -8
- data/registry/app/components/ui/input.rb +12 -36
- data/registry/app/components/ui/input_group/addon.rb +4 -1
- data/registry/app/components/ui/input_group/field.rb +4 -1
- data/registry/app/components/ui/input_group.rb +12 -10
- data/registry/app/components/ui/native_select.html.erb +2 -9
- data/registry/app/components/ui/native_select.rb +17 -31
- data/registry/app/components/ui/password_input.html.erb +1 -8
- data/registry/app/components/ui/password_input.rb +9 -27
- data/registry/app/components/ui/select.html.erb +4 -0
- data/registry/app/components/ui/select.rb +12 -29
- 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/table_of_contents/item.html.erb +8 -0
- data/registry/app/components/ui/table_of_contents/item.rb +41 -0
- data/registry/app/components/ui/table_of_contents.html.erb +13 -0
- data/registry/app/components/ui/table_of_contents.rb +58 -0
- data/registry/app/components/ui/textarea.html.erb +2 -0
- data/registry/app/components/ui/textarea.rb +101 -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/session_tabs_controller.ts +732 -0
- data/registry/app/javascript/controllers/ui/table_of_contents_controller.ts +179 -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 +17 -1
- data/registry/config/locales/velvetkit.en.yml +21 -0
- data/registry/test/components/previews/ui/collapsible_preview/default.html.erb +18 -0
- data/registry/test/components/previews/ui/collapsible_preview/show_more.html.erb +7 -0
- data/registry/test/components/previews/ui/collapsible_preview.rb +19 -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/form_field_preview/custom_control.html.erb +6 -0
- data/registry/test/components/previews/ui/form_field_preview/default.html.erb +7 -0
- data/registry/test/components/previews/ui/form_field_preview/model_errors.html.erb +11 -0
- data/registry/test/components/previews/ui/form_field_preview.rb +12 -0
- data/registry/test/components/previews/ui/input_preview/hint_and_error.html.erb +4 -0
- data/registry/test/components/previews/ui/input_preview.rb +3 -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/sidebar_preview/_brand.html.erb +4 -4
- data/registry/test/components/previews/ui/sidebar_preview/with_dropdown_header.html.erb +1 -1
- 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/table_of_contents_preview/default.html.erb +41 -0
- data/registry/test/components/previews/ui/table_of_contents_preview/flat.html.erb +17 -0
- data/registry/test/components/previews/ui/table_of_contents_preview.rb +9 -0
- data/registry/test/components/previews/ui/textarea_preview/autogrow.html.erb +4 -0
- data/registry/test/components/previews/ui/textarea_preview/default.html.erb +3 -0
- data/registry/test/components/previews/ui/textarea_preview/sizes.html.erb +5 -0
- data/registry/test/components/previews/ui/textarea_preview/states.html.erb +7 -0
- data/registry/test/components/previews/ui/textarea_preview.rb +33 -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/checkbox_test.rb +24 -0
- data/registry/test/components/ui/collapsible_test.rb +71 -0
- data/registry/test/components/ui/date_picker_test.rb +14 -0
- data/registry/test/components/ui/file_field_test.rb +101 -0
- data/registry/test/components/ui/form_field/messages_test.rb +19 -0
- data/registry/test/components/ui/form_field_test.rb +122 -0
- data/registry/test/components/ui/input_group_test.rb +30 -2
- data/registry/test/components/ui/input_test.rb +37 -0
- data/registry/test/components/ui/native_select_test.rb +25 -0
- data/registry/test/components/ui/password_input_test.rb +9 -0
- data/registry/test/components/ui/select_test.rb +14 -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/table_of_contents/item_test.rb +38 -0
- data/registry/test/components/ui/table_of_contents_test.rb +120 -0
- data/registry/test/components/ui/textarea_test.rb +178 -0
- data/registry/test/components/ui/toast/viewport_test.rb +72 -0
- data/registry/test/components/ui/toast_test.rb +183 -0
- metadata +77 -4
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
// Scrollspy: marks the link whose section was scrolled to last and slides the
|
|
4
|
+
// indicator beside it.
|
|
5
|
+
export default class extends Controller<HTMLElement> {
|
|
6
|
+
static targets = ['link', 'indicator', 'rail'];
|
|
7
|
+
static values = {
|
|
8
|
+
offset: { type: Number, default: 80 },
|
|
9
|
+
container: String,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
declare readonly linkTargets: HTMLAnchorElement[];
|
|
13
|
+
declare readonly indicatorTarget: HTMLElement;
|
|
14
|
+
declare readonly railTarget: HTMLElement;
|
|
15
|
+
declare offsetValue: number;
|
|
16
|
+
declare containerValue: string;
|
|
17
|
+
|
|
18
|
+
private scroller: HTMLElement | Window = window;
|
|
19
|
+
private active: HTMLAnchorElement | null = null;
|
|
20
|
+
private frame = 0;
|
|
21
|
+
private settleFrame = 0;
|
|
22
|
+
private locked = false;
|
|
23
|
+
private unlockTimer = 0;
|
|
24
|
+
private resizeObserver?: ResizeObserver;
|
|
25
|
+
|
|
26
|
+
connect() {
|
|
27
|
+
this.scroller =
|
|
28
|
+
(this.containerValue && document.querySelector<HTMLElement>(this.containerValue)) || window;
|
|
29
|
+
this.scroller.addEventListener('scroll', this.schedule, { passive: true });
|
|
30
|
+
this.scroller.addEventListener('scrollend', this.unlock);
|
|
31
|
+
this.resizeObserver = new ResizeObserver(this.schedule);
|
|
32
|
+
this.resizeObserver.observe(this.element);
|
|
33
|
+
this.update();
|
|
34
|
+
// The first state is drawn without transitions (data-settling); let them
|
|
35
|
+
// back in once it has been painted.
|
|
36
|
+
this.settleFrame = requestAnimationFrame(() => {
|
|
37
|
+
this.settleFrame = requestAnimationFrame(() => delete this.element.dataset.settling);
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
disconnect() {
|
|
42
|
+
this.scroller.removeEventListener('scroll', this.schedule);
|
|
43
|
+
this.scroller.removeEventListener('scrollend', this.unlock);
|
|
44
|
+
this.resizeObserver?.disconnect();
|
|
45
|
+
cancelAnimationFrame(this.frame);
|
|
46
|
+
cancelAnimationFrame(this.settleFrame);
|
|
47
|
+
clearTimeout(this.unlockTimer);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A click scrolls the section to the offset line itself: left to the browser
|
|
51
|
+
// (or Turbo), the jump scrolls every ancestor, the window included, and lands
|
|
52
|
+
// wherever scroll-margin says rather than where the section counts as reached.
|
|
53
|
+
// The link is marked at once and held while the scroll runs, so the
|
|
54
|
+
// indicator doesn't stop at every section on the way.
|
|
55
|
+
select(event: MouseEvent) {
|
|
56
|
+
if (event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
|
|
57
|
+
const link = event.currentTarget as HTMLAnchorElement;
|
|
58
|
+
const target = this.targetOf(link);
|
|
59
|
+
if (!target) return;
|
|
60
|
+
|
|
61
|
+
event.preventDefault();
|
|
62
|
+
this.scrollToTarget(target);
|
|
63
|
+
if (this.scroller instanceof Window) history.replaceState(history.state, '', link.hash);
|
|
64
|
+
// Keyboard focus continues from the section, as it would after a jump.
|
|
65
|
+
if (!target.hasAttribute('tabindex')) target.setAttribute('tabindex', '-1');
|
|
66
|
+
target.focus({ preventScroll: true });
|
|
67
|
+
|
|
68
|
+
this.setActive(link);
|
|
69
|
+
this.locked = true;
|
|
70
|
+
clearTimeout(this.unlockTimer);
|
|
71
|
+
// scrollend never fires when the section is already in place, and some
|
|
72
|
+
// browsers don't send it at all.
|
|
73
|
+
this.unlockTimer = window.setTimeout(this.unlock, 1000);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
private unlock = () => {
|
|
77
|
+
this.locked = false;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
private schedule = () => {
|
|
81
|
+
if (!this.frame) this.frame = requestAnimationFrame(this.update);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
private update = () => {
|
|
85
|
+
this.frame = 0;
|
|
86
|
+
if (this.locked) {
|
|
87
|
+
this.moveIndicator();
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const sections = this.sections();
|
|
92
|
+
if (sections.length === 0) return this.setActive(null);
|
|
93
|
+
|
|
94
|
+
// The last section whose top has passed the line; the first until one has.
|
|
95
|
+
// At the very bottom, the last section, which may be too short to reach it.
|
|
96
|
+
const line = this.scrollerTop() + this.offsetValue;
|
|
97
|
+
let current = sections[0];
|
|
98
|
+
if (this.atBottom()) {
|
|
99
|
+
current = sections[sections.length - 1];
|
|
100
|
+
} else {
|
|
101
|
+
for (const section of sections) {
|
|
102
|
+
if (section.top <= line) current = section;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
this.setActive(current.link);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
private targetOf(link: HTMLAnchorElement) {
|
|
109
|
+
const id = decodeURIComponent(link.hash.slice(1));
|
|
110
|
+
return id ? document.getElementById(id) : null;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private scrollToTarget(target: HTMLElement) {
|
|
114
|
+
const behavior = matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth';
|
|
115
|
+
const distance = target.getBoundingClientRect().top - this.scrollerTop() - this.offsetValue;
|
|
116
|
+
this.scroller.scrollBy({ top: distance, behavior });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Links paired with the sections they point at, in the order they appear.
|
|
120
|
+
private sections() {
|
|
121
|
+
return this.linkTargets
|
|
122
|
+
.map((link) => {
|
|
123
|
+
const target = this.targetOf(link);
|
|
124
|
+
return target && target.getClientRects().length > 0
|
|
125
|
+
? { link, top: target.getBoundingClientRect().top }
|
|
126
|
+
: null;
|
|
127
|
+
})
|
|
128
|
+
.filter((section): section is { link: HTMLAnchorElement; top: number } => section !== null)
|
|
129
|
+
.sort((a, b) => a.top - b.top);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private scrollerTop() {
|
|
133
|
+
return this.scroller instanceof Window ? 0 : this.scroller.getBoundingClientRect().top;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
private atBottom() {
|
|
137
|
+
const el = this.scroller instanceof Window ? document.documentElement : this.scroller;
|
|
138
|
+
return el.scrollHeight > el.clientHeight && el.scrollTop + el.clientHeight >= el.scrollHeight - 2;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
private setActive(link: HTMLAnchorElement | null) {
|
|
142
|
+
if (link !== this.active) {
|
|
143
|
+
this.active = link;
|
|
144
|
+
const ancestors = link ? this.ancestorsOf(link) : [];
|
|
145
|
+
this.linkTargets.forEach((l) => {
|
|
146
|
+
l.dataset.state = l === link ? 'active' : ancestors.includes(l) ? 'ancestor' : 'inactive';
|
|
147
|
+
if (l === link) l.setAttribute('aria-current', 'location');
|
|
148
|
+
else l.removeAttribute('aria-current');
|
|
149
|
+
});
|
|
150
|
+
this.dispatch('change', { detail: { href: link?.getAttribute('href') ?? null } });
|
|
151
|
+
}
|
|
152
|
+
this.moveIndicator();
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Links of the items this link's item is nested in.
|
|
156
|
+
private ancestorsOf(link: HTMLAnchorElement) {
|
|
157
|
+
const links: HTMLAnchorElement[] = [];
|
|
158
|
+
let item = link.closest('li')?.parentElement?.closest('li');
|
|
159
|
+
while (item && this.element.contains(item)) {
|
|
160
|
+
const parent = item.querySelector<HTMLAnchorElement>(':scope > a');
|
|
161
|
+
if (parent) links.push(parent);
|
|
162
|
+
item = item.parentElement?.closest('li');
|
|
163
|
+
}
|
|
164
|
+
return links;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
private moveIndicator() {
|
|
168
|
+
const indicator = this.indicatorTarget;
|
|
169
|
+
if (!this.active) {
|
|
170
|
+
indicator.dataset.state = 'hidden';
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const link = this.active.getBoundingClientRect();
|
|
174
|
+
const rail = this.railTarget.getBoundingClientRect();
|
|
175
|
+
indicator.style.transform = `translateY(${link.top - rail.top}px)`;
|
|
176
|
+
indicator.style.height = `${link.height}px`;
|
|
177
|
+
indicator.dataset.state = 'visible';
|
|
178
|
+
}
|
|
179
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
// Matches the exit animation's `duration-300` in UI::Toast (and its
|
|
4
|
+
// EXIT_DURATION constant). Removing on a fixed timer, rather than the exit
|
|
5
|
+
// animation's `animationend`, sidesteps a real race: a toast dismissed while
|
|
6
|
+
// its own *entrance* animation is still playing would otherwise have its
|
|
7
|
+
// entrance's animationend fire moments later and remove the element early,
|
|
8
|
+
// before the exit animation ever got a chance to play.
|
|
9
|
+
const EXIT_DURATION = 300;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* UI::Toast controller — owns one toast's lifecycle, however it arrived
|
|
13
|
+
* (rendered by ERB, appended by a Turbo Stream, or cloned by the
|
|
14
|
+
* toast-viewport controller from its templates).
|
|
15
|
+
*
|
|
16
|
+
* Auto-dismisses after durationValue ms, pausing while the pointer is over
|
|
17
|
+
* it. durationValue of 0 means "stays until dismissed".
|
|
18
|
+
*/
|
|
19
|
+
export default class extends Controller<HTMLElement> {
|
|
20
|
+
static values = { duration: { type: Number, default: 5000 } };
|
|
21
|
+
|
|
22
|
+
declare durationValue: number;
|
|
23
|
+
|
|
24
|
+
private timeout: number | null = null;
|
|
25
|
+
|
|
26
|
+
connect() {
|
|
27
|
+
this.arm();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
disconnect() {
|
|
31
|
+
this.disarm();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
pause() {
|
|
35
|
+
this.disarm();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
resume() {
|
|
39
|
+
this.arm();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
dismiss = (event?: Event) => {
|
|
43
|
+
event?.preventDefault();
|
|
44
|
+
this.disarm();
|
|
45
|
+
if (this.element.dataset.state === 'closed') return;
|
|
46
|
+
this.element.dataset.state = 'closed';
|
|
47
|
+
window.setTimeout(() => this.element.remove(), EXIT_DURATION);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
private arm() {
|
|
51
|
+
this.disarm();
|
|
52
|
+
if (!this.durationValue) return;
|
|
53
|
+
this.timeout = window.setTimeout(this.dismiss, this.durationValue);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
private disarm() {
|
|
57
|
+
if (this.timeout) {
|
|
58
|
+
clearTimeout(this.timeout);
|
|
59
|
+
this.timeout = null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Controller } from '@hotwired/stimulus';
|
|
2
|
+
|
|
3
|
+
export interface ToastOptions {
|
|
4
|
+
description?: string;
|
|
5
|
+
variant?: 'default' | 'success' | 'destructive' | 'warning' | 'info';
|
|
6
|
+
/** Seconds before it dismisses itself; 0 leaves it until clicked away. */
|
|
7
|
+
duration?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Pushes a toast from client-side JS, no server round trip. Dispatches an
|
|
12
|
+
* event the toast-viewport controller below listens for; it builds the
|
|
13
|
+
* toast by cloning one of the templates UI::Toast::Viewport renders (one per
|
|
14
|
+
* variant), so a JS-triggered toast is pixel-identical to a server-rendered
|
|
15
|
+
* one and never needs its own copy of the component's styling.
|
|
16
|
+
*
|
|
17
|
+
* Import `toast` from this same controller file, then call it:
|
|
18
|
+
* toast("Saved", { variant: "success" }).
|
|
19
|
+
*
|
|
20
|
+
* For a toast pushed from the server, skip this and append a UI::Toast to
|
|
21
|
+
* UI::Toast::Viewport::ID in a Turbo Stream response instead.
|
|
22
|
+
*/
|
|
23
|
+
export function toast(title: string, options: ToastOptions = {}) {
|
|
24
|
+
window.dispatchEvent(new CustomEvent<ToastOptions & { title: string }>('toast:show', {
|
|
25
|
+
detail: { title, ...options },
|
|
26
|
+
}));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* UI::Toast::Viewport controller — listens for toast:show and clones the
|
|
31
|
+
* matching variant template into itself. The cloned toast then connects its
|
|
32
|
+
* own ui--toast controller like any other toast, which is what actually
|
|
33
|
+
* drives its auto-dismiss and animations.
|
|
34
|
+
*/
|
|
35
|
+
export default class extends Controller<HTMLElement> {
|
|
36
|
+
static targets = ['template'];
|
|
37
|
+
|
|
38
|
+
declare readonly templateTargets: HTMLTemplateElement[];
|
|
39
|
+
|
|
40
|
+
connect() {
|
|
41
|
+
window.addEventListener('toast:show', this.show as EventListener);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
disconnect() {
|
|
45
|
+
window.removeEventListener('toast:show', this.show as EventListener);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
private show = (event: Event) => {
|
|
49
|
+
const detail = (event as CustomEvent<ToastOptions & { title?: string }>).detail || {};
|
|
50
|
+
const template = this.findTemplate(detail.variant);
|
|
51
|
+
if (!template) return;
|
|
52
|
+
|
|
53
|
+
const node = template.content.firstElementChild?.cloneNode(true) as HTMLElement | undefined;
|
|
54
|
+
if (!node) return;
|
|
55
|
+
|
|
56
|
+
this.fill(node, 'title', detail.title);
|
|
57
|
+
this.fill(node, 'description', detail.description);
|
|
58
|
+
|
|
59
|
+
if (detail.duration !== undefined) {
|
|
60
|
+
node.setAttribute('data-ui--toast-duration-value', String(detail.duration * 1000));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.element.appendChild(node);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
private findTemplate(variant?: string) {
|
|
67
|
+
return (
|
|
68
|
+
this.templateTargets.find((t) => t.dataset.variant === variant) ||
|
|
69
|
+
this.templateTargets.find((t) => t.dataset.variant === 'default')
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
private fill(node: HTMLElement, target: string, text?: string) {
|
|
74
|
+
const el = node.querySelector<HTMLElement>(`[data-ui--toast-target="${target}"]`);
|
|
75
|
+
if (!el) return;
|
|
76
|
+
if (text) {
|
|
77
|
+
el.textContent = text;
|
|
78
|
+
} else {
|
|
79
|
+
el.remove();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
@@ -13,6 +13,7 @@ import {
|
|
|
13
13
|
ChevronRight,
|
|
14
14
|
ChevronsUpDown,
|
|
15
15
|
CircleAlert,
|
|
16
|
+
CircleCheck,
|
|
16
17
|
Ellipsis,
|
|
17
18
|
Eye,
|
|
18
19
|
EyeOff,
|
|
@@ -23,6 +24,7 @@ import {
|
|
|
23
24
|
Moon,
|
|
24
25
|
PanelLeft,
|
|
25
26
|
Sun,
|
|
27
|
+
Upload,
|
|
26
28
|
X,
|
|
27
29
|
} from 'lucide';
|
|
28
30
|
|
|
@@ -34,6 +36,7 @@ const icons = {
|
|
|
34
36
|
ChevronRight,
|
|
35
37
|
ChevronsUpDown,
|
|
36
38
|
CircleAlert,
|
|
39
|
+
CircleCheck,
|
|
37
40
|
Ellipsis,
|
|
38
41
|
Eye,
|
|
39
42
|
EyeOff,
|
|
@@ -44,15 +47,24 @@ const icons = {
|
|
|
44
47
|
Moon,
|
|
45
48
|
PanelLeft,
|
|
46
49
|
Sun,
|
|
50
|
+
Upload,
|
|
47
51
|
X,
|
|
48
52
|
};
|
|
49
53
|
|
|
50
|
-
//
|
|
51
|
-
//
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
54
|
+
// Icons are drawn whenever one is added to the page, however it arrives: a
|
|
55
|
+
// Turbo visit, frame or stream, or a controller's own markup. Only <i> is
|
|
56
|
+
// watched: the SVG that replaces it keeps data-lucide, and drawing on that
|
|
57
|
+
// would draw forever.
|
|
58
|
+
const draw = () => createIcons({ icons });
|
|
59
|
+
const added = (records: MutationRecord[]) =>
|
|
60
|
+
records.some((record) =>
|
|
61
|
+
Array.from(record.addedNodes).some(
|
|
62
|
+
(node) => node instanceof Element && (node.matches('i[data-lucide]') || node.querySelector('i[data-lucide]') !== null),
|
|
63
|
+
),
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
draw();
|
|
67
|
+
new MutationObserver((records) => added(records) && draw()).observe(document.documentElement, {
|
|
68
|
+
childList: true,
|
|
69
|
+
subtree: true,
|
|
70
|
+
});
|
data/registry/components.yml
CHANGED
|
@@ -43,6 +43,8 @@ components:
|
|
|
43
43
|
description: A bordered container with header, content and footer.
|
|
44
44
|
checkbox:
|
|
45
45
|
description: A checkbox with label, standalone or bound to a form builder.
|
|
46
|
+
collapsible:
|
|
47
|
+
description: A section that opens and closes under its trigger, like a branch of a nav tree.
|
|
46
48
|
data_grid:
|
|
47
49
|
description: A sortable, filterable, server-paged table on AG Grid.
|
|
48
50
|
date_picker:
|
|
@@ -51,12 +53,16 @@ components:
|
|
|
51
53
|
description: A modal dialog on the native <dialog> element.
|
|
52
54
|
divider:
|
|
53
55
|
description: A horizontal or vertical rule, optionally labelled.
|
|
56
|
+
file_field:
|
|
57
|
+
description: A drag-and-drop file picker with a label, hint and error message, standalone or with a form builder.
|
|
58
|
+
form_field:
|
|
59
|
+
description: A label, hint and error message wired to any form control.
|
|
54
60
|
icon:
|
|
55
61
|
description: A Lucide icon.
|
|
56
62
|
image:
|
|
57
63
|
description: An image with consistent sizing and corner radius.
|
|
58
64
|
input:
|
|
59
|
-
description: A text field with label, hint and error, standalone or with a form builder.
|
|
65
|
+
description: A text field with label, hint and error message, standalone or with a form builder.
|
|
60
66
|
input_group:
|
|
61
67
|
description: A text field with addons before or after it.
|
|
62
68
|
label:
|
|
@@ -78,24 +84,34 @@ components:
|
|
|
78
84
|
description: The browser's own select, styled closed; best for phones' system pickers.
|
|
79
85
|
select:
|
|
80
86
|
description: A styled dropdown with keyboard navigation and type-ahead, posting like a native select.
|
|
87
|
+
session_tabs:
|
|
88
|
+
description: Browser-style tabs the user opens, renames, duplicates, reorders and closes.
|
|
81
89
|
sheet:
|
|
82
90
|
description: A panel that slides in from an edge of the screen.
|
|
83
91
|
sidebar:
|
|
84
92
|
description: A collapsible application sidebar with groups and menus.
|
|
93
|
+
skeleton:
|
|
94
|
+
description: A pulsing placeholder shown in a piece of content's shape while it loads.
|
|
85
95
|
spinner:
|
|
86
96
|
description: A loading indicator.
|
|
87
97
|
switch:
|
|
88
98
|
description: An on/off toggle.
|
|
89
99
|
table:
|
|
90
100
|
description: A styled table with header, rows and caption.
|
|
101
|
+
table_of_contents:
|
|
102
|
+
description: An "On this page" list that follows the reader's scroll.
|
|
91
103
|
tabs:
|
|
92
104
|
description: Tabbed panels with keyboard navigation.
|
|
93
105
|
text:
|
|
94
106
|
description: Body text in set sizes, weights and tones.
|
|
107
|
+
textarea:
|
|
108
|
+
description: A multi-line text field with label, hint and error, that can grow with its text.
|
|
95
109
|
theme_toggle:
|
|
96
110
|
description: A light/dark/system theme switcher.
|
|
97
111
|
files:
|
|
98
112
|
- app/views/layouts/_theme_script.html.erb
|
|
113
|
+
toast:
|
|
114
|
+
description: A stacked, auto-dismissing message, pushed from the client or a Turbo Stream.
|
|
99
115
|
tooltip:
|
|
100
116
|
description: A hint shown on hover and focus.
|
|
101
117
|
typography:
|
|
@@ -32,6 +32,8 @@ en:
|
|
|
32
32
|
hide: 'Hide password'
|
|
33
33
|
sidebar:
|
|
34
34
|
toggle: 'Toggle sidebar'
|
|
35
|
+
table_of_contents:
|
|
36
|
+
label: 'On this page'
|
|
35
37
|
pagination:
|
|
36
38
|
label: 'Pages'
|
|
37
39
|
summary: 'Showing %{from}–%{to} of %{count}'
|
|
@@ -47,6 +49,20 @@ en:
|
|
|
47
49
|
no_matches: 'No matches'
|
|
48
50
|
# Shown on a multiple select once more than two options are ticked.
|
|
49
51
|
selected: '%{count} selected'
|
|
52
|
+
session_tabs:
|
|
53
|
+
label: 'Open tabs'
|
|
54
|
+
add: 'New tab'
|
|
55
|
+
options: 'Tab options'
|
|
56
|
+
rename: 'Rename'
|
|
57
|
+
rename_label: 'Tab name'
|
|
58
|
+
duplicate: 'Duplicate'
|
|
59
|
+
move_left: 'Move left'
|
|
60
|
+
move_right: 'Move right'
|
|
61
|
+
close: 'Close'
|
|
62
|
+
confirm_close:
|
|
63
|
+
title: 'Close this tab?'
|
|
64
|
+
cancel: 'Keep it open'
|
|
65
|
+
confirm: 'Close tab'
|
|
50
66
|
theme_toggle:
|
|
51
67
|
label: 'Change theme'
|
|
52
68
|
heading: 'Theme'
|
|
@@ -54,3 +70,8 @@ en:
|
|
|
54
70
|
light: 'Light'
|
|
55
71
|
dark: 'Dark'
|
|
56
72
|
system: 'System'
|
|
73
|
+
toast:
|
|
74
|
+
close_label: 'Dismiss'
|
|
75
|
+
file_field:
|
|
76
|
+
browse: 'Click to upload'
|
|
77
|
+
drop: 'or drag and drop'
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<%# A sidebar tree: branches open on click, and the one holding the current page starts open. %>
|
|
2
|
+
<nav aria-label="Documentation" class="w-56 text-sm">
|
|
3
|
+
<%= render(UI::Collapsible.new(open: true, trigger_class: "font-medium hover:bg-accent", content_class: "ml-3 space-y-0.5 border-l pl-2")) do |c| %>
|
|
4
|
+
<% c.with_trigger { "Forms" } %>
|
|
5
|
+
<a href="#" class="block rounded-md px-3 py-1.5 text-muted-foreground hover:bg-accent hover:text-foreground">Input</a>
|
|
6
|
+
<a href="#" aria-current="page" class="block rounded-md bg-primary/10 px-3 py-1.5 font-medium text-primary">Textarea</a>
|
|
7
|
+
<%= render(UI::Collapsible.new(trigger_class: "text-muted-foreground hover:bg-accent hover:text-foreground", content_class: "ml-3 space-y-0.5 border-l pl-2")) do |inner| %>
|
|
8
|
+
<% inner.with_trigger { "Choices" } %>
|
|
9
|
+
<a href="#" class="block rounded-md px-3 py-1.5 text-muted-foreground hover:bg-accent hover:text-foreground">Select</a>
|
|
10
|
+
<a href="#" class="block rounded-md px-3 py-1.5 text-muted-foreground hover:bg-accent hover:text-foreground">Checkbox</a>
|
|
11
|
+
<% end %>
|
|
12
|
+
<% end %>
|
|
13
|
+
<%= render(UI::Collapsible.new(trigger_class: "font-medium hover:bg-accent", content_class: "ml-3 space-y-0.5 border-l pl-2")) do |c| %>
|
|
14
|
+
<% c.with_trigger { "Overlays" } %>
|
|
15
|
+
<a href="#" class="block rounded-md px-3 py-1.5 text-muted-foreground hover:bg-accent hover:text-foreground">Dialog</a>
|
|
16
|
+
<a href="#" class="block rounded-md px-3 py-1.5 text-muted-foreground hover:bg-accent hover:text-foreground">Sheet</a>
|
|
17
|
+
<% end %>
|
|
18
|
+
</nav>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<div class="max-w-md rounded-lg border p-4">
|
|
2
|
+
<p class="text-sm">Payments are collected on the 1st of each month.</p>
|
|
3
|
+
<%= render(UI::Collapsible.new(size: :sm, class: "mt-2", trigger_class: "w-auto -ml-2 text-primary hover:underline")) do |c| %>
|
|
4
|
+
<% c.with_trigger { "How refunds work" } %>
|
|
5
|
+
<p class="px-0 text-sm text-muted-foreground">Cancel within 14 days for a full refund. After that, the rest of the month is credited to your next invoice.</p>
|
|
6
|
+
<% end %>
|
|
7
|
+
</div>
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class UI::CollapsiblePreview < ViewComponent::Preview
|
|
4
|
+
# @label Playground
|
|
5
|
+
# @param open toggle
|
|
6
|
+
# @param size select { choices: [sm, default, lg] }
|
|
7
|
+
def playground(open: false, size: :default)
|
|
8
|
+
render(UI::Collapsible.new(open: open, size: size.to_sym, class: "max-w-xs")) do |c|
|
|
9
|
+
c.with_trigger { "Advanced settings" }
|
|
10
|
+
"Hidden until opened."
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# @label Navigation tree
|
|
15
|
+
def default; render_with_template; end
|
|
16
|
+
|
|
17
|
+
# @label Show more
|
|
18
|
+
def show_more; render_with_template; end
|
|
19
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class UI::FileFieldPreview < ViewComponent::Preview
|
|
4
|
+
# @label Playground
|
|
5
|
+
# @param label text
|
|
6
|
+
# @param hint text
|
|
7
|
+
# @param multiple toggle
|
|
8
|
+
# @param disabled toggle
|
|
9
|
+
def playground(label: "Avatar", hint: "PNG or JPG, up to 5MB", multiple: false, disabled: false)
|
|
10
|
+
render(UI::FileField.new(label: label, hint: hint, multiple: multiple, disabled: disabled))
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# @label With Error
|
|
14
|
+
def with_error; render_with_template; end
|
|
15
|
+
|
|
16
|
+
# @label Multiple Files
|
|
17
|
+
def multiple_files; render_with_template; end
|
|
18
|
+
end
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
<div class="max-w-md">
|
|
2
|
+
<%= render(UI::FormField.new(label: "Accent colour", hint: "Used for your avatar and links.")) do |field| %>
|
|
3
|
+
<%= tag.input(type: "color", name: "accent", value: "#4d1a7f",
|
|
4
|
+
class: "h-9 w-16 rounded-md border border-input bg-background p-1", **field.control_attributes) %>
|
|
5
|
+
<% end %>
|
|
6
|
+
</div>
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
<div class="flex flex-col gap-6 max-w-md">
|
|
2
|
+
<%= render(UI::Input.new(type: :email, label: "Email", hint: "We'll only use it to send receipts.", placeholder: "you@example.com")) %>
|
|
3
|
+
<%= render(UI::Input.new(label: "Username", value: "a", error: "Use at least 3 characters.")) %>
|
|
4
|
+
<%= render(UI::Textarea.new(label: "Bio", hint: "Shown on your public profile.", rows: 3)) %>
|
|
5
|
+
<%= render(UI::NativeSelect.new(label: "Team", options: %w[Design Engineering], prompt: "Choose a team", error: "Choose a team to continue.")) %>
|
|
6
|
+
<%= render(UI::Select.new(label: "Plan", options: %w[Free Pro Team], hint: "You can change plans at any time.")) %>
|
|
7
|
+
</div>
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
<%# In an app, @signup comes back from a failed save. Bound fields show its errors on their own. %>
|
|
2
|
+
<% signup = Struct.new(:email, :bio) do
|
|
3
|
+
include ActiveModel::Validations
|
|
4
|
+
def self.model_name = ActiveModel::Name.new(self, nil, "Signup")
|
|
5
|
+
validates :email, presence: true
|
|
6
|
+
validates :bio, length: { minimum: 20 }
|
|
7
|
+
end.new("", "Hi").tap(&:validate) %>
|
|
8
|
+
<%= form_with(model: signup, url: "#", class: "flex flex-col gap-6 max-w-md") do |f| %>
|
|
9
|
+
<%= render(UI::Input.new(form: f, field: :email, type: :email)) %>
|
|
10
|
+
<%= render(UI::Textarea.new(form: f, field: :bio, hint: "A sentence or two about you.")) %>
|
|
11
|
+
<% end %>
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class UI::FormFieldPreview < ViewComponent::Preview
|
|
4
|
+
# @label On the built-in controls
|
|
5
|
+
def default; render_with_template; end
|
|
6
|
+
|
|
7
|
+
# @label Errors from the model
|
|
8
|
+
def model_errors; render_with_template; end
|
|
9
|
+
|
|
10
|
+
# @label Your own control
|
|
11
|
+
def custom_control; render_with_template; end
|
|
12
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
<%# Every action dispatches a cancelable ui--session-tabs:<action> event.
|
|
2
|
+
This page logs them, refuses to close "Pinned", and handles "add" itself
|
|
3
|
+
by cloning a <template>. In a real app, pass URLs instead and let the
|
|
4
|
+
server re-render. %>
|
|
5
|
+
<div class="max-w-2xl space-y-3" id="session-tabs-events-demo">
|
|
6
|
+
<%= render(UI::SessionTabs.new) do |tabs| %>
|
|
7
|
+
<% tabs.with_tab(value: "pinned", active: true, rename: true, move: true, close: true) { "Pinned" } %>
|
|
8
|
+
<% tabs.with_tab(value: "notes", rename: true, duplicate: true, move: true, close: true) { "Notes" } %>
|
|
9
|
+
<% tabs.with_add %>
|
|
10
|
+
<% end %>
|
|
11
|
+
<pre class="h-40 overflow-auto rounded-md bg-muted p-3 text-xs" data-log></pre>
|
|
12
|
+
</div>
|
|
13
|
+
<script>
|
|
14
|
+
(() => {
|
|
15
|
+
const root = document.getElementById("session-tabs-events-demo");
|
|
16
|
+
const log = root.querySelector("[data-log]");
|
|
17
|
+
let n = 0;
|
|
18
|
+
const write = (line) => { log.textContent = line + "\n" + log.textContent; };
|
|
19
|
+
|
|
20
|
+
for (const action of ["select", "add", "rename", "duplicate", "move", "close"]) {
|
|
21
|
+
root.addEventListener(`ui--session-tabs:${action}`, (event) => {
|
|
22
|
+
write(`${action} ${JSON.stringify(event.detail)}`);
|
|
23
|
+
|
|
24
|
+
if (action === "close" && event.detail.value === "pinned") {
|
|
25
|
+
event.preventDefault();
|
|
26
|
+
write(" ↳ cancelled: Pinned can't be closed");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
if (action === "add" || action === "duplicate") {
|
|
30
|
+
// The app supplies new markup; here, copy an existing tab.
|
|
31
|
+
const list = root.querySelector('[role="tablist"]');
|
|
32
|
+
const source = list.querySelector('[data-value="notes"]') || list.lastElementChild;
|
|
33
|
+
const copy = source.cloneNode(true);
|
|
34
|
+
n += 1;
|
|
35
|
+
const value = `new-${n}`;
|
|
36
|
+
copy.dataset.value = value;
|
|
37
|
+
copy.dataset.state = "inactive";
|
|
38
|
+
copy.querySelectorAll("[id]").forEach((el) => { el.id = `${el.id}-${value}`; });
|
|
39
|
+
copy.querySelector('[data-ui--session-tabs-target="label"]').textContent =
|
|
40
|
+
action === "add" ? `New tab ${n}` : `Copy ${n}`;
|
|
41
|
+
list.appendChild(copy);
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
})();
|
|
46
|
+
</script>
|