velvetkit 0.5.1 → 0.6.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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +44 -0
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/carousel.html.erb +9 -4
  5. data/registry/app/components/ui/carousel.rb +42 -1
  6. data/registry/app/components/ui/file_tree/file_item.html.erb +9 -0
  7. data/registry/app/components/ui/file_tree/file_item.rb +50 -0
  8. data/registry/app/components/ui/file_tree/folder.html.erb +9 -0
  9. data/registry/app/components/ui/file_tree/folder.rb +76 -0
  10. data/registry/app/components/ui/file_tree.html.erb +1 -0
  11. data/registry/app/components/ui/file_tree.rb +93 -0
  12. data/registry/app/components/ui/rating.html.erb +16 -0
  13. data/registry/app/components/ui/rating.rb +200 -0
  14. data/registry/app/components/ui/sidebar/menu_button.html.erb +10 -1
  15. data/registry/app/components/ui/sidebar/menu_button.rb +44 -6
  16. data/registry/app/components/ui/sidebar/menu_item.rb +4 -3
  17. data/registry/app/components/ui/sidebar/wrapper.html.erb +1 -1
  18. data/registry/app/components/ui/sidebar/wrapper.rb +45 -7
  19. data/registry/app/components/ui/sidebar.html.erb +10 -1
  20. data/registry/app/components/ui/sidebar.rb +60 -4
  21. data/registry/app/javascript/controllers/ui/carousel_controller.ts +59 -1
  22. data/registry/app/javascript/controllers/ui/file_tree_controller.ts +189 -0
  23. data/registry/app/javascript/controllers/ui/rating_controller.ts +117 -0
  24. data/registry/app/javascript/controllers/ui/sidebar_controller.ts +17 -0
  25. data/registry/app/javascript/controllers/ui/tooltip_controller.ts +3 -0
  26. data/registry/components.yml +4 -0
  27. data/registry/config/locales/velvetkit.en.yml +13 -0
  28. data/registry/test/components/previews/ui/carousel_preview/with_indicators.html.erb +10 -0
  29. data/registry/test/components/previews/ui/carousel_preview.rb +7 -3
  30. data/registry/test/components/previews/ui/file_tree_preview/changes.html.erb +13 -0
  31. data/registry/test/components/previews/ui/file_tree_preview/from_data.html.erb +14 -0
  32. data/registry/test/components/previews/ui/file_tree_preview/picking_a_file.html.erb +25 -0
  33. data/registry/test/components/previews/ui/file_tree_preview/without_guides.html.erb +10 -0
  34. data/registry/test/components/previews/ui/file_tree_preview.rb +41 -0
  35. data/registry/test/components/previews/ui/image_preview/radii.html.erb +3 -3
  36. data/registry/test/components/previews/ui/image_preview/sizing.html.erb +9 -4
  37. data/registry/test/components/previews/ui/image_preview.rb +14 -1
  38. data/registry/test/components/previews/ui/rating_preview/in_form.html.erb +6 -0
  39. data/registry/test/components/previews/ui/rating_preview/model_errors.html.erb +20 -0
  40. data/registry/test/components/previews/ui/rating_preview/playground.html.erb +5 -0
  41. data/registry/test/components/previews/ui/rating_preview/scores.html.erb +18 -0
  42. data/registry/test/components/previews/ui/rating_preview/sizes.html.erb +6 -0
  43. data/registry/test/components/previews/ui/rating_preview.rb +28 -0
  44. data/registry/test/components/previews/ui/sidebar_preview/_top_bar.html.erb +19 -0
  45. data/registry/test/components/previews/ui/sidebar_preview/icon_rail.html.erb +81 -0
  46. data/registry/test/components/previews/ui/sidebar_preview/site_header.html.erb +52 -0
  47. data/registry/test/components/previews/ui/sidebar_preview.rb +23 -0
  48. data/registry/test/components/ui/carousel_test.rb +48 -0
  49. data/registry/test/components/ui/file_tree_test.rb +187 -0
  50. data/registry/test/components/ui/rating_test.rb +271 -0
  51. data/registry/test/components/ui/sidebar/menu_button_test.rb +49 -0
  52. data/registry/test/components/ui/sidebar/wrapper_test.rb +30 -0
  53. data/registry/test/components/ui/sidebar_test.rb +50 -0
  54. metadata +28 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a598255d821f68fb0ab11f28b32e1f60aaf51ee752ac22f39aca95fc7e2e2fb
4
- data.tar.gz: 12c189b8e5e1f6f76b240ad79e8c63e4644668eb8881ca311f7fb35d0a349627
3
+ metadata.gz: daf46130dd01588b7563ec7950283f3fda2cd9d20f3429fe819b20ea46654d2b
4
+ data.tar.gz: 5c796994f8d99576842568b142d5b4277eade33cf5eda99dca896ac9d2a79702
5
5
  SHA512:
6
- metadata.gz: 9eb743aa4315865dfaa72f1727e826522c74ff7025c3370ba5e5514b06d0d179787454a85ac9bd1de1539527ef1947ab9ab186cea36a5bc58953ad87a315ee89
7
- data.tar.gz: be5fdad891a582556befbe4e5593f6a30cd2a847e984ad7b8b54d48075e7b6b4e4764bf40ec1567236eb37c7c1ebbfc4bfbfed3e861db197ae4248f73881fbdb
6
+ metadata.gz: cf57b163ca22ec31d9b6d875711a440a89e6998a80ff8681cd31249a9113158315c5fcfdc1b0afa72440173b73c2e2ff27307855577473413701f03ddffcd934
7
+ data.tar.gz: 75ba39a318fe15e179ac20f262014e1ecc7549a136c0d10fe95577089ca263deb6dc6f7f08cd29ba4e404469832efd53342f5baab5654ba4aac0249ba1df2e57
data/CHANGELOG.md CHANGED
@@ -2,6 +2,50 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 0.6.1 — 2026-11-30
6
+
7
+ Changes:
8
+
9
+ - `Sidebar`: two more layouts. A `header` on `Sidebar::Wrapper` stretches
10
+ across the top, on the sidebar's surface so the two frame the page, with
11
+ the sidebar and page below it; the sidebar sticks just under the header,
12
+ as tall as what's left, with no height to work out by hand
13
+ (`header_height:`, 3.5rem by default). And an icon rail with a panel of
14
+ pages beside it, both `nested:` sidebars inside the outer one, which
15
+ collapses to the rail and opens both as the phone's drawer. A rail button
16
+ with `section:` shows that section's panel straight away, with no page to
17
+ load, and opens the sidebar if it was collapsed. `MenuButton` has
18
+ `shape: :block`, a row whose current page is filled in as a rounded block
19
+ with no bar at the edge; `shape: :icon` for the rail, a square filled in
20
+ for the current section with its name as a tooltip; and `tooltip:` for any
21
+ shape. `Wrapper` takes `sidebar_width:` and keeps a `style:` of its own.
22
+ (#115)
23
+ - `Tooltip`: positioned `fixed`, so one inside a clipped box, such as a
24
+ sidebar, shows in full beside it. (#115)
25
+
26
+ ## 0.6.0 — 2026-11-23
27
+
28
+ New components:
29
+
30
+ - `FileTree`: folders and files in a tree that opens and closes, with arrow
31
+ keys to move through it.
32
+ - `Rating`: a score in stars, shown to the half star with its count, or chosen
33
+ with a radio for each star. A star grows under the pointer and pops when
34
+ chosen with the arrow keys; `show_value: true` shows the chosen score
35
+ beside a field, as "4/5", and `thanks: true` swaps the field's error for
36
+ "Thank you for your rating" once a score is chosen.
37
+
38
+ Changes:
39
+
40
+ - `Carousel`: indicators, with `indicators: true`: a dot for each place the
41
+ strip stops, the lit one following the scroll as you swipe, and a tap on
42
+ one scrolling to its card. They're off by default. (#102)
43
+ - Docs: the Image page's Radii and Sizing examples draw a photo that fills
44
+ its frame, so each radius and each width reads differently, and Radii adds
45
+ `xs`. (#105)
46
+ - Docs site: runs on Ruby 3.4.13, with `net-ssh` and `puma` updated, to clear
47
+ security advisories. (#103)
48
+
5
49
  ## 0.5.1 — 2026-11-15
6
50
 
7
51
  Changes:
@@ -4,5 +4,5 @@ module Velvetkit
4
4
  # Not the released version: the release workflow takes that from the tag
5
5
  # (v0.3.0 publishes 0.3.0) and writes it here on the runner only, so this
6
6
  # file can stay behind. See .github/workflows/release.yml.
7
- VERSION = "0.5.1"
7
+ VERSION = "0.6.1"
8
8
  end
@@ -1,7 +1,12 @@
1
1
  <%= tag.div(**root_attrs) do %>
2
- <%= tag.ul id: track_id, class: track_classes, data: { "ui--carousel-target": "track", action: "scroll->ui--carousel#update" } do %>
3
- <% items.each do |item| %><%= tag.li item, class: item_classes %><% end %>
2
+ <div class="relative">
3
+ <%= tag.ul id: track_id, class: track_classes, data: { "ui--carousel-target": "track", action: "scroll->ui--carousel#update" } do %>
4
+ <% items.each do |item| %><%= tag.li item, class: item_classes %><% end %>
5
+ <% end %>
6
+ <%= render(UI::Button.new(**button_attrs(:previous))) do %><%= render(UI::Icon.new(name: "chevron-left", size: :sm, class: "rtl:rotate-180")) %><% end %>
7
+ <%= render(UI::Button.new(**button_attrs(:next))) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %>
8
+ </div>
9
+ <% if indicators? %>
10
+ <%= tag.div(**indicators_attrs) do %><% items.each_index do |index| %><%= tag.button(**indicator_attrs(index)) do %><span aria-hidden="true" class="<%= UI::Carousel::INDICATOR_FILL %>"></span><% end %><% end %><% end %>
4
11
  <% end %>
5
- <%= render(UI::Button.new(**button_attrs(:previous))) do %><%= render(UI::Icon.new(name: "chevron-left", size: :sm, class: "rtl:rotate-180")) %><% end %>
6
- <%= render(UI::Button.new(**button_attrs(:next))) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %>
7
12
  <% end %>
@@ -13,13 +13,22 @@
13
13
  # and next buttons scroll a strip's width at a time, and each shows only while
14
14
  # there's more to see that way. `item_class:` sizes the cards ("w-64",
15
15
  # "basis-1/3"); they're as wide as their content otherwise.
16
+ #
17
+ # `indicators: true` adds a row of indicators under the strip, a dot for each
18
+ # place it can stop, with the one in view lit. Each indicator scrolls to its
19
+ # card. They're the way through on a phone, where the buttons don't show.
20
+ # Without JavaScript the row is left empty, as the indicators couldn't scroll.
21
+ # (`dots: true`, what 0.6.0 called it, still works.)
22
+ #
23
+ # <%= render(UI::Carousel.new(label: "Featured", item_class: "basis-full", indicators: true)) do |carousel| %>
16
24
  class UI::Carousel < UI::Base
17
25
  renders_many :items
18
26
 
19
- def initialize(label:, item_class: nil, gap: 3, class: nil, **html_attrs)
27
+ def initialize(label:, item_class: nil, gap: 3, indicators: false, dots: nil, class: nil, **html_attrs)
20
28
  @label = label
21
29
  @item_class = item_class
22
30
  @gap = gap
31
+ @indicators = dots.nil? ? indicators : dots
23
32
  @user_class = binding.local_variable_get(:class)
24
33
  @html_attrs = html_attrs
25
34
  end
@@ -56,5 +65,37 @@ class UI::Carousel < UI::Base
56
65
  data: { action: "ui--carousel##{direction}", "ui--carousel-target": direction } }
57
66
  end
58
67
 
68
+ def indicators? = @indicators
69
+
70
+ # The row keeps its height while the indicators start hidden, so the page
71
+ # doesn't move when the controller shows them.
72
+ def indicators_attrs
73
+ { role: "group", class: "mt-4 flex h-2 flex-wrap items-center justify-center gap-2",
74
+ aria: { label: t("ui.carousel.indicators", default: "Choose a slide") } }
75
+ end
76
+
77
+ # An indicator for each card; the controller hides those past the last place
78
+ # the strip can stop, as they'd all scroll to the end. The indicator is a
79
+ # small dot, so its ::after reaches across the gap and a finger's height to
80
+ # be easy to tap.
81
+ #
82
+ # How lit an indicator is follows the scroll, the way the dots under a
83
+ # phone's home screen do: the controller sets `--fill` from 0 to 1 as its
84
+ # card comes into place, and the dot widens into a pill as its colour fills
85
+ # in. So there's no transition on either, which would trail behind a finger.
86
+ #
87
+ # An indicator draws no focus ring: the lit one already shows which card is
88
+ # in view, and a ring round a dot this small swamps it.
89
+ def indicator_attrs(index)
90
+ { type: "button", hidden: true,
91
+ class: cn("relative h-2 w-[calc(0.5rem+1rem*var(--fill,0))] shrink-0 outline-none",
92
+ "cursor-pointer rounded-full bg-foreground/20 hover:bg-foreground/40",
93
+ "after:absolute after:-inset-x-1 after:-inset-y-2"),
94
+ aria: { label: t("ui.carousel.indicator", number: index + 1, default: "Go to slide %{number}"), controls: track_id },
95
+ data: { action: "ui--carousel#go", "ui--carousel-target": "indicator", "ui--carousel-index-param": index } }
96
+ end
97
+
98
+ INDICATOR_FILL = "absolute inset-0 rounded-full bg-primary opacity-[var(--fill,0)]"
99
+
59
100
  def track_id = @track_id ||= "carousel-#{SecureRandom.hex(4)}"
60
101
  end
@@ -0,0 +1,9 @@
1
+ <%= tag.li(role: "none") do %>
2
+ <%= content_tag(tag_name, **row_attrs) do %>
3
+ <%# Where a folder has its chevron, so file and folder icons line up. %>
4
+ <%= tag.span class: "size-4 shrink-0", aria: { hidden: true } %>
5
+ <%= render(UI::Icon.new(name: icon, size: :sm, class: "shrink-0 text-muted-foreground group-aria-selected/row:text-primary", aria: { hidden: true })) %>
6
+ <%= tag.span name, class: "min-w-0 flex-1 truncate" %>
7
+ <% if content? %><%= tag.span content, class: "shrink-0 text-xs font-normal text-muted-foreground" %><% end %>
8
+ <% end %>
9
+ <% end %>
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A file in a UI::FileTree, added with `with_file`. With `href:` it's a link,
4
+ # and `active: true` marks it as the page being shown (aria-current="page").
5
+ # Without one it's a row to pick: the tree selects it and dispatches
6
+ # `ui--file-tree:select` with its `value:` (its name if not given) and name.
7
+ # `active: true` starts it selected.
8
+ #
9
+ # The block is shown at the end of the row, for a little more about the file
10
+ # (a size, a "M" for modified).
11
+ class UI::FileTree::FileItem < UI::Base
12
+ attr_reader :name, :icon
13
+
14
+ def initialize(name:, href: nil, active: false, value: nil, icon: "file", class: nil, **html_attrs)
15
+ @name = name
16
+ @href = href
17
+ @active = active
18
+ @value = value || name
19
+ @icon = icon
20
+ @user_class = binding.local_variable_get(:class)
21
+ @html_attrs = html_attrs
22
+ @level = 1
23
+ @guides = true
24
+ @tabbable = false
25
+ end
26
+
27
+ # Set by the tree or folder holding this file, before it draws it.
28
+ def place(level:, guides:, tabbable: false)
29
+ @level = level
30
+ @guides = guides
31
+ @tabbable = tabbable
32
+ end
33
+
34
+ def active? = @active
35
+ def link? = @href.present?
36
+ def tag_name = link? ? :a : :span
37
+
38
+ def row_attrs
39
+ attrs = @html_attrs.except(:data, :aria)
40
+ attrs[:href] = @href if link?
41
+ attrs[:role] = "treeitem"
42
+ attrs[:tabindex] = @tabbable ? 0 : -1
43
+ attrs[:class] = cn(UI::FileTree::ROW, @user_class)
44
+ attrs[:aria] = { selected: @active.to_s, level: @level, current: ("page" if @active && link?) }
45
+ .compact.merge(@html_attrs[:aria] || {})
46
+ attrs[:data] = { slot: "file-tree-file", name: @name, value: @value, "ui--file-tree-target": "item" }
47
+ .merge(@html_attrs[:data] || {})
48
+ attrs
49
+ end
50
+ end
@@ -0,0 +1,9 @@
1
+ <% rows = capture do %><% items.each do |item| %><%= item %><% end %><% end %>
2
+ <%= tag.li(role: "none") do %>
3
+ <%= tag.span(**row_attrs) do %>
4
+ <%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "shrink-0 text-muted-foreground transition-transform duration-350 ease-[cubic-bezier(0.32,0.72,0,1)] group-aria-expanded/row:rotate-90 motion-reduce:transition-none", aria: { hidden: true })) %>
5
+ <%= render(UI::Icon.new(name: icon, size: :sm, class: "shrink-0 text-primary/80 dark:text-primary", aria: { hidden: true })) %>
6
+ <%= tag.span name, class: "min-w-0 flex-1 truncate" %>
7
+ <% end %>
8
+ <%= tag.ul(rows, id: group_id, role: "group", hidden: !open?, data: { slot: "file-tree-group", state: open? ? "open" : "closed" }, class: group_classes) %>
9
+ <% end %>
@@ -0,0 +1,76 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A folder in a UI::FileTree: a row that opens and closes the folders and
4
+ # files added to it with `with_folder` and `with_file`, or listed in
5
+ # `children:` as hashes (see UI::FileTree).
6
+ #
7
+ # `open:` nil (the default) opens it when it holds the `active:` file, so the
8
+ # current file is in view; true or false decides. `icon:` swaps the folder
9
+ # icon for another.
10
+ class UI::FileTree::Folder < UI::Base
11
+ # The guide is a line of its own and not a border, so that while the folder
12
+ # has focus it can start lower down, past the ring, rather than run into it.
13
+ # It sits inside the padding, where the overflow the animation clips to
14
+ # can't cut it in half.
15
+ GUIDE = "relative ms-[calc(1rem-0.5px)] ps-[calc(0.375rem+0.5px)] " \
16
+ "before:absolute before:inset-y-0 before:start-0 before:w-px before:bg-border " \
17
+ "before:transition-[top] peer-focus/row:before:top-1.5"
18
+
19
+ renders_many :items, types: {
20
+ folder: { renders: UI::FileTree::Folder, as: :folder },
21
+ file: { renders: UI::FileTree::FileItem, as: :file }
22
+ }
23
+
24
+ attr_reader :name, :icon
25
+
26
+ def initialize(name:, open: nil, icon: "folder", children: nil, class: nil, **html_attrs)
27
+ @name = name
28
+ @open = open
29
+ @icon = icon
30
+ @children = children
31
+ @user_class = binding.local_variable_get(:class)
32
+ @html_attrs = html_attrs
33
+ @level = 1
34
+ @guides = true
35
+ @tabbable = false
36
+ end
37
+
38
+ # Set by the tree or folder holding this one, before it draws it.
39
+ def place(level:, guides:, tabbable: false)
40
+ @level = level
41
+ @guides = guides
42
+ @tabbable = tabbable
43
+ end
44
+
45
+ def before_render
46
+ UI::FileTree.add_entries(self, @children)
47
+ items.each { |item| item.place(level: @level + 1, guides: @guides) }
48
+ end
49
+
50
+ # Known once the items are drawn, since a folder inside finds out then.
51
+ def active? = items.any?(&:active?)
52
+ def open? = @open.nil? ? active? : @open
53
+
54
+ def group_id
55
+ @group_id ||= @html_attrs[:id] ? "#{@html_attrs[:id]}-group" : "ui-file-tree-group-#{SecureRandom.hex(4)}"
56
+ end
57
+
58
+ def row_attrs
59
+ attrs = @html_attrs.except(:data, :aria, :id)
60
+ attrs[:role] = "treeitem"
61
+ attrs[:tabindex] = @tabbable ? 0 : -1
62
+ attrs[:class] = cn(UI::FileTree::ROW, "peer/row", @user_class)
63
+ attrs[:aria] = { expanded: open?.to_s, level: @level, owns: group_id }.merge(@html_attrs[:aria] || {})
64
+ attrs[:data] = { slot: "file-tree-folder", name: @name, "ui--file-tree-target": "item" }
65
+ .merge(@html_attrs[:data] || {})
66
+ attrs
67
+ end
68
+
69
+ # Indented so a folder's rows start under its icon, with the guide line
70
+ # running down from the middle of its chevron. The space above the first
71
+ # row is its margin, not the group's padding: padding would hold the group
72
+ # open at that height until the closing animation ended, then let it drop.
73
+ def group_classes
74
+ cn("flex flex-col [&>li:first-child]:mt-1.5", UI::FileTree::GAP, @guides ? GUIDE : "ps-5.5")
75
+ end
76
+ end
@@ -0,0 +1 @@
1
+ <%= tag.ul(**root_attrs) do %><% items.each do |item| %><%= item %><% end %><% end %>
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Folders and files as a tree: a repository's source, a project's documents,
4
+ # the pages of a site. Folders open and close, and a file is a link or, with
5
+ # no `href:`, a row to pick.
6
+ #
7
+ # <%= render(UI::FileTree.new(label: "Project files")) do |tree| %>
8
+ # <% tree.with_folder(name: "app") do |app| %>
9
+ # <% app.with_folder(name: "models") do |models| %>
10
+ # <% models.with_file(name: "user.rb", href: blob_path("app/models/user.rb"), active: true) %>
11
+ # <% end %>
12
+ # <% end %>
13
+ # <% tree.with_file(name: "README.md", href: blob_path("README.md")) %>
14
+ # <% end %>
15
+ #
16
+ # Or from data, where a hash with `children:` is a folder and any other a
17
+ # file, taking the same options as UI::FileTree::Folder and
18
+ # UI::FileTree::FileItem:
19
+ #
20
+ # <%= render(UI::FileTree.new(items: [
21
+ # { name: "app", children: [ { name: "user.rb", value: "app/user.rb" } ] },
22
+ # { name: "README.md", value: "README.md" }
23
+ # ])) %>
24
+ #
25
+ # A folder holding the `active:` file starts open; `open:` decides it
26
+ # otherwise. Rows follow the WAI-ARIA tree pattern: one is in the tab order,
27
+ # Up and Down move between them, Right opens a folder and Left closes it or
28
+ # goes to its parent, and typing a letter jumps to the next name starting with
29
+ # it. Picking a file with no `href:` selects it and dispatches
30
+ # `ui--file-tree:select` with `{ value, name }`.
31
+ #
32
+ # A folder slides open and shut on the easing iOS uses for its sheets, and
33
+ # reverses smoothly if clicked again part way; with reduced motion asked for,
34
+ # it opens at once.
35
+ #
36
+ # `guides: false` drops the line down the side of each open folder.
37
+ class UI::FileTree < UI::Base
38
+ # Every row, folder or file. A treeitem is the row itself, so the focus ring
39
+ # and the selection are drawn on the row and not round a whole folder.
40
+ # --primary is too dark to read as text on the dark surface, so in dark the
41
+ # selected row keeps its tint and icon but not its text colour.
42
+ ROW = "group/row flex h-7 w-full min-w-0 cursor-pointer select-none items-center gap-1.5 rounded-md px-2 " \
43
+ "text-sm text-foreground hover:bg-accent hover:text-accent-foreground " \
44
+ "aria-selected:bg-primary/10 aria-selected:font-medium aria-selected:text-primary " \
45
+ "aria-selected:hover:bg-primary/15 aria-selected:hover:text-primary " \
46
+ "dark:aria-selected:bg-primary/20 dark:aria-selected:text-foreground " \
47
+ "dark:aria-selected:hover:bg-primary/25 dark:aria-selected:hover:text-foreground " \
48
+ "#{UI::Base::FOCUS_RING}".freeze
49
+
50
+ # Between rows, and between a folder and its first row. The focus ring
51
+ # reaches 3px past its row (2px ring, 1px offset), so this leaves as much
52
+ # again before the next row's hover.
53
+ GAP = "gap-1.5"
54
+
55
+ renders_many :items, types: {
56
+ folder: { renders: UI::FileTree::Folder, as: :folder },
57
+ file: { renders: UI::FileTree::FileItem, as: :file }
58
+ }
59
+
60
+ # Adds a row to `branch` (the tree or a folder) for each hash in `entries`.
61
+ def self.add_entries(branch, entries)
62
+ Array(entries).each do |entry|
63
+ entry = entry.to_h.symbolize_keys
64
+ entry.key?(:children) ? branch.with_folder(**entry) : branch.with_file(**entry)
65
+ end
66
+ end
67
+
68
+ def initialize(items: nil, label: nil, guides: true, class: nil, **html_attrs)
69
+ @entries = items
70
+ @label = label
71
+ @guides = guides
72
+ @user_class = binding.local_variable_get(:class)
73
+ @html_attrs = html_attrs
74
+ end
75
+
76
+ def before_render
77
+ UI::FileTree.add_entries(self, @entries)
78
+ items.each_with_index { |item, index| item.place(level: 1, guides: @guides, tabbable: index.zero?) }
79
+ end
80
+
81
+ def root_attrs
82
+ attrs = @html_attrs.except(:data, :aria)
83
+ attrs[:role] = "tree"
84
+ attrs[:class] = cn("flex w-full flex-col #{GAP}", @user_class)
85
+ attrs[:aria] = { label: @label || t("ui.file_tree.label", default: "Files") }.merge(@html_attrs[:aria] || {})
86
+ attrs[:data] = {
87
+ slot: "file-tree",
88
+ controller: "ui--file-tree",
89
+ action: "click->ui--file-tree#click keydown->ui--file-tree#keydown"
90
+ }.merge(@html_attrs[:data] || {})
91
+ attrs
92
+ end
93
+ end
@@ -0,0 +1,16 @@
1
+ <% if input? %><% row = capture do %><%= tag.div(**stars_attrs) do %><% 1.upto(max * 2) do |step| %><% value = option_value(step) %><%= tag.input(type: "radio", id: option_id(value), name: input_name, value: value, checked: chosen_steps == step, required: required?, disabled: disabled?, class: "sr-only", data: { action: "change->ui--rating#choose" }) %><%= tag.label(for: option_id(value), class: star_classes(step), data: { "ui--rating-target": "star", value: value, score: (score_text(Float(value)) if show_value?), action: "mouseenter->ui--rating#preview" }.compact) do %><span aria-hidden="true" class="pointer-events-none absolute inset-0 overflow-hidden"><span class="<%= star_icon_classes(step) %>"><%= render(UI::Icon.new(name: icon, class: "fill-current")) %></span></span><span class="sr-only"><%= option_label(value) %></span><% end %><% end %><% end %><% end %><% stars = capture do %><% if show_value? %><%= tag.div(class: "flex items-center gap-1.5") do %><%= row %><%= tag.output(score_label, id: score_id, aria: { hidden: true }, class: "text-sm font-medium tabular-nums") %><% end %><% else %><%= row %><% end %><% if thanks? %><%= tag.p(thanks_text, id: thanks_id, hidden: true, role: "status", class: "text-xs text-emerald-600 dark:text-emerald-400") %><% end %><% end %><% if fieldset.wrap? %><%= render(fieldset) { stars } %><% else %><%= stars %><% end %><% else %><%= tag.span(**display_attrs) do %>
2
+ <span role="img" aria-label="<%= image_label %>" class="<%= variant_classes(size: @size) %>">
3
+ <% fills.each do |fill| %>
4
+ <% if fill == :half %>
5
+ <span class="relative inline-flex <%= UI::Rating::OFF %>">
6
+ <%= render(UI::Icon.new(name: icon, class: "fill-current")) %>
7
+ <span class="absolute inset-y-0 start-0 flex w-1/2 overflow-hidden <%= UI::Rating::ON %>"><%= render(UI::Icon.new(name: icon, class: "fill-current")) %></span>
8
+ </span>
9
+ <% else %>
10
+ <%= render(UI::Icon.new(name: icon, class: class_names("fill-current", fill == :full ? UI::Rating::ON : UI::Rating::OFF))) %>
11
+ <% end %>
12
+ <% end %>
13
+ </span>
14
+ <% if show_value? %><span class="font-medium tabular-nums" aria-hidden="true"><%= formatted_score %></span><% end %>
15
+ <% if count? %><span class="text-muted-foreground"><span aria-hidden="true">(<%= formatted_count %>)</span><span class="sr-only"><%= count_label %></span></span><% end %>
16
+ <% end %><% end %>
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # A score out of five stars (or `max:`), shown or chosen.
6
+ #
7
+ # Shown, it fills to the nearest half star and reads as one image: "Rated 4.5
8
+ # out of 5". `show_value: true` prints the number beside the stars and
9
+ # `count:` how many ratings it's the average of.
10
+ #
11
+ # <%= render(UI::Rating.new(value: 4.5, count: 128, show_value: true)) %>
12
+ #
13
+ # Given a `name:` (or `form:` and `field:`), it's a field: a radio for each
14
+ # half-star, so it posts in 0.5-point steps, resets with the form and needs no
15
+ # JavaScript. Arrow keys move between the half-star values. With JavaScript,
16
+ # hovering either half previews the score it gives.
17
+ #
18
+ # <%= render(UI::Rating.new(name: "review[rating]", legend: "Your rating", required: true)) %>
19
+ #
20
+ # With JavaScript, choosing a score clears the field's error, as it's been
21
+ # answered. `thanks: true` then shows "Thank you for your rating" (or the
22
+ # string given) in its place. Resetting the form brings the error back.
23
+ # `show_value: true` prints the score beside the stars, "4.5/5", and keeps
24
+ # it to the one chosen; it goes back to where it started with the form.
25
+ #
26
+ # <%= render(UI::Rating.new(form: f, field: :rating, legend: true, thanks: true)) %>
27
+ #
28
+ # As with RadioGroup, a `legend:` (and a `hint:` or `error:`) renders it
29
+ # through UI::Fieldset, which names the group and shows the model's errors when
30
+ # it's bound to a form; `legend: true` uses the attribute's name. Without a
31
+ # legend, `label:` is the field's hidden name ("Rating" if not given).
32
+ #
33
+ # `icon:` draws another icon from the library.
34
+ class UI::Rating < UI::Base
35
+ variants(
36
+ base: "inline-flex shrink-0 items-center",
37
+ variants: {
38
+ size: {
39
+ sm: "gap-0.5 [&_svg]:size-4",
40
+ default: "gap-0.5 [&_svg]:size-5",
41
+ lg: "gap-1 [&_svg]:size-6"
42
+ }
43
+ },
44
+ defaults: { size: :default }
45
+ )
46
+
47
+ # A filled star's colour, and an empty one's. The empty grey is a solid
48
+ # token, not a see-through one: the icon's fill and stroke overlap, and a
49
+ # see-through colour doubles where they do, outlining every empty star.
50
+ ON = "text-amber-500"
51
+ OFF = "text-border dark:text-muted"
52
+
53
+ # A field's star: lit when its own radio or a later one is checked, so the
54
+ # stars up to the chosen one fill without JavaScript. While the pointer is
55
+ # over the stars, the controller marks each one lit or not, which wins. A
56
+ # star draws no focus ring: each is half a star wide, so a ring round one
57
+ # cuts across the icon, and the lit stars already show the chosen score.
58
+ #
59
+ # The star under the pointer grows, both halves marked `data-hovered` by the
60
+ # controller, and choosing it brings it back to size; choosing one from the
61
+ # keyboard pops it instead. Each half scales from the star's middle
62
+ # (STAR_ORIGIN), so the two move as one star.
63
+ STAR = "relative cursor-pointer transition-[color,scale] text-border dark:text-muted " \
64
+ "[input:checked+&]:text-amber-500 [&:has(~input:checked)]:text-amber-500 " \
65
+ "data-[lit=true]:!text-amber-500 data-[lit=false]:!text-border dark:data-[lit=false]:!text-muted " \
66
+ "data-[hovered=true]:z-10 motion-safe:data-[hovered=true]:scale-125"
67
+ STAR_HALF_SIZE = { sm: "h-4 w-2", default: "h-5 w-2.5", lg: "h-6 w-3" }.freeze
68
+ STAR_GAP = { sm: "me-0.5", default: "me-0.5", lg: "me-1" }.freeze
69
+ STAR_ORIGIN = { start: "origin-right rtl:origin-left", end: "origin-left rtl:origin-right" }.freeze
70
+
71
+ def initialize(value: nil, max: 5, size: :default, icon: "star",
72
+ show_value: false, count: nil,
73
+ name: nil, form: nil, field: nil, required: false, disabled: false, thanks: false,
74
+ label: nil, legend: nil, hint: nil, error: nil,
75
+ class: nil, **html_attrs)
76
+ @value = value
77
+ @max = Integer(max)
78
+ @size = size
79
+ @icon = icon
80
+ @show_value = show_value
81
+ @count = count
82
+ @name = name
83
+ @form = form
84
+ @field = field
85
+ @required = required
86
+ @disabled = disabled
87
+ @thanks = thanks
88
+ @label = label
89
+ @legend = legend
90
+ @hint = hint
91
+ @error = error
92
+ @user_class = binding.local_variable_get(:class)
93
+ @html_attrs = html_attrs
94
+ end
95
+
96
+ attr_reader :max, :icon
97
+
98
+ def form_field? = @form.present? && @field.present?
99
+
100
+ # A field when it has a name to post under; otherwise it shows a score.
101
+ def input? = input_name.present?
102
+
103
+ def input_name = form_field? ? "#{@form.object_name}[#{@field}]" : @name
104
+
105
+ def score
106
+ value = @value
107
+ value = @form.object.public_send(@field) if value.nil? && form_field? && @form.object.respond_to?(@field)
108
+ Float(value, exception: false)&.clamp(0, @max)
109
+ end
110
+
111
+ # --- Shown ------------------------------------------------------------------
112
+
113
+ # :full, :half or :empty for each star, to the nearest half.
114
+ def fills
115
+ halves = ((score || 0) * 2).round
116
+ (1..@max).map { |star| halves >= star * 2 ? :full : (halves == star * 2 - 1 ? :half : :empty) }
117
+ end
118
+
119
+ # "4.5", "4" or "4,5": the score as the page's locale writes it.
120
+ def formatted_score = ActiveSupport::NumberHelper.number_to_rounded(score || 0, precision: 1, strip_insignificant_zeros: true)
121
+
122
+ def image_label = t("ui.rating.label", value: formatted_score, max: @max, default: "Rated %{value} out of %{max}")
123
+
124
+ def count? = !@count.nil?
125
+ def formatted_count = ActiveSupport::NumberHelper.number_to_delimited(@count)
126
+ def count_label = t("ui.rating.count", count: @count, default: { one: "%{count} rating", other: "%{count} ratings" })
127
+ def show_value? = @show_value
128
+
129
+ def display_attrs
130
+ @html_attrs.merge(class: cn("inline-flex items-center gap-1.5 text-sm", @user_class))
131
+ end
132
+
133
+ # --- Chosen -----------------------------------------------------------------
134
+
135
+ # Twice the selected score, so a half-star radio is an integer step.
136
+ def chosen_steps = score && (score * 2).round
137
+
138
+ def group_id = @group_id ||= @html_attrs[:id] || "ui-rating-#{SecureRandom.hex(4)}"
139
+ def option_id(value) = "#{group_id}-#{value}"
140
+ def option_value(step)
141
+ whole, half = step.divmod(2)
142
+ half.zero? ? whole : "#{whole}.5"
143
+ end
144
+ def option_label(value) = t("ui.rating.option", count: value, max: @max, default: "%{count} out of %{max}")
145
+ def required? = @required
146
+ def disabled? = @disabled
147
+
148
+ def thanks? = @thanks.present?
149
+ def thanks_text = @thanks.is_a?(String) ? @thanks : t("ui.rating.thanks", default: "Thank you for your rating")
150
+ def thanks_id = "#{group_id}-thanks"
151
+
152
+ # "4.5/5", beside a field's stars; each star carries its own, for the
153
+ # controller to show once it's chosen.
154
+ def score_label = score_text(score || 0)
155
+ def score_text(value)
156
+ t("ui.rating.score", value: ActiveSupport::NumberHelper.number_to_rounded(value, precision: 1, strip_insignificant_zeros: true),
157
+ max: @max, default: "%{value}/%{max}")
158
+ end
159
+ def score_id = "#{group_id}-score"
160
+
161
+ # The legend shows only when given: a bound field that's never been named
162
+ # doesn't grow one when it has an error.
163
+ def fieldset
164
+ @fieldset ||= UI::Fieldset.new(legend: @legend.presence || false, hint: @hint, error: @error,
165
+ form: @form, field: @field, required: @required, disabled: @disabled,
166
+ role: "radiogroup", aria: group_aria)
167
+ end
168
+
169
+ # Without a legend to name it, the field's name is `label:`.
170
+ def group_aria = { label: (@label.presence || t("ui.rating.name", default: "Rating") unless legend_names_it?) }.compact.merge(@html_attrs[:aria] || {})
171
+
172
+ # `legend: true` names it only when bound to a form, from the attribute.
173
+ def legend_names_it? = @legend == true ? form_field? : @legend.present?
174
+
175
+ # Inside a fieldset, the fieldset has the role and the aria attributes that
176
+ # name and describe the group; the stars' row keeps the rest.
177
+ def stars_attrs
178
+ wrapped = fieldset.wrap?
179
+ attrs = @html_attrs.except(:id, :aria, :data)
180
+ attrs[:role] = "radiogroup" unless wrapped
181
+ attrs[:aria] = group_aria unless wrapped
182
+ attrs[:id] = group_id
183
+ attrs[:class] = cn(variant_classes(size: @size), "w-fit gap-0", @user_class)
184
+ attrs[:data] = { controller: "ui--rating",
185
+ action: "mouseleave->ui--rating#reset reset@document->ui--rating#formReset",
186
+ "ui--rating-error-value": (fieldset.error_id if fieldset.error_message.present?),
187
+ "ui--rating-thanks-value": (thanks_id if thanks?),
188
+ "ui--rating-score-value": (score_id if show_value?) }
189
+ .compact.merge(@html_attrs[:data] || {})
190
+ attrs
191
+ end
192
+
193
+ def star_classes(step)
194
+ cn(STAR, STAR_HALF_SIZE.fetch(@size), (STAR_GAP.fetch(@size) if step.even? && step < @max * 2),
195
+ STAR_ORIGIN.fetch(step.odd? ? :start : :end),
196
+ { "cursor-not-allowed opacity-50" => @disabled })
197
+ end
198
+
199
+ def star_icon_classes(step) = cn("absolute inset-y-0 flex", step.odd? ? "start-0" : "-start-full")
200
+ end
@@ -1 +1,10 @@
1
- <%= link_to @path, **@system_args do %><%= content %><% end %>
1
+ <% label = tooltip_text %>
2
+ <% button = capture do %><% if section? %><%= tag.button(**@system_args) do %><%= content %><% end %><% else %><%= link_to @path, **@system_args do %><%= content %><% end %><% end %><% end %>
3
+ <% if label %>
4
+ <%= render(UI::Tooltip.new(class: "flex")) do |tooltip| %>
5
+ <% tooltip.with_trigger(class: "flex w-full") { button } %>
6
+ <% tooltip.with_content(side: :right) { label } %>
7
+ <% end %>
8
+ <% else %>
9
+ <%= button %>
10
+ <% end %>