velvetkit 0.1.0 → 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.
Files changed (46) 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/session_tabs/add.html.erb +1 -0
  7. data/registry/app/components/ui/session_tabs/add.rb +52 -0
  8. data/registry/app/components/ui/session_tabs/tab.html.erb +37 -0
  9. data/registry/app/components/ui/session_tabs/tab.rb +243 -0
  10. data/registry/app/components/ui/session_tabs.html.erb +15 -0
  11. data/registry/app/components/ui/session_tabs.rb +101 -0
  12. data/registry/app/components/ui/skeleton.html.erb +1 -0
  13. data/registry/app/components/ui/skeleton.rb +30 -0
  14. data/registry/app/components/ui/toast/viewport.html.erb +4 -0
  15. data/registry/app/components/ui/toast/viewport.rb +41 -0
  16. data/registry/app/components/ui/toast.html.erb +11 -0
  17. data/registry/app/components/ui/toast.rb +89 -0
  18. data/registry/app/javascript/controllers/ui/file_field_controller.ts +49 -0
  19. data/registry/app/javascript/controllers/ui/menu_controller.ts +3 -0
  20. data/registry/app/javascript/controllers/ui/session_tabs_controller.ts +732 -0
  21. data/registry/app/javascript/controllers/ui/toast_controller.ts +62 -0
  22. data/registry/app/javascript/controllers/ui/toast_viewport_controller.ts +82 -0
  23. data/registry/app/javascript/velvetkit_icons.ts +21 -9
  24. data/registry/components.yml +8 -0
  25. data/registry/config/locales/velvetkit.en.yml +19 -0
  26. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +3 -0
  27. data/registry/test/components/previews/ui/file_field_preview/with_error.html.erb +3 -0
  28. data/registry/test/components/previews/ui/file_field_preview.rb +18 -0
  29. data/registry/test/components/previews/ui/session_tabs_preview/handling_events.html.erb +46 -0
  30. data/registry/test/components/previews/ui/session_tabs_preview/saved_views.html.erb +16 -0
  31. data/registry/test/components/previews/ui/session_tabs_preview/touch_with_confirmation.html.erb +20 -0
  32. data/registry/test/components/previews/ui/session_tabs_preview.rb +35 -0
  33. data/registry/test/components/previews/ui/skeleton_preview/card_placeholder.html.erb +10 -0
  34. data/registry/test/components/previews/ui/skeleton_preview/shapes.html.erb +9 -0
  35. data/registry/test/components/previews/ui/skeleton_preview.rb +15 -0
  36. data/registry/test/components/previews/ui/toast_preview/live_viewport.html.erb +21 -0
  37. data/registry/test/components/previews/ui/toast_preview/variants.html.erb +7 -0
  38. data/registry/test/components/previews/ui/toast_preview/with_action.html.erb +7 -0
  39. data/registry/test/components/previews/ui/toast_preview.rb +29 -0
  40. data/registry/test/components/ui/file_field_test.rb +101 -0
  41. data/registry/test/components/ui/session_tabs/add_test.rb +52 -0
  42. data/registry/test/components/ui/session_tabs_test.rb +281 -0
  43. data/registry/test/components/ui/skeleton_test.rb +65 -0
  44. data/registry/test/components/ui/toast/viewport_test.rb +72 -0
  45. data/registry/test/components/ui/toast_test.rb +183 -0
  46. metadata +39 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 783fb129530172b7add0378661a6542b52014dfb663b558b625aa5fef0489bdd
4
- data.tar.gz: 9cb5e73f8e4ae8a94b3be1cb7ddbed5e8dbfd9ff36d9fbdddbfed317ba576769
3
+ metadata.gz: 3f1a9f75abb6d157cc285024ed4c2e3e711a7a9d1dc44447506c0747427ccc03
4
+ data.tar.gz: 2e07ef2baa716064b2e629e08acacd331576449434ebadf18288eb136870b044
5
5
  SHA512:
6
- metadata.gz: 3b399bf5d193fa63a3b92f7e686622761c56b453e14137bb6b8783ee95d703103ab6ca455dcade1f5695e868c44a8f6faa54eb652e0c4a141d0a6b4895553ed8
7
- data.tar.gz: 4a7b4fb856f8645f7ed4ece0f8eeb5de9228d71ced91ae9ecd908814b51a59c8f1b9744bc40f99ca3af2443fb894014bdc548e7cc23074ec59b3ecb652fee1b5
6
+ metadata.gz: 6eafdabafa53ea356315c8ebe68f36d816cc3937844c013970b0a27360a92b0b623daa27bd4501f8bcb83b65b2a1eaabf8a7421b06a8096b3df4b3f209a87629
7
+ data.tar.gz: e675121815d2bf88bab7b9689ef1bcaa916ed8eae2865e04400cc060e5a6d9425a2bc93b3f34dd306c9f5fe993e988af7e85755206e7a452c4f4b8befb572abd
data/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  copied into your app one at a time, under your namespace.</p>
8
8
 
9
9
  <p>
10
- <a href="https://rubygems.org/gems/velvetkit"><img alt="Gem version" src="https://img.shields.io/gem/v/velvetkit?include_prereleases&color=4D1A7F"></a>
10
+ <a href="https://rubygems.org/gems/velvetkit"><img alt="Gem version" src="https://img.shields.io/gem/v/velvetkit?color=4D1A7F"></a>
11
11
  <a href="https://github.com/kitforge-tech/velvetkit/actions/workflows/ci.yml"><img alt="CI" src="https://github.com/kitforge-tech/velvetkit/actions/workflows/ci.yml/badge.svg"></a>
12
12
  <img alt="Rails 7.1+" src="https://img.shields.io/badge/Rails-7.1%2B-CC0000?logo=rubyonrails&logoColor=white">
13
13
  <img alt="Tailwind CSS 4" src="https://img.shields.io/badge/Tailwind_CSS-4-38BDF8?logo=tailwindcss&logoColor=white">
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Velvetkit
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
@@ -0,0 +1,4 @@
1
+ <%= render(wrapper) do %><%= tag.label for: @input_id, class: dropzone_classes,
2
+ data: { controller: "ui--file-field",
3
+ "ui--file-field-target": "dropzone",
4
+ action: "dragover->ui--file-field#dragOver dragleave->ui--file-field#dragLeave drop->ui--file-field#drop" } do %><%= render(UI::Icon.new(name: "upload", size: :lg, class: "text-muted-foreground", aria: { hidden: true })) %><p class="text-sm text-muted-foreground"><span class="font-medium text-primary"><%= t("ui.file_field.browse", default: "Click to upload") %></span> <%= t("ui.file_field.drop", default: "or drag and drop") %></p><p class="text-xs text-muted-foreground empty:hidden" data-ui--file-field-target="filenames"></p><% if form_field? %><%= @form.file_field(@field, **form_field_options) %><% else %><%= tag.input(**html_options) %><% end %><% end %><% end %>
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # UI::FileField — a drag-and-drop dropzone over a native file input.
6
+ #
7
+ # The input itself is sr-only, not hidden: it stays in the accessibility
8
+ # tree and keyboard/native-picker behavior (Tab, Enter/Space, the OS file
9
+ # dialog) comes from the real <input type="file">, same as every other
10
+ # styled-native-control component here. The dropzone is a <label> around it,
11
+ # so clicking anywhere in it opens the picker without any JS, and the
12
+ # ui--file-field controller only adds what a label can't: drag-over
13
+ # highlighting and listing the chosen file names.
14
+ class UI::FileField < UI::Base
15
+ def initialize(multiple: false, accept: nil,
16
+ label: nil, label_size: :default, label_weight: :medium,
17
+ hint: nil, required: false, error: nil, disabled: false,
18
+ form: nil, field: nil,
19
+ class: nil, **html_attrs)
20
+ @form = form
21
+ @field = field
22
+ @multiple = multiple
23
+ @accept = accept
24
+ @required = required
25
+ @disabled = disabled
26
+ @user_class = binding.local_variable_get(:class)
27
+ @html_attrs = html_attrs
28
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
29
+ required: required, disabled: disabled,
30
+ label_size: label_size, label_weight: label_weight,
31
+ form: form, field: field)
32
+ @input_id = @html_attrs[:id] || "ui-file-field-#{SecureRandom.hex(4)}"
33
+ @html_attrs[:id] = @input_id
34
+ @wrapper.for = @input_id
35
+ end
36
+
37
+ attr_reader :wrapper
38
+
39
+ def form_field?
40
+ @form.present? && @field.present?
41
+ end
42
+
43
+ def dropzone_classes
44
+ cn(
45
+ "relative flex cursor-pointer flex-col items-center gap-2 rounded-lg border-2 border-dashed p-6 text-center transition-colors",
46
+ "border-input hover:bg-accent/50",
47
+ "data-[drag=true]:border-primary data-[drag=true]:bg-primary/5",
48
+ { "border-destructive" => wrapper.error?,
49
+ "pointer-events-none opacity-50" => @disabled },
50
+ @user_class
51
+ )
52
+ end
53
+
54
+ def html_options
55
+ opts = @html_attrs.dup
56
+ opts[:type] = "file"
57
+ opts[:class] = "sr-only"
58
+ opts[:multiple] = true if @multiple
59
+ opts[:accept] = @accept if @accept
60
+ opts[:disabled] = true if @disabled
61
+ opts[:required] = true if @required
62
+ opts[:"data-ui--file-field-target"] = "input"
63
+ opts[:"data-action"] = "change->ui--file-field#updateNames"
64
+ opts[:"aria-invalid"] = "true" if wrapper.error?
65
+ opts[:"aria-describedby"] = wrapper.describedby if wrapper.describedby
66
+ opts
67
+ end
68
+
69
+ # The builder derives the name itself, so drop any explicit one.
70
+ def form_field_options
71
+ html_options.except(:name, :type)
72
+ end
73
+ end
@@ -0,0 +1 @@
1
+ <% if url? %><%= render(UI::ButtonTo.new(url, method: :post, form_class: "contents", **button_options)) do |b| %><% b.with_leading_icon(name: "plus", size: icon_size) %><% end %><% else %><%= render(UI::Button.new(**button_options)) do |b| %><% b.with_leading_icon(name: "plus", size: icon_size) %><% end %><% end %>
@@ -0,0 +1,52 @@
1
+ # frozen_string_literal: true
2
+
3
+ # UI::SessionTabs::Add — the "+" button after the tabs.
4
+ #
5
+ # url: POSTs a real form there (works without JS; Turbo disables the button
6
+ # while the request is in flight, so a double click opens one tab).
7
+ # Omit it to only dispatch `ui--session-tabs:add`.
8
+ # label: accessible name; defaults to the "New tab" translation.
9
+ class UI::SessionTabs::Add < UI::Base
10
+ def initialize(url: nil, label: nil, class: nil, **html_attrs)
11
+ @url = url
12
+ @label = label
13
+ @user_class = binding.local_variable_get(:class)
14
+ @html_attrs = html_attrs
15
+ end
16
+
17
+ def set_session_tabs_context(tabs)
18
+ @tabs = tabs
19
+ end
20
+
21
+ def url?
22
+ @url.present?
23
+ end
24
+
25
+ attr_reader :url
26
+
27
+ def label
28
+ @label || t("ui.session_tabs.add", default: "New tab")
29
+ end
30
+
31
+ def touch?
32
+ @tabs.size == :touch
33
+ end
34
+
35
+ def icon_size
36
+ touch? ? :sm : :xs
37
+ end
38
+
39
+ def button_options
40
+ user_data = @html_attrs[:data] || {}
41
+ @html_attrs.except(:data).merge(
42
+ variant: :ghost,
43
+ size: :icon_sm,
44
+ class: cn("mb-1 shrink-0", touch? ? "size-8" : "size-7", @user_class),
45
+ "aria-label": label,
46
+ title: label,
47
+ data: user_data.merge(
48
+ action: [ user_data[:action], "click->ui--session-tabs#add" ].compact.join(" ")
49
+ )
50
+ )
51
+ end
52
+ end
@@ -0,0 +1,37 @@
1
+ <% label_html = content %>
2
+ <%= tag.div(**wrapper_options) do %>
3
+ <% if movable? %><%= tag.span(**handle_options) do %><%= render(UI::Icon.new(name: "grip-vertical", size: menu_icon_size, class: handle_icon_classes)) %><% end %><% end %>
4
+ <%= tag.public_send(link? ? :a : :button, **trigger_options) do %>
5
+ <%= tag.span(label_html, id: label_id, class: "truncate max-w-[20ch]", data: { "ui--session-tabs-target": "label" }) %>
6
+ <% if meta? %><span class="shrink-0 text-xs font-normal tabular-nums text-muted-foreground"><%= meta %></span><% end %>
7
+ <% end %>
8
+ <% if any_menu? %>
9
+ <%= render(UI::Menu.new(class: "flex shrink-0")) do |menu| %>
10
+ <% menu.with_trigger(**menu_trigger_options) do %><%= render(UI::Icon.new(name: "ellipsis-vertical", size: menu_icon_size, class: menu_icon_classes)) %><% end %>
11
+ <% menu.with_content(align: :end) do |c| %>
12
+ <% if enabled?(:rename) %>
13
+ <% c.with_item(t("ui.session_tabs.rename", default: "Rename"), **action_item_options(:rename)) %>
14
+ <% end %>
15
+ <% if enabled?(:duplicate) %>
16
+ <% c.with_item(t("ui.session_tabs.duplicate", default: "Duplicate"), **action_item_options(:duplicate)) %>
17
+ <% end %>
18
+ <% if enabled?(:move) %>
19
+ <% c.with_item(t("ui.session_tabs.move_left", default: "Move left"),
20
+ **action_item_options(:moveLeft, move: "left"), **hidden_item_options(!can_move_left?)) %>
21
+ <% c.with_item(t("ui.session_tabs.move_right", default: "Move right"),
22
+ **action_item_options(:moveRight, move: "right"), **hidden_item_options(!can_move_right?)) %>
23
+ <% end %>
24
+ <% menu_items.each do |label, opts, block| %>
25
+ <% c.with_item(label, **opts, &block) %>
26
+ <% end %>
27
+ <% if enabled?(:close) %>
28
+ <% if enabled?(:rename) || enabled?(:duplicate) || enabled?(:move) || menu_items.any? %>
29
+ <% c.with_divider(hidden: !close_available?, data: { "close-part": true }) %>
30
+ <% end %>
31
+ <% c.with_item(t("ui.session_tabs.close", default: "Close"), variant: :destructive,
32
+ **action_item_options(:close, "close-part": true), **hidden_item_options(!close_available?)) %>
33
+ <% end %>
34
+ <% end %>
35
+ <% end %>
36
+ <% end %>
37
+ <% end %>
@@ -0,0 +1,243 @@
1
+ # frozen_string_literal: true
2
+
3
+ # UI::SessionTabs::Tab — one tab plus its options menu.
4
+ #
5
+ # value: stable identifier, sent with every event
6
+ # href: makes the tab a link; omit it for an event-only tab
7
+ # active: whether this is the current tab
8
+ # rename: true (event only) or a URL (PATCH, label as `rename_param`)
9
+ # duplicate: true or a URL (POST)
10
+ # move: true or a URL (PATCH, with the new 1-based `position`);
11
+ # makes the tab draggable (a grip handle shows on hover,
12
+ # and is what touch drags by) and adds Move left/right
13
+ # close: true or a URL (DELETE)
14
+ # close_confirm: text asking the user to confirm before closing; nil closes
15
+ # straight away
16
+ #
17
+ # Extra html attributes (e.g. `data: { turbo_frame: "pane" }`) go on the tab
18
+ # element itself. `with_meta` adds small secondary text after the label;
19
+ # `with_menu_item` adds app-specific items above Close.
20
+ class UI::SessionTabs::Tab < UI::Base
21
+ ACTIONS = %i[rename duplicate move close].freeze
22
+
23
+ # The grip and the options button stay hidden until the tab is hovered,
24
+ # focused (keyboard), has its menu open, or is being dragged — and are
25
+ # always shown where there is no hover (touch screens). They slide in from
26
+ # their own edge. Written out in full so Tailwind's scanner sees each class.
27
+ REVEAL_OPACITY =
28
+ "opacity-0 group-hover/tab:opacity-100 group-focus-within/tab:opacity-100 " \
29
+ "group-data-[dragging]/tab:opacity-100 group-has-[[aria-expanded=true]]/tab:opacity-100 " \
30
+ "[@media(hover:none)]:opacity-100"
31
+ REVEAL_SLIDE =
32
+ "group-hover/tab:translate-x-0 group-focus-within/tab:translate-x-0 " \
33
+ "group-data-[dragging]/tab:translate-x-0 group-has-[[aria-expanded=true]]/tab:translate-x-0 " \
34
+ "[@media(hover:none)]:translate-x-0"
35
+ REVEAL_MOTION = "transition-[opacity,translate] duration-200 ease-out motion-reduce:transition-none"
36
+
37
+ variants(
38
+ base: "group/tab relative flex shrink-0 items-center rounded-t-md border border-b-0 " \
39
+ "border-transparent bg-muted/60 text-muted-foreground transition-colors " \
40
+ "hover:border-primary/20 hover:bg-primary/5 hover:text-foreground " \
41
+ "data-[state=active]:border-border data-[state=active]:bg-background " \
42
+ "data-[state=active]:text-foreground " \
43
+ "data-[dragging]:z-10 data-[dragging]:border-border data-[dragging]:bg-background " \
44
+ "data-[dragging]:shadow-md data-[dragging]:transition-none",
45
+ variants: {
46
+ size: {
47
+ default: "h-9",
48
+ touch: "h-11"
49
+ }
50
+ },
51
+ defaults: { size: :default }
52
+ )
53
+
54
+ renders_one :meta
55
+
56
+ def initialize(value:, href: nil, active: false, rename: false, duplicate: false,
57
+ move: false, close: false, close_confirm: nil, class: nil, **html_attrs)
58
+ @value = value.to_s
59
+ @href = href
60
+ @active = active
61
+ @actions = { rename: rename, duplicate: duplicate, move: move, close: close }
62
+ @close_confirm = close_confirm
63
+ @user_class = binding.local_variable_get(:class)
64
+ @html_attrs = html_attrs
65
+ @menu_items = []
66
+ end
67
+
68
+ attr_reader :value, :menu_items
69
+
70
+ def set_session_tabs_context(tabs)
71
+ @tabs = tabs
72
+ end
73
+
74
+ # App-specific menu items, rendered above Close. Same arguments as
75
+ # UI::Menu::Content#with_item.
76
+ def with_menu_item(label = nil, **opts, &block)
77
+ @menu_items << [ label, opts, block ]
78
+ self
79
+ end
80
+
81
+ def active?
82
+ @active
83
+ end
84
+
85
+ def link?
86
+ @href.present?
87
+ end
88
+
89
+ def enabled?(action)
90
+ @actions[action].present?
91
+ end
92
+
93
+ def any_menu?
94
+ ACTIONS.any? { |a| enabled?(a) } || @menu_items.any?
95
+ end
96
+
97
+ # Close stays rendered (so the controller can bring it back) but is hidden
98
+ # while the bar is down to its minimum number of tabs.
99
+ def close_available?
100
+ @tabs.closable_count?
101
+ end
102
+
103
+ def movable?
104
+ enabled?(:move)
105
+ end
106
+
107
+ def can_move_left?; @tabs.movable_neighbour?(self, -1); end
108
+ def can_move_right?; @tabs.movable_neighbour?(self, 1); end
109
+
110
+ def size; @tabs.size; end
111
+
112
+ def dom_key
113
+ @value.parameterize.presence || Digest::SHA1.hexdigest(@value)[0, 8]
114
+ end
115
+
116
+ def trigger_id; "#{@tabs.id}-tab-#{dom_key}"; end
117
+ def label_id; "#{@tabs.id}-tab-#{dom_key}-label"; end
118
+
119
+ def wrapper_classes
120
+ cn(variant_classes(size: size), @user_class)
121
+ end
122
+
123
+ def wrapper_options
124
+ data = {
125
+ "ui--session-tabs-target": "tab",
126
+ value: @value,
127
+ state: active? ? "active" : "inactive"
128
+ }
129
+ ACTIONS.each do |action|
130
+ url = @actions[action]
131
+ data[:"#{action}_url"] = url if url.is_a?(String)
132
+ data[:"#{action}_enabled"] = true if enabled?(action)
133
+ end
134
+ data[:close_confirm] = @close_confirm if @close_confirm.present?
135
+ { role: "presentation", class: wrapper_classes, data: data }
136
+ end
137
+
138
+ def trigger_classes
139
+ cn(
140
+ "flex h-full min-w-0 items-center gap-2 rounded-t-md pl-3 text-sm font-medium outline-none",
141
+ # Long-press opens our menu, not the browser's link callout.
142
+ "select-none [-webkit-touch-callout:none]",
143
+ "focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-inset",
144
+ any_menu? ? "pr-1" : "pr-3",
145
+ { "pl-4 text-base" => size == :touch },
146
+ { "pl-0.5" => movable? }
147
+ )
148
+ end
149
+
150
+ def trigger_options
151
+ user_data = @html_attrs[:data] || {}
152
+ opts = @html_attrs.except(:data)
153
+ opts[:id] = trigger_id
154
+ opts[:role] = "tab"
155
+ opts[:class] = trigger_classes
156
+ opts[:"aria-selected"] = active? ? "true" : "false"
157
+ opts[:"aria-current"] = "page" if link? && active?
158
+ opts[:tabindex] = active? ? "0" : "-1"
159
+ # A native link drag would cancel our pointer events mid-reorder.
160
+ opts[:draggable] = "false"
161
+ opts[:data] = user_data.merge(
162
+ "ui--session-tabs-target": "trigger",
163
+ action: [
164
+ user_data[:action],
165
+ "click->ui--session-tabs#select",
166
+ "keydown->ui--session-tabs#keydown",
167
+ "contextmenu->ui--session-tabs#contextMenu",
168
+ "pointerdown->ui--session-tabs#pressStart"
169
+ ].compact.join(" ")
170
+ )
171
+ if link?
172
+ opts[:href] = @href
173
+ else
174
+ opts[:type] = "button"
175
+ end
176
+ opts
177
+ end
178
+
179
+ # The grip is the only place a drag starts. `touch-none` lets a finger drag
180
+ # it straight away instead of scrolling the tab list; the rest of the tab
181
+ # still scrolls, selects, and long-presses for the menu. Not focusable:
182
+ # Move left/right in the menu is the keyboard path.
183
+ def handle_options
184
+ {
185
+ class: cn(
186
+ "flex h-full shrink-0 cursor-grab touch-none select-none items-center justify-center overflow-hidden " \
187
+ "rounded-tl-md text-muted-foreground hover:text-primary active:cursor-grabbing",
188
+ size == :touch ? "w-7 pl-1" : "w-5 pl-1"
189
+ ),
190
+ "aria-hidden": "true",
191
+ data: {
192
+ "ui--session-tabs-target": "handle",
193
+ action: "pointerdown->ui--session-tabs#dragStart"
194
+ }
195
+ }
196
+ end
197
+
198
+ # Slides in from the left.
199
+ def handle_icon_classes
200
+ cn(REVEAL_MOTION, REVEAL_OPACITY, "-translate-x-2", REVEAL_SLIDE)
201
+ end
202
+
203
+ # The button fades (so its hover background never shows half-way); the
204
+ # icon inside slides in from the right.
205
+ def menu_trigger_classes
206
+ cn(
207
+ "mr-1 overflow-hidden text-muted-foreground hover:bg-primary/10 hover:text-primary " \
208
+ "aria-expanded:bg-primary/10 aria-expanded:text-primary",
209
+ size == :touch ? "size-7" : "size-6",
210
+ REVEAL_MOTION, REVEAL_OPACITY
211
+ )
212
+ end
213
+
214
+ def menu_icon_classes
215
+ cn(REVEAL_MOTION, "translate-x-2", REVEAL_SLIDE)
216
+ end
217
+
218
+ def menu_trigger_options
219
+ {
220
+ variant: :ghost,
221
+ size: :icon_sm,
222
+ class: menu_trigger_classes,
223
+ tabindex: active? ? "0" : "-1",
224
+ "aria-label": t("ui.session_tabs.options", default: "Tab options"),
225
+ "aria-describedby": label_id,
226
+ data: { "ui--session-tabs-target": "menuTrigger" }
227
+ }
228
+ end
229
+
230
+ def menu_icon_size
231
+ size == :touch ? :sm : :xs
232
+ end
233
+
234
+ def action_item_options(action, extra = {})
235
+ { data: { action: "click->ui--session-tabs##{action}" }.merge(extra) }
236
+ end
237
+
238
+ # Hidden menu items must also be disabled, or UI::Menu's arrow-key
239
+ # navigation would still try to focus them.
240
+ def hidden_item_options(hidden)
241
+ hidden ? { hidden: true, disabled: true } : {}
242
+ end
243
+ end
@@ -0,0 +1,15 @@
1
+ <%= tag.div(id: id, class: root_classes, data: root_data, **@html_attrs.except(:data)) do %>
2
+ <%= tag.div(role: "tablist", "aria-label": tablist_label, "aria-orientation": "horizontal",
3
+ class: tablist_classes, data: { "ui--session-tabs-target": "list" }) do %><% tabs.each do |tab| %><%= tab %><% end %><% end %>
4
+ <% if add? %><%= add %><% end %>
5
+ <%= render(UI::Dialog.new(id: dialog_id, data: { "ui--session-tabs-target": "dialog" })) do |dialog| %>
6
+ <% dialog.with_content(size: :sm, show_close: false) do |c| %>
7
+ <% c.with_title(t("ui.session_tabs.confirm_close.title", default: "Close this tab?")) %>
8
+ <% c.with_description do %><span data-ui--session-tabs-target="confirmMessage"></span><% end %>
9
+ <% c.with_footer do %>
10
+ <%= render(UI::Button.new(variant: :outline, data: { action: "click->ui--dialog#close" })) do %><%= t("ui.session_tabs.confirm_close.cancel", default: "Keep it open") %><% end %>
11
+ <%= render(UI::Button.new(variant: :destructive, data: { action: "click->ui--session-tabs#confirmClose" })) do %><%= t("ui.session_tabs.confirm_close.confirm", default: "Close tab") %><% end %>
12
+ <% end %>
13
+ <% end %>
14
+ <% end %>
15
+ <% end %>
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ # UI::SessionTabs — tabs the *user* manages: open, rename, duplicate, reorder,
4
+ # close — a browser-style tab bar over records the app keeps. (For a fixed set
5
+ # of panels the developer defines, use UI::Tabs.)
6
+ #
7
+ # The component never stores tabs. Each tab is either:
8
+ # - a link (`href:`) — the server decides which tab is active, or
9
+ # - a button (`value:` only) — the page reacts to `ui--session-tabs:select`.
10
+ #
11
+ # Each action (rename, duplicate, move, close) is opt-in per tab and takes
12
+ # either a URL — submitted as a Turbo form (PATCH/POST/PATCH/DELETE) — or
13
+ # `true`, in which case only the event fires and the app does the work.
14
+ # Every action dispatches a cancelable `ui--session-tabs:<action>` event
15
+ # first; `preventDefault()` stops it.
16
+ #
17
+ # Compound:
18
+ # UI::SessionTabs — the bar, the confirm-close dialog
19
+ # UI::SessionTabs::Tab — one tab + its options menu
20
+ # UI::SessionTabs::Add — the "+" button
21
+ class UI::SessionTabs < UI::Base
22
+ renders_many :tabs, ->(value:, **opts) {
23
+ UI::SessionTabs::Tab.new(value: value, **opts).tap do |t|
24
+ t.set_session_tabs_context(self)
25
+ (@tab_components ||= []) << t
26
+ end
27
+ }
28
+ renders_one :add, ->(**opts) {
29
+ UI::SessionTabs::Add.new(**opts).tap { |a| a.set_session_tabs_context(self) }
30
+ }
31
+
32
+ SIZES = %i[default touch].freeze
33
+
34
+ # label: accessible name of the tablist
35
+ # size: :default, or :touch for 44px-tall touch targets
36
+ # min_tabs: Close is hidden while this many tabs (or fewer) remain
37
+ # return_focus: CSS selector focused after an action, so typing goes back
38
+ # to the page's main input instead of staying on the tab bar
39
+ # rename_param: form field name the new label is submitted under
40
+ def initialize(label: nil, size: :default, min_tabs: 1, return_focus: nil,
41
+ rename_param: "name", class: nil, **html_attrs)
42
+ @label = label
43
+ @size = SIZES.include?(size) ? size : :default
44
+ @min_tabs = min_tabs
45
+ @return_focus = return_focus
46
+ @rename_param = rename_param
47
+ @user_class = binding.local_variable_get(:class)
48
+ @html_attrs = html_attrs
49
+ @id = @html_attrs.delete(:id) || "ui-session-tabs-#{SecureRandom.hex(4)}"
50
+ end
51
+
52
+ attr_reader :id, :size, :min_tabs
53
+
54
+ def touch?
55
+ @size == :touch
56
+ end
57
+
58
+ # Read by each tab to decide whether Close is offered at render time; the
59
+ # controller keeps it in sync as tabs come and go client-side.
60
+ def closable_count?
61
+ tabs.size > @min_tabs
62
+ end
63
+
64
+ # Tabs without `move:` stay put: Move left/right (and dragging) only swap
65
+ # with movable tabs, so a fixed first tab stays first.
66
+ def movable_neighbour?(tab, step)
67
+ list = @tab_components || []
68
+ i = list.index(tab) || 0
69
+ others = step.negative? ? list[0...i] : list[(i + 1)..]
70
+ others.any?(&:movable?)
71
+ end
72
+
73
+ def tablist_label
74
+ @label || t("ui.session_tabs.label", default: "Open tabs")
75
+ end
76
+
77
+ def dialog_id
78
+ "#{@id}-confirm"
79
+ end
80
+
81
+ def root_classes
82
+ # The bottom rule is an inset shadow, painted beneath the tabs, so the
83
+ # active tab's background covers it without overflow tricks.
84
+ cn("flex w-full min-w-0 items-end gap-1 shadow-[inset_0_-1px_0_var(--color-border)]", @user_class)
85
+ end
86
+
87
+ def tablist_classes
88
+ "flex min-w-0 items-end gap-1 overflow-x-auto overflow-y-hidden " \
89
+ "[scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
90
+ end
91
+
92
+ def root_data
93
+ {
94
+ controller: "ui--session-tabs",
95
+ "ui--session-tabs-min-tabs-value": @min_tabs,
96
+ "ui--session-tabs-return-focus-value": @return_focus.to_s,
97
+ "ui--session-tabs-rename-param-value": @rename_param,
98
+ "ui--session-tabs-rename-label-value": t("ui.session_tabs.rename_label", default: "Tab name")
99
+ }.merge(@html_attrs[:data] || {})
100
+ end
101
+ end
@@ -0,0 +1 @@
1
+ <%= tag.div class: classes, aria: { hidden: true }.merge(@html_attrs[:aria] || {}), **@html_attrs.except(:aria) %>
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ # UI::Skeleton — a pulsing placeholder shown in a piece of content's shape
4
+ # while it loads. Purely visual: aria-hidden, since there's nothing here for
5
+ # a screen reader to announce. Pair it with a surrounding region that does
6
+ # the announcing (role="status", like UI::Spinner already sets up) if the
7
+ # loading state itself needs to be heard.
8
+ class UI::Skeleton < UI::Base
9
+ variants(
10
+ base: "animate-pulse rounded-md bg-muted",
11
+ variants: {
12
+ shape: {
13
+ text: "h-4 w-full rounded-sm",
14
+ circle: "size-10 rounded-full",
15
+ rectangle: "h-24 w-full"
16
+ }
17
+ },
18
+ defaults: { shape: :text }
19
+ )
20
+
21
+ def initialize(shape: :text, class: nil, **html_attrs)
22
+ @shape = shape
23
+ @user_class = binding.local_variable_get(:class)
24
+ @html_attrs = html_attrs
25
+ end
26
+
27
+ def classes
28
+ cn(variant_classes(shape: @shape), @user_class)
29
+ end
30
+ end
@@ -0,0 +1,4 @@
1
+ <%= tag.div(id: @id,
2
+ class: classes,
3
+ data: { controller: "ui--toast-viewport" }.merge(@html_attrs[:data] || {}),
4
+ **@html_attrs.except(:data)) do %><% UI::Toast::Viewport::VARIANTS.each do |variant| %><template data-ui--toast-viewport-target="template" data-variant="<%= variant %>"><%= render(UI::Toast.new(variant: variant, title: "Title", description: "Description")) %></template><% end %><% end %>
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # UI::Toast::Viewport — stacks UI::Toast instances in a corner of the screen.
4
+ #
5
+ # Render exactly one, anywhere in the layout (it's fixed-positioned, so
6
+ # placement in the DOM doesn't matter). It also carries one hidden template
7
+ # per variant, which the toast-viewport controller clones to build a toast
8
+ # triggered from client-side JS, so a toast looks the same whether it arrived
9
+ # via ERB, a Turbo Stream, or `toast()`.
10
+ class UI::Toast::Viewport < UI::Base
11
+ ID = "ui-toast-viewport"
12
+ VARIANTS = %i[default success destructive warning info].freeze
13
+
14
+ variants(
15
+ base: "fixed z-50 flex flex-col w-full max-w-sm gap-2 p-4 pointer-events-none",
16
+ variants: {
17
+ position: {
18
+ bottom_right: "bottom-0 right-0 items-end",
19
+ bottom_left: "bottom-0 left-0 items-start",
20
+ bottom_center: "bottom-0 left-1/2 -translate-x-1/2 items-center",
21
+ top_right: "top-0 right-0 items-end flex-col-reverse",
22
+ top_left: "top-0 left-0 items-start flex-col-reverse",
23
+ top_center: "top-0 left-1/2 -translate-x-1/2 items-center flex-col-reverse"
24
+ }
25
+ },
26
+ defaults: { position: :bottom_right }
27
+ )
28
+
29
+ def initialize(position: :bottom_right, id: ID, class: nil, **html_attrs)
30
+ @position = position
31
+ @id = id
32
+ @user_class = binding.local_variable_get(:class)
33
+ @html_attrs = html_attrs
34
+ end
35
+
36
+ attr_reader :id
37
+
38
+ def classes
39
+ cn(variant_classes(position: @position), @user_class)
40
+ end
41
+ end
@@ -0,0 +1,11 @@
1
+ <%= tag.div(class: classes,
2
+ role: role,
3
+ aria: { live: live }.merge(@html_attrs[:aria] || {}),
4
+ data: {
5
+ controller: "ui--toast",
6
+ "ui--toast-duration-value": @duration_ms,
7
+ action: "mouseenter->ui--toast#pause mouseleave->ui--toast#resume",
8
+ state: "open",
9
+ slot: "toast"
10
+ }.merge(@html_attrs[:data] || {}),
11
+ **@html_attrs.except(:data, :aria)) do %><% if icon? %><%= render(UI::Icon.new(name: @icon_name, size: :sm, aria: { hidden: true })) %><% end %><div class="flex-1 min-w-0 space-y-1"><% if title? %><p class="text-sm font-semibold leading-none" data-ui--toast-target="title"><%= @title %></p><% end %><% if description? %><p class="text-sm opacity-90" data-ui--toast-target="description"><%= @description %></p><% end %><% if action? %><%= action %><% end %></div><% if dismissible? %><%= render(UI::Button.new(variant: :ghost, size: :icon_sm, class: "shrink-0 -m-1 text-current/70 hover:text-current hover:bg-current/10", aria: { label: t("ui.toast.close_label", default: "Dismiss") }, data: { action: "click->ui--toast#dismiss" })) do |btn| %><% btn.with_leading_icon(name: "x", size: :sm) %><% end %><% end %><% end %>