velvetkit 0.4.1 → 0.5.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 (148) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +143 -2
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/accordion/trigger.rb +1 -1
  5. data/registry/app/components/ui/badge.rb +1 -1
  6. data/registry/app/components/ui/base.rb +2 -2
  7. data/registry/app/components/ui/button.rb +1 -1
  8. data/registry/app/components/ui/card/action.html.erb +1 -0
  9. data/registry/app/components/ui/card/action.rb +14 -0
  10. data/registry/app/components/ui/card/content.html.erb +1 -1
  11. data/registry/app/components/ui/card/content.rb +4 -2
  12. data/registry/app/components/ui/card/description.html.erb +1 -1
  13. data/registry/app/components/ui/card/description.rb +4 -2
  14. data/registry/app/components/ui/card/footer.html.erb +1 -1
  15. data/registry/app/components/ui/card/footer.rb +4 -2
  16. data/registry/app/components/ui/card/header.html.erb +1 -1
  17. data/registry/app/components/ui/card/header.rb +9 -2
  18. data/registry/app/components/ui/card/title.html.erb +1 -1
  19. data/registry/app/components/ui/card/title.rb +4 -2
  20. data/registry/app/components/ui/card.html.erb +1 -1
  21. data/registry/app/components/ui/card.rb +49 -3
  22. data/registry/app/components/ui/carousel.html.erb +7 -0
  23. data/registry/app/components/ui/carousel.rb +60 -0
  24. data/registry/app/components/ui/chart.html.erb +3 -3
  25. data/registry/app/components/ui/chart.rb +18 -10
  26. data/registry/app/components/ui/checkbox.html.erb +6 -0
  27. data/registry/app/components/ui/checkbox.rb +11 -4
  28. data/registry/app/components/ui/fieldset.html.erb +6 -0
  29. data/registry/app/components/ui/fieldset.rb +76 -0
  30. data/registry/app/components/ui/gallery/image.rb +14 -1
  31. data/registry/app/components/ui/gallery.html.erb +32 -3
  32. data/registry/app/components/ui/gallery.rb +46 -7
  33. data/registry/app/components/ui/link.rb +1 -1
  34. data/registry/app/components/ui/menu/item.rb +1 -1
  35. data/registry/app/components/ui/radio_group/item.html.erb +1 -1
  36. data/registry/app/components/ui/radio_group/item.rb +5 -1
  37. data/registry/app/components/ui/radio_group.html.erb +1 -1
  38. data/registry/app/components/ui/radio_group.rb +42 -2
  39. data/registry/app/components/ui/scrubber.html.erb +18 -0
  40. data/registry/app/components/ui/scrubber.rb +62 -0
  41. data/registry/app/components/ui/select.html.erb +1 -1
  42. data/registry/app/components/ui/select.rb +16 -1
  43. data/registry/app/components/ui/selection_bar.html.erb +7 -0
  44. data/registry/app/components/ui/selection_bar.rb +75 -0
  45. data/registry/app/components/ui/sidebar/group_label.rb +1 -1
  46. data/registry/app/components/ui/sidebar/inset.rb +5 -2
  47. data/registry/app/components/ui/sidebar/menu_button.rb +24 -9
  48. data/registry/app/components/ui/sidebar/menu_item.html.erb +1 -1
  49. data/registry/app/components/ui/sidebar/menu_item.rb +31 -5
  50. data/registry/app/components/ui/sidebar/menu_sub.rb +59 -0
  51. data/registry/app/components/ui/sidebar.html.erb +3 -3
  52. data/registry/app/components/ui/sidebar.rb +91 -13
  53. data/registry/app/components/ui/switch.rb +2 -2
  54. data/registry/app/components/ui/table/column.html.erb +1 -1
  55. data/registry/app/components/ui/table/column.rb +26 -2
  56. data/registry/app/components/ui/table/head.html.erb +1 -1
  57. data/registry/app/components/ui/table/head.rb +15 -2
  58. data/registry/app/components/ui/table/row.html.erb +1 -1
  59. data/registry/app/components/ui/table/row.rb +19 -5
  60. data/registry/app/components/ui/table.html.erb +2 -2
  61. data/registry/app/components/ui/table.rb +29 -4
  62. data/registry/app/components/ui/tabs/trigger.rb +1 -1
  63. data/registry/app/components/ui/toggle_group.html.erb +1 -1
  64. data/registry/app/components/ui/toggle_group.rb +26 -3
  65. data/registry/app/javascript/controllers/ui/carousel_controller.ts +63 -0
  66. data/registry/app/javascript/controllers/ui/file_field_controller.ts +1 -1
  67. data/registry/app/javascript/controllers/ui/gallery_controller.ts +52 -14
  68. data/registry/app/javascript/controllers/ui/scrubber_controller.ts +188 -0
  69. data/registry/app/javascript/controllers/ui/selection_bar_controller.ts +122 -0
  70. data/registry/app/javascript/controllers/ui/sidebar_controller.ts +102 -32
  71. data/registry/components.yml +12 -4
  72. data/registry/config/locales/velvetkit.en.yml +16 -0
  73. data/registry/icons/lucide/arrow-down.svg +16 -0
  74. data/registry/icons/lucide/arrow-up.svg +16 -0
  75. data/registry/icons/lucide/image.svg +17 -0
  76. data/registry/icons/lucide/star.svg +15 -0
  77. data/registry/icons/phosphor/arrow-down.svg +2 -0
  78. data/registry/icons/phosphor/arrow-up.svg +2 -0
  79. data/registry/icons/phosphor/image.svg +2 -0
  80. data/registry/icons/phosphor/star.svg +2 -0
  81. data/registry/icons/tabler/arrow-down.svg +18 -0
  82. data/registry/icons/tabler/arrow-up.svg +18 -0
  83. data/registry/icons/tabler/photo.svg +19 -0
  84. data/registry/icons/tabler/star.svg +16 -0
  85. data/registry/icons.yml +4 -0
  86. data/registry/test/components/previews/ui/card_preview/edge_to_edge.html.erb +19 -0
  87. data/registry/test/components/previews/ui/card_preview/image.html.erb +12 -0
  88. data/registry/test/components/previews/ui/card_preview/just_content.html.erb +10 -0
  89. data/registry/test/components/previews/ui/card_preview/open_bill.html.erb +18 -0
  90. data/registry/test/components/previews/ui/card_preview/playground.html.erb +17 -0
  91. data/registry/test/components/previews/ui/card_preview/settings_form.html.erb +15 -24
  92. data/registry/test/components/previews/ui/card_preview/sign_in.html.erb +23 -0
  93. data/registry/test/components/previews/ui/card_preview/small.html.erb +17 -0
  94. data/registry/test/components/previews/ui/card_preview/spacing.html.erb +13 -0
  95. data/registry/test/components/previews/ui/card_preview/stats.html.erb +11 -0
  96. data/registry/test/components/previews/ui/card_preview/team.html.erb +21 -0
  97. data/registry/test/components/previews/ui/card_preview.rb +42 -28
  98. data/registry/test/components/previews/ui/carousel_preview/highlights.html.erb +17 -0
  99. data/registry/test/components/previews/ui/carousel_preview.rb +22 -0
  100. data/registry/test/components/previews/ui/chart_preview/whole_dollars.html.erb +19 -0
  101. data/registry/test/components/previews/ui/chart_preview.rb +7 -1
  102. data/registry/test/components/previews/ui/checkbox_preview/in_form.html.erb +2 -3
  103. data/registry/test/components/previews/ui/fieldset_preview/block_of_fields.html.erb +18 -0
  104. data/registry/test/components/previews/ui/fieldset_preview/groups.html.erb +14 -0
  105. data/registry/test/components/previews/ui/fieldset_preview/model_errors.html.erb +22 -0
  106. data/registry/test/components/previews/ui/fieldset_preview/playground.html.erb +5 -0
  107. data/registry/test/components/previews/ui/fieldset_preview.rb +24 -0
  108. data/registry/test/components/previews/ui/gallery_preview/grid.html.erb +25 -0
  109. data/registry/test/components/previews/ui/gallery_preview.rb +3 -0
  110. data/registry/test/components/previews/ui/scrubber_preview/default.html.erb +24 -0
  111. data/registry/test/components/previews/ui/scrubber_preview.rb +6 -0
  112. data/registry/test/components/previews/ui/selection_bar_preview/floating.html.erb +16 -0
  113. data/registry/test/components/previews/ui/selection_bar_preview/table.html.erb +26 -0
  114. data/registry/test/components/previews/ui/selection_bar_preview.rb +9 -0
  115. data/registry/test/components/previews/ui/sidebar_preview/_shell.html.erb +5 -4
  116. data/registry/test/components/previews/ui/sidebar_preview/pill_links.html.erb +52 -0
  117. data/registry/test/components/previews/ui/sidebar_preview.rb +3 -0
  118. data/registry/test/components/previews/ui/table_preview/selectable.html.erb +26 -0
  119. data/registry/test/components/previews/ui/table_preview/sortable.html.erb +20 -0
  120. data/registry/test/components/previews/ui/table_preview/sticky_header.html.erb +13 -0
  121. data/registry/test/components/previews/ui/table_preview.rb +9 -0
  122. data/registry/test/components/ui/button_test.rb +2 -2
  123. data/registry/test/components/ui/card/action_test.rb +19 -0
  124. data/registry/test/components/ui/card/content_test.rb +1 -11
  125. data/registry/test/components/ui/card/footer_test.rb +3 -2
  126. data/registry/test/components/ui/card/header_test.rb +3 -4
  127. data/registry/test/components/ui/card/title_test.rb +3 -4
  128. data/registry/test/components/ui/card_test.rb +63 -2
  129. data/registry/test/components/ui/carousel_test.rb +38 -0
  130. data/registry/test/components/ui/chart_test.rb +20 -0
  131. data/registry/test/components/ui/checkbox_test.rb +11 -2
  132. data/registry/test/components/ui/fieldset_test.rb +119 -0
  133. data/registry/test/components/ui/gallery_test.rb +40 -5
  134. data/registry/test/components/ui/link_test.rb +1 -1
  135. data/registry/test/components/ui/number_input_test.rb +1 -1
  136. data/registry/test/components/ui/radio_group_test.rb +106 -0
  137. data/registry/test/components/ui/scrubber_test.rb +43 -0
  138. data/registry/test/components/ui/select_test.rb +21 -0
  139. data/registry/test/components/ui/selection_bar_test.rb +46 -0
  140. data/registry/test/components/ui/sidebar/menu_button_test.rb +9 -0
  141. data/registry/test/components/ui/sidebar_test.rb +79 -1
  142. data/registry/test/components/ui/switch_test.rb +4 -4
  143. data/registry/test/components/ui/toggle_group_test.rb +45 -0
  144. metadata +60 -5
  145. data/registry/test/components/previews/ui/card_preview/complete.html.erb +0 -31
  146. data/registry/test/components/previews/ui/card_preview/default.html.erb +0 -7
  147. data/registry/test/components/previews/ui/card_preview/with_footer.html.erb +0 -13
  148. data/registry/test/components/previews/ui/card_preview/with_header.html.erb +0 -11
@@ -6,8 +6,8 @@ class UI::Checkbox < UI::Base
6
6
  variants(
7
7
  base: "peer appearance-none shrink-0 border border-input bg-background " \
8
8
  "transition-all " \
9
- "checked:bg-primary checked:border-primary " \
10
- "focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-1 " \
9
+ "checked:bg-primary checked:border-primary indeterminate:bg-primary indeterminate:border-primary " \
10
+ "focus:outline-none focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-1 " \
11
11
  "ring-offset-background",
12
12
  variants: {
13
13
  size: {
@@ -113,14 +113,21 @@ class UI::Checkbox < UI::Base
113
113
  def classes
114
114
  cn(
115
115
  variant_classes(size: @size),
116
- { "border-destructive aria-invalid:focus:ring-destructive" => wrapper.error? },
116
+ { "border-destructive aria-invalid:focus-visible:ring-destructive" => wrapper.error? },
117
117
  @user_class
118
118
  )
119
119
  end
120
120
 
121
121
  def overlay_classes
122
122
  "pointer-events-none absolute inset-0 flex items-center justify-center " \
123
- "text-primary-foreground opacity-0 peer-checked:opacity-100 transition-opacity"
123
+ "text-primary-foreground opacity-0 peer-checked:opacity-100 peer-indeterminate:opacity-0 transition-opacity"
124
+ end
125
+
126
+ # A dash for a box that's partly checked, such as "select all" with some of
127
+ # the rows chosen. Only script sets that state, as `indeterminate`.
128
+ def mixed_overlay_classes
129
+ "pointer-events-none absolute inset-0 flex items-center justify-center " \
130
+ "text-primary-foreground opacity-0 peer-indeterminate:opacity-100 transition-opacity"
124
131
  end
125
132
 
126
133
  def wrapper_classes
@@ -0,0 +1,6 @@
1
+ <%= tag.fieldset(**fieldset_attrs) do %>
2
+ <% if legend? %><%= tag.legend(id: legend_id, class: legend_classes) do %><%= legend_text %><% if @required %><%= tag.span(" *", class: "text-destructive ml-0.5") %><% end %><% end %><% end %>
3
+ <% if hint.present? %><%= tag.p(hint, id: hint_id, class: "text-xs text-muted-foreground") %><% end %>
4
+ <%= content %>
5
+ <% if error_message.present? %><%= tag.p(error_message, id: error_id, class: "text-xs text-destructive") %><% end %>
6
+ <% end %>
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # Controls that belong together under one caption: a <fieldset> whose
6
+ # <legend> names the group, a hint under it and an error message for the
7
+ # group as a whole. Use it for a set of checkboxes, a block of settings or an
8
+ # address. RadioGroup and ToggleGroup render through it themselves when given
9
+ # `legend:`, `hint:` or `error:`.
10
+ #
11
+ # <%= render(UI::Fieldset.new(legend: "Notifications", hint: "Where we tell you about replies.")) do %>
12
+ # <%= render(UI::Checkbox.new(name: "notify[]", value: "email", label: "Email")) %>
13
+ # <%= render(UI::Checkbox.new(name: "notify[]", value: "push", label: "Push")) %>
14
+ # <% end %>
15
+ #
16
+ # `disabled: true` disables every control inside, natively, and the browser
17
+ # then leaves them out of the form's submission. `required: true` marks the
18
+ # legend only; set `required` on the controls that need it. `error:` takes a
19
+ # message, or `true` to mark the group invalid without one. Bound to a form
20
+ # (`form:` + `field:`), the model's errors on that attribute are the message
21
+ # and its human name is the legend (`legend:` overrides it, `legend: false`
22
+ # opts out), the way UI::FormField does it for one control.
23
+ class UI::Fieldset < UI::Base
24
+ def initialize(legend: nil, hint: nil, error: nil, required: false, disabled: false,
25
+ form: nil, field: nil, class: nil, **html_attrs)
26
+ @required = required
27
+ @disabled = disabled
28
+ @user_class = binding.local_variable_get(:class)
29
+ @html_attrs = html_attrs
30
+ # FormField already works out a label, hint and error, from a form or not;
31
+ # the legend is that label.
32
+ @details = UI::FormField.new(label: legend, hint: hint, error: error, form: form, field: field,
33
+ for: html_attrs[:id] || "ui-fieldset-#{SecureRandom.hex(4)}")
34
+ end
35
+
36
+ def id = @details.control_id
37
+ def legend_id = "#{id}-legend"
38
+ def hint_id = @details.hint_id
39
+ def error_id = @details.error_id
40
+
41
+ # Never an empty legend: it would name the group "".
42
+ def legend? = @details.render_label? && legend_text.present?
43
+ def legend_text = @details.label_text
44
+ def hint = @details.hint
45
+ def error_message = @details.error_message
46
+
47
+ # Whether there's anything to show around the controls. RadioGroup and
48
+ # ToggleGroup render bare without it.
49
+ def wrap? = legend? || hint.present? || error_message.present?
50
+
51
+ def fieldset_attrs
52
+ # A radiogroup takes aria-required and aria-invalid; a plain fieldset is a
53
+ # group, which takes neither.
54
+ radiogroup = @html_attrs[:role].to_s == "radiogroup"
55
+ user_aria = (@html_attrs[:aria] || {}).symbolize_keys
56
+ aria = {
57
+ labelledby: (legend_id if legend?),
58
+ required: ("true" if radiogroup && @required),
59
+ invalid: ("true" if radiogroup && @details.error?)
60
+ }.compact.merge(user_aria)
61
+ # Your own aria-describedby adds to the hint's and error's ids.
62
+ aria[:describedby] = [ @details.describedby, user_aria[:describedby] ].compact_blank.join(" ").presence
63
+ @html_attrs.except(:aria).merge(id: id, class: classes, disabled: @disabled, aria: aria.compact)
64
+ end
65
+
66
+ def classes
67
+ cn("flex min-w-0 flex-col gap-3", @user_class)
68
+ end
69
+
70
+ # The legend sits outside the fieldset's flex layout, so its own margin
71
+ # spaces it: close to a hint, further from the controls.
72
+ def legend_classes
73
+ cn("text-sm font-medium leading-snug", hint.present? ? "mb-1.5" : "mb-3",
74
+ { "text-muted-foreground" => @disabled })
75
+ end
76
+ end
@@ -3,9 +3,17 @@
3
3
  # One image in a UI::Gallery. Holds what the gallery needs to draw it in three
4
4
  # places (the main view, its thumbnail and the zoomed view); it draws nothing
5
5
  # itself.
6
+ #
7
+ # In the zoomed view, `with_info` is shown in a panel beside the image, opened
8
+ # with the Info button (when it was taken, where, the camera), and each
9
+ # `with_action` is a button in the top bar (favourite, download, delete), for
10
+ # this image alone.
6
11
  class UI::Gallery::Image < UI::Base
7
12
  attr_reader :src, :alt
8
13
 
14
+ renders_one :info
15
+ renders_many :actions
16
+
9
17
  def initialize(src:, alt:, zoom_src: nil, thumbnail_src: nil)
10
18
  @src = src
11
19
  @alt = alt
@@ -16,5 +24,10 @@ class UI::Gallery::Image < UI::Base
16
24
  def zoom_src = @zoom_src || @src
17
25
  def thumbnail_src = @thumbnail_src || @src
18
26
 
19
- def call = nil
27
+ # Runs the block given to `with_image`, which fills in the info and actions.
28
+ # The gallery draws them where they go.
29
+ def call
30
+ content
31
+ nil
32
+ end
20
33
  end
@@ -1,4 +1,6 @@
1
1
  <%= tag.div(**root_attrs) do %>
2
+ <% images.each(&:to_s) %>
3
+ <% if strip? %>
2
4
  <%= tag.div class: "relative min-w-0" do %>
3
5
  <%= tag.ul class: track_classes, data: { "ui--gallery-target": "track", action: "scroll->ui--gallery#scrolled pointerdown->ui--gallery#released" } do %>
4
6
  <% images.each_with_index do |image, index| %>
@@ -19,8 +21,15 @@
19
21
  <%= render(UI::Button.new(**nav_button_attrs(:next))) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %>
20
22
  <% end %>
21
23
  <% end %>
24
+ <% else %>
25
+ <%# What the overlay needs to know of each image; the page shows them its own way. %>
26
+ <%= tag.ul hidden: true do %>
27
+ <% images.each do |image| %><%= tag.li data: { "ui--gallery-target": "slide", zoom_src: asset_path(image.zoom_src), alt: image.alt } %><% end %>
28
+ <% end %>
29
+ <%= content %>
30
+ <% end %>
22
31
  <%= tag.p position_text, class: "sr-only", aria: { live: "polite" }, data: { "ui--gallery-target": "status" } %>
23
- <% if multiple? %>
32
+ <% if multiple? && strip? %>
24
33
  <%= tag.ul class: thumbnail_strip_classes do %>
25
34
  <% images.each_with_index do |image, index| %>
26
35
  <li class="shrink-0"><%= tag.button(**thumbnail_attrs(index)) do %><%= render(UI::Image.new(src: image.thumbnail_src, alt: "", rounded: :none, class: "size-full object-cover", draggable: "false")) %><% end %></li>
@@ -30,7 +39,7 @@
30
39
  <%= tag.dialog class: dialog_classes, tabindex: -1,
31
40
  aria: { label: t("ui.gallery.dialog", default: "Zoomed image") },
32
41
  data: { "ui--gallery-target": "dialog", action: "close->ui--gallery#closed cancel->ui--gallery#cancel" } do %>
33
- <%= tag.div class: viewport_classes,
42
+ <%= tag.div class: cn(viewport_classes, "sm:group-data-[info]/dialog:end-80"),
34
43
  data: { "ui--gallery-target": "viewport",
35
44
  action: "click->ui--gallery#viewportClick pointerdown->ui--gallery#panStart pointermove->ui--gallery#panMove " \
36
45
  "pointerup->ui--gallery#panEnd pointercancel->ui--gallery#panEnd wheel->ui--gallery#wheel" } do %>
@@ -43,7 +52,27 @@
43
52
  <%= render(UI::Button.new(**zoom_button_attrs(:out, "Zoom out"))) do %><%= render(UI::Icon.new(name: "zoom-out", size: :sm)) %><% end %>
44
53
  <%= render(UI::Button.new(**zoom_button_attrs(:in, "Zoom in"))) do %><%= render(UI::Icon.new(name: "zoom-in", size: :sm)) %><% end %>
45
54
  <% end %>
46
- <%= render(UI::Button.new(variant: :ghost, size: :icon, class: cn(UI::Gallery::OVERLAY_BUTTON, "absolute end-4 top-4 z-10"), aria: { label: t("ui.gallery.close", default: "Close") }, data: { action: "ui--gallery#close" })) do %><%= render(UI::Icon.new(name: "x", size: :sm)) %><% end %>
55
+ <%= tag.div class: "absolute end-4 top-4 z-40 flex items-center gap-2" do %>
56
+ <% images.each_with_index do |image, index| %>
57
+ <% if image.actions? %>
58
+ <%= tag.div class: "flex items-center gap-2", hidden: !index.zero?, data: { "ui--gallery-target": "perImage", index: index } do %><% image.actions.each do |action| %><%= action %><% end %><% end %>
59
+ <% end %>
60
+ <% end %>
61
+ <% if info? %>
62
+ <%= render(UI::Button.new(**info_toggle_attrs)) do %><%= render(UI::Icon.new(name: "info", size: :sm)) %><% end %>
63
+ <% end %>
64
+ <%= render(UI::Button.new(variant: :ghost, size: :icon, class: UI::Gallery::OVERLAY_BUTTON, aria: { label: t("ui.gallery.close", default: "Close") }, data: { action: "ui--gallery#close" })) do %><%= render(UI::Icon.new(name: "x", size: :sm)) %><% end %>
65
+ <% end %>
66
+ <% if info? %>
67
+ <%= tag.aside id: info_id, hidden: true, aria: { label: t("ui.gallery.info", default: "Info") },
68
+ class: "absolute inset-x-0 bottom-0 z-30 max-h-[55%] overflow-y-auto rounded-t-xl bg-background p-5 text-foreground shadow-xl " \
69
+ "sm:inset-x-auto sm:inset-y-0 sm:end-0 sm:max-h-none sm:w-80 sm:rounded-none sm:pt-20",
70
+ data: { "ui--gallery-target": "infoPanel" } do %>
71
+ <% images.each_with_index do |image, index| %>
72
+ <%= tag.div image.info, hidden: !index.zero?, data: { "ui--gallery-target": "perImage", index: index } %>
73
+ <% end %>
74
+ <% end %>
75
+ <% end %>
47
76
  <% if multiple? %>
48
77
  <%= render(UI::Button.new(**overlay_nav_attrs(:previous))) do %><%= render(UI::Icon.new(name: "chevron-left", size: :sm, class: "rtl:rotate-180")) %><% end %>
49
78
  <%= render(UI::Button.new(**overlay_nav_attrs(:next))) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %>
@@ -31,6 +31,20 @@
31
31
  # `aspect:` is :square, :landscape or :portrait, and `fit:` is :contain (the whole
32
32
  # photo) or :cover (fills the frame, cropping). A single image gets no thumbnails
33
33
  # or arrows.
34
+ #
35
+ # `strip: false` leaves out the large image and thumbnails, for a page that
36
+ # shows the images its own way, such as a grid of photos. Anything made with
37
+ # `gallery.trigger(index)` opens that image in the overlay, and previous and
38
+ # next go through all of them:
39
+ #
40
+ # <%= render(UI::Gallery.new(strip: false, label: "Photos")) do |gallery| %>
41
+ # <% @photos.each { |photo| gallery.with_image(src: photo.url, alt: photo.alt) } %>
42
+ # <div class="grid grid-cols-4 gap-1">
43
+ # <% @photos.each_with_index do |photo, index| %>
44
+ # <%= gallery.trigger(index, class: "aspect-square") { image_tag(photo.thumb_url, alt: "") } %>
45
+ # <% end %>
46
+ # </div>
47
+ # <% end %>
34
48
  class UI::Gallery < UI::Base
35
49
  ASPECTS = { square: "aspect-square", landscape: "aspect-video", portrait: "aspect-[4/5]" }.freeze
36
50
  FITS = { contain: "object-contain", cover: "object-cover" }.freeze
@@ -45,12 +59,13 @@ class UI::Gallery < UI::Base
45
59
  # Always light on the dark backdrop, whatever the page's theme, ring included.
46
60
  # The border keeps them in sight on a dark page, where the backdrop is black too.
47
61
  OVERLAY_BUTTON = "rounded-full border border-white/25 bg-black/60 text-white hover:bg-black/80 hover:text-white " \
48
- "focus:ring-white disabled:bg-black/60! disabled:text-white/50!"
62
+ "focus-visible:ring-white disabled:bg-black/60! disabled:text-white/50!"
49
63
 
50
64
  renders_many :images, UI::Gallery::Image
51
65
 
52
- def initialize(label: nil, aspect: :square, fit: :contain, zoom: :overlay, zoom_scale: 2.5, class: nil, **html_attrs)
66
+ def initialize(label: nil, aspect: :square, fit: :contain, zoom: :overlay, zoom_scale: 2.5, strip: true, class: nil, **html_attrs)
53
67
  @label = label
68
+ @strip = strip
54
69
  @aspect = ASPECTS.key?(aspect.to_sym) ? aspect.to_sym : :square
55
70
  @fit = FITS.key?(fit.to_sym) ? fit.to_sym : :contain
56
71
  @zoom = ZOOMS.include?(zoom.to_sym) ? zoom.to_sym : :overlay
@@ -61,12 +76,36 @@ class UI::Gallery < UI::Base
61
76
 
62
77
  def render? = images.any?
63
78
  def multiple? = images.size > 1
64
- def hover_zoom? = @zoom == :hover
79
+ def strip? = @strip
80
+ def hover_zoom? = @zoom == :hover && strip?
81
+ def info? = images.any?(&:info?)
82
+
83
+ # A button that opens image `index` in the overlay. Its own image, if it has
84
+ # one, is where the overlay grows from and shrinks back to.
85
+ def trigger(index, class: nil, **html_attrs, &block)
86
+ image = images[index]
87
+ tag.button(type: "button", **html_attrs,
88
+ class: cn("group/trigger relative block cursor-zoom-in overflow-hidden focus:outline-none focus-visible:ring-2 " \
89
+ "focus-visible:ring-primary focus-visible:ring-offset-2 ring-offset-background",
90
+ binding.local_variable_get(:class)),
91
+ aria: { label: phrase(t("ui.gallery.open", default: "Open %{alt}"), alt: image&.alt), haspopup: "dialog" },
92
+ data: { action: "ui--gallery#open", "ui--gallery-index-param": index, "ui--gallery-target": "trigger" }) do
93
+ capture(&block) if block
94
+ end
95
+ end
96
+
97
+ def info_toggle_attrs
98
+ { variant: :ghost, size: :icon, class: cn(OVERLAY_BUTTON, "aria-expanded:bg-white aria-expanded:text-black aria-expanded:hover:bg-white/90"),
99
+ aria: { label: t("ui.gallery.info", default: "Info"), expanded: "false", controls: info_id },
100
+ data: { action: "ui--gallery#toggleInfo", "ui--gallery-target": "infoToggle" } }
101
+ end
102
+
103
+ def info_id = @info_id ||= "gallery-info-#{SecureRandom.hex(4)}"
65
104
 
66
105
  def root_attrs
67
106
  attrs = @html_attrs.except(:data, :aria)
68
107
  attrs[:role] = "group"
69
- attrs[:class] = cn("flex w-full min-w-0 flex-col gap-3", @user_class)
108
+ attrs[:class] = cn(strip? ? "flex w-full min-w-0 flex-col gap-3" : "block min-w-0", @user_class)
70
109
  attrs[:aria] = { label: label, roledescription: t("ui.gallery.roledescription", default: "gallery") }
71
110
  .merge(@html_attrs[:aria] || {})
72
111
  attrs[:data] = { slot: "gallery", controller: "ui--gallery", zoom: @zoom,
@@ -87,7 +126,7 @@ class UI::Gallery < UI::Base
87
126
  # One tab stop for the strip: the image showing. The rest are out of sight.
88
127
  { type: "button", tabindex: index.zero? ? 0 : -1,
89
128
  class: cn("group/slide relative block size-full rounded-lg transition-all focus:outline-none",
90
- "focus:ring-2 focus:ring-inset focus:ring-primary", hover_zoom? ? "cursor-crosshair" : "cursor-zoom-in"),
129
+ "focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-primary", hover_zoom? ? "cursor-crosshair" : "cursor-zoom-in"),
91
130
  aria: { label: phrase(t("ui.gallery.zoom", default: "Zoom image %{current}: %{alt}"), current: index + 1, alt: image.alt),
92
131
  haspopup: "dialog" },
93
132
  data: { action: slide_action, "ui--gallery-index-param": index } }
@@ -96,7 +135,7 @@ class UI::Gallery < UI::Base
96
135
  # Covers the window with nothing of its own: only the image and its buttons sit
97
136
  # on the backdrop.
98
137
  def dialog_classes
99
- "m-0 size-full max-h-none max-w-none overflow-hidden bg-transparent p-0 outline-none backdrop:bg-black/80"
138
+ "group/dialog m-0 size-full max-h-none max-w-none overflow-hidden bg-transparent p-0 outline-none backdrop:bg-black/80"
100
139
  end
101
140
 
102
141
  def viewport_classes
@@ -129,7 +168,7 @@ class UI::Gallery < UI::Base
129
168
  end
130
169
 
131
170
  def overlay_nav_attrs(direction)
132
- edge = direction == :previous ? "start-4" : "end-4"
171
+ edge = direction == :previous ? "start-4" : "end-4 sm:group-data-[info]/dialog:end-84"
133
172
  { variant: :ghost, size: :icon,
134
173
  class: cn(OVERLAY_BUTTON, "absolute top-1/2 z-10 -translate-y-1/2", edge),
135
174
  aria: { label: nav_label(direction) }, data: { action: "ui--gallery##{direction}" } }
@@ -14,7 +14,7 @@ class UI::Link < UI::Base
14
14
  variant: {
15
15
  link: "text-primary underline-offset-4 hover:underline",
16
16
  default: "bg-primary text-primary-foreground hover:bg-primary/90",
17
- destructive: "bg-destructive text-white hover:bg-destructive/90 focus:ring-destructive",
17
+ destructive: "bg-destructive text-white hover:bg-destructive/90 focus-visible:ring-destructive",
18
18
  outline: "border border-input bg-background hover:bg-accent hover:text-accent-foreground",
19
19
  secondary: "bg-secondary text-secondary-foreground hover:bg-secondary/80",
20
20
  ghost: "hover:bg-accent hover:text-accent-foreground"
@@ -5,7 +5,7 @@ class UI::Menu::Item < UI::Base
5
5
  base:
6
6
  "relative flex cursor-default select-none items-center gap-2 rounded w-full px-2 py-1 text-sm whitespace-nowrap border-0 bg-transparent outline-none transition-colors " \
7
7
  "hover:bg-accent hover:text-accent-foreground " \
8
- "focus:bg-accent focus:text-accent-foreground focus:ring-2 focus:ring-ring focus:ring-offset-1 " \
8
+ "focus:bg-accent focus:text-accent-foreground focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 " \
9
9
  "aria-checked:bg-accent aria-checked:text-accent-foreground aria-checked:font-medium " \
10
10
  "data-[disabled]:pointer-events-none data-[disabled]:opacity-50 transition-all",
11
11
  variants: {
@@ -6,7 +6,7 @@
6
6
  <%= tag.span class: dot_size_class + " rounded-full bg-primary-foreground" %>
7
7
  <% end %>
8
8
  <% end %>
9
- <%= tag.span class: label_text_classes do %><%= label %><% if required? %><%= tag.span(" *", class: "text-destructive ml-0.5") %><% end %><% end %>
9
+ <%= tag.span class: label_text_classes do %><%= label %><% if required_mark? %><%= tag.span(" *", class: "text-destructive ml-0.5") %><% end %><% end %>
10
10
  <% end %>
11
11
  <% else %>
12
12
  <%= tag.span class: "relative inline-flex" do %>
@@ -5,7 +5,7 @@ class UI::RadioGroup::Item < UI::Base
5
5
  base: "peer appearance-none shrink-0 rounded-full border border-input bg-background " \
6
6
  "transition-all " \
7
7
  "checked:bg-primary checked:border-primary " \
8
- "focus:outline-none focus:ring-2 focus:ring-primary focus:ring-offset-1 " \
8
+ "focus:outline-none focus-visible:ring-2 focus-visible:ring-primary focus-visible:ring-offset-1 " \
9
9
  "ring-offset-background",
10
10
  variants: {
11
11
  size: {
@@ -38,6 +38,9 @@ class UI::RadioGroup::Item < UI::Base
38
38
  def disabled?; @disabled; end
39
39
  def required?; @group_required; end
40
40
 
41
+ # A legend marks the whole group required, so its items don't repeat it.
42
+ def required_mark?; @group_required && !@group_legend; end
43
+
41
44
  def input_options
42
45
  opts = @html_attrs.dup
43
46
  opts[:type] = :radio
@@ -87,6 +90,7 @@ class UI::RadioGroup::Item < UI::Base
87
90
  @group_name = group.name
88
91
  @group_value = group.value
89
92
  @group_required = group.required?
93
+ @group_legend = group.fieldset.legend?
90
94
  @group_disabled = group.disabled?
91
95
  @size ||= group.size
92
96
  end
@@ -1 +1 @@
1
- <%= tag.div role: "radiogroup", id: group_id, class: classes, **@html_attrs.except(:id) do %><% items.each do |item| %><%= render item %><% end %><% end %>
1
+ <% group = capture do %><%= tag.div(**group_attrs) do %><% items.each do |item| %><%= render item %><% end %><% end %><% end %><% if fieldset.wrap? %><%= render(fieldset) { group } %><% else %><%= group %><% end %>
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # A set of radio buttons. Give it a `legend:` (and a `hint:` or `error:`) and
4
+ # it renders through UI::Fieldset: a <fieldset role="radiogroup"> named by its
5
+ # legend, around the radios. Bind it to a form builder with `form:` and
6
+ # `field:`, and the model's errors on that attribute show on their own;
7
+ # `legend: true` then uses the attribute's name.
3
8
  class UI::RadioGroup < UI::Base
4
9
  renders_many :items, ->(value:, **opts) {
5
10
  item = UI::RadioGroup::Item.new(value: value, **opts)
@@ -9,12 +14,19 @@ class UI::RadioGroup < UI::Base
9
14
 
10
15
  def initialize(name: nil, value: nil, size: :default,
11
16
  required: false, disabled: false,
17
+ legend: nil, hint: nil, error: nil,
18
+ form: nil, field: nil,
12
19
  class: nil, **html_attrs)
13
20
  @name = name
14
21
  @value = value
15
22
  @size = size
16
23
  @required = required
17
24
  @disabled = disabled
25
+ @legend = legend
26
+ @hint = hint
27
+ @error = error
28
+ @form = form
29
+ @field = field
18
30
  @user_class = binding.local_variable_get(:class)
19
31
  @html_attrs = html_attrs
20
32
  end
@@ -27,9 +39,37 @@ class UI::RadioGroup < UI::Base
27
39
  @html_attrs[:id] || "ui-rg-#{SecureRandom.hex(4)}"
28
40
  end
29
41
 
42
+ # The legend shows only when given: a bound group that's never been named
43
+ # doesn't grow one when it has an error.
44
+ def fieldset
45
+ @fieldset ||= UI::Fieldset.new(legend: @legend.presence || false, hint: @hint, error: @error,
46
+ form: @form, field: @field,
47
+ required: @required, disabled: @disabled,
48
+ role: "radiogroup", aria: @html_attrs[:aria])
49
+ end
50
+
51
+ # Inside a fieldset, the fieldset has the role and the aria attributes that
52
+ # name and describe the group; the radios' container keeps the rest.
53
+ def group_attrs
54
+ wrapped = fieldset.wrap?
55
+ attrs = wrapped ? @html_attrs.except(:id, :aria) : @html_attrs.except(:id)
56
+ { role: ("radiogroup" unless wrapped), id: group_id, class: classes, **attrs }
57
+ end
58
+
59
+ def form_field? = @form.present? && @field.present?
60
+
30
61
  def required?; @required; end
31
62
  def disabled?; @disabled; end
32
- def name; @name; end
33
- def value; @value; end
34
63
  def size; @size; end
64
+
65
+ def name
66
+ form_field? ? "#{@form.object_name}[#{@field}]" : @name
67
+ end
68
+
69
+ # As a string, since each item compares it with its own value as one.
70
+ def value
71
+ value = @value
72
+ value = @form.object.public_send(@field) if value.nil? && form_field? && @form.object.respond_to?(@field)
73
+ value&.to_s
74
+ end
35
75
  end
@@ -0,0 +1,18 @@
1
+ <%= tag.nav(**root_attrs) do %>
2
+ <%= tag.div class: "absolute inset-y-3 inset-x-0 cursor-ns-resize",
3
+ data: { "ui--scrubber-target": "rail",
4
+ action: "pointerdown->ui--scrubber#grab pointermove->ui--scrubber#point pointerup->ui--scrubber#release " \
5
+ "pointercancel->ui--scrubber#release pointerleave->ui--scrubber#leave" } do %>
6
+ <ol>
7
+ <% marks.each_with_index do |mark, index| %>
8
+ <li><%= tag.a(**mark_attrs(mark, index)) do %><% if mark.major %><%= mark.major %><% else %><span class="size-1 rounded-full bg-muted-foreground/40"></span><% end %><% end %></li>
9
+ <% end %>
10
+ </ol>
11
+ <%= tag.div aria: { hidden: true }, data: { "ui--scrubber-target": "thumb" },
12
+ class: "pointer-events-none absolute end-0 top-0 h-0.5 w-7 -translate-y-1/2 rounded-full bg-primary invisible group-data-[ready]/scrubber:visible" %>
13
+ <%= tag.div aria: { hidden: true }, data: { "ui--scrubber-target": "tip" },
14
+ class: "pointer-events-none absolute end-full top-0 me-1 -translate-y-1/2 whitespace-nowrap rounded-md border bg-popover px-2 py-1 " \
15
+ "text-xs font-medium text-popover-foreground shadow-md invisible opacity-0 transition-opacity duration-100 " \
16
+ "data-[shown]:visible data-[shown]:opacity-100 motion-reduce:transition-none" %>
17
+ <% end %>
18
+ <% end %>
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A rail down the side of a long list for jumping through it by date: the
4
+ # years marked on it, a dot for each month, and the date under the pointer.
5
+ #
6
+ # <%= render(UI::Scrubber.new(label: "Jump to a date", for: "library", class: "fixed inset-y-16 end-0")) do |scrubber| %>
7
+ # <% @months.each do |month| %>
8
+ # <% scrubber.with_mark(href: "##{month.id}", label: month.name, major: (month.year if month.january?)) %>
9
+ # <% end %>
10
+ # <% end %>
11
+ #
12
+ # Each mark is a link to a heading in the list (`href: "#march-2026"`), placed
13
+ # on the rail as far down as that heading is in the list, so a busy month takes
14
+ # more of the rail. `major:` is the text shown beside the mark, such as the
15
+ # year; the others are dots. A line follows how far down the list is.
16
+ #
17
+ # Dragging along the rail scrolls the list with it, the way a scrollbar does,
18
+ # with the date under the pointer beside it. From the keyboard it's one stop:
19
+ # ↑ and ↓ move between the marks, Home and End to the ends, and Enter goes
20
+ # there. `for:` is the id of the element that scrolls, or the window when
21
+ # it's left out.
22
+ class UI::Scrubber < UI::Base
23
+ Mark = Struct.new(:href, :label, :major, keyword_init: true)
24
+
25
+ def initialize(label:, for: nil, class: nil, **html_attrs)
26
+ @label = label
27
+ @for = binding.local_variable_get(:for)
28
+ @user_class = binding.local_variable_get(:class)
29
+ @html_attrs = html_attrs
30
+ @marks = []
31
+ end
32
+
33
+ attr_reader :marks
34
+
35
+ def with_mark(href:, label:, major: nil)
36
+ @marks << Mark.new(href: href, label: label, major: major)
37
+ nil
38
+ end
39
+
40
+ def before_render = content
41
+
42
+ def render? = true
43
+
44
+ def root_attrs
45
+ attrs = @html_attrs.except(:data, :aria)
46
+ attrs[:class] = cn("group/scrubber relative w-14 touch-none select-none", @user_class)
47
+ attrs[:aria] = { label: @label }.merge(@html_attrs[:aria] || {})
48
+ attrs[:data] = { slot: "scrubber", controller: "ui--scrubber", "ui--scrubber-for-value": @for,
49
+ action: "keydown->ui--scrubber#keydown resize@window->ui--scrubber#layout" }
50
+ .merge(@html_attrs[:data] || {})
51
+ attrs
52
+ end
53
+
54
+ def mark_attrs(mark, index)
55
+ { href: mark.href, tabindex: index.zero? ? 0 : -1,
56
+ class: cn("absolute end-0 top-0 flex h-4 -translate-y-1/2 items-center justify-end rounded-sm pe-2 outline-none " \
57
+ "invisible group-data-[ready]/scrubber:visible focus-visible:ring-2 focus-visible:ring-primary",
58
+ mark.major ? "min-w-8 text-[11px] font-medium text-muted-foreground" : "w-4"),
59
+ aria: { label: mark.label },
60
+ data: { "ui--scrubber-target": "mark", label: mark.label, major: mark.major.present? } }
61
+ end
62
+ end
@@ -31,7 +31,7 @@
31
31
  <% end %>
32
32
 
33
33
  <%= tag.div(id: listbox_id, role: "listbox", tabindex: -1,
34
- aria: { labelledby: (render_label? ? trigger_id : nil), multiselectable: (multiple? ? "true" : nil) },
34
+ aria: { labelledby: (named? ? trigger_id : nil), multiselectable: (multiple? ? "true" : nil) },
35
35
  data: { "ui--select-target": "listbox", action: "mousedown->ui--select#keepFocus" },
36
36
  class: "max-h-64 overflow-y-auto p-1") do %>
37
37
  <% listbox_options.each_with_index do |(label, value), index| %>
@@ -81,6 +81,7 @@ class UI::Select < UI::Base
81
81
  @form = form
82
82
  @field = field
83
83
  @user_class = binding.local_variable_get(:class)
84
+ @name_attrs = take_accessible_name(html_attrs)
84
85
  @html_attrs = html_attrs
85
86
  @uid = SecureRandom.hex(4)
86
87
  @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
@@ -115,7 +116,7 @@ class UI::Select < UI::Base
115
116
  class: trigger_classes,
116
117
  role: "combobox",
117
118
  disabled: @disabled,
118
- aria: { haspopup: "listbox", expanded: "false", controls: listbox_id,
119
+ aria: { haspopup: "listbox", expanded: "false", controls: listbox_id, **@name_attrs,
119
120
  invalid: ("true" if wrapper.error?), describedby: wrapper.describedby },
120
121
  data: { "ui--select-target": "trigger",
121
122
  action: "click->ui--select#toggle keydown->ui--select#keydown" }
@@ -197,7 +198,21 @@ class UI::Select < UI::Base
197
198
  attrs
198
199
  end
199
200
 
201
+ # Whether the button has a name for the list to borrow.
202
+ def named? = render_label? || @name_attrs.any?
203
+
200
204
  def native_choices
201
205
  listbox_options.map { |label, value| [ label, value ] }
202
206
  end
207
+
208
+ private
209
+
210
+ # `aria-label` or `aria-labelledby` names what people reach, the visible
211
+ # button. Left in html_attrs it would go on the native select, which is
212
+ # hidden from screen readers, and the button would have no name at all.
213
+ def take_accessible_name(attrs)
214
+ aria = attrs[:aria] = (attrs[:aria] || {}).dup
215
+ { label: attrs.delete(:"aria-label") || aria.delete(:label),
216
+ labelledby: attrs.delete(:"aria-labelledby") || aria.delete(:labelledby) }.compact
217
+ end
203
218
  end
@@ -0,0 +1,7 @@
1
+ <%= tag.div(**root_attrs) do %>
2
+ <%= render(UI::Button.new(**clear_attrs)) do %><%= render(UI::Icon.new(name: "x", size: :sm)) %><% end %>
3
+ <%= tag.p class: "min-w-0 flex-1 truncate text-sm font-medium tabular-nums", aria: { live: "polite" }, data: { "ui--selection-bar-target": "count" } %>
4
+ <% if actions? %>
5
+ <div class="flex shrink-0 items-center gap-1"><% actions.each do |action| %><%= action %><% end %></div>
6
+ <% end %>
7
+ <% end %>