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,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 %>
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ # UI::Toast — a single dismissible, auto-expiring message.
4
+ #
5
+ # A toast has nowhere to appear on its own: render a UI::Toast::Viewport once
6
+ # per page (e.g. in the layout), then push toasts into it either from the
7
+ # server, by appending one in a Turbo Stream response —
8
+ #
9
+ # turbo_stream.append(UI::Toast::Viewport::ID, render(UI::Toast.new(variant: :success, title: "Saved")))
10
+ #
11
+ # — or from the client, with the `toast()` helper exported by the
12
+ # toast-viewport controller.
13
+ class UI::Toast < UI::Base
14
+ DEFAULT_ICONS = {
15
+ default: "info",
16
+ success: "circle-check",
17
+ destructive: "circle-alert",
18
+ warning: "circle-alert",
19
+ info: "info"
20
+ }.freeze
21
+
22
+ # The same motion in reverse for in/out, and explicitly longer than the
23
+ # plugin's 150ms default: fine for an 8px tooltip nudge, too snappy for a
24
+ # whole card to travel, which read as an unsmoothed flicker rather than a
25
+ # dismissal. The ui--toast controller's own EXIT_DURATION constant waits
26
+ # this same 300ms before removing a dismissed toast from the DOM — keep
27
+ # them in sync if this changes.
28
+ #
29
+ # A neutral bg-popover surface, same as Dialog/Sheet/Popover/Menu — the
30
+ # other floating, elevated surfaces in the kit. Variant color is carried by
31
+ # the icon alone, nothing else on the card changes: Alert is the one place
32
+ # that tints its whole surface, because it sits inline in the page rather
33
+ # than floating above it.
34
+ # fill-mode-forwards matters here specifically: tw-animate-css's
35
+ # animate-in/animate-out default to animation-fill-mode: none, so without
36
+ # it the element snaps back to its unanimated state (fully visible, no
37
+ # transform) the instant the exit animation ends — a flicker right before
38
+ # the timeout below removes it. Dialog/Sheet never hit this because they
39
+ # never remove the element after animating; Toast is the first thing here
40
+ # that does.
41
+ variants(
42
+ base: "relative flex w-full items-start gap-3 rounded-lg border bg-popover p-4 " \
43
+ "text-popover-foreground shadow-lg pointer-events-auto duration-300 fill-mode-forwards " \
44
+ "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:slide-in-from-bottom-2 " \
45
+ "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-bottom-2 " \
46
+ "[&>i]:translate-y-0.5 [&>i]:shrink-0",
47
+ variants: {
48
+ variant: {
49
+ default: "[&>i]:text-muted-foreground",
50
+ success: "[&>i]:text-emerald-600 dark:[&>i]:text-emerald-400",
51
+ destructive: "[&>i]:text-destructive",
52
+ warning: "[&>i]:text-amber-600 dark:[&>i]:text-amber-400",
53
+ info: "[&>i]:text-sky-600 dark:[&>i]:text-sky-400"
54
+ }
55
+ },
56
+ defaults: { variant: :default }
57
+ )
58
+
59
+ renders_one :action, ->(**opts, &block) { tag.div(class: "mt-2", **opts, &block) }
60
+
61
+ # duration is seconds before the toast dismisses itself; 0 (or nil) leaves
62
+ # it on screen until the visitor dismisses it. Takes a plain number of
63
+ # seconds or an ActiveSupport::Duration (duration: 10.seconds).
64
+ def initialize(variant: :default, title: nil, description: nil, duration: 5,
65
+ dismissible: true, icon: true, class: nil, **html_attrs)
66
+ @variant = variant
67
+ @title = title
68
+ @description = description
69
+ @duration_ms = duration.to_i * 1000
70
+ @dismissible = dismissible
71
+ @icon_name = icon == true ? DEFAULT_ICONS.fetch(variant, "info") : icon
72
+ @user_class = binding.local_variable_get(:class)
73
+ @html_attrs = html_attrs
74
+ end
75
+
76
+ def classes
77
+ cn(variant_classes(variant: @variant), @user_class)
78
+ end
79
+
80
+ def title?; @title.present?; end
81
+ def description?; @description.present?; end
82
+ def icon?; @icon_name.present?; end
83
+ def dismissible?; @dismissible; end
84
+
85
+ # A destructive toast interrupts (assertive); anything else just announces
86
+ # once it's there (polite), same split Alert makes for role/aria-live.
87
+ def role; @variant == :destructive ? "alert" : "status"; end
88
+ def live; @variant == :destructive ? "assertive" : "polite"; end
89
+ end
@@ -0,0 +1,49 @@
1
+ import { Controller } from '@hotwired/stimulus';
2
+
3
+ /**
4
+ * UI::FileField controller — drag-and-drop over a native <input type="file">.
5
+ *
6
+ * The <label> wrapping the input already opens the native picker on click,
7
+ * no JS needed for that. This only adds what a label can't: highlighting the
8
+ * dropzone while dragging, assigning dropped files to the input, and
9
+ * listing the chosen file names.
10
+ */
11
+ export default class extends Controller<HTMLElement> {
12
+ static targets = ['dropzone', 'input', 'filenames'];
13
+
14
+ declare readonly dropzoneTarget: HTMLElement;
15
+ declare readonly inputTarget: HTMLInputElement;
16
+ declare readonly hasFilenamesTarget: boolean;
17
+ declare readonly filenamesTarget: HTMLElement;
18
+
19
+ connect() {
20
+ this.updateNames();
21
+ }
22
+
23
+ dragOver(event: DragEvent) {
24
+ event.preventDefault();
25
+ if (this.inputTarget.disabled) return;
26
+ this.dropzoneTarget.dataset.drag = 'true';
27
+ }
28
+
29
+ dragLeave() {
30
+ delete this.dropzoneTarget.dataset.drag;
31
+ }
32
+
33
+ drop(event: DragEvent) {
34
+ event.preventDefault();
35
+ delete this.dropzoneTarget.dataset.drag;
36
+
37
+ const files = event.dataTransfer?.files;
38
+ if (!files || !files.length || this.inputTarget.disabled) return;
39
+
40
+ this.inputTarget.files = files;
41
+ this.inputTarget.dispatchEvent(new Event('change', { bubbles: true }));
42
+ }
43
+
44
+ updateNames() {
45
+ if (!this.hasFilenamesTarget) return;
46
+ const names = Array.from(this.inputTarget.files ?? []).map((file) => file.name);
47
+ this.filenamesTarget.textContent = names.join(', ');
48
+ }
49
+ }
@@ -38,6 +38,9 @@ export default class extends Controller<HTMLElement> {
38
38
  offset: 4,
39
39
  flip: true,
40
40
  shift: true,
41
+ // Escape scrolling or clipped ancestors (a scrolling tab bar, a table
42
+ // cell, a drawer), as UI::Select does.
43
+ strategy: 'fixed',
41
44
  });
42
45
  this.floatingCleanup = floating.destroy;
43
46