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.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/file_field.html.erb +4 -0
  5. data/registry/app/components/ui/file_field.rb +73 -0
  6. data/registry/app/components/ui/navigation_menu/item.html.erb +16 -0
  7. data/registry/app/components/ui/navigation_menu/item.rb +102 -0
  8. data/registry/app/components/ui/navigation_menu/link.html.erb +9 -0
  9. data/registry/app/components/ui/navigation_menu/link.rb +41 -0
  10. data/registry/app/components/ui/navigation_menu.html.erb +3 -0
  11. data/registry/app/components/ui/navigation_menu.rb +64 -0
  12. data/registry/app/components/ui/session_tabs/add.html.erb +1 -0
  13. data/registry/app/components/ui/session_tabs/add.rb +52 -0
  14. data/registry/app/components/ui/session_tabs/tab.html.erb +37 -0
  15. data/registry/app/components/ui/session_tabs/tab.rb +243 -0
  16. data/registry/app/components/ui/session_tabs.html.erb +15 -0
  17. data/registry/app/components/ui/session_tabs.rb +101 -0
  18. data/registry/app/components/ui/skeleton.html.erb +1 -0
  19. data/registry/app/components/ui/skeleton.rb +30 -0
  20. data/registry/app/components/ui/toast/viewport.html.erb +4 -0
  21. data/registry/app/components/ui/toast/viewport.rb +41 -0
  22. data/registry/app/components/ui/toast.html.erb +11 -0
  23. data/registry/app/components/ui/toast.rb +89 -0
  24. data/registry/app/javascript/controllers/ui/file_field_controller.ts +49 -0
  25. data/registry/app/javascript/controllers/ui/menu_controller.ts +3 -0
  26. data/registry/app/javascript/controllers/ui/navigation_menu_controller.ts +265 -0
  27. data/registry/app/javascript/controllers/ui/session_tabs_controller.ts +732 -0
  28. data/registry/app/javascript/controllers/ui/toast_controller.ts +62 -0
  29. data/registry/app/javascript/controllers/ui/toast_viewport_controller.ts +82 -0
  30. data/registry/app/javascript/velvetkit_icons.ts +21 -9
  31. data/registry/components.yml +10 -0
  32. data/registry/config/locales/velvetkit.en.yml +21 -0
  33. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +3 -0
  34. data/registry/test/components/previews/ui/file_field_preview/with_error.html.erb +3 -0
  35. data/registry/test/components/previews/ui/file_field_preview.rb +18 -0
  36. data/registry/test/components/previews/ui/navigation_menu_preview/custom_panel.html.erb +20 -0
  37. data/registry/test/components/previews/ui/navigation_menu_preview/playground.html.erb +73 -0
  38. data/registry/test/components/previews/ui/navigation_menu_preview/simple.html.erb +23 -0
  39. data/registry/test/components/previews/ui/navigation_menu_preview.rb +23 -0
  40. data/registry/test/components/previews/ui/session_tabs_preview/handling_events.html.erb +46 -0
  41. data/registry/test/components/previews/ui/session_tabs_preview/saved_views.html.erb +16 -0
  42. data/registry/test/components/previews/ui/session_tabs_preview/touch_with_confirmation.html.erb +20 -0
  43. data/registry/test/components/previews/ui/session_tabs_preview.rb +35 -0
  44. data/registry/test/components/previews/ui/skeleton_preview/card_placeholder.html.erb +10 -0
  45. data/registry/test/components/previews/ui/skeleton_preview/shapes.html.erb +9 -0
  46. data/registry/test/components/previews/ui/skeleton_preview.rb +15 -0
  47. data/registry/test/components/previews/ui/toast_preview/live_viewport.html.erb +21 -0
  48. data/registry/test/components/previews/ui/toast_preview/variants.html.erb +7 -0
  49. data/registry/test/components/previews/ui/toast_preview/with_action.html.erb +7 -0
  50. data/registry/test/components/previews/ui/toast_preview.rb +29 -0
  51. data/registry/test/components/ui/file_field_test.rb +101 -0
  52. data/registry/test/components/ui/navigation_menu_test.rb +130 -0
  53. data/registry/test/components/ui/session_tabs/add_test.rb +52 -0
  54. data/registry/test/components/ui/session_tabs_test.rb +281 -0
  55. data/registry/test/components/ui/skeleton_test.rb +65 -0
  56. data/registry/test/components/ui/toast/viewport_test.rb +72 -0
  57. data/registry/test/components/ui/toast_test.rb +183 -0
  58. metadata +51 -1
@@ -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
- // Turbo replaces markup without a full page load, so icons are drawn again
51
- // after every kind of render, not just the first.
52
- [
53
- 'turbo:load',
54
- 'turbo:frame-load',
55
- 'turbo:render',
56
- 'turbo:submit-end',
57
- 'turbo:render-stream',
58
- ].forEach((event) => document.addEventListener(event, () => createIcons({ icons })));
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
+ });
@@ -53,6 +53,8 @@ components:
53
53
  description: A modal dialog on the native <dialog> element.
54
54
  divider:
55
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.
56
58
  form_field:
57
59
  description: A label, hint and error message wired to any form control.
58
60
  icon:
@@ -69,6 +71,8 @@ components:
69
71
  description: A link styled as text or as a button.
70
72
  menu:
71
73
  description: A dropdown menu with keyboard navigation.
74
+ navigation_menu:
75
+ description: Site navigation with links and hover or click panels of rich links.
72
76
  pagination:
73
77
  description: Page links for a long list, from a Pagy object.
74
78
  gems: [pagy]
@@ -82,10 +86,14 @@ components:
82
86
  description: The browser's own select, styled closed; best for phones' system pickers.
83
87
  select:
84
88
  description: A styled dropdown with keyboard navigation and type-ahead, posting like a native select.
89
+ session_tabs:
90
+ description: Browser-style tabs the user opens, renames, duplicates, reorders and closes.
85
91
  sheet:
86
92
  description: A panel that slides in from an edge of the screen.
87
93
  sidebar:
88
94
  description: A collapsible application sidebar with groups and menus.
95
+ skeleton:
96
+ description: A pulsing placeholder shown in a piece of content's shape while it loads.
89
97
  spinner:
90
98
  description: A loading indicator.
91
99
  switch:
@@ -104,6 +112,8 @@ components:
104
112
  description: A light/dark/system theme switcher.
105
113
  files:
106
114
  - app/views/layouts/_theme_script.html.erb
115
+ toast:
116
+ description: A stacked, auto-dismissing message, pushed from the client or a Turbo Stream.
107
117
  tooltip:
108
118
  description: A hint shown on hover and focus.
109
119
  typography:
@@ -34,6 +34,8 @@ en:
34
34
  toggle: 'Toggle sidebar'
35
35
  table_of_contents:
36
36
  label: 'On this page'
37
+ navigation_menu:
38
+ label: 'Main'
37
39
  pagination:
38
40
  label: 'Pages'
39
41
  summary: 'Showing %{from}–%{to} of %{count}'
@@ -49,6 +51,20 @@ en:
49
51
  no_matches: 'No matches'
50
52
  # Shown on a multiple select once more than two options are ticked.
51
53
  selected: '%{count} selected'
54
+ session_tabs:
55
+ label: 'Open tabs'
56
+ add: 'New tab'
57
+ options: 'Tab options'
58
+ rename: 'Rename'
59
+ rename_label: 'Tab name'
60
+ duplicate: 'Duplicate'
61
+ move_left: 'Move left'
62
+ move_right: 'Move right'
63
+ close: 'Close'
64
+ confirm_close:
65
+ title: 'Close this tab?'
66
+ cancel: 'Keep it open'
67
+ confirm: 'Close tab'
52
68
  theme_toggle:
53
69
  label: 'Change theme'
54
70
  heading: 'Theme'
@@ -56,3 +72,8 @@ en:
56
72
  light: 'Light'
57
73
  dark: 'Dark'
58
74
  system: 'System'
75
+ toast:
76
+ close_label: 'Dismiss'
77
+ file_field:
78
+ browse: 'Click to upload'
79
+ drop: 'or drag and drop'
@@ -0,0 +1,3 @@
1
+ <div class="max-w-sm">
2
+ <%= render(UI::FileField.new(label: "Attachments", hint: "Drop as many as you like", multiple: true)) %>
3
+ </div>
@@ -0,0 +1,3 @@
1
+ <div class="max-w-sm">
2
+ <%= render(UI::FileField.new(label: "Resume", error: "Must be a PDF under 5MB")) %>
3
+ </div>
@@ -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,20 @@
1
+ <%# Any markup can go in a panel alongside, or instead of, its links. %>
2
+ <div class="flex min-h-[28rem] justify-center pt-8">
3
+ <%= render(UI::NavigationMenu.new) do |nav| %>
4
+ <% nav.with_item(label: "Company", panel_class: "w-[32rem]") do |item| %>
5
+ <% item.with_link(title: "About", href: "#about", description: "Who we are and why we build this.") %>
6
+ <% item.with_link(title: "Careers", href: "#careers", description: "Open roles, remote-first.") %>
7
+ <% item.with_link(title: "Customers", href: "#customers", description: "Teams that run on Acme, in their words.") %>
8
+ <% item.with_link(title: "Contact", href: "#contact", description: "Sales, support and partnerships.") %>
9
+ <div class="mt-2 flex items-center justify-between gap-4 rounded-md bg-muted p-4">
10
+ <div>
11
+ <p class="text-sm font-medium">We're hiring</p>
12
+ <p class="mt-1 text-sm text-muted-foreground">Three engineering roles are open this quarter.</p>
13
+ </div>
14
+ <%= render(UI::Button.new(size: :sm, variant: :outline)) { "See roles" } %>
15
+ </div>
16
+ <% end %>
17
+ <% nav.with_item(label: "Blog", href: "#blog") %>
18
+ <% nav.with_item(label: "Contact", href: "#contact") %>
19
+ <% end %>
20
+ </div>
@@ -0,0 +1,73 @@
1
+ <div class="min-h-screen bg-background">
2
+ <header class="sticky top-0 z-40 border-b bg-background/95 backdrop-blur">
3
+ <div class="mx-auto flex h-14 max-w-6xl items-center gap-6 px-4">
4
+ <a href="#" class="flex items-center gap-2 font-semibold"><span class="size-6 rounded-md bg-primary"></span>Acme</a>
5
+
6
+ <%= render(UI::NavigationMenu.new(label: "Main", open_delay: open_delay, close_delay: close_delay)) do |nav| %>
7
+ <% nav.with_item(label: "Products", columns: 2) do |item| %>
8
+ <% item.with_link(title: "Analytics", href: "#analytics", icon: "chart-column", description: "See what customers do, as they do it.") %>
9
+ <% item.with_link(title: "Automations", href: "#automations", icon: "workflow", description: "Run the repetitive parts on a schedule.") %>
10
+ <% item.with_link(title: "Inbox", href: "#inbox", icon: "message-circle", description: "Every conversation in one place.") %>
11
+ <% item.with_link(title: "Billing", href: "#billing", icon: "credit-card", description: "Invoices, plans and payment methods.") %>
12
+ <% item.with_link(title: "Storefront", href: "#storefront", icon: "store", description: "Sell online with a page you own.") %>
13
+ <% item.with_link(title: "Notifications", href: "#notifications", icon: "bell", description: "Email, SMS and push from one API.") %>
14
+ <% item.with_link(title: "Security", href: "#security", icon: "shield", description: "SSO, audit logs and roles.") %>
15
+ <% item.with_link(title: "Integrations", href: "#integrations", icon: "layout-grid", description: "Connect the tools you already use.") %>
16
+ <% end %>
17
+ <% nav.with_item(label: "Solutions", columns: 2) do |item| %>
18
+ <% item.with_link(title: "Startups", href: "#startups", description: "Everything to launch, on a free plan.") %>
19
+ <% item.with_link(title: "Enterprise", href: "#enterprise", description: "Scale, compliance and a named contact.") %>
20
+ <% item.with_link(title: "Agencies", href: "#agencies", description: "Run every client from one account.") %>
21
+ <% item.with_link(title: "Education", href: "#education", description: "Discounts for schools and students.") %>
22
+ <% item.with_link(title: "Nonprofits", href: "#nonprofits", description: "Half price for registered charities.") %>
23
+ <% item.with_link(title: "Developers", href: "#developers", description: "APIs, webhooks and SDKs.") %>
24
+ <% end %>
25
+ <% nav.with_item(label: "Resources") do |item| %>
26
+ <% item.with_link(title: "Documentation", href: "#docs", icon: "book-open", description: "Guides and the API reference.") %>
27
+ <% item.with_link(title: "Blog", href: "#blog", icon: "newspaper", description: "Product news and how-tos.") %>
28
+ <% item.with_link(title: "Changelog", href: "#changelog", icon: "rocket", description: "What shipped this week.") %>
29
+ <% item.with_link(title: "Academy", href: "#academy", icon: "graduation-cap", description: "Free courses, start to finish.") %>
30
+ <% item.with_link(title: "Help centre", href: "#help", icon: "life-buoy", description: "Answers, and a human when you need one.") %>
31
+ <% end %>
32
+ <% nav.with_item(label: "Pricing", href: "#pricing") %>
33
+ <% nav.with_item(label: "Docs", href: "#docs", active: true) %>
34
+ <% end %>
35
+
36
+ <div class="ml-auto flex items-center gap-2">
37
+ <%= render(UI::Button.new(variant: :ghost, size: :sm)) { "Sign in" } %>
38
+ <%= render(UI::Button.new(size: :sm)) { "Get started" } %>
39
+ </div>
40
+ </div>
41
+ </header>
42
+
43
+ <main class="mx-auto max-w-3xl space-y-6 px-4 py-12">
44
+ <h1 class="text-3xl font-bold tracking-tight">Scroll this page with a menu open</h1>
45
+ <p class="text-muted-foreground">The header sticks to the top and its panels stay attached to their triggers. At the bottom there's a footer menu: its panels open upwards, because there's no room below.</p>
46
+ <% 8.times do |i| %>
47
+ <section class="space-y-2">
48
+ <h2 class="text-lg font-semibold">Section <%= i + 1 %></h2>
49
+ <p class="text-sm text-muted-foreground">Placeholder copy so the page is long enough to scroll. Open a menu in the header, then scroll up and down: the panel follows the trigger. Then scroll to the footer and open one of its menus.</p>
50
+ <div class="h-32 rounded-lg border bg-muted/40"></div>
51
+ </section>
52
+ <% end %>
53
+ </main>
54
+
55
+ <footer class="border-t">
56
+ <div class="mx-auto flex max-w-6xl items-center justify-between gap-4 px-4 py-6">
57
+ <span class="text-sm text-muted-foreground">© Acme</span>
58
+ <%= render(UI::NavigationMenu.new(label: "Footer")) do |nav| %>
59
+ <% nav.with_item(label: "Company") do |item| %>
60
+ <% item.with_link(title: "About", href: "#about", description: "Who we are and why we build this.") %>
61
+ <% item.with_link(title: "Careers", href: "#careers", description: "Open roles, remote-first.") %>
62
+ <% item.with_link(title: "Press", href: "#press", description: "Logos, photos and contacts.") %>
63
+ <% end %>
64
+ <% nav.with_item(label: "Legal") do |item| %>
65
+ <% item.with_link(title: "Terms", href: "#terms") %>
66
+ <% item.with_link(title: "Privacy", href: "#privacy") %>
67
+ <% item.with_link(title: "Cookies", href: "#cookies") %>
68
+ <% end %>
69
+ <% nav.with_item(label: "Status", href: "#status") %>
70
+ <% end %>
71
+ </div>
72
+ </footer>
73
+ </div>
@@ -0,0 +1,23 @@
1
+ <div class="flex min-h-96 justify-center pt-8">
2
+ <%= render(UI::NavigationMenu.new) do |nav| %>
3
+ <% nav.with_item(label: "Getting started") do |item| %>
4
+ <% item.with_link(title: "Introduction", href: "#introduction", description: "Re-usable components built with Tailwind CSS.") %>
5
+ <% item.with_link(title: "Installation", href: "#installation", description: "How to install dependencies and structure your app.") %>
6
+ <% item.with_link(title: "Typography", href: "#typography", description: "Styles for headings, paragraphs, lists…etc") %>
7
+ <% end %>
8
+ <% nav.with_item(label: "Components", columns: 2) do |item| %>
9
+ <% item.with_link(title: "Alert", href: "#alert", description: "A callout for important information.") %>
10
+ <% item.with_link(title: "Dialog", href: "#dialog", description: "A modal window on top of the page.") %>
11
+ <% item.with_link(title: "Menu", href: "#menu", description: "A list of actions behind a button.") %>
12
+ <% item.with_link(title: "Tabs", href: "#tabs", description: "Panels shown one at a time.") %>
13
+ <% item.with_link(title: "Toast", href: "#toast", description: "A short message that disappears on its own.") %>
14
+ <% item.with_link(title: "Tooltip", href: "#tooltip", description: "A label that appears on hover or focus.") %>
15
+ <% end %>
16
+ <% nav.with_item(label: "With Icon") do |item| %>
17
+ <% item.with_link(title: "Backlog", href: "#backlog", icon: "circle-alert") %>
18
+ <% item.with_link(title: "To do", href: "#todo", icon: "info") %>
19
+ <% item.with_link(title: "Done", href: "#done", icon: "circle-check") %>
20
+ <% end %>
21
+ <% nav.with_item(label: "Docs", href: "#docs") %>
22
+ <% end %>
23
+ </div>
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A site's main navigation. Hover a trigger with a mouse (or click, or press
4
+ # Enter / ArrowDown) to open its panel; Escape, a click outside or Tab away
5
+ # closes it. Panels follow their trigger as the page scrolls, and open
6
+ # upwards when there isn't room below.
7
+ class UI::NavigationMenuPreview < ViewComponent::Preview
8
+ # A sticky site header with several menus, on a page long enough to
9
+ # scroll, and a second menu in the footer whose panels open upwards.
10
+ #
11
+ # @label Site header
12
+ # @param open_delay number
13
+ # @param close_delay number
14
+ def playground(open_delay: 150, close_delay: 200)
15
+ render_with_template(locals: { open_delay: open_delay.to_i, close_delay: close_delay.to_i })
16
+ end
17
+
18
+ # @label Simple
19
+ def simple; render_with_template; end
20
+
21
+ # @label Custom panel
22
+ def custom_panel; render_with_template; end
23
+ 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>
@@ -0,0 +1,16 @@
1
+ <%# A "Default" view the user can't close or rename, plus views they made. %>
2
+ <div class="max-w-2xl">
3
+ <%= render(UI::SessionTabs.new(label: "Saved views")) do |tabs| %>
4
+ <% tabs.with_tab(value: "default", active: true, duplicate: true) do |tab| %>
5
+ <% tab.with_meta { "128" } %>Default
6
+ <% end %>
7
+ <% tabs.with_tab(value: "unpaid", rename: true, duplicate: true, move: true, close: true) do |tab| %>
8
+ <% tab.with_meta { "12" } %>Unpaid
9
+ <% end %>
10
+ <% tabs.with_tab(value: "this-week", rename: true, duplicate: true, move: true, close: true) do |tab| %>
11
+ <% tab.with_meta { "41" } %>This week
12
+ <% end %>
13
+ <% tabs.with_add %>
14
+ <% end %>
15
+ <div class="rounded-b-md border border-t-0 p-4 text-sm text-muted-foreground">The table for the selected view goes here.</div>
16
+ </div>
@@ -0,0 +1,20 @@
1
+ <%# Touch size: 44px tabs; long-press a tab to open its menu. Tabs with
2
+ unsaved work ask before closing; empty ones close straight away. %>
3
+ <div class="max-w-2xl space-y-3">
4
+ <%= render(UI::SessionTabs.new(size: :touch, return_focus: "#session-tabs-demo-input")) do |tabs| %>
5
+ <% tabs.with_tab(value: "1", active: true, rename: true, move: true, close: true,
6
+ close_confirm: "This tab has 3 unsaved items. They will be lost.") do |tab| %>
7
+ <% tab.with_meta { "3 items" } %>Tab 1
8
+ <% end %>
9
+ <% tabs.with_tab(value: "2", rename: true, move: true, close: true,
10
+ close_confirm: "This tab has 1 unsaved item. It will be lost.") do |tab| %>
11
+ <% tab.with_meta { "1 item" } %>Tab 2
12
+ <% end %>
13
+ <% tabs.with_tab(value: "3", rename: true, move: true, close: true) do |tab| %>
14
+ <% tab.with_meta { "Empty" } %>Tab 3
15
+ <% end %>
16
+ <% tabs.with_add %>
17
+ <% end %>
18
+ <input id="session-tabs-demo-input" class="h-11 w-full rounded-md border border-input bg-background px-3 text-sm"
19
+ placeholder="Focus comes back here after each tab action (return_focus:)">
20
+ </div>
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Tabs the user manages: rename, duplicate, reorder, close, add.
4
+ #
5
+ # Each action is opt-in per tab and takes either a URL (submitted as a Turbo
6
+ # form — rename PATCH, duplicate POST, move PATCH with a 1-based `position`,
7
+ # close DELETE) or `true` (only the cancelable `ui--session-tabs:<action>`
8
+ # event fires). These previews use events only, so nothing is saved.
9
+ class UI::SessionTabsPreview < ViewComponent::Preview
10
+ # @label Playground
11
+ # @param size select { choices: [default, touch] }
12
+ # @param min_tabs number
13
+ # @param rename toggle
14
+ # @param duplicate toggle
15
+ # @param move toggle
16
+ # @param close toggle
17
+ def playground(size: :default, min_tabs: 1, rename: true, duplicate: true, move: true, close: true)
18
+ render(UI::SessionTabs.new(size: size.to_sym, min_tabs: min_tabs.to_i)) do |tabs|
19
+ %w[Default Drafts Archive].each_with_index do |name, i|
20
+ tabs.with_tab(value: name.downcase, active: i.zero?,
21
+ rename: rename, duplicate: duplicate, move: move, close: close) { name }
22
+ end
23
+ tabs.with_add
24
+ end
25
+ end
26
+
27
+ # @label Saved views
28
+ def saved_views; render_with_template; end
29
+
30
+ # @label Touch, with close confirmation
31
+ def touch_with_confirmation; render_with_template; end
32
+
33
+ # @label Handling the events
34
+ def handling_events; render_with_template; end
35
+ end
@@ -0,0 +1,10 @@
1
+ <%= render(UI::Card.new(class: "w-80 p-6")) do %>
2
+ <div class="flex items-center gap-4">
3
+ <%= render(UI::Skeleton.new(shape: :circle)) %>
4
+ <div class="flex-1 space-y-2">
5
+ <%= render(UI::Skeleton.new(shape: :text, class: "w-3/4")) %>
6
+ <%= render(UI::Skeleton.new(shape: :text, class: "w-1/2")) %>
7
+ </div>
8
+ </div>
9
+ <%= render(UI::Skeleton.new(shape: :rectangle, class: "mt-4")) %>
10
+ <% end %>
@@ -0,0 +1,9 @@
1
+ <div class="flex flex-col gap-6">
2
+ <div class="space-y-2">
3
+ <%= render(UI::Skeleton.new(shape: :text, class: "w-64")) %>
4
+ <%= render(UI::Skeleton.new(shape: :text, class: "w-48")) %>
5
+ <%= render(UI::Skeleton.new(shape: :text, class: "w-56")) %>
6
+ </div>
7
+ <%= render(UI::Skeleton.new(shape: :circle)) %>
8
+ <%= render(UI::Skeleton.new(shape: :rectangle, class: "w-64")) %>
9
+ </div>
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UI::SkeletonPreview < ViewComponent::Preview
4
+ # @label Playground
5
+ # @param shape select { choices: [text, circle, rectangle] }
6
+ def playground(shape: :text)
7
+ render(UI::Skeleton.new(shape: shape.to_sym, class: shape.to_sym == :circle ? nil : "w-64"))
8
+ end
9
+
10
+ # @label Shapes
11
+ def shapes; render_with_template; end
12
+
13
+ # @label Card Placeholder
14
+ def card_placeholder; render_with_template; end
15
+ end
@@ -0,0 +1,21 @@
1
+ <div class="flex flex-wrap gap-2">
2
+ <%
3
+ demos = {
4
+ default: ["Notification", "Heads up, something happened."],
5
+ success: ["Saved", "Your changes have been saved."],
6
+ destructive: ["Something broke", "There was a problem with your request."],
7
+ warning: ["Careful", "This action cannot be undone."],
8
+ info: ["FYI", "Here's some helpful information."]
9
+ }
10
+ %>
11
+ <% demos.each do |variant, (title, description)| %>
12
+ <% payload = { title: title, description: description, variant: variant }.to_json %>
13
+ <%= render(UI::Button.new(variant: :outline, size: :sm,
14
+ onclick: "window.dispatchEvent(new CustomEvent('toast:show', { detail: #{payload} }))")) { title } %>
15
+ <% end %>
16
+ <% persistent = { title: "Stays until dismissed", description: "duration: 0 skips the auto-dismiss timer.", variant: :default, duration: 0 }.to_json %>
17
+ <%= render(UI::Button.new(variant: :outline, size: :sm,
18
+ onclick: "window.dispatchEvent(new CustomEvent('toast:show', { detail: #{persistent} }))")) { "Persistent" } %>
19
+ </div>
20
+
21
+ <%= render(UI::Toast::Viewport.new) %>
@@ -0,0 +1,7 @@
1
+ <div class="flex flex-col gap-4 max-w-sm">
2
+ <%= render(UI::Toast.new(variant: :default, title: "Default", description: "Heads up, something happened.")) %>
3
+ <%= render(UI::Toast.new(variant: :success, title: "Success", description: "Your changes have been saved.")) %>
4
+ <%= render(UI::Toast.new(variant: :destructive, title: "Destructive", description: "There was a problem with your request.")) %>
5
+ <%= render(UI::Toast.new(variant: :warning, title: "Warning", description: "This action cannot be undone.")) %>
6
+ <%= render(UI::Toast.new(variant: :info, title: "Info", description: "Here's some helpful information.")) %>
7
+ </div>