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,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # A label, a control, a hint and an error message, wired together: the label
6
+ # points at the control, and the control is described by the hint and error.
7
+ #
8
+ # Input, Textarea, NativeSelect and Select render through it, so they take
9
+ # `hint:` and `error:` directly. Wrap any other control yourself and spread
10
+ # `field.control_attributes` onto it:
11
+ #
12
+ # <%= render(UI::FormField.new(label: "Colour", hint: "Used for your avatar")) do |field| %>
13
+ # <%= tag.input(type: "color", name: "colour", **field.control_attributes) %>
14
+ # <% end %>
15
+ #
16
+ # `error:` takes a message, or `true` to mark the control invalid without one.
17
+ # Bound to a form (`form:` + `field:`), the model's errors on that attribute
18
+ # are the message (for `team_id`, the `team` association's too, where
19
+ # belongs_to puts "must exist"), and the attribute's human name is the label
20
+ # (`label:` overrides it, `label: false` opts out). Nothing to show means no
21
+ # wrapper: just the control.
22
+ class UI::FormField < UI::Base
23
+ def initialize(label: nil, hint: nil, error: nil, required: false, disabled: false,
24
+ label_size: :default, label_weight: :medium,
25
+ form: nil, field: nil, for: nil,
26
+ class: nil, **html_attrs)
27
+ @label = label
28
+ @hint = hint
29
+ @error = error
30
+ @required = required
31
+ @disabled = disabled
32
+ @label_size = label_size
33
+ @label_weight = label_weight
34
+ @form = form
35
+ @field = field
36
+ @for = binding.local_variable_get(:for)
37
+ @user_class = binding.local_variable_get(:class)
38
+ @html_attrs = html_attrs
39
+ end
40
+
41
+ attr_reader :hint
42
+ attr_writer :for
43
+
44
+ def form_field? = @form.present? && @field.present?
45
+
46
+ def wrap? = render_label? || hint.present? || error_message.present?
47
+
48
+ # The id the label points at and the messages hang off. Controls set their
49
+ # own (`for:`); otherwise one is made the first time it's asked for.
50
+ def control_id
51
+ @for ||= "ui-field-#{SecureRandom.hex(4)}"
52
+ end
53
+
54
+ def hint_id = "#{control_id}-hint"
55
+ def error_id = "#{control_id}-error"
56
+
57
+ def error_message
58
+ return @error if @error.is_a?(String)
59
+ return if @error == false || !form_field?
60
+
61
+ errors = @form.object.respond_to?(:errors) ? @form.object.errors : nil
62
+ return unless errors
63
+
64
+ attributes = [ @field.to_sym, @field.to_s.delete_suffix("_id").to_sym ].uniq
65
+ attributes.flat_map { |attribute| errors.full_messages_for(attribute) }.uniq.to_sentence.presence
66
+ end
67
+
68
+ def error? = @error == true || error_message.present?
69
+
70
+ def describedby
71
+ [ (hint_id if hint.present?), (error_id if error_message.present?) ].compact.join(" ").presence
72
+ end
73
+
74
+ # Spread onto a control you render yourself.
75
+ def control_attributes
76
+ { id: control_id, "aria-describedby": describedby, "aria-invalid": ("true" if error?) }.compact
77
+ end
78
+
79
+ def render_label?
80
+ return false if @label == false
81
+
82
+ @label.present? || form_field?
83
+ end
84
+
85
+ def label_text
86
+ return @label if @label.is_a?(String) && @label.present?
87
+ return nil unless form_field?
88
+
89
+ object = @form.object
90
+ if object.respond_to?(:class) && object.class.respond_to?(:human_attribute_name)
91
+ object.class.human_attribute_name(@field)
92
+ else
93
+ @field.to_s.humanize
94
+ end
95
+ end
96
+
97
+ def label_component
98
+ UI::Label.new(for: control_id, size: @label_size, weight: @label_weight,
99
+ required: @required, disabled: @disabled)
100
+ end
101
+
102
+ def classes
103
+ cn("flex flex-col gap-2 w-full", @user_class)
104
+ end
105
+ end
@@ -1,9 +1,2 @@
1
1
  <%# frozen_string_literal: true %>
2
- <% if render_label? %>
3
- <div class="flex flex-col gap-2 w-full">
4
- <%= render(label_component) { label_text } %>
5
- <% if form_field? %><%= @form.public_send(form_builder_method, @field, **form_field_options) %><% else %><%= tag.input(**html_options) %><% end %>
6
- </div>
7
- <% else %>
8
- <% if form_field? %><%= @form.public_send(form_builder_method, @field, **form_field_options) %><% else %><%= tag.input(**html_options) %><% end %>
9
- <% end %>
2
+ <%= render(wrapper) do %><% if form_field? %><%= @form.public_send(form_builder_method, @field, **form_field_options) %><% else %><%= tag.input(**html_options) %><% end %><% end %>
@@ -42,7 +42,7 @@ class UI::Input < UI::Base
42
42
 
43
43
  def initialize(type: :text, size: :default, variant: :default,
44
44
  label: nil, label_size: :default, label_weight: :medium,
45
- required: false, error: false, disabled: false,
45
+ hint: nil, required: false, error: nil, disabled: false,
46
46
  form: nil, field: nil,
47
47
  class: nil, **html_attrs)
48
48
  @form = form
@@ -58,13 +58,20 @@ class UI::Input < UI::Base
58
58
  @disabled = disabled
59
59
  @user_class = binding.local_variable_get(:class)
60
60
  @html_attrs = html_attrs
61
- @input_id = @html_attrs[:id] || (render_label? ? "ui-input-#{SecureRandom.hex(4)}" : nil)
61
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
62
+ required: required, disabled: disabled,
63
+ label_size: label_size, label_weight: label_weight,
64
+ form: form, field: field)
65
+ @input_id = @html_attrs[:id] || (@wrapper.wrap? ? "ui-input-#{SecureRandom.hex(4)}" : nil)
62
66
  @html_attrs[:id] = @input_id if @input_id
67
+ @wrapper.for = @input_id
63
68
  end
64
69
 
70
+ attr_reader :wrapper
71
+
65
72
  def classes
66
73
  cn(
67
- variant_classes(size: @size, variant: @variant),
74
+ variant_classes(size: @size, variant: wrapper.error? ? :error : @variant),
68
75
  @user_class
69
76
  )
70
77
  end
@@ -76,7 +83,8 @@ class UI::Input < UI::Base
76
83
  opts[:class] = classes
77
84
  opts[:disabled] = true if @disabled
78
85
  opts[:required] = true if @required
79
- opts[:"aria-invalid"] = "true" if @error
86
+ opts[:"aria-invalid"] = "true" if wrapper.error?
87
+ opts[:"aria-describedby"] = wrapper.describedby if wrapper.describedby
80
88
  opts
81
89
  end
82
90
 
@@ -92,36 +100,4 @@ class UI::Input < UI::Base
92
100
  def form_builder_method
93
101
  BUILDER_METHODS.fetch(@type.to_sym, :text_field)
94
102
  end
95
-
96
- # Labels follow Rails' own convention: bound to a form object, the attribute
97
- # name supplies the text (via human_attribute_name, so i18n and
98
- # `activerecord.attributes.*` overrides apply) with no per-call-site string.
99
- # Pass `label:` to override the text, or `label: false` to opt out entirely.
100
- def render_label?
101
- return false if @label == false
102
-
103
- @label.present? || form_field?
104
- end
105
-
106
- def label_text
107
- return @label if @label.is_a?(String) && @label.present?
108
- return nil unless form_field?
109
-
110
- object = @form.object
111
- if object.respond_to?(:class) && object.class.respond_to?(:human_attribute_name)
112
- object.class.human_attribute_name(@field)
113
- else
114
- @field.to_s.humanize
115
- end
116
- end
117
-
118
- def label_component
119
- UI::Label.new(
120
- for: @input_id,
121
- size: @label_size,
122
- weight: @label_weight,
123
- required: @required,
124
- disabled: @disabled
125
- )
126
- end
127
103
  end
@@ -33,7 +33,10 @@ class UI::InputGroup::Addon < UI::Base
33
33
  class: cn(
34
34
  "text-sm flex gap-2 items-center h-6 gap-1 px-2",
35
35
  "rounded-[calc(var(--radius)-5px)]",
36
- "[&>svg:not([class*='size-'])]:size-3.5 has-[>svg]:px-2"
36
+ "[&>svg:not([class*='size-'])]:size-3.5 has-[>svg]:px-2",
37
+ # Inside a field a click shouldn't ring the button (it reads as a
38
+ # second ring on the field); keyboard focus still does.
39
+ "focus:ring-0 focus:ring-offset-0 focus-visible:ring-2 focus-visible:ring-offset-1"
37
40
  ),
38
41
  **opts
39
42
  )
@@ -3,7 +3,7 @@
3
3
  # UI::InputGroup::Field — a UI::Input variant stripped of its own
4
4
  # border/shadow/ring so the parent wrapper owns those styles.
5
5
  #
6
- # The wrapper owns the focus treatment via :focus-within; the field strips
6
+ # The wrapper owns the focus treatment (lit while this field has focus); the field strips
7
7
  # its own outline + ring so there is no double indicator on focus.
8
8
  class UI::InputGroup::Field < UI::Input
9
9
  STRIPPED_CLASSES =
@@ -16,6 +16,9 @@ class UI::InputGroup::Field < UI::Input
16
16
 
17
17
  def initialize(**args)
18
18
  super(**args)
19
+ # Inside the group's border there is no room for a label or messages: the
20
+ # component composing the group shows those. Keep only the invalid state.
21
+ @wrapper = UI::FormField.new(label: false, error: @wrapper.error?)
19
22
  @html_attrs[:data] ||= {}
20
23
  @html_attrs[:data][:slot] = "input-group-control"
21
24
  end
@@ -13,8 +13,11 @@
13
13
  # <%= g.with_field(type: :email, placeholder: "you@example.com") %>
14
14
  # <% end %>
15
15
  class UI::InputGroup < UI::Base
16
- # Wrapper owns the focus ring via :focus-within so it lights up on any
17
- # descendant focus (mouse click or keyboard). focus-visible is too narrow.
16
+ # Wrapper owns the control's focus ring, lit when the control itself has
17
+ # focus (mouse or keyboard). Not :focus-within: an addon button has its own
18
+ # ring, and the two would stack.
19
+ # An invalid, enabled control turns the border red: [aria-invalid]:enabled,
20
+ # because a :has() nested inside has-[] is invalid CSS and drops the rule.
18
21
  WRAPPER_BASE =
19
22
  "group/input-group relative flex w-full items-center rounded-md border " \
20
23
  "border-input bg-background " \
@@ -28,12 +31,12 @@ class UI::InputGroup < UI::Base
28
31
  "has-[>[data-align=block-end]]:h-auto " \
29
32
  "has-[>[data-align=block-end]]:flex-col " \
30
33
  "has-[>[data-align=block-end]]:[&>input]:pt-3 " \
31
- "focus-within:ring-2 " \
32
- "focus-within:ring-primary " \
33
- "focus-within:ring-offset-1 " \
34
+ "has-[[data-slot]:focus]:ring-2 " \
35
+ "has-[[data-slot]:focus]:ring-primary " \
36
+ "has-[[data-slot]:focus]:ring-offset-1 " \
34
37
  "ring-offset-background " \
35
- "has-[[data-slot][aria-invalid=true]:not(:has(input:disabled))]:border-destructive " \
36
- "has-[[data-slot][aria-invalid=true]:not(:has(input:disabled))]:ring-destructive " \
38
+ "has-[[data-slot][aria-invalid=true]:enabled]:border-destructive " \
39
+ "has-[[data-slot][aria-invalid=true]:enabled]:ring-destructive " \
37
40
  "has-[input:disabled]:border-input " \
38
41
  "has-[input:disabled]:bg-muted " \
39
42
  "has-[input:disabled]:opacity-50"
@@ -47,9 +50,8 @@ class UI::InputGroup < UI::Base
47
50
  @html_attrs = html_attrs
48
51
  end
49
52
 
53
+ # The invalid state comes from the field's aria-invalid, via WRAPPER_BASE.
50
54
  def wrapper_classes
51
- # Propagate error styling to the wrapper when the field is in error state.
52
- error = field&.instance_variable_get(:@error)
53
- cn(WRAPPER_BASE, { "border-destructive" => error }, @user_class)
55
+ cn(WRAPPER_BASE, @user_class)
54
56
  end
55
57
  end
@@ -2,15 +2,8 @@
2
2
  is drawn back with a positioned icon over the control. %>
3
3
  <% control = capture do %>
4
4
  <div class="relative w-full">
5
- <% if form_field? %><%= @form.select(@field, normalized_options, { include_blank: prompt.presence, selected: @selected }, form_select_options) %><% else %><%= tag.select(**select_options) do %><% if prompt.present? %><option value=""><%= prompt %></option><% end %><% normalized_options.each do |label, value| %><option value="<%= value %>" <%= "selected" if selected?(value) %>><%= label %></option><% end %><% end %><% end %>
5
+ <% if form_field? %><%= @form.select(@field, normalized_options, builder_select_options, form_select_options) %><% else %><%= tag.select(**select_options) do %><% if prompt.present? %><option value=""><%= prompt %></option><% end %><% normalized_options.each do |label, value| %><option value="<%= value %>" <%= "selected" if selected?(value) %>><%= label %></option><% end %><% end %><% end %>
6
6
  <span class="pointer-events-none absolute inset-y-0 right-0 flex items-center pr-3 text-muted-foreground"><%= render(UI::Icon.new(name: "chevron-down", size: :sm, aria: { hidden: true })) %></span>
7
7
  </div>
8
8
  <% end %>
9
- <% if render_label? %>
10
- <div class="flex flex-col gap-2 w-full">
11
- <%= render(label_component) { label_text } %>
12
- <%= control %>
13
- </div>
14
- <% else %>
15
- <%= control %>
16
- <% end %>
9
+ <%= render(wrapper) { control } %>
@@ -30,8 +30,8 @@ class UI::NativeSelect < UI::Base
30
30
  )
31
31
 
32
32
  def initialize(name: nil, options: [], selected: nil, prompt: nil,
33
- label: nil, size: :default,
34
- required: false, error: false, disabled: false,
33
+ label: nil, hint: nil, size: :default,
34
+ required: false, error: nil, disabled: false,
35
35
  form: nil, field: nil,
36
36
  class: nil, **html_attrs)
37
37
  @name = name
@@ -47,52 +47,32 @@ class UI::NativeSelect < UI::Base
47
47
  @field = field
48
48
  @user_class = binding.local_variable_get(:class)
49
49
  @html_attrs = html_attrs
50
- @select_id = @html_attrs[:id] || (render_label? ? "ui-native-select-#{SecureRandom.hex(4)}" : nil)
50
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
51
+ required: required, disabled: disabled,
52
+ form: form, field: field)
53
+ @select_id = @html_attrs[:id] || (@wrapper.wrap? ? "ui-native-select-#{SecureRandom.hex(4)}" : nil)
51
54
  @html_attrs[:id] = @select_id if @select_id
55
+ @wrapper.for = @select_id
52
56
  end
53
57
 
54
- attr_reader :options, :prompt
58
+ attr_reader :options, :prompt, :wrapper
55
59
 
56
60
  def classes
57
61
  cn(variant_classes(size: @size),
58
- { "border-destructive focus:ring-destructive" => @error },
62
+ { "border-destructive focus:ring-destructive" => wrapper.error? },
59
63
  @user_class)
60
64
  end
61
65
 
62
66
  def form_field? = @form.present? && @field.present?
63
67
 
64
- # Same convention as UI::Input: bound to a form object the attribute name
65
- # supplies the label; `label:` overrides, `label: false` opts out.
66
- def render_label?
67
- return false if @label == false
68
-
69
- @label.present? || form_field?
70
- end
71
-
72
- def label_text
73
- return nil unless render_label?
74
- return @label if @label.is_a?(String) && @label.present?
75
- return nil unless form_field?
76
-
77
- object = @form.object
78
- if object.respond_to?(:class) && object.class.respond_to?(:human_attribute_name)
79
- object.class.human_attribute_name(@field)
80
- else
81
- @field.to_s.humanize
82
- end
83
- end
84
-
85
- def label_component
86
- UI::Label.new(for: @select_id, required: @required, disabled: @disabled)
87
- end
88
-
89
68
  def select_options
90
69
  opts = @html_attrs.dup
91
70
  opts[:class] = classes
92
71
  opts[:name] = @name if @name
93
72
  opts[:disabled] = true if @disabled
94
73
  opts[:required] = true if @required
95
- opts[:"aria-invalid"] = "true" if @error
74
+ opts[:"aria-invalid"] = "true" if wrapper.error?
75
+ opts[:"aria-describedby"] = wrapper.describedby if wrapper.describedby
96
76
  opts
97
77
  end
98
78
 
@@ -100,6 +80,12 @@ class UI::NativeSelect < UI::Base
100
80
  # to avoid overriding the scoped name with a bare one.
101
81
  def form_select_options = select_options.except(:name)
102
82
 
83
+ # Without an explicit `selected:` the builder selects the model's value;
84
+ # passing `selected: nil` would select nothing, losing it on a re-render.
85
+ def builder_select_options
86
+ { include_blank: prompt.presence, selected: @selected }.compact
87
+ end
88
+
103
89
  # Accepts [["Label", value], ...] or a plain list of scalars.
104
90
  def normalized_options
105
91
  options.map { |option| option.is_a?(Array) ? option : [ option, option ] }
@@ -10,11 +10,4 @@
10
10
  <% addon.with_icon_button(**toggle_button_options) do %><span class="inline-flex" data-ui--password-visibility-target="iconShow"><%= render(UI::Icon.new(name: "eye", size: :sm)) %></span><span class="inline-flex" style="display: none" data-ui--password-visibility-target="iconHide"><%= render(UI::Icon.new(name: "eye-off", size: :sm)) %></span><% end %>
11
11
  <% end %>
12
12
  <% end %><% end %>
13
- <% if render_label? %>
14
- <div class="flex flex-col gap-2 w-full">
15
- <%= render(label_component) { label_text } %>
16
- <%= group %>
17
- </div>
18
- <% else %>
19
- <%= group %>
20
- <% end %>
13
+ <%= render(wrapper) { group } %>
@@ -16,8 +16,8 @@ require "securerandom"
16
16
  # <%= render(UI::PasswordInput.new(form: f, field: :password, required: true)) %>
17
17
  # <%= render(UI::PasswordInput.new(name: "user[password]")) %>
18
18
  class UI::PasswordInput < UI::Base
19
- def initialize(form: nil, field: nil, label: nil, required: false,
20
- disabled: false, error: false, class: nil, **html_attrs)
19
+ def initialize(form: nil, field: nil, label: nil, hint: nil, required: false,
20
+ disabled: false, error: nil, class: nil, **html_attrs)
21
21
  @form = form
22
22
  @field = field
23
23
  @label = label
@@ -29,8 +29,13 @@ class UI::PasswordInput < UI::Base
29
29
  @error = error
30
30
  @user_class = binding.local_variable_get(:class)
31
31
  @html_attrs = html_attrs
32
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
33
+ required: required, disabled: disabled,
34
+ form: form, field: field, for: @input_id)
32
35
  end
33
36
 
37
+ attr_reader :wrapper
38
+
34
39
  def group_component
35
40
  UI::InputGroup.new(
36
41
  class: @user_class,
@@ -38,34 +43,10 @@ class UI::PasswordInput < UI::Base
38
43
  )
39
44
  end
40
45
 
41
- # Same convention as UI::Input: bound to a form object the attribute name
42
- # supplies the label text; `label:` overrides it, `label: false` opts out.
43
- def render_label?
44
- return false if @label == false
45
-
46
- @label.present? || form_field?
47
- end
48
-
49
46
  def form_field?
50
47
  @form.present? && @field.present?
51
48
  end
52
49
 
53
- def label_text
54
- return @label if @label.is_a?(String) && @label.present?
55
- return nil unless form_field?
56
-
57
- object = @form.object
58
- if object.respond_to?(:class) && object.class.respond_to?(:human_attribute_name)
59
- object.class.human_attribute_name(@field)
60
- else
61
- @field.to_s.humanize
62
- end
63
- end
64
-
65
- def label_component
66
- UI::Label.new(for: @input_id, required: @required, disabled: @disabled)
67
- end
68
-
69
50
  def field_options
70
51
  {
71
52
  type: :password,
@@ -75,7 +56,8 @@ class UI::PasswordInput < UI::Base
75
56
  label: false,
76
57
  required: @required,
77
58
  disabled: @disabled,
78
- error: @error,
59
+ error: wrapper.error?,
60
+ "aria-describedby": wrapper.describedby,
79
61
  data: { "ui--password-visibility-target" => "input" },
80
62
  **@html_attrs
81
63
  }
@@ -48,6 +48,10 @@
48
48
  <% end %>
49
49
  <% end %>
50
50
 
51
+ <% if wrapper.hint.present? || wrapper.error_message.present? %>
52
+ <div class="mt-2 flex flex-col gap-2"><%= render(UI::FormField::Messages.new(wrapper)) %></div>
53
+ <% end %>
54
+
51
55
  <% if form_field? %>
52
56
  <%= @form.select(@field, native_choices, { selected: current_values }, native_options) %>
53
57
  <% else %>
@@ -42,8 +42,8 @@ class UI::Select < UI::Base
42
42
  )
43
43
 
44
44
  def initialize(name: nil, options: [], selected: nil, prompt: nil,
45
- label: nil, size: :default, searchable: false, multiple: false,
46
- required: false, error: false, disabled: false,
45
+ label: nil, hint: nil, size: :default, searchable: false, multiple: false,
46
+ required: false, error: nil, disabled: false,
47
47
  form: nil, field: nil,
48
48
  class: nil, **html_attrs)
49
49
  @name = name
@@ -62,9 +62,15 @@ class UI::Select < UI::Base
62
62
  @user_class = binding.local_variable_get(:class)
63
63
  @html_attrs = html_attrs
64
64
  @uid = SecureRandom.hex(4)
65
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
66
+ required: required, disabled: disabled,
67
+ form: form, field: field, for: trigger_id)
65
68
  end
66
69
 
67
- attr_reader :prompt, :size
70
+ attr_reader :prompt, :size, :wrapper
71
+
72
+ # Lays out its own label, so it takes FormField's parts rather than its wrapper.
73
+ delegate :render_label?, :label_text, :label_component, to: :wrapper
68
74
 
69
75
  def searchable? = @searchable
70
76
  def multiple? = @multiple
@@ -76,7 +82,7 @@ class UI::Select < UI::Base
76
82
 
77
83
  def trigger_classes
78
84
  cn(variant_classes(size: @size),
79
- { "border-destructive focus:ring-destructive" => @error },
85
+ { "border-destructive focus:ring-destructive" => wrapper.error? },
80
86
  @user_class)
81
87
  end
82
88
 
@@ -87,7 +93,8 @@ class UI::Select < UI::Base
87
93
  class: trigger_classes,
88
94
  role: "combobox",
89
95
  disabled: @disabled,
90
- aria: { haspopup: "listbox", expanded: "false", controls: listbox_id, invalid: (@error ? "true" : nil) },
96
+ aria: { haspopup: "listbox", expanded: "false", controls: listbox_id,
97
+ invalid: ("true" if wrapper.error?), describedby: wrapper.describedby },
91
98
  data: { "ui--select-target": "trigger",
92
99
  action: "click->ui--select#toggle keydown->ui--select#keydown" }
93
100
  }
@@ -95,30 +102,6 @@ class UI::Select < UI::Base
95
102
 
96
103
  def form_field? = @form.present? && @field.present?
97
104
 
98
- # Same convention as UI::Input: bound to a form object the attribute name
99
- # supplies the label; `label:` overrides, `label: false` opts out.
100
- def render_label?
101
- return false if @label == false
102
-
103
- @label.present? || form_field?
104
- end
105
-
106
- def label_text
107
- return @label if @label.is_a?(String) && @label.present?
108
- return nil unless form_field?
109
-
110
- object = @form.object
111
- if object.respond_to?(:class) && object.class.respond_to?(:human_attribute_name)
112
- object.class.human_attribute_name(@field)
113
- else
114
- @field.to_s.humanize
115
- end
116
- end
117
-
118
- def label_component
119
- UI::Label.new(for: trigger_id, required: @required, disabled: @disabled)
120
- end
121
-
122
105
  # Accepts [["Label", value], ...] or a plain list of scalars. A prompt
123
106
  # becomes a first, empty option, as `include_blank` makes it natively;
124
107
  # a multiple select has no "nothing" option, since nothing is unticking.
@@ -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 %>