velvetkit 0.3.0 → 0.4.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 (216) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +109 -0
  3. data/README.md +5 -1
  4. data/lib/generators/velvetkit/add/add_generator.rb +58 -0
  5. data/lib/velvetkit/version.rb +4 -1
  6. data/lib/velvetkit.rb +7 -0
  7. data/registry/app/assets/tailwind/velvetkit.css +90 -155
  8. data/registry/app/components/ui/chart.html.erb +47 -0
  9. data/registry/app/components/ui/chart.rb +177 -0
  10. data/registry/app/components/ui/command/group.rb +27 -0
  11. data/registry/app/components/ui/command/item.rb +64 -0
  12. data/registry/app/components/ui/command.html.erb +41 -0
  13. data/registry/app/components/ui/command.rb +122 -0
  14. data/registry/app/components/ui/date_picker.html.erb +58 -64
  15. data/registry/app/components/ui/date_picker.rb +199 -75
  16. data/registry/app/components/ui/file_field.html.erb +14 -4
  17. data/registry/app/components/ui/file_field.rb +179 -28
  18. data/registry/app/components/ui/gallery/image.rb +20 -0
  19. data/registry/app/components/ui/gallery.html.erb +52 -0
  20. data/registry/app/components/ui/gallery.rb +164 -0
  21. data/registry/app/components/ui/kbd.html.erb +27 -0
  22. data/registry/app/components/ui/kbd.rb +134 -0
  23. data/registry/app/components/ui/money_input.html.erb +8 -0
  24. data/registry/app/components/ui/money_input.rb +153 -0
  25. data/registry/app/components/ui/number_input.html.erb +11 -0
  26. data/registry/app/components/ui/number_input.rb +103 -0
  27. data/registry/app/components/ui/progress/segment.rb +25 -0
  28. data/registry/app/components/ui/progress.html.erb +25 -0
  29. data/registry/app/components/ui/progress.rb +182 -0
  30. data/registry/app/components/ui/scan_input.html.erb +6 -0
  31. data/registry/app/components/ui/scan_input.rb +94 -0
  32. data/registry/app/components/ui/select.html.erb +24 -5
  33. data/registry/app/components/ui/select.rb +44 -5
  34. data/registry/app/components/ui/slider.html.erb +23 -0
  35. data/registry/app/components/ui/slider.rb +254 -0
  36. data/registry/app/components/ui/timeline/item.html.erb +21 -0
  37. data/registry/app/components/ui/timeline/item.rb +145 -0
  38. data/registry/app/components/ui/timeline.html.erb +1 -0
  39. data/registry/app/components/ui/timeline.rb +42 -0
  40. data/registry/app/javascript/chart_js.ts +37 -0
  41. data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
  42. data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
  43. data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
  44. data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
  45. data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
  46. data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
  47. data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
  48. data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
  49. data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
  50. data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
  51. data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
  52. data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
  53. data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
  54. data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
  55. data/registry/app/javascript/vanilla_calendar.ts +15 -0
  56. data/registry/components.yml +25 -3
  57. data/registry/config/locales/velvetkit.en.yml +79 -0
  58. data/registry/icons/lucide/download.svg +17 -0
  59. data/registry/icons/lucide/file-plus.svg +18 -0
  60. data/registry/icons/lucide/file.svg +16 -0
  61. data/registry/icons/lucide/folder.svg +15 -0
  62. data/registry/icons/lucide/keyboard.svg +23 -0
  63. data/registry/icons/lucide/minus.svg +15 -0
  64. data/registry/icons/lucide/package.svg +18 -0
  65. data/registry/icons/lucide/pause.svg +16 -0
  66. data/registry/icons/lucide/percent.svg +17 -0
  67. data/registry/icons/lucide/printer.svg +17 -0
  68. data/registry/icons/lucide/receipt.svg +17 -0
  69. data/registry/icons/lucide/scan-barcode.svg +21 -0
  70. data/registry/icons/lucide/sun-moon.svg +19 -0
  71. data/registry/icons/lucide/truck.svg +19 -0
  72. data/registry/icons/lucide/user-plus.svg +18 -0
  73. data/registry/icons/lucide/volume-2.svg +17 -0
  74. data/registry/icons/lucide/volume-x.svg +17 -0
  75. data/registry/icons/lucide/zoom-in.svg +18 -0
  76. data/registry/icons/lucide/zoom-out.svg +17 -0
  77. data/registry/icons/phosphor/barcode.svg +2 -0
  78. data/registry/icons/phosphor/circle-half.svg +2 -0
  79. data/registry/icons/phosphor/download-simple.svg +2 -0
  80. data/registry/icons/phosphor/file-plus.svg +2 -0
  81. data/registry/icons/phosphor/file.svg +2 -0
  82. data/registry/icons/phosphor/folder.svg +2 -0
  83. data/registry/icons/phosphor/keyboard.svg +2 -0
  84. data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
  85. data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
  86. data/registry/icons/phosphor/minus.svg +2 -0
  87. data/registry/icons/phosphor/package.svg +2 -0
  88. data/registry/icons/phosphor/pause.svg +2 -0
  89. data/registry/icons/phosphor/percent.svg +2 -0
  90. data/registry/icons/phosphor/printer.svg +2 -0
  91. data/registry/icons/phosphor/receipt.svg +2 -0
  92. data/registry/icons/phosphor/speaker-high.svg +2 -0
  93. data/registry/icons/phosphor/speaker-x.svg +2 -0
  94. data/registry/icons/phosphor/truck.svg +2 -0
  95. data/registry/icons/phosphor/user-plus.svg +2 -0
  96. data/registry/icons/tabler/barcode.svg +23 -0
  97. data/registry/icons/tabler/download.svg +18 -0
  98. data/registry/icons/tabler/file-plus.svg +19 -0
  99. data/registry/icons/tabler/file.svg +17 -0
  100. data/registry/icons/tabler/folder.svg +16 -0
  101. data/registry/icons/tabler/keyboard.svg +23 -0
  102. data/registry/icons/tabler/minus.svg +16 -0
  103. data/registry/icons/tabler/package.svg +20 -0
  104. data/registry/icons/tabler/percentage.svg +18 -0
  105. data/registry/icons/tabler/player-pause.svg +17 -0
  106. data/registry/icons/tabler/printer.svg +18 -0
  107. data/registry/icons/tabler/receipt.svg +16 -0
  108. data/registry/icons/tabler/sun-moon.svg +21 -0
  109. data/registry/icons/tabler/truck.svg +18 -0
  110. data/registry/icons/tabler/user-plus.svg +19 -0
  111. data/registry/icons/tabler/volume-3.svg +17 -0
  112. data/registry/icons/tabler/volume.svg +18 -0
  113. data/registry/icons/tabler/zoom-in.svg +19 -0
  114. data/registry/icons/tabler/zoom-out.svg +18 -0
  115. data/registry/icons.yml +19 -0
  116. data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
  117. data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
  118. data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
  119. data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
  120. data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
  121. data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
  122. data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
  123. data/registry/test/components/previews/ui/chart_preview.rb +46 -0
  124. data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
  125. data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
  126. data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
  127. data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
  128. data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
  129. data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
  130. data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
  131. data/registry/test/components/previews/ui/command_preview.rb +25 -0
  132. data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
  133. data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
  134. data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
  135. data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
  136. data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
  137. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
  138. data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
  139. data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
  140. data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
  141. data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
  142. data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
  143. data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
  144. data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
  145. data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
  146. data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
  147. data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
  148. data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
  149. data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
  150. data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
  151. data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
  152. data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
  153. data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
  154. data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
  155. data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
  156. data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
  157. data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
  158. data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
  159. data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
  160. data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
  161. data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
  162. data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
  163. data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
  164. data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
  165. data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
  166. data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
  167. data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
  168. data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
  169. data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
  170. data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
  171. data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
  172. data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
  173. data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
  174. data/registry/test/components/previews/ui/progress_preview.rb +46 -0
  175. data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
  176. data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
  177. data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
  178. data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
  179. data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
  180. data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
  181. data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
  182. data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
  183. data/registry/test/components/previews/ui/select_preview.rb +5 -0
  184. data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
  185. data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
  186. data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
  187. data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
  188. data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
  189. data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
  190. data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
  191. data/registry/test/components/previews/ui/slider_preview.rb +45 -0
  192. data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
  193. data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
  194. data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
  195. data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
  196. data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
  197. data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
  198. data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
  199. data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
  200. data/registry/test/components/ui/chart_test.rb +187 -0
  201. data/registry/test/components/ui/command/group_test.rb +19 -0
  202. data/registry/test/components/ui/command/item_test.rb +47 -0
  203. data/registry/test/components/ui/command_test.rb +110 -0
  204. data/registry/test/components/ui/date_picker_test.rb +129 -111
  205. data/registry/test/components/ui/file_field_test.rb +105 -9
  206. data/registry/test/components/ui/gallery_test.rb +192 -0
  207. data/registry/test/components/ui/kbd_test.rb +150 -0
  208. data/registry/test/components/ui/money_input_test.rb +149 -0
  209. data/registry/test/components/ui/number_input_test.rb +155 -0
  210. data/registry/test/components/ui/progress_test.rb +188 -0
  211. data/registry/test/components/ui/scan_input_test.rb +110 -0
  212. data/registry/test/components/ui/select_test.rb +62 -0
  213. data/registry/test/components/ui/slider_test.rb +190 -0
  214. data/registry/test/components/ui/timeline_test.rb +175 -0
  215. metadata +188 -2
  216. data/registry/app/javascript/cally.ts +0 -8
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ # One part of a UI::Progress that's split by category, like the photos in a
4
+ # storage bar. Drawn by UI::Progress, not on its own.
5
+ #
6
+ # <% progress.with_segment(value: 6.4, label: "Photos") %>
7
+ # <% progress.with_segment(value: 2, label: "Backups", color: 5, value_text: "2 GB, last night") %>
8
+ #
9
+ # `color:` is one of the kit's five chart colours, 1 to 5; segments take them
10
+ # in order otherwise. `class:` sets anything else, like a background of your own.
11
+ class UI::Progress::Segment < UI::Base
12
+ attr_reader :label, :color, :user_class, :value_text
13
+
14
+ def initialize(value:, label:, color: nil, value_text: nil, class: nil)
15
+ @value = value
16
+ @label = label
17
+ @color = color
18
+ @value_text = value_text
19
+ @user_class = binding.local_variable_get(:class)
20
+ end
21
+
22
+ def value = [ @value.to_f, 0 ].max
23
+
24
+ def call = nil
25
+ end
@@ -0,0 +1,25 @@
1
+ <%# frozen_string_literal: true %>
2
+ <%= tag.div(**root_attrs) do %>
3
+ <% if header? %>
4
+ <div class="flex items-baseline justify-between gap-3 text-sm">
5
+ <% if label.present? %><span id="<%= label_id %>" class="font-medium text-foreground"><%= label %></span><% end %>
6
+ <span class="<%= cn("ms-auto text-muted-foreground tabular-nums", ("hidden" unless show_value?)) %>" data-ui--progress-target="text" aria-hidden="true"><%= value_text %></span>
7
+ </div>
8
+ <% end %>
9
+ <%= tag.div(**bar_attrs) do %>
10
+ <%= tag.div(**indicator_attrs) do %>
11
+ <% segments.each_with_index do |segment, index| %><%= tag.div(**segment_attrs(segment, index)) %><% end %>
12
+ <% end %>
13
+ <% end %>
14
+ <% if legend? %>
15
+ <ul class="flex flex-wrap gap-x-4 gap-y-1 pt-1 text-sm" data-slot="progress-legend">
16
+ <% segments.each_with_index do |segment, index| %>
17
+ <li class="flex items-center gap-1.5">
18
+ <span class="<%= cn("size-2 shrink-0 rounded-full", swatch(segment, index), segment.user_class) %>" aria-hidden="true"></span>
19
+ <span class="text-foreground"><%= segment.label %></span>
20
+ <span class="text-muted-foreground tabular-nums"><%= segment_text(segment) %></span>
21
+ </li>
22
+ <% end %>
23
+ </ul>
24
+ <% end %>
25
+ <% end %>
@@ -0,0 +1,182 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # A bar showing how far along something is: an upload, an import, storage
6
+ # used, a profile half filled in. Leave out `value:` and it shows that work is
7
+ # going on without saying how much is left.
8
+ #
9
+ # <%= render(UI::Progress.new(value: 64, label: "Uploading report.pdf", show_value: true)) %>
10
+ # <%= render(UI::Progress.new(value: 3, max: 12, label: "Importing", value_text: "%{value} of %{max} files", show_value: true)) %>
11
+ # <%= render(UI::Progress.new(label: "Preparing your export")) %>
12
+ #
13
+ # Split by category, it adds up its segments and shows a legend:
14
+ #
15
+ # <%= render(UI::Progress.new(max: 15, label: "Storage", value_text: "%{value} GB of %{max} GB",
16
+ # segment_text: "%{value} GB", show_value: true)) do |progress| %>
17
+ # <% progress.with_segment(value: 6.4, label: "Photos") %>
18
+ # <% progress.with_segment(value: 3.1, label: "Mail") %>
19
+ # <% end %>
20
+ #
21
+ # With `max:` the segments fill as far as their total and the rest of the track
22
+ # is what's left: free space, tasks to do. Leave `max:` out and they fill the
23
+ # bar as shares of a whole, like traffic by source:
24
+ #
25
+ # <%= render(UI::Progress.new(label: "Visits by source")) do |progress| %>
26
+ # <% progress.with_segment(value: 5_210, label: "Search") %>
27
+ # <% progress.with_segment(value: 2_870, label: "Direct") %>
28
+ # <% end %>
29
+ #
30
+ # Segments take the chart colours in order. The legend names them, since a
31
+ # colour alone doesn't say which is which; `legend: false` leaves it out when
32
+ # the page already says. `segment_text:` is each one's amount in the legend,
33
+ # with %{value} and %{percent} (its share of max). It defaults to the value
34
+ # against a max, and to the percentage for shares of a whole.
35
+ #
36
+ # `value_text:` is what's shown (with `show_value: true`) and what a screen
37
+ # reader hears. It can use %{value}, %{max} and %{percent}, and defaults to
38
+ # "40%". `tone:` colours the bar (:success, :warning, :destructive) for a bar
39
+ # that means something, like storage nearly full.
40
+ #
41
+ # Updating it: re-render it from a Turbo Stream (with `method: :morph` the bar
42
+ # slides to its new width), or from JavaScript set its
43
+ # `data-ui--progress-value-value` attribute, as an upload's progress event
44
+ # would. It fires `ui--progress:complete` when it reaches the end.
45
+ class UI::Progress < UI::Base
46
+ SIZES = { sm: "h-1", default: "h-2", lg: "h-3" }.freeze
47
+
48
+ TONES = {
49
+ default: { track: "bg-primary/15", bar: "bg-primary" },
50
+ success: { track: "bg-emerald-500/15", bar: "bg-emerald-500 dark:bg-emerald-600" },
51
+ warning: { track: "bg-amber-500/15", bar: "bg-amber-500 dark:bg-amber-600" },
52
+ destructive: { track: "bg-destructive/15", bar: "bg-destructive" }
53
+ }.freeze
54
+
55
+ # Slides a short bar across, again and again. With reduced motion it's a
56
+ # full, faint bar instead: still there, not moving.
57
+ INDETERMINATE = "absolute inset-y-0 w-2/5 animate-[velvetkit-progress_1.4s_ease-in-out_infinite] " \
58
+ "motion-reduce:w-full motion-reduce:animate-none motion-reduce:opacity-50"
59
+ DETERMINATE = "h-full transition-[width] duration-300 ease-out motion-reduce:transition-none"
60
+
61
+ SWATCHES = { 1 => "bg-chart-1", 2 => "bg-chart-2", 3 => "bg-chart-3", 4 => "bg-chart-4", 5 => "bg-chart-5" }.freeze
62
+
63
+ renders_many :segments, UI::Progress::Segment
64
+
65
+ def initialize(value: nil, max: nil, label: nil, value_text: nil, show_value: false, size: :default,
66
+ tone: :default, legend: true, segment_text: nil, class: nil, **html_attrs)
67
+ @given_max = max if max.to_f.positive?
68
+ @value = value&.clamp(0, @given_max || 100)
69
+ @legend = legend
70
+ @segment_text = segment_text
71
+ @label = label
72
+ @value_text = value_text
73
+ @show_value = show_value
74
+ @size = SIZES.key?(size.to_sym) ? size.to_sym : :default
75
+ @tone = TONES.key?(tone.to_sym) ? tone.to_sym : :default
76
+ @user_class = binding.local_variable_get(:class)
77
+ @aria = html_attrs.delete(:aria) || {}
78
+ @html_attrs = html_attrs
79
+ @label_id = "ui-progress-#{SecureRandom.hex(4)}"
80
+ end
81
+
82
+ attr_reader :label, :label_id
83
+
84
+ # 100 unless given; with segments and no max:, their total, so they fill the
85
+ # bar as shares of a whole.
86
+ def max
87
+ return @given_max if @given_max
88
+
89
+ total = segments.sum(&:value)
90
+ whole? ? total : 100
91
+ end
92
+
93
+ # Segments with no max: shares of a whole, not parts of a limit.
94
+ def whole? = @given_max.nil? && segmented? && segments.sum(&:value).positive?
95
+
96
+ # With segments, their total; the bar's own value: is ignored.
97
+ def value
98
+ return @value unless segmented?
99
+
100
+ segments.sum(&:value).clamp(0, max)
101
+ end
102
+
103
+ def segmented? = segments.any?
104
+
105
+ def indeterminate? = value.nil?
106
+
107
+ # Rounded down, so it never says 100% before it's done.
108
+ def percent
109
+ return nil if indeterminate?
110
+
111
+ (value.to_f / max * 100).floor
112
+ end
113
+
114
+ def text_format = @value_text || t("ui.progress.value_text", default: "%{percent}%")
115
+
116
+ def value_text
117
+ return nil if indeterminate?
118
+
119
+ text_format.gsub("%{value}", number(value)).gsub("%{max}", number(max)).gsub("%{percent}", percent.to_s)
120
+ end
121
+
122
+ def header? = @label.present? || (@show_value && !indeterminate?)
123
+
124
+ def root_attrs
125
+ { **@html_attrs, class: cn("w-full", (header? || legend?) && "space-y-1.5", @user_class),
126
+ data: { slot: "progress", controller: "ui--progress", "ui--progress-max-value": number(max),
127
+ "ui--progress-format-value": text_format,
128
+ "ui--progress-determinate-class": DETERMINATE, "ui--progress-indeterminate-class": INDETERMINATE,
129
+ **(@html_attrs[:data] || {}),
130
+ **(indeterminate? ? {} : { "ui--progress-value-value": number(value) }) } }
131
+ end
132
+
133
+ def bar_attrs
134
+ named = @label.present? ? { labelledby: label_id } : { label: t("ui.progress.label", default: "Progress") }
135
+ { role: "progressbar", class: cn("relative w-full overflow-hidden rounded-full", SIZES[@size], TONES[@tone][:track]),
136
+ data: { state: state, "ui--progress-target": "bar" },
137
+ aria: { valuemin: 0, valuemax: number(max), valuenow: (number(value) unless indeterminate?),
138
+ valuetext: value_text, busy: (true if state != "complete"), **named, **@aria }.compact }
139
+ end
140
+
141
+ def indicator_attrs
142
+ colour = segmented? ? "flex gap-0.5 overflow-hidden" : TONES[@tone][:bar]
143
+ { class: cn("rounded-full", colour, indeterminate? ? INDETERMINATE : DETERMINATE),
144
+ style: ("width: #{percent}%" unless indeterminate?),
145
+ data: { slot: "progress-indicator", "ui--progress-target": "indicator" } }.compact
146
+ end
147
+
148
+ def show_value? = @show_value && !indeterminate?
149
+
150
+ def legend? = segmented? && @legend
151
+
152
+ # Each segment's share of the filled part, so together they fill it; a
153
+ # small one keeps a sliver, so it doesn't vanish.
154
+ def segment_attrs(segment, index)
155
+ { class: cn("h-full min-w-1", swatch(segment, index), segment.user_class),
156
+ style: "flex: #{number(segment.value)} 1 0%", title: "#{segment.label}: #{segment_text(segment)}",
157
+ data: { slot: "progress-segment" } }
158
+ end
159
+
160
+ def swatch(segment, index) = SWATCHES[segment.color] || SWATCHES[(index % SWATCHES.size) + 1]
161
+
162
+ def segment_text(segment)
163
+ segment.value_text ||
164
+ (@segment_text || (whole? ? "%{percent}%" : "%{value}"))
165
+ .gsub("%{value}", number(segment.value))
166
+ .gsub("%{percent}", share(segment).to_s)
167
+ end
168
+
169
+ private
170
+
171
+ def state
172
+ return "indeterminate" if indeterminate?
173
+
174
+ value >= max ? "complete" : "loading"
175
+ end
176
+
177
+ # Rounded, not down: shares of a whole should add up to about 100.
178
+ def share(segment) = (segment.value / max.to_f * 100).round
179
+
180
+ # 3, not 3.0; 2.5 stays 2.5.
181
+ def number(n) = (n.to_f % 1).zero? ? n.to_i.to_s : n.to_s
182
+ end
@@ -0,0 +1,6 @@
1
+ <%# frozen_string_literal: true %>
2
+ <% group = capture do %><%= render(group_component) do |g| %>
3
+ <% g.with_field(**field_options) %>
4
+ <% g.with_addon(align: "inline-start") { |a| a.with_icon(name: "scan-barcode") } %>
5
+ <% end %><% end %>
6
+ <%= render(wrapper) { group } %>
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # A field for a barcode or QR scanner, or a code typed by hand. A scanner is
6
+ # a keyboard that types very fast and presses Enter; this field is ready for
7
+ # it: focused, with nothing in the way, and sending each code as it comes.
8
+ #
9
+ # <%= form_with url: cart_items_path do |f| %>
10
+ # <%= render(UI::ScanInput.new(name: "code", label: "Scan an item")) %>
11
+ # <% end %>
12
+ #
13
+ # Enter submits the form with the code and empties the field for the next
14
+ # one. A code that arrives while the last is still being sent (a Turbo form
15
+ # submission) waits its turn, so two quick scans are two requests, not one
16
+ # lost. Before that it fires `ui--scan-input:scan` with `{ code }`; call
17
+ # `preventDefault()` on it to handle the code in JavaScript instead.
18
+ # `submit: false` never submits, and only fires the event.
19
+ #
20
+ # It's focused on load and when it comes back in a Turbo update, and
21
+ # `keep_focus:` (on by default) takes focus back when it falls to the page,
22
+ # after a click on empty space or a dialog closing; moving to another field
23
+ # or button is left alone. `capture: true` goes further: keys typed with
24
+ # nothing focused go into the field, for a page that's only for scanning.
25
+ #
26
+ # `keyboard: false` keeps the on-screen keyboard down, for handheld scanners
27
+ # with a phone's screen. `suffix: :tab` is for scanners set to end a code
28
+ # with Tab instead of Enter. Spelling, capitals and autofill are off, since a
29
+ # code isn't a word.
30
+ #
31
+ # Each scan briefly rings the field, so the person scanning can see it took.
32
+ # It takes the same label, hint, error, size and form-builder options as
33
+ # UI::Input.
34
+ class UI::ScanInput < UI::Base
35
+ SIZES = {
36
+ sm: "h-8 [&>input]:h-full [&>input]:text-xs",
37
+ default: "h-9",
38
+ lg: "h-12 [&>input]:h-full [&>input]:text-base"
39
+ }.freeze
40
+
41
+ SUFFIXES = %i[enter tab].freeze
42
+
43
+ # The ring after a scan, over the group's own focus ring.
44
+ SCANNED = "data-scanned:border-emerald-500 data-scanned:ring-3 data-scanned:ring-emerald-500/30 " \
45
+ "motion-reduce:transition-none"
46
+
47
+ def initialize(name: nil, value: nil, form: nil, field: nil, label: nil, hint: nil, error: nil, placeholder: nil,
48
+ submit: true, clear: true, keep_focus: true, capture: false, keyboard: true, suffix: :enter,
49
+ autofocus: true, size: :default, required: false, disabled: false, class: nil, **html_attrs)
50
+ @name = name
51
+ @value = value
52
+ @form = form
53
+ @field = field
54
+ @placeholder = placeholder
55
+ @submit = submit
56
+ @clear = clear
57
+ @keep_focus = keep_focus
58
+ @capture = capture
59
+ @keyboard = keyboard
60
+ @suffix = SUFFIXES.include?(suffix&.to_sym) ? suffix.to_sym : :enter
61
+ @autofocus = autofocus
62
+ @size = SIZES.key?(size.to_sym) ? size.to_sym : :default
63
+ @required = required
64
+ @disabled = disabled
65
+ @user_class = binding.local_variable_get(:class)
66
+ @html_attrs = html_attrs
67
+ @input_id = html_attrs.delete(:id) || "ui-scan-input-#{SecureRandom.hex(4)}"
68
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error, required: required, disabled: disabled,
69
+ form: form, field: field, for: @input_id)
70
+ end
71
+
72
+ attr_reader :wrapper
73
+
74
+ def group_component
75
+ UI::InputGroup.new(
76
+ class: cn(SCANNED, SIZES[@size], @user_class),
77
+ data: { controller: "ui--scan-input", disabled: (true if @disabled),
78
+ "ui--scan-input-submit-value": @submit, "ui--scan-input-clear-value": @clear,
79
+ "ui--scan-input-keep-focus-value": @keep_focus, "ui--scan-input-capture-value": @capture,
80
+ "ui--scan-input-suffix-value": @suffix }.compact
81
+ )
82
+ end
83
+
84
+ def field_options
85
+ data = @html_attrs[:data] || {}
86
+ action = [ "keydown->ui--scan-input#key", data[:action] ].compact.join(" ")
87
+ { type: :text, form: @form, field: @field, name: (@name unless @form && @field), value: @value, label: false,
88
+ placeholder: @placeholder || t("ui.scan_input.placeholder", default: "Scan or type a code"),
89
+ required: @required, disabled: @disabled, error: wrapper.error?, "aria-describedby": wrapper.describedby,
90
+ autocomplete: "off", autocapitalize: "off", autocorrect: "off", spellcheck: false, enterkeyhint: "go",
91
+ inputmode: ("none" unless @keyboard), autofocus: (true if @autofocus && !@disabled),
92
+ **@html_attrs, id: @input_id, data: data.merge("ui--scan-input-target": "field", action: action) }.compact
93
+ end
94
+ end
@@ -1,4 +1,4 @@
1
- <%= tag.div(class: "relative w-full", data: root_data) do %>
1
+ <%= tag.div(class: "relative w-full", data: remote? ? root_data.merge(status_texts) : root_data) do %>
2
2
  <% if render_label? %>
3
3
  <div class="mb-2"><%= render(label_component) { label_text } %></div>
4
4
  <% end %>
@@ -13,7 +13,13 @@
13
13
  "data-[state=open]:animate-in data-[state=open]:fade-in-0 data-[state=open]:zoom-in-95") do %>
14
14
  <% if searchable? %>
15
15
  <div class="flex items-center gap-2 border-b px-3 py-2">
16
- <%= render(UI::Icon.new(name: "search", size: :sm, class: "shrink-0 text-muted-foreground")) %>
16
+ <% if remote? %>
17
+ <%# The magnifying glass turns into a spinner while a search is slow. %>
18
+ <span class="flex shrink-0 text-muted-foreground" data-ui--select-target="searchIcon"><%= render(UI::Icon.new(name: "search", size: :sm)) %></span>
19
+ <span class="hidden shrink-0 text-muted-foreground" data-ui--select-target="spinner" aria-hidden="true"><%= render(UI::Icon.new(name: "loader", size: :sm, class: "animate-spin motion-reduce:animate-[spin_1.5s_linear_infinite]")) %></span>
20
+ <% else %>
21
+ <%= render(UI::Icon.new(name: "search", size: :sm, class: "shrink-0 text-muted-foreground")) %>
22
+ <% end %>
17
23
  <%= tag.input(type: "search", id: search_id, autocomplete: "off",
18
24
  placeholder: t("ui.select.search", default: "Search"),
19
25
  class: "w-full bg-transparent text-sm outline-none placeholder:text-muted-foreground [&::-webkit-search-cancel-button]:hidden",
@@ -32,9 +38,7 @@
32
38
  <%= tag.div(id: option_id(index), role: "option", aria: { selected: selected?(value).to_s },
33
39
  data: { value: value, "ui--select-target": "option",
34
40
  action: "click->ui--select#choose mousemove->ui--select#hover" },
35
- class: "relative flex cursor-default select-none items-center rounded-sm py-1.5 pr-8 pl-2 #{size == :sm ? "text-xs" : "text-sm"} " \
36
- "outline-none data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground aria-selected:font-medium " \
37
- "#{value.to_s.empty? ? "text-muted-foreground" : ""}") do %>
41
+ class: option_classes(value)) do %>
38
42
  <span class="truncate"><%= label %></span>
39
43
  <span class="absolute right-2 flex size-4 items-center justify-center text-primary dark:text-foreground" data-ui--select-target="check" <%= "hidden" unless selected?(value) %>>
40
44
  <%= render(UI::Icon.new(name: "check", size: :sm)) %>
@@ -46,6 +50,21 @@
46
50
  <% if searchable? %>
47
51
  <p hidden data-ui--select-target="empty" class="px-3 py-3 text-sm text-muted-foreground"><%= t("ui.select.no_matches", default: "No matches") %></p>
48
52
  <% end %>
53
+ <% if remote? %>
54
+ <%# Searching and failures are announced; the options themselves are read as they're highlighted. %>
55
+ <p hidden role="status" data-ui--select-target="status" class="flex items-center gap-2 px-3 py-3 text-sm text-muted-foreground"></p>
56
+ <template data-ui--select-target="template">
57
+ <%= tag.div(role: "option", aria: { selected: "false" },
58
+ data: { value: "", "ui--select-target": "option",
59
+ action: "click->ui--select#choose mousemove->ui--select#hover" },
60
+ class: option_classes("x")) do %>
61
+ <span class="truncate"></span>
62
+ <span class="absolute right-2 flex size-4 items-center justify-center text-primary dark:text-foreground" data-ui--select-target="check" hidden>
63
+ <%= render(UI::Icon.new(name: "check", size: :sm)) %>
64
+ </span>
65
+ <% end %>
66
+ </template>
67
+ <% end %>
49
68
  <% end %>
50
69
 
51
70
  <% if wrapper.hint.present? || wrapper.error_message.present? %>
@@ -17,6 +17,25 @@ require "securerandom"
17
17
  # `searchable: true` adds a search box that filters the list; `multiple: true`
18
18
  # lets people pick several, posting `name[]` like a native multiple select.
19
19
  #
20
+ # `url:` searches on the server instead, for lists too long to send to the
21
+ # page, such as products, customers or suppliers. Opening the list asks
22
+ # `GET url?q=` for a starting list, and typing asks again with what was typed,
23
+ # a moment after the typing stops. The server answers with JSON, either
24
+ # `[{ "value": 7, "label": "Acme Traders" }, ...]` or `[["Acme Traders", 7], ...]`;
25
+ # it decides what matches and how many come back. `param:` renames `q`.
26
+ # `options:` are what show before then, so pass the current choice, or an edit
27
+ # form opens with nothing named:
28
+ #
29
+ # <%= render(UI::Select.new(form: f, field: :customer_id, url: customers_search_path,
30
+ # options: [[@order.customer&.name, @order.customer_id]].reject { _1.first.nil? },
31
+ # prompt: "Find a customer")) %>
32
+ #
33
+ # # app/controllers/customers_controller.rb
34
+ # def search
35
+ # customers = Customer.where("name ILIKE ?", "%#{Customer.sanitize_sql_like(params[:q].to_s)}%").order(:name).limit(20)
36
+ # render json: customers.map { { value: _1.id, label: _1.name } }
37
+ # end
38
+ #
20
39
  # UI::NativeSelect is the browser's own control, for when a phone's system
21
40
  # picker is the better fit.
22
41
  #
@@ -42,7 +61,7 @@ class UI::Select < UI::Base
42
61
  )
43
62
 
44
63
  def initialize(name: nil, options: [], selected: nil, prompt: nil,
45
- label: nil, hint: nil, size: :default, searchable: false, multiple: false,
64
+ label: nil, hint: nil, size: :default, searchable: false, multiple: false, url: nil, param: "q",
46
65
  required: false, error: nil, disabled: false,
47
66
  form: nil, field: nil,
48
67
  class: nil, **html_attrs)
@@ -52,7 +71,9 @@ class UI::Select < UI::Base
52
71
  @prompt = prompt
53
72
  @label = label
54
73
  @size = size
55
- @searchable = searchable
74
+ @url = url
75
+ @param = param
76
+ @searchable = searchable || url.present?
56
77
  @multiple = multiple
57
78
  @required = required
58
79
  @error = error
@@ -73,6 +94,7 @@ class UI::Select < UI::Base
73
94
  delegate :render_label?, :label_text, :label_component, to: :wrapper
74
95
 
75
96
  def searchable? = @searchable
97
+ def remote? = @url.present?
76
98
  def multiple? = @multiple
77
99
 
78
100
  def trigger_id = "ui-select-#{@uid}"
@@ -138,9 +160,26 @@ class UI::Select < UI::Base
138
160
  def summary = t("ui.select.selected", default: "%{count} selected", count: "%{count}")
139
161
 
140
162
  def root_data
141
- { controller: "ui--select", "ui--select-multiple-value": multiple?.to_s,
142
- "ui--select-summary-value": summary,
143
- "ui--select-placeholder-value": prompt || t("ui.select.placeholder", default: "Choose…") }
163
+ data = { controller: "ui--select", "ui--select-multiple-value": multiple?.to_s,
164
+ "ui--select-summary-value": summary,
165
+ "ui--select-placeholder-value": prompt || t("ui.select.placeholder", default: "Choose…") }
166
+ return data unless remote?
167
+
168
+ data.merge("ui--select-url-value": @url, "ui--select-param-value": @param)
169
+ end
170
+
171
+ # What a fetched option is built from: one rendered here, so it has the same
172
+ # classes as the options the page arrived with.
173
+ def option_classes(value)
174
+ "relative flex cursor-default select-none items-center rounded-sm py-1.5 pr-8 pl-2 #{size == :sm ? "text-xs" : "text-sm"} " \
175
+ "outline-none data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground aria-selected:font-medium " \
176
+ "#{value.to_s.empty? ? "text-muted-foreground" : ""}"
177
+ end
178
+
179
+ # Said while a search is on its way, and when one fails.
180
+ def status_texts
181
+ { "ui--select-searching-value": t("ui.select.searching", default: "Searching…"),
182
+ "ui--select-failed-value": t("ui.select.failed", default: "Couldn't load the results. Try again.") }
144
183
  end
145
184
 
146
185
  # The hidden native select. Everything the caller passed beyond our own
@@ -0,0 +1,23 @@
1
+ <%# frozen_string_literal: true %>
2
+ <%= tag.div(**root_attrs) do %>
3
+ <% if header? %>
4
+ <div class="flex items-baseline justify-between gap-3 text-sm">
5
+ <% if label_text %><%= tag.label(label_text, id: label_id, for: (fields.first[:id] unless range?), class: "font-medium leading-none") %><% end %>
6
+ <% if show_value? %><%= tag.output(output_text, class: "ms-auto tabular-nums text-muted-foreground", "aria-hidden": "true", data: { "ui--slider-target": "output" }) %><% end %>
7
+ </div>
8
+ <% end %>
9
+ <%= render(wrapper) do %>
10
+ <%= tag.div(**track_attrs) do %>
11
+ <%= tag.div(class: track_class) do %>
12
+ <%= tag.div(class: UI::Slider::FILL, style: fill_style, data: { slot: "slider-fill" }) %>
13
+ <% end %>
14
+ <%# Each knob follows the input just before it; the span keeps a range's pairs apart. %>
15
+ <% fields.each do |handle| %><span class="contents"><%= render_input(handle) %><%= tag.span(class: UI::Slider::KNOB, style: knob_style(handle), "aria-hidden": "true", data: { slot: "slider-thumb" }) %></span><% end %>
16
+ <% end %>
17
+ <% if marks? %>
18
+ <%= tag.div(class: "relative h-4 text-xs text-muted-foreground", "aria-hidden": "true") do %>
19
+ <% marks.each do |mark| %><%= tag.span(mark[:label], **mark_attrs(mark)) %><% end %>
20
+ <% end %>
21
+ <% end %>
22
+ <% end %>
23
+ <% end %>