velvetkit 0.1.0.beta → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +143 -75
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/checkbox.html.erb +2 -0
  5. data/registry/app/components/ui/checkbox.rb +11 -5
  6. data/registry/app/components/ui/collapsible.html.erb +7 -0
  7. data/registry/app/components/ui/collapsible.rb +67 -0
  8. data/registry/app/components/ui/date_picker.html.erb +4 -1
  9. data/registry/app/components/ui/date_picker.rb +10 -2
  10. data/registry/app/components/ui/file_field.html.erb +4 -0
  11. data/registry/app/components/ui/file_field.rb +73 -0
  12. data/registry/app/components/ui/form_field/messages.html.erb +6 -0
  13. data/registry/app/components/ui/form_field/messages.rb +11 -0
  14. data/registry/app/components/ui/form_field.html.erb +9 -0
  15. data/registry/app/components/ui/form_field.rb +105 -0
  16. data/registry/app/components/ui/input.html.erb +1 -8
  17. data/registry/app/components/ui/input.rb +12 -36
  18. data/registry/app/components/ui/input_group/addon.rb +4 -1
  19. data/registry/app/components/ui/input_group/field.rb +4 -1
  20. data/registry/app/components/ui/input_group.rb +12 -10
  21. data/registry/app/components/ui/native_select.html.erb +2 -9
  22. data/registry/app/components/ui/native_select.rb +17 -31
  23. data/registry/app/components/ui/password_input.html.erb +1 -8
  24. data/registry/app/components/ui/password_input.rb +9 -27
  25. data/registry/app/components/ui/select.html.erb +4 -0
  26. data/registry/app/components/ui/select.rb +12 -29
  27. data/registry/app/components/ui/session_tabs/add.html.erb +1 -0
  28. data/registry/app/components/ui/session_tabs/add.rb +52 -0
  29. data/registry/app/components/ui/session_tabs/tab.html.erb +37 -0
  30. data/registry/app/components/ui/session_tabs/tab.rb +243 -0
  31. data/registry/app/components/ui/session_tabs.html.erb +15 -0
  32. data/registry/app/components/ui/session_tabs.rb +101 -0
  33. data/registry/app/components/ui/skeleton.html.erb +1 -0
  34. data/registry/app/components/ui/skeleton.rb +30 -0
  35. data/registry/app/components/ui/table_of_contents/item.html.erb +8 -0
  36. data/registry/app/components/ui/table_of_contents/item.rb +41 -0
  37. data/registry/app/components/ui/table_of_contents.html.erb +13 -0
  38. data/registry/app/components/ui/table_of_contents.rb +58 -0
  39. data/registry/app/components/ui/textarea.html.erb +2 -0
  40. data/registry/app/components/ui/textarea.rb +101 -0
  41. data/registry/app/components/ui/toast/viewport.html.erb +4 -0
  42. data/registry/app/components/ui/toast/viewport.rb +41 -0
  43. data/registry/app/components/ui/toast.html.erb +11 -0
  44. data/registry/app/components/ui/toast.rb +89 -0
  45. data/registry/app/javascript/controllers/ui/file_field_controller.ts +49 -0
  46. data/registry/app/javascript/controllers/ui/menu_controller.ts +3 -0
  47. data/registry/app/javascript/controllers/ui/session_tabs_controller.ts +732 -0
  48. data/registry/app/javascript/controllers/ui/table_of_contents_controller.ts +179 -0
  49. data/registry/app/javascript/controllers/ui/toast_controller.ts +62 -0
  50. data/registry/app/javascript/controllers/ui/toast_viewport_controller.ts +82 -0
  51. data/registry/app/javascript/velvetkit_icons.ts +21 -9
  52. data/registry/components.yml +17 -1
  53. data/registry/config/locales/velvetkit.en.yml +21 -0
  54. data/registry/test/components/previews/ui/collapsible_preview/default.html.erb +18 -0
  55. data/registry/test/components/previews/ui/collapsible_preview/show_more.html.erb +7 -0
  56. data/registry/test/components/previews/ui/collapsible_preview.rb +19 -0
  57. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +3 -0
  58. data/registry/test/components/previews/ui/file_field_preview/with_error.html.erb +3 -0
  59. data/registry/test/components/previews/ui/file_field_preview.rb +18 -0
  60. data/registry/test/components/previews/ui/form_field_preview/custom_control.html.erb +6 -0
  61. data/registry/test/components/previews/ui/form_field_preview/default.html.erb +7 -0
  62. data/registry/test/components/previews/ui/form_field_preview/model_errors.html.erb +11 -0
  63. data/registry/test/components/previews/ui/form_field_preview.rb +12 -0
  64. data/registry/test/components/previews/ui/input_preview/hint_and_error.html.erb +4 -0
  65. data/registry/test/components/previews/ui/input_preview.rb +3 -0
  66. data/registry/test/components/previews/ui/session_tabs_preview/handling_events.html.erb +46 -0
  67. data/registry/test/components/previews/ui/session_tabs_preview/saved_views.html.erb +16 -0
  68. data/registry/test/components/previews/ui/session_tabs_preview/touch_with_confirmation.html.erb +20 -0
  69. data/registry/test/components/previews/ui/session_tabs_preview.rb +35 -0
  70. data/registry/test/components/previews/ui/sidebar_preview/_brand.html.erb +4 -4
  71. data/registry/test/components/previews/ui/sidebar_preview/with_dropdown_header.html.erb +1 -1
  72. data/registry/test/components/previews/ui/skeleton_preview/card_placeholder.html.erb +10 -0
  73. data/registry/test/components/previews/ui/skeleton_preview/shapes.html.erb +9 -0
  74. data/registry/test/components/previews/ui/skeleton_preview.rb +15 -0
  75. data/registry/test/components/previews/ui/table_of_contents_preview/default.html.erb +41 -0
  76. data/registry/test/components/previews/ui/table_of_contents_preview/flat.html.erb +17 -0
  77. data/registry/test/components/previews/ui/table_of_contents_preview.rb +9 -0
  78. data/registry/test/components/previews/ui/textarea_preview/autogrow.html.erb +4 -0
  79. data/registry/test/components/previews/ui/textarea_preview/default.html.erb +3 -0
  80. data/registry/test/components/previews/ui/textarea_preview/sizes.html.erb +5 -0
  81. data/registry/test/components/previews/ui/textarea_preview/states.html.erb +7 -0
  82. data/registry/test/components/previews/ui/textarea_preview.rb +33 -0
  83. data/registry/test/components/previews/ui/toast_preview/live_viewport.html.erb +21 -0
  84. data/registry/test/components/previews/ui/toast_preview/variants.html.erb +7 -0
  85. data/registry/test/components/previews/ui/toast_preview/with_action.html.erb +7 -0
  86. data/registry/test/components/previews/ui/toast_preview.rb +29 -0
  87. data/registry/test/components/ui/checkbox_test.rb +24 -0
  88. data/registry/test/components/ui/collapsible_test.rb +71 -0
  89. data/registry/test/components/ui/date_picker_test.rb +14 -0
  90. data/registry/test/components/ui/file_field_test.rb +101 -0
  91. data/registry/test/components/ui/form_field/messages_test.rb +19 -0
  92. data/registry/test/components/ui/form_field_test.rb +122 -0
  93. data/registry/test/components/ui/input_group_test.rb +30 -2
  94. data/registry/test/components/ui/input_test.rb +37 -0
  95. data/registry/test/components/ui/native_select_test.rb +25 -0
  96. data/registry/test/components/ui/password_input_test.rb +9 -0
  97. data/registry/test/components/ui/select_test.rb +14 -0
  98. data/registry/test/components/ui/session_tabs/add_test.rb +52 -0
  99. data/registry/test/components/ui/session_tabs_test.rb +281 -0
  100. data/registry/test/components/ui/skeleton_test.rb +65 -0
  101. data/registry/test/components/ui/table_of_contents/item_test.rb +38 -0
  102. data/registry/test/components/ui/table_of_contents_test.rb +120 -0
  103. data/registry/test/components/ui/textarea_test.rb +178 -0
  104. data/registry/test/components/ui/toast/viewport_test.rb +72 -0
  105. data/registry/test/components/ui/toast_test.rb +183 -0
  106. metadata +77 -4
@@ -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,8 @@
1
+ <li>
2
+ <%= tag.a(@text, **link_options) %>
3
+ <% if items? %>
4
+ <ul class="ms-4 mt-1 space-y-1">
5
+ <% items.each do |item| %><%= item %><% end %>
6
+ </ul>
7
+ <% end %>
8
+ </li>
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UI::TableOfContents::Item < UI::Base
4
+ variants(
5
+ base: "block rounded-md px-3 text-muted-foreground hover:text-foreground " \
6
+ "data-[state=ancestor]:text-foreground " \
7
+ "data-[state=active]:bg-muted data-[state=active]:font-medium data-[state=active]:text-foreground " \
8
+ "#{FOCUS_RING}",
9
+ variants: {
10
+ level: {
11
+ top: "py-1.5",
12
+ nested: "py-1"
13
+ }
14
+ },
15
+ defaults: { level: :top }
16
+ )
17
+
18
+ renders_many :items, ->(href:, text:, **opts) {
19
+ UI::TableOfContents::Item.new(href: href, text: text, depth: @depth + 1, **opts)
20
+ }
21
+
22
+ def initialize(href:, text:, depth: 0, class: nil, **html_attrs)
23
+ @href = href
24
+ @text = text
25
+ @depth = depth
26
+ @user_class = binding.local_variable_get(:class)
27
+ @html_attrs = html_attrs
28
+ end
29
+
30
+ def link_options
31
+ opts = @html_attrs.except(:data)
32
+ opts[:href] = @href
33
+ opts[:class] = cn(variant_classes(level: @depth.zero? ? :top : :nested), @user_class)
34
+ opts[:data] = {
35
+ state: "inactive",
36
+ "ui--table-of-contents-target": "link",
37
+ action: "click->ui--table-of-contents#select"
38
+ }.merge(@html_attrs[:data] || {})
39
+ opts
40
+ end
41
+ end
@@ -0,0 +1,13 @@
1
+ <%= tag.nav(**html_options) do %>
2
+ <% if @title %>
3
+ <%= tag.p(@title, id: title_id, class: "mb-3 font-medium text-foreground") %>
4
+ <% end %>
5
+ <div class="relative border-s border-border ps-3" data-ui--table-of-contents-target="rail">
6
+ <%# Slides to the current link; positioned by the controller. %>
7
+ <span aria-hidden="true" data-state="hidden" data-ui--table-of-contents-target="indicator"
8
+ class="absolute -start-px top-0 h-0 w-0.5 rounded-full bg-primary opacity-0 data-[state=visible]:opacity-100 transition-[transform,height,opacity] duration-200 ease-out motion-reduce:transition-none"></span>
9
+ <ul class="space-y-1">
10
+ <% items.each do |item| %><%= item %><% end %>
11
+ </ul>
12
+ </div>
13
+ <% end %>
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # "On this page" navigation with scrollspy. As the reader scrolls, the link to
4
+ # the section they are reading is marked current (aria-current="location") and
5
+ # an indicator slides beside it. A link jumps to its section like any anchor.
6
+ #
7
+ # Each item links to an element id on the page, and items nest:
8
+ #
9
+ # <%= render(UI::TableOfContents.new(title: "On this page")) do |toc| %>
10
+ # <% toc.with_item(href: "#install", text: "Install") do |item| %>
11
+ # <% item.with_item(href: "#requirements", text: "Requirements") %>
12
+ # <% end %>
13
+ # <% toc.with_item(href: "#usage", text: "Usage") %>
14
+ # <% end %>
15
+ #
16
+ # A section counts as reached once its top passes `offset:` pixels below the
17
+ # top of the scroll area; raise it to clear a sticky header. When the content
18
+ # scrolls inside an element rather than the window, pass that element's CSS
19
+ # selector as `container:`.
20
+ class UI::TableOfContents < UI::Base
21
+ renders_many :items, ->(href:, text:, **opts) {
22
+ UI::TableOfContents::Item.new(href: href, text: text, depth: 0, **opts)
23
+ }
24
+
25
+ def initialize(title: nil, label: nil, offset: 80, container: nil, class: nil, **html_attrs)
26
+ @title = title
27
+ @label = label
28
+ @offset = offset
29
+ @container = container
30
+ @user_class = binding.local_variable_get(:class)
31
+ @html_attrs = html_attrs
32
+ @id = @html_attrs[:id] || "ui-toc-#{SecureRandom.hex(4)}"
33
+ @html_attrs[:id] = @id
34
+ end
35
+
36
+ def title_id = "#{@id}-title"
37
+
38
+ def html_options
39
+ data = {
40
+ controller: "ui--table-of-contents",
41
+ # Until the controller has marked the current link, nothing animates:
42
+ # otherwise every page load fades the highlight in.
43
+ settling: "",
44
+ "ui--table-of-contents-offset-value": @offset,
45
+ "ui--table-of-contents-container-value": @container
46
+ }.compact
47
+
48
+ opts = @html_attrs.except(:data)
49
+ opts[:class] = cn("text-sm data-[settling]:[&_*]:transition-none", @user_class)
50
+ opts[:data] = data.merge(@html_attrs[:data] || {})
51
+ if @title
52
+ opts[:"aria-labelledby"] = title_id
53
+ else
54
+ opts[:"aria-label"] = @label || I18n.t("ui.table_of_contents.label", default: "On this page")
55
+ end
56
+ opts
57
+ end
58
+ end
@@ -0,0 +1,2 @@
1
+ <%# frozen_string_literal: true %>
2
+ <%= render(wrapper) do %><% if form_field? %><%= @form.text_area(@field, **form_field_options) %><% else %><%= tag.textarea(value, **html_options) %><% end %><% end %>
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # A multi-line text field, with the same label, hint, required, error and
6
+ # form-builder options as UI::Input.
7
+ #
8
+ # `autogrow: true` grows the box with its text (CSS field-sizing, no
9
+ # JavaScript); browsers without it keep `rows:` lines and scroll. Cap the
10
+ # height with a class such as `max-h-60`.
11
+ class UI::Textarea < UI::Base
12
+ variants(
13
+ base: "flex w-full rounded-md border border-input bg-background " \
14
+ "text-foreground " \
15
+ "placeholder:text-muted-foreground " \
16
+ "#{UI::Base::FOCUS_RING} " \
17
+ "disabled:cursor-not-allowed disabled:opacity-50",
18
+ variants: {
19
+ size: {
20
+ sm: "px-2.5 py-1.5 text-xs",
21
+ default: "px-3 py-2 text-sm",
22
+ lg: "px-4 py-2.5 text-base"
23
+ },
24
+ resize: {
25
+ vertical: "resize-y",
26
+ none: "resize-none",
27
+ both: "resize"
28
+ },
29
+ autogrow: {
30
+ true => "field-sizing-content min-h-16 resize-none",
31
+ false => ""
32
+ },
33
+ error: {
34
+ true => "border-destructive focus:ring-destructive",
35
+ false => ""
36
+ }
37
+ },
38
+ defaults: { size: :default, resize: :vertical, autogrow: false, error: false }
39
+ )
40
+
41
+ # `form:` + `field:` render through the form builder's text_area, so the
42
+ # field gets a scoped name like `post[body]` and its value from the model.
43
+ # Without them a plain <textarea> is rendered; pass `name:` and `value:`.
44
+ def initialize(size: :default, rows: 3, resize: :vertical, autogrow: false,
45
+ label: nil, label_size: :default, label_weight: :medium,
46
+ hint: nil, required: false, error: nil, disabled: false,
47
+ value: nil, form: nil, field: nil,
48
+ class: nil, **html_attrs)
49
+ @form = form
50
+ @field = field
51
+ @size = size
52
+ @rows = rows
53
+ @resize = resize
54
+ @autogrow = autogrow
55
+ @label = label
56
+ @label_size = label_size
57
+ @label_weight = label_weight
58
+ @required = required
59
+ @error = error
60
+ @disabled = disabled
61
+ @value = value
62
+ @user_class = binding.local_variable_get(:class)
63
+ @html_attrs = html_attrs
64
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
65
+ required: required, disabled: disabled,
66
+ label_size: label_size, label_weight: label_weight,
67
+ form: form, field: field)
68
+ @textarea_id = @html_attrs[:id] || (@wrapper.wrap? ? "ui-textarea-#{SecureRandom.hex(4)}" : nil)
69
+ @html_attrs[:id] = @textarea_id if @textarea_id
70
+ @wrapper.for = @textarea_id
71
+ end
72
+
73
+ attr_reader :value, :wrapper
74
+
75
+ def classes
76
+ cn(
77
+ variant_classes(size: @size, resize: @resize, autogrow: @autogrow, error: wrapper.error?),
78
+ @user_class
79
+ )
80
+ end
81
+
82
+ def html_options
83
+ opts = @html_attrs.dup
84
+ opts[:rows] = @rows
85
+ opts[:class] = classes
86
+ opts[:disabled] = true if @disabled
87
+ opts[:required] = true if @required
88
+ opts[:"aria-invalid"] = "true" if wrapper.error?
89
+ opts[:"aria-describedby"] = wrapper.describedby if wrapper.describedby
90
+ opts
91
+ end
92
+
93
+ def form_field?
94
+ @form.present? && @field.present?
95
+ end
96
+
97
+ # The builder derives the name and value itself.
98
+ def form_field_options
99
+ html_options.except(:name)
100
+ end
101
+ 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