velvetkit 0.4.2 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +149 -2
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/carousel.html.erb +7 -0
  5. data/registry/app/components/ui/carousel.rb +60 -0
  6. data/registry/app/components/ui/chart.html.erb +3 -3
  7. data/registry/app/components/ui/chart.rb +18 -10
  8. data/registry/app/components/ui/checkbox.html.erb +6 -0
  9. data/registry/app/components/ui/checkbox.rb +9 -2
  10. data/registry/app/components/ui/fieldset.html.erb +6 -0
  11. data/registry/app/components/ui/fieldset.rb +76 -0
  12. data/registry/app/components/ui/gallery/image.rb +14 -1
  13. data/registry/app/components/ui/gallery.html.erb +32 -3
  14. data/registry/app/components/ui/gallery.rb +44 -5
  15. data/registry/app/components/ui/radio_group/item.html.erb +1 -1
  16. data/registry/app/components/ui/radio_group/item.rb +4 -0
  17. data/registry/app/components/ui/radio_group.html.erb +1 -1
  18. data/registry/app/components/ui/radio_group.rb +42 -2
  19. data/registry/app/components/ui/scrubber.html.erb +18 -0
  20. data/registry/app/components/ui/scrubber.rb +62 -0
  21. data/registry/app/components/ui/select.html.erb +1 -1
  22. data/registry/app/components/ui/select.rb +16 -1
  23. data/registry/app/components/ui/selection_bar.html.erb +7 -0
  24. data/registry/app/components/ui/selection_bar.rb +75 -0
  25. data/registry/app/components/ui/sidebar/inset.rb +5 -2
  26. data/registry/app/components/ui/sidebar/menu_button.rb +24 -9
  27. data/registry/app/components/ui/sidebar/menu_item.html.erb +1 -1
  28. data/registry/app/components/ui/sidebar/menu_item.rb +31 -5
  29. data/registry/app/components/ui/sidebar/menu_sub.rb +59 -0
  30. data/registry/app/components/ui/sidebar.html.erb +3 -3
  31. data/registry/app/components/ui/sidebar.rb +91 -13
  32. data/registry/app/components/ui/table/column.html.erb +1 -1
  33. data/registry/app/components/ui/table/column.rb +26 -2
  34. data/registry/app/components/ui/table/head.html.erb +1 -1
  35. data/registry/app/components/ui/table/head.rb +15 -2
  36. data/registry/app/components/ui/table/row.html.erb +1 -1
  37. data/registry/app/components/ui/table/row.rb +19 -5
  38. data/registry/app/components/ui/table.html.erb +2 -2
  39. data/registry/app/components/ui/table.rb +29 -4
  40. data/registry/app/components/ui/toggle_group.html.erb +1 -1
  41. data/registry/app/components/ui/toggle_group.rb +26 -3
  42. data/registry/app/javascript/controllers/ui/carousel_controller.ts +63 -0
  43. data/registry/app/javascript/controllers/ui/file_field_controller.ts +1 -1
  44. data/registry/app/javascript/controllers/ui/gallery_controller.ts +52 -14
  45. data/registry/app/javascript/controllers/ui/scrubber_controller.ts +188 -0
  46. data/registry/app/javascript/controllers/ui/selection_bar_controller.ts +122 -0
  47. data/registry/app/javascript/controllers/ui/sidebar_controller.ts +102 -32
  48. data/registry/components.yml +11 -3
  49. data/registry/config/locales/velvetkit.en.yml +16 -0
  50. data/registry/icons/lucide/arrow-down.svg +16 -0
  51. data/registry/icons/lucide/arrow-up.svg +16 -0
  52. data/registry/icons/lucide/image.svg +17 -0
  53. data/registry/icons/lucide/star.svg +15 -0
  54. data/registry/icons/phosphor/arrow-down.svg +2 -0
  55. data/registry/icons/phosphor/arrow-up.svg +2 -0
  56. data/registry/icons/phosphor/image.svg +2 -0
  57. data/registry/icons/phosphor/star.svg +2 -0
  58. data/registry/icons/tabler/arrow-down.svg +18 -0
  59. data/registry/icons/tabler/arrow-up.svg +18 -0
  60. data/registry/icons/tabler/photo.svg +19 -0
  61. data/registry/icons/tabler/star.svg +16 -0
  62. data/registry/icons.yml +4 -0
  63. data/registry/test/components/previews/ui/carousel_preview/highlights.html.erb +17 -0
  64. data/registry/test/components/previews/ui/carousel_preview.rb +22 -0
  65. data/registry/test/components/previews/ui/chart_preview/whole_dollars.html.erb +19 -0
  66. data/registry/test/components/previews/ui/chart_preview.rb +7 -1
  67. data/registry/test/components/previews/ui/checkbox_preview/in_form.html.erb +2 -3
  68. data/registry/test/components/previews/ui/fieldset_preview/block_of_fields.html.erb +18 -0
  69. data/registry/test/components/previews/ui/fieldset_preview/groups.html.erb +14 -0
  70. data/registry/test/components/previews/ui/fieldset_preview/model_errors.html.erb +22 -0
  71. data/registry/test/components/previews/ui/fieldset_preview/playground.html.erb +5 -0
  72. data/registry/test/components/previews/ui/fieldset_preview.rb +24 -0
  73. data/registry/test/components/previews/ui/gallery_preview/grid.html.erb +25 -0
  74. data/registry/test/components/previews/ui/gallery_preview.rb +3 -0
  75. data/registry/test/components/previews/ui/scrubber_preview/default.html.erb +24 -0
  76. data/registry/test/components/previews/ui/scrubber_preview.rb +6 -0
  77. data/registry/test/components/previews/ui/selection_bar_preview/floating.html.erb +16 -0
  78. data/registry/test/components/previews/ui/selection_bar_preview/table.html.erb +26 -0
  79. data/registry/test/components/previews/ui/selection_bar_preview.rb +9 -0
  80. data/registry/test/components/previews/ui/sidebar_preview/_shell.html.erb +5 -4
  81. data/registry/test/components/previews/ui/sidebar_preview/pill_links.html.erb +52 -0
  82. data/registry/test/components/previews/ui/sidebar_preview.rb +3 -0
  83. data/registry/test/components/previews/ui/table_preview/selectable.html.erb +26 -0
  84. data/registry/test/components/previews/ui/table_preview/sortable.html.erb +20 -0
  85. data/registry/test/components/previews/ui/table_preview/sticky_header.html.erb +13 -0
  86. data/registry/test/components/previews/ui/table_preview.rb +9 -0
  87. data/registry/test/components/ui/base_test.rb +27 -0
  88. data/registry/test/components/ui/carousel_test.rb +38 -0
  89. data/registry/test/components/ui/chart_test.rb +20 -0
  90. data/registry/test/components/ui/checkbox_test.rb +11 -2
  91. data/registry/test/components/ui/confirm_dialog_test.rb +3 -0
  92. data/registry/test/components/ui/fieldset_test.rb +119 -0
  93. data/registry/test/components/ui/gallery_test.rb +36 -1
  94. data/registry/test/components/ui/radio_group_test.rb +106 -0
  95. data/registry/test/components/ui/scrubber_test.rb +43 -0
  96. data/registry/test/components/ui/select_test.rb +21 -0
  97. data/registry/test/components/ui/selection_bar_test.rb +46 -0
  98. data/registry/test/components/ui/sidebar/menu_button_test.rb +9 -0
  99. data/registry/test/components/ui/sidebar_test.rb +79 -1
  100. data/registry/test/components/ui/toggle_group_test.rb +45 -0
  101. metadata +47 -1
@@ -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
@@ -49,8 +63,9 @@ class UI::Gallery < UI::Base
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:ring-2 " \
89
+ "focus:ring-primary focus: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,
@@ -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}" } }
@@ -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 %>
@@ -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 %>
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ # What to do with the things chosen on a page: "3 selected", a button to clear
4
+ # them, and the actions that work on all of them at once.
5
+ #
6
+ # <%= render(UI::SelectionBar.new(for: "customers")) do |bar| %>
7
+ # <% bar.with_action do %>
8
+ # <%= render(UI::Button.new(variant: :ghost, type: :submit, form: "bulk", formaction: export_customers_path)) { "Export" } %>
9
+ # <% end %>
10
+ # <% end %>
11
+ #
12
+ # `for:` is the id of what holds the checkboxes: a `UI::Table` with
13
+ # `selection:`, or any list whose boxes carry `data-selection="item"` (and one
14
+ # with `data-selection="all"` to choose them all). The bar shows once one is
15
+ # checked and hides when none are, keeps "select all" checked, unchecked or
16
+ # partly checked to match, and lets Shift-click check a run of them. Escape
17
+ # or the clear button unchecks them all.
18
+ #
19
+ # An "all" box inside an element with `data-selection-group` chooses only the
20
+ # items in it: a day's photos under that day's heading.
21
+ #
22
+ # The boxes post like any checkbox, so an action is a submit button for the
23
+ # form they belong to (`form:` and `formaction:` on the button, `form:` on the
24
+ # table). A `selection-bar:change` event carries the count and the values, for
25
+ # a page that does more.
26
+ #
27
+ # `placement:` is :top, a bar that sticks to the top of its scrolling area, or
28
+ # :floating, a rounded bar near the bottom of the window.
29
+ class UI::SelectionBar < UI::Base
30
+ PLACEMENTS = {
31
+ top: "sticky top-0 z-30 flex h-14 w-full items-center gap-2 border-b bg-background/95 px-2 backdrop-blur " \
32
+ "supports-[backdrop-filter]:bg-background/80 sm:px-4",
33
+ floating: "fixed inset-x-4 bottom-4 z-40 mx-auto flex w-auto max-w-xl items-center gap-2 rounded-full border " \
34
+ "bg-popover py-1.5 ps-1.5 pe-2 text-popover-foreground shadow-lg sm:inset-x-0 sm:w-fit"
35
+ }.freeze
36
+
37
+ renders_many :actions
38
+
39
+ def initialize(for:, placement: :top, label: nil, class: nil, **html_attrs)
40
+ @for = binding.local_variable_get(:for)
41
+ @placement = PLACEMENTS.key?(placement.to_sym) ? placement.to_sym : :top
42
+ @label = label
43
+ @user_class = binding.local_variable_get(:class)
44
+ @html_attrs = html_attrs
45
+ end
46
+
47
+ def root_attrs
48
+ attrs = @html_attrs.except(:data, :aria)
49
+ attrs[:role] = "region"
50
+ attrs[:hidden] = true
51
+ attrs[:class] = cn(PLACEMENTS[@placement], "[&[hidden]]:hidden", @user_class)
52
+ attrs[:aria] = { label: @label || t("ui.selection_bar.label", default: "Selection") }.merge(@html_attrs[:aria] || {})
53
+ attrs[:data] = { slot: "selection-bar", placement: @placement, controller: "ui--selection-bar",
54
+ "ui--selection-bar-for-value": @for,
55
+ "ui--selection-bar-one-value": t("ui.selection_bar.count.one", default: "%{count} selected"),
56
+ "ui--selection-bar-other-value": t("ui.selection_bar.count.other", default: "%{count} selected") }
57
+ .merge(@html_attrs[:data] || {})
58
+ attrs
59
+ end
60
+
61
+ # A checkbox the bar counts: `:item` for each thing to choose, `:all` for
62
+ # one that chooses them all. `form:` is the id of the form it posts with;
63
+ # `class:` restyles it, such as a round box over a photo.
64
+ def self.checkbox(kind, label:, name: nil, value: nil, form: nil, checked: false, **options)
65
+ attrs = { "aria-label": label, data: { selection: kind }, checked: checked, **options }
66
+ attrs["form"] = form if form # the HTML attribute; `form:` is a form builder to Checkbox
67
+ UI::Checkbox.new(name: name, value: value, unchecked_value: nil, **attrs)
68
+ end
69
+
70
+ def clear_attrs
71
+ { variant: :ghost, size: :icon, class: "shrink-0 rounded-full",
72
+ aria: { label: t("ui.selection_bar.clear", default: "Clear selection") },
73
+ data: { action: "ui--selection-bar#clear" } }
74
+ end
75
+ end
@@ -7,9 +7,12 @@ class UI::Sidebar::Inset < UI::Base
7
7
  @system_args[:data][:slot] = "sidebar-inset"
8
8
  end
9
9
 
10
+ # cn, not class_names: a class passed in replaces the one it clashes with
11
+ # (bg-transparent for bg-background, shadow-none for shadow-sm), rather than
12
+ # both landing on the element and CSS order picking one.
10
13
  def before_render
11
- @system_args[:class] = class_names(
12
- "bg-background relative flex w-full flex-1 flex-col",
14
+ @system_args[:class] = cn(
15
+ "bg-background relative flex w-full min-w-0 flex-1 flex-col",
13
16
  "md:peer-data-[variant=inset]:m-2 md:peer-data-[variant=inset]:ml-0 md:peer-data-[variant=inset]:rounded-xl md:peer-data-[variant=inset]:shadow-sm md:peer-data-[variant=inset]:peer-data-[state=collapsed]:ml-2",
14
17
  @system_args[:class]
15
18
  )
@@ -1,16 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class UI::Sidebar::MenuButton < ViewComponent::Base
4
- def initialize(path, active: false, current: nil, icon: nil, **system_args)
3
+ # A link in the sidebar. `active: true` marks the page being shown, for screen
4
+ # readers too (aria-current="page").
5
+ #
6
+ # `shape:` is :default, a compact row with a bar at the sidebar's edge for the
7
+ # current page, or :pill, a taller row whose current page is filled in edge to
8
+ # edge, rounded at both ends.
9
+ class UI::Sidebar::MenuButton < UI::Base
10
+ SHAPES = {
11
+ default: "h-8 rounded-md p-2 data-[active=true]:bg-sidebar-primary/10",
12
+ pill: "h-10 gap-3 rounded-full px-4 data-[active=true]:bg-sidebar-primary/15 group-data-[collapsible=icon]:rounded-full!"
13
+ }.freeze
14
+
15
+ def initialize(path, active: false, current: nil, shape: :default, icon: nil, **system_args) # rubocop:disable Lint/UnusedMethodArgument
5
16
  @path = path
6
17
  @is_active = active
7
-
18
+ @shape = SHAPES.key?(shape.to_sym) ? shape.to_sym : :default
8
19
  @system_args = system_args
9
20
 
10
21
  @system_args[:data] ||= {}
11
22
  @system_args[:data][:slot] = "sidebar-menu-button"
12
23
  @system_args[:data][:sidebar] = "menu-button"
13
24
  @system_args[:data][:active] = @is_active.to_s
25
+ @system_args[:data][:shape] = @shape
14
26
 
15
27
  # An explicit `current:` always wins; an active button falls back to "page"
16
28
  # so assistive tech announces it without the caller repeating themselves.
@@ -19,12 +31,15 @@ class UI::Sidebar::MenuButton < ViewComponent::Base
19
31
  @system_args[:aria] = (@system_args[:aria] || {}).merge(current: aria_current)
20
32
  end
21
33
 
22
- @system_args[:class] = class_names(
23
- "peer/menu-button flex w-full items-center gap-2 overflow-hidden rounded-md p-2 text-left text-sm outline-hidden ring-sidebar-ring transition-[width,height,padding] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus:ring-2 active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 group-has-data-[sidebar=menu-action]/menu-item:pr-8 aria-disabled:pointer-events-none aria-disabled:opacity-50 data-[active=true]:bg-sidebar-primary/10 data-[active=true]:font-semibold data-[active=true]:text-sidebar-primary data-[active=true]:[&>svg]:stroke-current data-[state=open]:hover:bg-sidebar-accent data-[state=open]:hover:text-sidebar-accent-foreground group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-2! [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
24
- "hover:bg-sidebar-accent hover:text-sidebar-accent-foreground",
25
- "h-8 text-sm",
26
- @system_args[:class],
27
- "bg-accent" => @is_active
34
+ @system_args[:class] = cn(
35
+ "peer/menu-button flex w-full items-center gap-2 overflow-hidden text-left text-sm outline-hidden ring-sidebar-ring " \
36
+ "transition-[width,height,padding] hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus:ring-2 " \
37
+ "active:bg-sidebar-accent active:text-sidebar-accent-foreground disabled:pointer-events-none disabled:opacity-50 " \
38
+ "group-has-data-[sidebar=menu-action]/menu-item:pe-8 aria-disabled:pointer-events-none aria-disabled:opacity-50 " \
39
+ "data-[active=true]:font-semibold data-[active=true]:text-sidebar-primary data-[active=true]:[&>svg]:stroke-current " \
40
+ "group-data-[collapsible=icon]:size-8! group-data-[collapsible=icon]:p-2! [&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0",
41
+ SHAPES[@shape],
42
+ @system_args[:class]
28
43
  )
29
44
  end
30
45
  end
@@ -1 +1 @@
1
- <%= tag.li(**@system_args) do %><%= content %><%= menu_button if menu_button %><% end %>
1
+ <%= tag.li(**@system_args) do %><%= content %><%= menu_button if menu_button %><% if sub_menu? %><% links = sub_menu_html %><%= tag.button(**toggle_attrs) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %><%= links %><% end %><% end %>
@@ -1,9 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # One entry in a sidebar menu: a `menu_button`, and optionally a `sub_menu` of
4
+ # links under it, such as a list of albums. The sub-menu has a button of its
5
+ # own to open and close it, beside the link, so the link still goes to its
6
+ # page. It starts open when `open:` says so, or when one of its links is the
7
+ # current page.
3
8
  class UI::Sidebar::MenuItem < UI::Base
4
9
  renders_one :menu_button, UI::Sidebar::MenuButton
10
+ renders_one :sub_menu, UI::Sidebar::MenuSub
5
11
 
6
- def initialize(**system_args)
12
+ def initialize(open: nil, **system_args)
13
+ @open = open
7
14
  @system_args = system_args
8
15
 
9
16
  @system_args[:data] ||= {}
@@ -12,12 +19,31 @@ class UI::Sidebar::MenuItem < UI::Base
12
19
 
13
20
  @system_args[:class] = class_names(
14
21
  "group/menu-item relative",
15
- # Active indicator: a primary bar flush with the sidebar edge.
16
- "before:absolute before:-left-2 before:top-1/2 before:h-6 before:w-1 " \
17
- "before:-translate-y-1/2 before:rounded-r-full before:bg-sidebar-primary " \
22
+ # Active indicator: a primary bar flush with the sidebar edge, for the
23
+ # default shape only; a pill is filled in instead.
24
+ "before:absolute before:-start-2 before:top-4 before:h-6 before:w-1 " \
25
+ "before:-translate-y-1/2 before:rounded-e-full before:bg-sidebar-primary " \
18
26
  "before:opacity-0 before:transition-opacity " \
19
- "has-[[data-active=true]]:before:opacity-100",
27
+ "has-[>[data-active=true][data-shape=default]]:before:opacity-100",
20
28
  @system_args[:class]
21
29
  )
22
30
  end
31
+
32
+ def sub_menu_id = @sub_menu_id ||= "sidebar-sub-#{SecureRandom.hex(4)}"
33
+
34
+ # Drawn first, so it knows whether one of its links is the current page.
35
+ def sub_menu_html
36
+ sub_menu.configure(id: sub_menu_id, open: @open)
37
+ sub_menu.to_s
38
+ end
39
+
40
+ def toggle_attrs
41
+ { type: "button", data: { sidebar: "menu-action", action: "ui--sidebar#toggleSub" },
42
+ aria: { expanded: sub_menu.open?.to_s, controls: sub_menu_id,
43
+ label: t("ui.sidebar.toggle_sub", name: sub_menu.name, default: "Show the links under %{name}") },
44
+ class: "absolute end-1 top-1 flex size-6 items-center justify-center rounded-md text-sidebar-foreground/70 outline-hidden " \
45
+ "ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus:ring-2 " \
46
+ "group-data-[collapsible=icon]:hidden group-has-[>[data-shape=pill]]/menu-item:top-2 " \
47
+ "[&[aria-expanded=true]>svg]:rotate-90 [&>svg]:transition-transform motion-reduce:[&>svg]:transition-none" }
48
+ end
23
49
  end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Links under a sidebar menu item, indented beneath it, such as the albums
4
+ # under Albums. `name:` is the item's name, for the button that opens them.
5
+ #
6
+ # <% item.with_sub_menu(name: "Albums") do |sub| %>
7
+ # <% sub.with_item(album_path(album), active: current_page?(album_path(album))) { album.name } %>
8
+ # <% end %>
9
+ class UI::Sidebar::MenuSub < UI::Base
10
+ Item = Struct.new(:path, :active, :content, keyword_init: true)
11
+
12
+ attr_reader :name
13
+
14
+ def initialize(name: nil, class: nil, **html_attrs)
15
+ @name = name
16
+ @user_class = binding.local_variable_get(:class)
17
+ @html_attrs = html_attrs
18
+ @items = []
19
+ end
20
+
21
+ def with_item(path, active: false, &block)
22
+ @items << Item.new(path: path, active: active, content: block)
23
+ nil
24
+ end
25
+
26
+ def active? = @items.any?(&:active)
27
+
28
+ # Set by the menu item before it draws this: the id its button points at,
29
+ # and `open:` as given to the item (nil to follow the current page).
30
+ def configure(id:, open:)
31
+ @id = id
32
+ @open = open
33
+ end
34
+
35
+ # Known once drawn, since the items are added as the block runs.
36
+ def open? = @open.nil? ? active? : @open
37
+
38
+ def call
39
+ content # runs the block, which adds the items
40
+ tag.ul(id: @id, hidden: !open?, data: { slot: "sidebar-menu-sub", sidebar: "menu-sub" },
41
+ class: cn("ms-5 flex min-w-0 flex-col gap-0.5 border-s border-sidebar-border ps-2.5 py-0.5 group-data-[collapsible=icon]:hidden",
42
+ @user_class), **@html_attrs) do
43
+ safe_join(@items.map { |item| tag.li(item_link(item)) })
44
+ end
45
+ end
46
+
47
+ private
48
+
49
+ def item_link(item)
50
+ link_to(item.path, aria: { current: ("page" if item.active) },
51
+ data: { active: item.active.to_s },
52
+ class: "flex h-7 min-w-0 items-center gap-2 rounded-md px-2 text-sm text-sidebar-foreground/80 outline-hidden " \
53
+ "ring-sidebar-ring hover:bg-sidebar-accent hover:text-sidebar-accent-foreground focus:ring-2 " \
54
+ "data-[active=true]:bg-sidebar-accent data-[active=true]:font-medium data-[active=true]:text-sidebar-accent-foreground " \
55
+ "[&>span:last-child]:truncate [&>svg]:size-4 [&>svg]:shrink-0") do
56
+ capture(&item.content)
57
+ end
58
+ end
59
+ end
@@ -1,7 +1,7 @@
1
1
  <%= tag.aside(**@system_args) do %>
2
- <div data-slot="sidebar-gap" class="relative w-(--sidebar-width) bg-transparent transition-[width] duration-200 ease-linear group-data-[collapsible=offcanvas]:w-0 group-data-[side=right]:rotate-180 group-data-[collapsible=icon]:w-(--sidebar-width-icon)"></div>
3
- <div data-slot="sidebar-container" class="fixed inset-y-0 z-10 h-svh w-(--sidebar-width) transition-[left,right,width] duration-200 ease-linear md:flex left-0 group-data-[collapsible=offcanvas]:left-[calc(var(--sidebar-width)*-1)] group-data-[collapsible=icon]:w-(--sidebar-width-icon) group-data-[side=left]:border-r group-data-[side=right]:border-l">
4
- <div data-sidebar="sidebar" data-slot="sidebar-inner" class="bg-sidebar group-data-[variant=floating]:border-sidebar-border flex h-full w-full flex-col group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:border group-data-[variant=floating]:shadow-sm">
2
+ <div data-slot="sidebar-backdrop" aria-hidden="true" class="<%= backdrop_classes %>" data-action="click->ui--sidebar#close"></div>
3
+ <div data-slot="sidebar-container" class="<%= container_classes %>" data-ui--sidebar-target="container" data-action="click->ui--sidebar#navigate">
4
+ <div data-sidebar="sidebar" data-slot="sidebar-inner" class="bg-sidebar flex h-full w-full flex-col overflow-hidden shadow-xl md:shadow-none group-data-[variant=floating]:border-sidebar-border group-data-[variant=floating]:rounded-lg group-data-[variant=floating]:border md:group-data-[variant=floating]:shadow-sm">
5
5
  <%= sidebar_header if sidebar_header %>
6
6
  <%= sidebar_content if sidebar_content %>
7
7
  <%= sidebar_footer if sidebar_footer %>