velvetkit 0.2.1 → 0.3.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 (292) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +25 -7
  3. data/lib/generators/velvetkit/add/add_generator.rb +19 -0
  4. data/lib/generators/velvetkit/helpers.rb +29 -2
  5. data/lib/generators/velvetkit/icons/icons_generator.rb +71 -0
  6. data/lib/generators/velvetkit/install/install_generator.rb +9 -12
  7. data/lib/velvetkit/icons.rb +148 -0
  8. data/lib/velvetkit/registry.rb +5 -2
  9. data/lib/velvetkit/version.rb +1 -1
  10. data/lib/velvetkit.rb +1 -0
  11. data/registry/app/components/ui/alert.rb +1 -1
  12. data/registry/app/components/ui/confirm_dialog.html.erb +13 -0
  13. data/registry/app/components/ui/confirm_dialog.rb +55 -0
  14. data/registry/app/components/ui/empty_state.html.erb +20 -0
  15. data/registry/app/components/ui/empty_state.rb +46 -0
  16. data/registry/app/components/ui/icon.html.erb +1 -1
  17. data/registry/app/components/ui/icon.rb +80 -8
  18. data/registry/app/components/ui/password_input.html.erb +1 -1
  19. data/registry/app/components/ui/stepper/step.html.erb +27 -0
  20. data/registry/app/components/ui/stepper/step.rb +213 -0
  21. data/registry/app/components/ui/stepper.html.erb +19 -0
  22. data/registry/app/components/ui/stepper.rb +145 -0
  23. data/registry/app/components/ui/theme_toggle.rb +2 -4
  24. data/registry/app/components/ui/toast.rb +6 -6
  25. data/registry/app/components/ui/toggle_group/item.html.erb +1 -0
  26. data/registry/app/components/ui/toggle_group/item.rb +85 -0
  27. data/registry/app/components/ui/toggle_group.html.erb +1 -0
  28. data/registry/app/components/ui/toggle_group.rb +79 -0
  29. data/registry/app/javascript/controllers/ui/confirm_dialog_controller.ts +110 -0
  30. data/registry/app/javascript/controllers/ui/stepper_controller.ts +187 -0
  31. data/registry/components.yml +9 -2
  32. data/registry/config/locales/velvetkit.en.yml +11 -0
  33. data/registry/docs/agents.md +7 -4
  34. data/registry/icons/lucide/LICENSE +39 -0
  35. data/registry/icons/lucide/align-center.svg +17 -0
  36. data/registry/icons/lucide/align-left.svg +17 -0
  37. data/registry/icons/lucide/align-right.svg +17 -0
  38. data/registry/icons/lucide/arrow-left.svg +16 -0
  39. data/registry/icons/lucide/arrow-right.svg +16 -0
  40. data/registry/icons/lucide/badge-check.svg +16 -0
  41. data/registry/icons/lucide/bar-chart-3.svg +18 -0
  42. data/registry/icons/lucide/bell.svg +16 -0
  43. data/registry/icons/lucide/bold.svg +15 -0
  44. data/registry/icons/lucide/book-open.svg +16 -0
  45. data/registry/icons/lucide/calendar.svg +18 -0
  46. data/registry/icons/lucide/chart-column.svg +18 -0
  47. data/registry/icons/lucide/check.svg +15 -0
  48. data/registry/icons/lucide/chevron-down.svg +15 -0
  49. data/registry/icons/lucide/chevron-left.svg +15 -0
  50. data/registry/icons/lucide/chevron-right.svg +15 -0
  51. data/registry/icons/lucide/chevrons-up-down.svg +16 -0
  52. data/registry/icons/lucide/circle-alert.svg +17 -0
  53. data/registry/icons/lucide/circle-check.svg +16 -0
  54. data/registry/icons/lucide/cloud.svg +15 -0
  55. data/registry/icons/lucide/credit-card.svg +16 -0
  56. data/registry/icons/lucide/ellipsis-vertical.svg +17 -0
  57. data/registry/icons/lucide/ellipsis.svg +17 -0
  58. data/registry/icons/lucide/eye-off.svg +18 -0
  59. data/registry/icons/lucide/eye.svg +16 -0
  60. data/registry/icons/lucide/file-text.svg +19 -0
  61. data/registry/icons/lucide/folder-kanban.svg +18 -0
  62. data/registry/icons/lucide/git-branch.svg +18 -0
  63. data/registry/icons/lucide/globe.svg +17 -0
  64. data/registry/icons/lucide/graduation-cap.svg +17 -0
  65. data/registry/icons/lucide/grip-vertical.svg +20 -0
  66. data/registry/icons/lucide/home.svg +16 -0
  67. data/registry/icons/lucide/inbox.svg +16 -0
  68. data/registry/icons/lucide/info.svg +17 -0
  69. data/registry/icons/lucide/italic.svg +17 -0
  70. data/registry/icons/lucide/layout-dashboard.svg +18 -0
  71. data/registry/icons/lucide/layout-grid.svg +18 -0
  72. data/registry/icons/lucide/life-buoy.svg +20 -0
  73. data/registry/icons/lucide/list.svg +20 -0
  74. data/registry/icons/lucide/loader.svg +22 -0
  75. data/registry/icons/lucide/lock.svg +16 -0
  76. data/registry/icons/lucide/log-out.svg +17 -0
  77. data/registry/icons/lucide/mail.svg +16 -0
  78. data/registry/icons/lucide/menu.svg +17 -0
  79. data/registry/icons/lucide/message-circle.svg +15 -0
  80. data/registry/icons/lucide/monitor.svg +17 -0
  81. data/registry/icons/lucide/moon.svg +15 -0
  82. data/registry/icons/lucide/newspaper.svg +18 -0
  83. data/registry/icons/lucide/panel-left.svg +16 -0
  84. data/registry/icons/lucide/plus.svg +16 -0
  85. data/registry/icons/lucide/qr-code.svg +26 -0
  86. data/registry/icons/lucide/rocket.svg +18 -0
  87. data/registry/icons/lucide/rotate-cw.svg +16 -0
  88. data/registry/icons/lucide/search.svg +16 -0
  89. data/registry/icons/lucide/settings.svg +16 -0
  90. data/registry/icons/lucide/shield-check.svg +16 -0
  91. data/registry/icons/lucide/shield.svg +15 -0
  92. data/registry/icons/lucide/store.svg +17 -0
  93. data/registry/icons/lucide/sun.svg +23 -0
  94. data/registry/icons/lucide/trash-2.svg +19 -0
  95. data/registry/icons/lucide/underline.svg +16 -0
  96. data/registry/icons/lucide/upload.svg +17 -0
  97. data/registry/icons/lucide/user.svg +16 -0
  98. data/registry/icons/lucide/users.svg +18 -0
  99. data/registry/icons/lucide/workflow.svg +17 -0
  100. data/registry/icons/lucide/x.svg +16 -0
  101. data/registry/icons/phosphor/LICENSE +21 -0
  102. data/registry/icons/phosphor/arrow-clockwise.svg +2 -0
  103. data/registry/icons/phosphor/arrow-left.svg +2 -0
  104. data/registry/icons/phosphor/arrow-right.svg +2 -0
  105. data/registry/icons/phosphor/bell.svg +2 -0
  106. data/registry/icons/phosphor/book-open.svg +2 -0
  107. data/registry/icons/phosphor/calendar-blank.svg +2 -0
  108. data/registry/icons/phosphor/caret-down.svg +2 -0
  109. data/registry/icons/phosphor/caret-left.svg +2 -0
  110. data/registry/icons/phosphor/caret-right.svg +2 -0
  111. data/registry/icons/phosphor/caret-up-down.svg +2 -0
  112. data/registry/icons/phosphor/chart-bar.svg +2 -0
  113. data/registry/icons/phosphor/chat-circle.svg +2 -0
  114. data/registry/icons/phosphor/check-circle.svg +2 -0
  115. data/registry/icons/phosphor/check.svg +2 -0
  116. data/registry/icons/phosphor/cloud.svg +2 -0
  117. data/registry/icons/phosphor/credit-card.svg +2 -0
  118. data/registry/icons/phosphor/desktop.svg +2 -0
  119. data/registry/icons/phosphor/dots-six-vertical.svg +2 -0
  120. data/registry/icons/phosphor/dots-three-vertical.svg +2 -0
  121. data/registry/icons/phosphor/dots-three.svg +2 -0
  122. data/registry/icons/phosphor/envelope.svg +2 -0
  123. data/registry/icons/phosphor/eye-slash.svg +2 -0
  124. data/registry/icons/phosphor/eye.svg +2 -0
  125. data/registry/icons/phosphor/file-text.svg +2 -0
  126. data/registry/icons/phosphor/flow-arrow.svg +2 -0
  127. data/registry/icons/phosphor/gear.svg +2 -0
  128. data/registry/icons/phosphor/git-branch.svg +2 -0
  129. data/registry/icons/phosphor/globe.svg +2 -0
  130. data/registry/icons/phosphor/graduation-cap.svg +2 -0
  131. data/registry/icons/phosphor/grid-four.svg +2 -0
  132. data/registry/icons/phosphor/house.svg +2 -0
  133. data/registry/icons/phosphor/info.svg +2 -0
  134. data/registry/icons/phosphor/kanban.svg +2 -0
  135. data/registry/icons/phosphor/lifebuoy.svg +2 -0
  136. data/registry/icons/phosphor/list.svg +2 -0
  137. data/registry/icons/phosphor/lock.svg +2 -0
  138. data/registry/icons/phosphor/magnifying-glass.svg +2 -0
  139. data/registry/icons/phosphor/moon.svg +2 -0
  140. data/registry/icons/phosphor/newspaper.svg +2 -0
  141. data/registry/icons/phosphor/plus.svg +2 -0
  142. data/registry/icons/phosphor/qr-code.svg +2 -0
  143. data/registry/icons/phosphor/rocket.svg +2 -0
  144. data/registry/icons/phosphor/seal-check.svg +2 -0
  145. data/registry/icons/phosphor/shield-check.svg +2 -0
  146. data/registry/icons/phosphor/shield.svg +2 -0
  147. data/registry/icons/phosphor/sidebar.svg +2 -0
  148. data/registry/icons/phosphor/sign-out.svg +2 -0
  149. data/registry/icons/phosphor/spinner.svg +2 -0
  150. data/registry/icons/phosphor/squares-four.svg +2 -0
  151. data/registry/icons/phosphor/storefront.svg +2 -0
  152. data/registry/icons/phosphor/sun.svg +2 -0
  153. data/registry/icons/phosphor/text-align-center.svg +2 -0
  154. data/registry/icons/phosphor/text-align-left.svg +2 -0
  155. data/registry/icons/phosphor/text-align-right.svg +2 -0
  156. data/registry/icons/phosphor/text-b.svg +2 -0
  157. data/registry/icons/phosphor/text-italic.svg +2 -0
  158. data/registry/icons/phosphor/text-underline.svg +2 -0
  159. data/registry/icons/phosphor/trash.svg +2 -0
  160. data/registry/icons/phosphor/tray.svg +2 -0
  161. data/registry/icons/phosphor/upload-simple.svg +2 -0
  162. data/registry/icons/phosphor/user.svg +2 -0
  163. data/registry/icons/phosphor/users.svg +2 -0
  164. data/registry/icons/phosphor/warning-circle.svg +2 -0
  165. data/registry/icons/phosphor/x.svg +2 -0
  166. data/registry/icons/tabler/LICENSE +21 -0
  167. data/registry/icons/tabler/alert-circle.svg +18 -0
  168. data/registry/icons/tabler/align-center.svg +18 -0
  169. data/registry/icons/tabler/align-left.svg +18 -0
  170. data/registry/icons/tabler/align-right.svg +18 -0
  171. data/registry/icons/tabler/arrow-left.svg +18 -0
  172. data/registry/icons/tabler/arrow-right.svg +18 -0
  173. data/registry/icons/tabler/bell.svg +17 -0
  174. data/registry/icons/tabler/bold.svg +17 -0
  175. data/registry/icons/tabler/book.svg +20 -0
  176. data/registry/icons/tabler/building-store.svg +20 -0
  177. data/registry/icons/tabler/calendar.svg +21 -0
  178. data/registry/icons/tabler/chart-bar.svg +19 -0
  179. data/registry/icons/tabler/check.svg +16 -0
  180. data/registry/icons/tabler/chevron-down.svg +16 -0
  181. data/registry/icons/tabler/chevron-left.svg +16 -0
  182. data/registry/icons/tabler/chevron-right.svg +16 -0
  183. data/registry/icons/tabler/circle-check.svg +17 -0
  184. data/registry/icons/tabler/cloud.svg +16 -0
  185. data/registry/icons/tabler/credit-card.svg +19 -0
  186. data/registry/icons/tabler/device-desktop.svg +19 -0
  187. data/registry/icons/tabler/dots-vertical.svg +18 -0
  188. data/registry/icons/tabler/dots.svg +18 -0
  189. data/registry/icons/tabler/eye-off.svg +18 -0
  190. data/registry/icons/tabler/eye.svg +17 -0
  191. data/registry/icons/tabler/file-text.svg +20 -0
  192. data/registry/icons/tabler/git-branch.svg +21 -0
  193. data/registry/icons/tabler/grip-vertical.svg +21 -0
  194. data/registry/icons/tabler/home.svg +18 -0
  195. data/registry/icons/tabler/inbox.svg +17 -0
  196. data/registry/icons/tabler/info-circle.svg +18 -0
  197. data/registry/icons/tabler/italic.svg +18 -0
  198. data/registry/icons/tabler/layout-dashboard.svg +19 -0
  199. data/registry/icons/tabler/layout-grid.svg +19 -0
  200. data/registry/icons/tabler/layout-kanban.svg +19 -0
  201. data/registry/icons/tabler/layout-sidebar.svg +17 -0
  202. data/registry/icons/tabler/lifebuoy.svg +21 -0
  203. data/registry/icons/tabler/list.svg +21 -0
  204. data/registry/icons/tabler/loader.svg +23 -0
  205. data/registry/icons/tabler/lock.svg +18 -0
  206. data/registry/icons/tabler/logout.svg +18 -0
  207. data/registry/icons/tabler/mail.svg +17 -0
  208. data/registry/icons/tabler/menu-2.svg +18 -0
  209. data/registry/icons/tabler/message-circle.svg +16 -0
  210. data/registry/icons/tabler/moon.svg +16 -0
  211. data/registry/icons/tabler/news.svg +19 -0
  212. data/registry/icons/tabler/plus.svg +17 -0
  213. data/registry/icons/tabler/qrcode.svg +27 -0
  214. data/registry/icons/tabler/rocket.svg +18 -0
  215. data/registry/icons/tabler/rosette-discount-check.svg +17 -0
  216. data/registry/icons/tabler/rotate-clockwise.svg +16 -0
  217. data/registry/icons/tabler/school.svg +17 -0
  218. data/registry/icons/tabler/search.svg +17 -0
  219. data/registry/icons/tabler/selector.svg +17 -0
  220. data/registry/icons/tabler/settings.svg +17 -0
  221. data/registry/icons/tabler/shield-check.svg +17 -0
  222. data/registry/icons/tabler/shield.svg +16 -0
  223. data/registry/icons/tabler/sitemap.svg +20 -0
  224. data/registry/icons/tabler/sun.svg +17 -0
  225. data/registry/icons/tabler/trash.svg +20 -0
  226. data/registry/icons/tabler/underline.svg +17 -0
  227. data/registry/icons/tabler/upload.svg +18 -0
  228. data/registry/icons/tabler/user.svg +17 -0
  229. data/registry/icons/tabler/users.svg +19 -0
  230. data/registry/icons/tabler/world.svg +20 -0
  231. data/registry/icons/tabler/x.svg +17 -0
  232. data/registry/icons.yml +74 -0
  233. data/registry/test/components/previews/ui/confirm_dialog_preview/destructive.html.erb +7 -0
  234. data/registry/test/components/previews/ui/confirm_dialog_preview/in_form.html.erb +6 -0
  235. data/registry/test/components/previews/ui/confirm_dialog_preview/on_link.html.erb +5 -0
  236. data/registry/test/components/previews/ui/confirm_dialog_preview/plain.html.erb +3 -0
  237. data/registry/test/components/previews/ui/confirm_dialog_preview/with_description.html.erb +4 -0
  238. data/registry/test/components/previews/ui/confirm_dialog_preview.rb +20 -0
  239. data/registry/test/components/previews/ui/empty_state_preview/in_card.html.erb +7 -0
  240. data/registry/test/components/previews/ui/empty_state_preview/no_results.html.erb +3 -0
  241. data/registry/test/components/previews/ui/empty_state_preview/outline.html.erb +3 -0
  242. data/registry/test/components/previews/ui/empty_state_preview/with_actions.html.erb +4 -0
  243. data/registry/test/components/previews/ui/empty_state_preview/with_media.html.erb +8 -0
  244. data/registry/test/components/previews/ui/empty_state_preview.rb +26 -0
  245. data/registry/test/components/previews/ui/icon_preview/common.html.erb +19 -3
  246. data/registry/test/components/previews/ui/icon_preview/labelled.html.erb +4 -0
  247. data/registry/test/components/previews/ui/icon_preview/missing.html.erb +3 -0
  248. data/registry/test/components/previews/ui/icon_preview.rb +18 -3
  249. data/registry/test/components/previews/ui/input_group_preview/playground.html.erb +2 -2
  250. data/registry/test/components/previews/ui/input_group_preview.rb +3 -3
  251. data/registry/test/components/previews/ui/stepper_preview/approvals.html.erb +9 -0
  252. data/registry/test/components/previews/ui/stepper_preview/checklist.html.erb +12 -0
  253. data/registry/test/components/previews/ui/stepper_preview/dot_wizard.html.erb +41 -0
  254. data/registry/test/components/previews/ui/stepper_preview/failed_deploy.html.erb +13 -0
  255. data/registry/test/components/previews/ui/stepper_preview/inline.html.erb +31 -0
  256. data/registry/test/components/previews/ui/stepper_preview/onboarding.html.erb +12 -0
  257. data/registry/test/components/previews/ui/stepper_preview/order_tracking.html.erb +10 -0
  258. data/registry/test/components/previews/ui/stepper_preview/price_calculator.html.erb +14 -0
  259. data/registry/test/components/previews/ui/stepper_preview/server_driven.html.erb +20 -0
  260. data/registry/test/components/previews/ui/stepper_preview/sidebar.html.erb +43 -0
  261. data/registry/test/components/previews/ui/stepper_preview/wizard.html.erb +55 -0
  262. data/registry/test/components/previews/ui/stepper_preview.rb +57 -0
  263. data/registry/test/components/previews/ui/toggle_group_preview/icon_only.html.erb +5 -0
  264. data/registry/test/components/previews/ui/toggle_group_preview/in_form.html.erb +11 -0
  265. data/registry/test/components/previews/ui/toggle_group_preview/multiple.html.erb +5 -0
  266. data/registry/test/components/previews/ui/toggle_group_preview/outline.html.erb +5 -0
  267. data/registry/test/components/previews/ui/toggle_group_preview/sizes.html.erb +9 -0
  268. data/registry/test/components/previews/ui/toggle_group_preview/with_icons.html.erb +5 -0
  269. data/registry/test/components/previews/ui/toggle_group_preview.rb +36 -0
  270. data/registry/test/components/ui/accordion/trigger_test.rb +1 -1
  271. data/registry/test/components/ui/alert_test.rb +10 -10
  272. data/registry/test/components/ui/badge_test.rb +3 -3
  273. data/registry/test/components/ui/banner_test.rb +3 -3
  274. data/registry/test/components/ui/breadcrumb/ellipsis_test.rb +2 -2
  275. data/registry/test/components/ui/button_test.rb +17 -14
  276. data/registry/test/components/ui/button_to_test.rb +2 -2
  277. data/registry/test/components/ui/checkbox_test.rb +2 -2
  278. data/registry/test/components/ui/confirm_dialog_test.rb +68 -0
  279. data/registry/test/components/ui/empty_state_test.rb +87 -0
  280. data/registry/test/components/ui/icon_test.rb +60 -20
  281. data/registry/test/components/ui/input_group_test.rb +1 -1
  282. data/registry/test/components/ui/link_test.rb +3 -3
  283. data/registry/test/components/ui/navigation_menu_test.rb +2 -2
  284. data/registry/test/components/ui/session_tabs_test.rb +3 -3
  285. data/registry/test/components/ui/spinner_test.rb +9 -9
  286. data/registry/test/components/ui/stepper_test.rb +239 -0
  287. data/registry/test/components/ui/tabs/trigger_test.rb +2 -2
  288. data/registry/test/components/ui/theme_toggle_test.rb +6 -8
  289. data/registry/test/components/ui/toast_test.rb +11 -10
  290. data/registry/test/components/ui/toggle_group_test.rb +162 -0
  291. metadata +253 -2
  292. data/registry/app/javascript/velvetkit_icons.ts +0 -70
@@ -1,5 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # UI::Icon — an SVG icon, drawn into the page on the server.
4
+ #
5
+ # Each icon is a file in app/assets/icons, named as your icon library names
6
+ # it: `UI::Icon.new(name: "rocket")` draws app/assets/icons/rocket.svg. The
7
+ # library is the one in config/velvetkit.yml; `velvetkit:add` copies the
8
+ # icons the components draw, and this adds any other:
9
+ #
10
+ # bin/rails generate velvetkit:icons rocket user
11
+ #
12
+ # There is no JavaScript and nothing to register. An icon with no file draws
13
+ # a dashed square (nothing in production) and logs which file is missing.
14
+ #
15
+ # name: the file's name, without .svg
16
+ # size: xs, sm, default, lg or xl
17
+ # label: makes it an image with this accessible name; without one it is
18
+ # decorative and hidden from assistive technology
19
+ #
20
+ # To keep the SVGs somewhere else, set `config.x.velvetkit.icons_path`.
3
21
  class UI::Icon < UI::Base
4
22
  variants(
5
23
  base: "inline-block shrink-0",
@@ -15,27 +33,81 @@ class UI::Icon < UI::Base
15
33
  defaults: { size: :default }
16
34
  )
17
35
 
36
+ # Shown in place of an icon with no file, outside production.
37
+ MISSING = { viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", "stroke-width": "2",
38
+ "stroke-dasharray": "3 3", inner: '<rect x="3" y="3" width="18" height="18" rx="2"/>' }.freeze
39
+
40
+ class << self
41
+ def path
42
+ Rails.configuration.x.velvetkit.icons_path || Rails.root.join("app/assets/icons")
43
+ end
44
+
45
+ # The SVG's attributes and markup, or nil if there's no such file. The
46
+ # library's own size and class are dropped: the component sets those.
47
+ def svg(name)
48
+ return unless name.match?(/\A[\w-]+\z/) # a file name, never a path
49
+
50
+ cache = Rails.configuration.cache_classes ? (@svgs ||= {}) : {}
51
+ cache.fetch(name) do
52
+ file = File.join(path, "#{name}.svg")
53
+ next unless File.file?(file)
54
+
55
+ cache[name] = parse(File.read(file))
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def parse(source)
62
+ open_tag, inner = source.gsub(/<!--.*?-->/m, "").match(%r{<svg\b([^>]*)>(.*)</svg>}m)&.captures
63
+ return unless open_tag
64
+
65
+ attributes = open_tag.scan(/([\w:-]+)\s*=\s*"([^"]*)"/).to_h.except("width", "height", "class")
66
+ { attributes: attributes.freeze, inner: inner.gsub(/>\s+</, "><").strip.freeze }.freeze
67
+ end
68
+ end
69
+
18
70
  def initialize(name:, size: :default, label: nil, class: nil, **html_attrs)
19
- @name = name
71
+ @name = name.to_s
20
72
  @size = size
21
73
  @label = label
22
74
  @user_class = binding.local_variable_get(:class)
23
75
  @html_attrs = html_attrs
24
76
  end
25
77
 
78
+ attr_reader :name
79
+
26
80
  def render?
27
81
  @name.present?
28
82
  end
29
83
 
30
- def classes
31
- cn(variant_classes(size: @size), @user_class)
84
+ def svg_tag
85
+ svg = self.class.svg(@name) || missing
86
+ tag.svg(
87
+ svg[:inner].html_safe, # the app's own SVG files
88
+ **svg[:attributes].symbolize_keys,
89
+ **@html_attrs.except(:data, :aria),
90
+ class: cn(variant_classes(size: @size), @user_class),
91
+ data: (@html_attrs[:data] || {}).merge(icon: @name),
92
+ aria: aria_attributes,
93
+ role: (accessible_name ? "img" : nil)
94
+ )
95
+ end
96
+
97
+ private
98
+
99
+ def accessible_name
100
+ @label || @html_attrs.dig(:aria, :label)
32
101
  end
33
102
 
34
103
  def aria_attributes
35
- if @label
36
- { label: @label }
37
- else
38
- { hidden: true }
39
- end
104
+ (accessible_name ? { label: accessible_name } : { hidden: true }).merge(@html_attrs[:aria] || {})
105
+ end
106
+
107
+ def missing
108
+ Rails.logger&.warn("[UI::Icon] no #{@name}.svg in #{self.class.path}: bin/rails generate velvetkit:icons #{@name}")
109
+ return { attributes: { "data-icon-missing" => "" }, inner: "" } if Rails.env.production?
110
+
111
+ { attributes: MISSING.except(:inner).transform_keys(&:to_s).merge("data-icon-missing" => ""), inner: MISSING[:inner] }
40
112
  end
41
113
  end
@@ -1,6 +1,6 @@
1
1
  <%# frozen_string_literal: true %>
2
2
  <%# The Stimulus targets live on wrapper spans, not the icons themselves:
3
- UI::Icon sets its own `data: { lucide: ... }`, and a second `data:` hash
3
+ UI::Icon sets its own `data: { icon: ... }`, and a second `data:` hash
4
4
  passed through html_attrs replaces it wholesale, leaving a blank icon.
5
5
  Visibility uses an inline style rather than a `hidden` class so no
6
6
  utility-layer ordering can leave both icons showing at once. %>
@@ -0,0 +1,27 @@
1
+ <%= tag.li(**item_attrs) do %>
2
+ <%= tag.div class: head_classes do %>
3
+ <%= tag.span(**indicator_attrs) do %>
4
+ <% unless dot? %>
5
+ <%= tag.span number_glyph, class: glyph_classes(:number) %>
6
+ <%= tag.span render(UI::Icon.new(name: "check", size: :sm)), class: glyph_classes(:complete) %>
7
+ <%= tag.span "!", class: glyph_classes(:error) + " font-bold" %>
8
+ <%= tag.span render(UI::Icon.new(name: "loader", size: :sm, class: "animate-spin")), class: glyph_classes(:loading) %>
9
+ <% end %>
10
+ <% end %>
11
+ <% if connector? %>
12
+ <%= tag.span class: connector_classes, aria: { hidden: true } do %><%= tag.span class: connector_fill_classes %><% end %>
13
+ <% end %>
14
+ <% end %>
15
+ <%= tag.div class: text_classes do %>
16
+ <% if link? %>
17
+ <%= link_to @href, class: link_classes do %><%= tag.span @title, id: title_id, class: title_classes %><% end %>
18
+ <% elsif navigable? %>
19
+ <%= tag.button(**select_attrs) do %><%= tag.span @title, id: title_id, class: title_classes %><% end %>
20
+ <% else %>
21
+ <%= tag.span @title, id: title_id, class: title_classes %>
22
+ <% end %>
23
+ <% if description? %><%= tag.span @description, class: description_classes %><% end %>
24
+ <% status_texts.each do |text, classes| %><%= tag.span ", #{text}", class: classes %><% end %>
25
+ <% if inline_panel? %><%= tag.div(panel, **panel_attrs) %><% end %>
26
+ <% end %>
27
+ <% end %>
@@ -0,0 +1,213 @@
1
+ # frozen_string_literal: true
2
+
3
+ # One step in a UI::Stepper: its indicator, its title and description, and the
4
+ # line on to the next step. Its state comes from the stepper's `current:`
5
+ # unless `status:` sets it. `icon:` draws an icon in place of the number.
6
+ #
7
+ # Every state's look hangs off the step's data-status, so the stepper's
8
+ # controller only has to change that attribute to move someone along.
9
+ class UI::Stepper::Step < UI::Base
10
+ STATUSES = %i[complete current upcoming error loading].freeze
11
+
12
+ # Shown when a panel appears, after the first move, so a page load is still.
13
+ PANEL_CLASSES = "outline-none group-data-[moved]/stepper:animate-in group-data-[moved]/stepper:fade-in " \
14
+ "group-data-[moved]/stepper:duration-300 group-data-[moved]/stepper:slide-in-from-right-4 " \
15
+ "group-data-[direction=back]/stepper:slide-in-from-left-4 motion-reduce:animate-none"
16
+
17
+ # Written out in full, state by state, so Tailwind finds every class. Current
18
+ # and loading look alike: a ring round the step someone is on.
19
+ INDICATOR = {
20
+ default: "size-8 border text-sm font-medium border-transparent bg-muted text-muted-foreground " \
21
+ "group-data-[status=complete]/step:border-primary group-data-[status=complete]/step:bg-primary " \
22
+ "group-data-[status=complete]/step:text-primary-foreground " \
23
+ "group-data-[status=current]/step:border-2 group-data-[status=current]/step:border-primary " \
24
+ "group-data-[status=current]/step:bg-transparent group-data-[status=current]/step:text-primary " \
25
+ "group-data-[status=current]/step:ring-4 group-data-[status=current]/step:ring-primary/15 " \
26
+ "group-data-[status=loading]/step:border-2 group-data-[status=loading]/step:border-primary " \
27
+ "group-data-[status=loading]/step:bg-transparent group-data-[status=loading]/step:text-primary " \
28
+ "group-data-[status=loading]/step:ring-4 group-data-[status=loading]/step:ring-primary/15 " \
29
+ "group-data-[status=error]/step:border-destructive group-data-[status=error]/step:bg-destructive " \
30
+ "group-data-[status=error]/step:text-white group-data-[status=error]/step:ring-4 " \
31
+ "group-data-[status=error]/step:ring-destructive/15",
32
+ dot: "size-3 bg-muted-foreground/30 group-data-[status=complete]/step:bg-primary " \
33
+ "group-data-[status=current]/step:border-2 group-data-[status=current]/step:border-primary " \
34
+ "group-data-[status=current]/step:bg-transparent group-data-[status=current]/step:ring-4 " \
35
+ "group-data-[status=current]/step:ring-primary/15 " \
36
+ "group-data-[status=loading]/step:border-2 group-data-[status=loading]/step:border-primary " \
37
+ "group-data-[status=loading]/step:bg-transparent group-data-[status=loading]/step:ring-4 " \
38
+ "group-data-[status=loading]/step:ring-primary/15 group-data-[status=loading]/step:animate-pulse " \
39
+ "group-data-[status=error]/step:bg-destructive group-data-[status=error]/step:ring-4 " \
40
+ "group-data-[status=error]/step:ring-destructive/15"
41
+ }.freeze
42
+
43
+ # Each glyph shows in its own state only; the swap animates after a move.
44
+ GLYPH = "group-data-[moved]/stepper:animate-in group-data-[moved]/stepper:zoom-in-50 " \
45
+ "group-data-[moved]/stepper:fade-in group-data-[moved]/stepper:duration-300 motion-reduce:animate-none"
46
+
47
+ attr_reader :number, :title
48
+ attr_accessor :panel
49
+
50
+ def initialize(title:, description: nil, href: nil, status: nil, icon: nil, class: nil, **html_attrs)
51
+ @title = title
52
+ @description = description
53
+ @href = href
54
+ @status = status&.to_sym
55
+ @icon = icon
56
+ @user_class = binding.local_variable_get(:class)
57
+ @html_attrs = html_attrs
58
+ end
59
+
60
+ def place(stepper, number, last:)
61
+ @stepper = stepper
62
+ @number = number
63
+ @last = last
64
+ end
65
+
66
+ def status
67
+ return @status if STATUSES.include?(@status)
68
+
69
+ if @number < @stepper.current then :complete
70
+ elsif @number == @stepper.current then :current
71
+ else :upcoming
72
+ end
73
+ end
74
+
75
+ def panel? = panel.present?
76
+ def last? = @last
77
+ def vertical? = @stepper.vertical?
78
+ def dot? = @stepper.dot?
79
+ def center? = !vertical? && @stepper.align == :center
80
+ def link? = @href.present? && %i[complete error].include?(status)
81
+ def navigable? = !link? && @stepper.navigable?
82
+ def title_id = "#{@stepper.id}-step-#{@number}"
83
+ def description? = @description.present?
84
+ def inline_panel? = @stepper.inline? && panel?
85
+ def number_glyph = @icon ? render(UI::Icon.new(name: @icon, size: :sm)) : @number.to_s
86
+
87
+ def item_attrs
88
+ attrs = @html_attrs.except(:data, :aria)
89
+ attrs[:class] = cn("group/step relative min-w-0", item_layout, @user_class)
90
+ attrs[:aria] = { current: ("step" if %i[current loading].include?(status)) }.compact.merge(@html_attrs[:aria] || {})
91
+ attrs[:data] = { slot: "stepper-step", status: status, "ui--stepper-target": "step" }
92
+ .merge(@html_attrs[:data] || {})
93
+ attrs
94
+ end
95
+
96
+ def item_layout
97
+ if vertical?
98
+ cn("flex gap-3", { "pb-8" => !last? && @stepper.connector?, "pb-5" => !last? && !@stepper.connector? })
99
+ elsif center?
100
+ "flex flex-1 flex-col items-center text-center"
101
+ else
102
+ cn("flex flex-col", last? ? "shrink-0" : "flex-1")
103
+ end
104
+ end
105
+
106
+ def indicator_classes
107
+ cn("relative z-10 flex shrink-0 items-center justify-center rounded-full",
108
+ "transition-[background-color,border-color,color,box-shadow] duration-300 motion-reduce:transition-none",
109
+ INDICATOR[@stepper.variant],
110
+ { "mt-1" => dot? && vertical?,
111
+ "group-data-[status=complete]/step:cursor-pointer group-data-[status=complete]/step:hover:ring-4 " \
112
+ "group-data-[status=complete]/step:hover:ring-primary/15" => navigable? })
113
+ end
114
+
115
+ # The number (or the step's icon) for steps to come and the current one; a
116
+ # tick once done, "!" on an error, a spinner while it's being checked.
117
+ def glyph_classes(state)
118
+ shown = {
119
+ number: "group-data-[status=complete]/step:hidden group-data-[status=error]/step:hidden " \
120
+ "group-data-[status=loading]/step:hidden",
121
+ complete: "hidden group-data-[status=complete]/step:flex",
122
+ error: "hidden group-data-[status=error]/step:flex",
123
+ loading: "hidden group-data-[status=loading]/step:flex"
124
+ }
125
+ cn("items-center justify-center", shown[state], { GLYPH => state != :number })
126
+ end
127
+
128
+ def text_classes
129
+ cn("flex min-w-0 flex-col gap-0.5",
130
+ if vertical? then dot? ? "flex-1" : "flex-1 pt-1.5"
131
+ elsif center? then "mt-3 px-2"
132
+ else "mt-3 pr-4"
133
+ end,
134
+ { "sr-only" => !@stepper.labels?, "@max-md/stepper:sr-only" => !vertical? && @stepper.labels? })
135
+ end
136
+
137
+ def title_classes
138
+ cn("text-sm font-medium text-foreground transition-colors duration-300",
139
+ "group-data-[status=upcoming]/step:text-muted-foreground group-data-[status=error]/step:text-destructive",
140
+ { "underline-offset-4 group-hover/link:underline" => link?,
141
+ "underline-offset-4 group-enabled/link:group-hover/link:underline" => navigable? })
142
+ end
143
+
144
+ def description_classes
145
+ "text-sm text-muted-foreground group-data-[status=error]/step:text-destructive/90"
146
+ end
147
+
148
+ # A done step's title is a button back to it, and so (for the mouse) is its
149
+ # indicator. The controller enables it as each step is done.
150
+ def select_attrs
151
+ { type: "button", disabled: status != :complete, data: { action: "ui--stepper#select" },
152
+ class: "group/link flex w-fit cursor-pointer rounded-sm text-start outline-none focus-visible:ring-[3px] " \
153
+ "focus-visible:ring-ring/40 disabled:cursor-default #{'self-center' if center?}" }
154
+ end
155
+
156
+ def indicator_attrs
157
+ attrs = { class: indicator_classes, aria: { hidden: true } }
158
+ attrs[:data] = { action: "click->ui--stepper#select" } if navigable?
159
+ attrs
160
+ end
161
+
162
+ def link_classes
163
+ "group/link flex w-fit rounded-sm outline-none focus-visible:ring-[3px] focus-visible:ring-ring/40"
164
+ end
165
+
166
+ def connector? = @stepper.connector? && !last?
167
+
168
+ # The line on to the next step: a grey track, and a fill that grows along it
169
+ # once this step is done.
170
+ def connector_classes
171
+ if vertical?
172
+ cn("absolute w-0.5 -translate-x-1/2 overflow-hidden rounded-full bg-border",
173
+ dot? ? "left-1.5 top-6 bottom-1" : "left-4 top-10 bottom-2")
174
+ elsif center?
175
+ cn("absolute h-0.5 -translate-y-1/2 overflow-hidden rounded-full bg-border",
176
+ dot? ? "top-1.5 left-[calc(50%+1rem)] right-[calc(-50%+1rem)]"
177
+ : "top-4 left-[calc(50%+1.75rem)] right-[calc(-50%+1.75rem)]")
178
+ else
179
+ "mx-3 h-0.5 flex-1 overflow-hidden rounded-full bg-border"
180
+ end
181
+ end
182
+
183
+ def connector_fill_classes
184
+ # Filled once this step is done, or the next one is: going back to change
185
+ # an answer leaves the way on to the steps already done drawn in.
186
+ cn("block size-full bg-primary transition-transform duration-500 ease-out motion-reduce:transition-none",
187
+ vertical? ? "origin-top scale-y-0 group-data-[status=complete]/step:scale-y-100 " \
188
+ "[li:has(+li[data-status=complete])_&]:scale-y-100"
189
+ : "origin-left scale-x-0 group-data-[status=complete]/step:scale-x-100 " \
190
+ "[li:has(+li[data-status=complete])_&]:scale-x-100 rtl:origin-right")
191
+ end
192
+
193
+ def head_classes
194
+ vertical? || center? ? "contents" : "flex items-center"
195
+ end
196
+
197
+ def panel_attrs
198
+ @stepper.panel_attrs(self).merge(class: cn(PANEL_CLASSES, "pt-4"))
199
+ end
200
+
201
+ # Each state's words for screen readers, shown (to them) only in that state.
202
+ STATUS_TEXT = {
203
+ complete: [ "Completed", "hidden group-data-[status=complete]/step:inline" ],
204
+ current: [ "Current step", "hidden group-data-[status=current]/step:inline" ],
205
+ upcoming: [ "Not started", "hidden group-data-[status=upcoming]/step:inline" ],
206
+ error: [ "Needs attention", "hidden group-data-[status=error]/step:inline" ],
207
+ loading: [ "Checking", "hidden group-data-[status=loading]/step:inline" ]
208
+ }.freeze
209
+
210
+ def status_texts
211
+ STATUS_TEXT.map { |state, (default, shown)| [ t("ui.stepper.#{state}", default: default), "sr-only #{shown}" ] }
212
+ end
213
+ end
@@ -0,0 +1,19 @@
1
+ <%= tag.div(**root_attrs) do %><%= tag.div class: frame_classes do %>
2
+ <%= tag.nav(**nav_attrs) do %>
3
+ <%= tag.ol class: list_classes do %><% steps.each do |step| %><%= step %><% end %><% end %>
4
+ <% if summary? %>
5
+ <%= tag.p summary_text, class: "mt-4 text-sm font-medium text-foreground @md/stepper:hidden", hidden: finished?, aria: { hidden: true }, data: { "ui--stepper-target": "summary" } %>
6
+ <% end %>
7
+ <% end %>
8
+ <% if body? %>
9
+ <%= tag.div class: body_classes do %>
10
+ <% panels.each do |step| %>
11
+ <%= tag.div(step.panel, **panel_attrs(step)) %>
12
+ <% end %>
13
+ <% if completion? %>
14
+ <%= tag.div(completion, **completion_attrs) %>
15
+ <% end %>
16
+ <%= content %>
17
+ <% end %>
18
+ <% end %>
19
+ <% end %><% end %>
@@ -0,0 +1,145 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # The steps of a multi-step flow, each one done, current, still to come, or in
6
+ # trouble. `current:` is the 1-based number of the step someone is on: steps
7
+ # before it are done, and `current: steps + 1` marks them all done. A step can
8
+ # set its own state with `status:` (:complete, :current, :upcoming, :error or
9
+ # :loading), for one that failed or is still being checked.
10
+ #
11
+ # Looks: `variant: :default` draws numbered circles and `:dot` small dots.
12
+ # Horizontal steppers put each title under its step, `align: :start` (at the
13
+ # left of it) or `:center`; `orientation: :vertical` puts it beside, with room
14
+ # for a description and more. `connector: false` drops the lines, and
15
+ # `labels: false` keeps the titles for screen readers only. Where it has less
16
+ # than 28rem (a phone, a narrow card), a horizontal stepper shows just its
17
+ # steps, with "Step 2 of 4" and the current title under them.
18
+ #
19
+ # A step's block is its panel: what someone fills in on that step. Give the
20
+ # steps panels and the stepper moves between them in the browser; a button
21
+ # with `data-action="ui--stepper#next"` checks the panel's fields and moves
22
+ # on, marking the step as an error if one is invalid, and `ui--stepper#previous`
23
+ # goes back, as does clicking a done step (`navigable: false` turns that
24
+ # off). Moving on is only ever by Continue, so a panel is never skipped
25
+ # unchecked. Give a button `data-ui--stepper-target="next"` or `"previous"` and
26
+ # it hides when there's nowhere to go, and `"submit"` shows only on the last
27
+ # step. Panels sit under the steps; `layout: :side` puts a vertical stepper
28
+ # beside them, given the room, and `layout: :inline` puts each one under its
29
+ # step's title. The stepper's own block comes after the panels, for those
30
+ # buttons; `with_completion` is shown once every step is done.
31
+ #
32
+ # To validate on the server instead, render the stepper with the step the
33
+ # server says someone is on and post each panel's form. While a form inside
34
+ # the stepper is submitting, its current step shows a spinner.
35
+ #
36
+ # Give a done step an `href:` to let people go back to it. Screen readers hear
37
+ # each step's state, and the current one has aria-current="step".
38
+ class UI::Stepper < UI::Base
39
+ ORIENTATIONS = %i[horizontal vertical].freeze
40
+
41
+ # The step's block is captured here, as its panel, rather than as the step's
42
+ # content: the stepper puts it after the list, apart from the step.
43
+ renders_many :steps, ->(title:, **opts, &block) {
44
+ UI::Stepper::Step.new(title: title, **opts).tap { |step| step.panel = block&.call }
45
+ }
46
+ renders_one :completion
47
+
48
+ def initialize(current: 1, orientation: :horizontal, variant: :default, align: :start, layout: :stacked,
49
+ connector: true, labels: true, navigable: nil, label: nil, class: nil, **html_attrs)
50
+ @current = current.to_i
51
+ @orientation = ORIENTATIONS.include?(orientation.to_sym) ? orientation.to_sym : :horizontal
52
+ @variant = variant.to_sym == :dot ? :dot : :default
53
+ @align = align.to_sym == :center ? :center : :start
54
+ @layout = layout.to_sym
55
+ @connector = connector
56
+ @labels = labels
57
+ @navigable = navigable
58
+ @label = label
59
+ @user_class = binding.local_variable_get(:class)
60
+ @html_attrs = html_attrs
61
+ end
62
+
63
+ attr_reader :current, :orientation, :variant, :align
64
+
65
+ def vertical? = @orientation == :vertical
66
+ def dot? = @variant == :dot
67
+ def connector? = @connector
68
+ def labels? = @labels
69
+ def side? = vertical? && @layout == :side
70
+ def inline? = vertical? && @layout == :inline
71
+ def finished? = @current > steps.size
72
+
73
+ # Steps with panels go back when a done one is clicked, unless
74
+ # `navigable: false` keeps people on Back.
75
+ def navigable? = @navigable.nil? ? steps.any?(&:panel?) : @navigable
76
+
77
+ def id = @html_attrs[:id] || (@id ||= "ui-stepper-#{SecureRandom.hex(4)}")
78
+
79
+ def before_render
80
+ steps.each_with_index { |step, index| step.place(self, index + 1, last: index == steps.size - 1) }
81
+ end
82
+
83
+ # Panels that sit apart from the list: every layout but :inline, which puts
84
+ # each one inside its step.
85
+ def panels = inline? ? [] : steps.select(&:panel?)
86
+ def body? = panels.any? || completion? || content.present?
87
+
88
+ def root_attrs
89
+ attrs = @html_attrs.except(:data, :aria, :id)
90
+ attrs[:id] = id
91
+ attrs[:class] = cn("group/stepper @container/stepper w-full", @user_class)
92
+ attrs[:data] = {
93
+ slot: "stepper", orientation: @orientation, variant: @variant,
94
+ controller: "ui--stepper",
95
+ "ui--stepper-current-value": @current,
96
+ "ui--stepper-summary-value": t("ui.stepper.summary", default: "Step %{current} of %{total}"),
97
+ action: "keydown->ui--stepper#keydown input->ui--stepper#revalidate change->ui--stepper#revalidate " \
98
+ "turbo:submit-start->ui--stepper#submitting turbo:submit-end->ui--stepper#submitted"
99
+ }.merge(@html_attrs[:data] || {})
100
+ attrs
101
+ end
102
+
103
+ def nav_attrs
104
+ { aria: { label: @label || t("ui.stepper.label", default: "Progress") }.merge(@html_attrs[:aria] || {}),
105
+ class: cn("min-w-0", { "@md/stepper:sticky @md/stepper:top-6 @md/stepper:self-start" => side? }) }
106
+ end
107
+
108
+ # Side by side once the stepper has the room, measured on itself rather
109
+ # than the window, so it works in a card or a docs column too.
110
+ def frame_classes
111
+ side? ? "grid gap-8 @md/stepper:grid-cols-[minmax(10rem,14rem)_1fr] @md/stepper:gap-10" : "contents"
112
+ end
113
+
114
+ def list_classes
115
+ vertical? ? "flex flex-col" : "flex w-full items-start"
116
+ end
117
+
118
+ def body_classes
119
+ cn("min-w-0", { "mt-8" => !side? && !inline? })
120
+ end
121
+
122
+ def panel_attrs(step)
123
+ { id: "#{id}-panel-#{step.number}", hidden: step.number != @current, tabindex: -1, role: "group",
124
+ aria: { labelledby: step.title_id },
125
+ data: { "ui--stepper-target": "panel", step: step.number },
126
+ class: UI::Stepper::Step::PANEL_CLASSES }
127
+ end
128
+
129
+ def completion_attrs
130
+ { hidden: !finished?, tabindex: -1, data: { "ui--stepper-target": "completion" },
131
+ class: UI::Stepper::Step::PANEL_CLASSES }
132
+ end
133
+
134
+ # "Step 2 of 4" and the current title, under a horizontal stepper on a phone,
135
+ # where the titles don't fit. Screen readers get each step's own text instead.
136
+ def summary?
137
+ !vertical? && steps.size > 1
138
+ end
139
+
140
+ def summary_text
141
+ shown = @current.clamp(1, steps.size)
142
+ format = t("ui.stepper.summary", default: "Step %{current} of %{total}")
143
+ "#{format.sub('%{current}', shown.to_s).sub('%{total}', steps.size.to_s)}: #{steps[shown - 1].title}"
144
+ end
145
+ end
@@ -37,10 +37,8 @@ class UI::ThemeToggle < UI::Base
37
37
  }
38
38
  end
39
39
 
40
- # Both icons are rendered and swapped with `dark:` variants rather than by
41
- # changing `data-lucide` at runtime: lucide replaces each <i> with an <svg>
42
- # on first render, so the original element is gone and a later attribute
43
- # change would do nothing.
40
+ # All three icons are rendered and shown by CSS, so the right one is there
41
+ # before any script runs.
44
42
  # The trigger shows the preference (html[data-theme], set before paint by
45
43
  # the theme script): sun for light, which is the default, moon for dark,
46
44
  # and a monitor while following the system.
@@ -43,14 +43,14 @@ class UI::Toast < UI::Base
43
43
  "text-popover-foreground shadow-lg pointer-events-auto duration-300 fill-mode-forwards " \
44
44
  "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:slide-in-from-bottom-2 " \
45
45
  "data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:slide-out-to-bottom-2 " \
46
- "[&>i]:translate-y-0.5 [&>i]:shrink-0",
46
+ "[&>svg]:translate-y-0.5 [&>svg]:shrink-0",
47
47
  variants: {
48
48
  variant: {
49
- default: "[&>i]:text-muted-foreground",
50
- success: "[&>i]:text-emerald-600 dark:[&>i]:text-emerald-400",
51
- destructive: "[&>i]:text-destructive",
52
- warning: "[&>i]:text-amber-600 dark:[&>i]:text-amber-400",
53
- info: "[&>i]:text-sky-600 dark:[&>i]:text-sky-400"
49
+ default: "[&>svg]:text-muted-foreground",
50
+ success: "[&>svg]:text-emerald-600 dark:[&>svg]:text-emerald-400",
51
+ destructive: "[&>svg]:text-destructive",
52
+ warning: "[&>svg]:text-amber-600 dark:[&>svg]:text-amber-400",
53
+ info: "[&>svg]:text-sky-600 dark:[&>svg]:text-sky-400"
54
54
  }
55
55
  },
56
56
  defaults: { variant: :default }
@@ -0,0 +1 @@
1
+ <%= tag.label(**label_attrs) do %><%= tag.input(**input_attrs) %><% if icon? %><%= render(UI::Icon.new(name: @icon, size: icon_size)) %><% end %><% if text.present? %><%= tag.span text, class: ("sr-only" if icon_only?) %><% end %><% end %>
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ # One choice in a UI::ToggleGroup: a label wrapping a visually hidden radio or
4
+ # checkbox. The block, or `label:`, is its text; `icon:` draws an icon before
5
+ # it, and `icon_only: true` hides the text from sight but keeps it for screen
6
+ # readers and as a tooltip.
7
+ class UI::ToggleGroup::Item < UI::Base
8
+ HEIGHTS = {
9
+ default: { sm: "h-7 px-2.5", default: "h-8 px-3", lg: "h-9 px-3.5" },
10
+ outline: { sm: "h-8 px-2.5", default: "h-9 px-3", lg: "h-10 px-3.5" }
11
+ }.freeze
12
+
13
+ ICON_ONLY = {
14
+ default: { sm: "h-7 w-7", default: "h-8 w-8", lg: "h-9 w-9" },
15
+ outline: { sm: "h-8 w-8", default: "h-9 w-9", lg: "h-10 w-10" }
16
+ }.freeze
17
+
18
+ # In the outline group only a checked item has a background; hover just
19
+ # darkens the text, so it can't be mistaken for checked. The group clips its
20
+ # items to its corners, so the focus ring goes inside the item there.
21
+ STATES = {
22
+ default: "rounded-md hover:text-foreground " \
23
+ "has-[:checked]:bg-background has-[:checked]:text-foreground has-[:checked]:shadow-sm",
24
+ outline: "text-muted-foreground " \
25
+ "hover:text-foreground " \
26
+ "has-[:checked]:bg-foreground/8 has-[:checked]:text-foreground has-[:focus-visible]:ring-inset"
27
+ }.freeze
28
+
29
+ def initialize(value:, label: nil, icon: nil, icon_only: false, disabled: false, class: nil, **html_attrs)
30
+ @value = value.to_s
31
+ @label = label
32
+ @icon = icon
33
+ @icon_only = icon_only
34
+ @disabled = disabled
35
+ @user_class = binding.local_variable_get(:class)
36
+ @html_attrs = html_attrs
37
+ end
38
+
39
+ def set_group_context(group)
40
+ @group = group
41
+ end
42
+
43
+ def icon? = @icon.present?
44
+ def icon_only? = @icon_only && icon?
45
+ def text = content.presence || @label
46
+
47
+ def classes
48
+ variant = @group.variant.to_sym
49
+ size = @group.size.to_sym
50
+ cn(
51
+ "relative inline-flex shrink-0 cursor-pointer select-none items-center justify-center gap-1.5 " \
52
+ "whitespace-nowrap text-sm font-medium transition-colors " \
53
+ "has-[:focus-visible]:z-10 has-[:focus-visible]:outline-none " \
54
+ "has-[:focus-visible]:ring-[3px] has-[:focus-visible]:ring-ring/40 " \
55
+ "has-[:disabled]:pointer-events-none has-[:disabled]:opacity-50",
56
+ (icon_only? ? ICON_ONLY : HEIGHTS).dig(variant, size),
57
+ STATES[variant],
58
+ @user_class
59
+ )
60
+ end
61
+
62
+ def label_attrs
63
+ attrs = @html_attrs.except(:data)
64
+ attrs[:class] = classes
65
+ attrs[:title] = text if icon_only? && text.present?
66
+ attrs[:data] = { slot: "toggle-group-item" }.merge(@html_attrs[:data] || {})
67
+ attrs
68
+ end
69
+
70
+ def input_attrs
71
+ {
72
+ type: @group.multiple? ? "checkbox" : "radio",
73
+ name: @group.input_name,
74
+ value: @value,
75
+ checked: @group.selected.include?(@value),
76
+ disabled: @disabled || @group.disabled?,
77
+ required: @group.required?,
78
+ class: "peer sr-only"
79
+ }
80
+ end
81
+
82
+ def icon_size
83
+ @group.size.to_sym == :lg ? :default : :sm
84
+ end
85
+ end
@@ -0,0 +1 @@
1
+ <%= tag.div(**root_attrs) do %><% if multiple? && input_name %><%= tag.input type: "hidden", name: input_name, value: "", autocomplete: "off" %><% end %><% items.each do |item| %><%= item %><% end %><% end %>