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,52 @@
1
+ <%= tag.div(**root_attrs) do %>
2
+ <%= tag.div class: "relative min-w-0" do %>
3
+ <%= tag.ul class: track_classes, data: { "ui--gallery-target": "track", action: "scroll->ui--gallery#scrolled pointerdown->ui--gallery#released" } do %>
4
+ <% images.each_with_index do |image, index| %>
5
+ <%= tag.li class: slide_classes, data: { "ui--gallery-target": "slide", zoom_src: asset_path(image.zoom_src), alt: image.alt } do %>
6
+ <%= tag.button(**slide_button_attrs(image, index)) do %>
7
+ <%= render(UI::Image.new(src: image.src, alt: image.alt, rounded: :none, loading: image_loading(index), class: image_classes, draggable: "false")) %>
8
+ <%= tag.span class: "pointer-events-none absolute end-2 top-2 flex rounded-full bg-background/90 p-1.5 text-foreground", aria: { hidden: true } do %><%= render(UI::Icon.new(name: "zoom-in", size: :sm)) %><% end %>
9
+ <% end %>
10
+ <% end %>
11
+ <% end %>
12
+ <% end %>
13
+ <% if hover_zoom? %>
14
+ <%= tag.div aria: { hidden: true }, class: cn(UI::Gallery::HOVER_REVEAL, "left-0 top-0 z-10 rounded-sm border border-primary/50 bg-primary/15"), data: { "ui--gallery-target": "lens" } %>
15
+ <%= tag.div aria: { hidden: true }, class: cn(UI::Gallery::HOVER_REVEAL, "start-full top-0 z-50 ms-4 origin-[0_50%] scale-95 overflow-hidden rounded-lg border bg-background bg-no-repeat shadow-lg data-[inside]:start-0 data-[inside]:ms-0 data-[shown]:scale-100 rtl:origin-[100%_50%]"), data: { "ui--gallery-target": "pane" } %>
16
+ <% end %>
17
+ <% if multiple? %>
18
+ <%= render(UI::Button.new(**nav_button_attrs(:previous))) do %><%= render(UI::Icon.new(name: "chevron-left", size: :sm, class: "rtl:rotate-180")) %><% end %>
19
+ <%= render(UI::Button.new(**nav_button_attrs(:next))) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %>
20
+ <% end %>
21
+ <% end %>
22
+ <%= tag.p position_text, class: "sr-only", aria: { live: "polite" }, data: { "ui--gallery-target": "status" } %>
23
+ <% if multiple? %>
24
+ <%= tag.ul class: thumbnail_strip_classes do %>
25
+ <% images.each_with_index do |image, index| %>
26
+ <li class="shrink-0"><%= tag.button(**thumbnail_attrs(index)) do %><%= render(UI::Image.new(src: image.thumbnail_src, alt: "", rounded: :none, class: "size-full object-cover", draggable: "false")) %><% end %></li>
27
+ <% end %>
28
+ <% end %>
29
+ <% end %>
30
+ <%= tag.dialog class: dialog_classes, tabindex: -1,
31
+ aria: { label: t("ui.gallery.dialog", default: "Zoomed image") },
32
+ data: { "ui--gallery-target": "dialog", action: "close->ui--gallery#closed cancel->ui--gallery#cancel" } do %>
33
+ <%= tag.div class: viewport_classes,
34
+ data: { "ui--gallery-target": "viewport",
35
+ action: "click->ui--gallery#viewportClick pointerdown->ui--gallery#panStart pointermove->ui--gallery#panMove " \
36
+ "pointerup->ui--gallery#panEnd pointercancel->ui--gallery#panEnd wheel->ui--gallery#wheel" } do %>
37
+ <%= tag.img alt: "", draggable: "false",
38
+ class: "m-auto max-w-none shrink-0 cursor-zoom-in select-none group-data-[zoomed]/viewport:cursor-grab",
39
+ data: { "ui--gallery-target": "zoomImage", action: "load->ui--gallery#layout" } %>
40
+ <% end %>
41
+ <%= tag.p position_text, class: "sr-only", aria: { live: "polite" }, data: { "ui--gallery-target": "zoomStatus" } %>
42
+ <%= tag.div class: "absolute start-4 top-4 z-10 flex gap-2" do %>
43
+ <%= render(UI::Button.new(**zoom_button_attrs(:out, "Zoom out"))) do %><%= render(UI::Icon.new(name: "zoom-out", size: :sm)) %><% end %>
44
+ <%= render(UI::Button.new(**zoom_button_attrs(:in, "Zoom in"))) do %><%= render(UI::Icon.new(name: "zoom-in", size: :sm)) %><% end %>
45
+ <% end %>
46
+ <%= render(UI::Button.new(variant: :ghost, size: :icon, class: cn(UI::Gallery::OVERLAY_BUTTON, "absolute end-4 top-4 z-10"), aria: { label: t("ui.gallery.close", default: "Close") }, data: { action: "ui--gallery#close" })) do %><%= render(UI::Icon.new(name: "x", size: :sm)) %><% end %>
47
+ <% if multiple? %>
48
+ <%= render(UI::Button.new(**overlay_nav_attrs(:previous))) do %><%= render(UI::Icon.new(name: "chevron-left", size: :sm, class: "rtl:rotate-180")) %><% end %>
49
+ <%= render(UI::Button.new(**overlay_nav_attrs(:next))) do %><%= render(UI::Icon.new(name: "chevron-right", size: :sm, class: "rtl:rotate-180")) %><% end %>
50
+ <% end %>
51
+ <% end %>
52
+ <% end %>
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A product's photos: one large image, thumbnails to pick another, and two ways
4
+ # to look closer.
5
+ #
6
+ # <%= render(UI::Gallery.new(label: "Photos of the ceramic mug")) do |gallery| %>
7
+ # <% gallery.with_image(src: "mug.jpg", alt: "Mug, front", zoom_src: "mug-2000.jpg", thumbnail_src: "mug-160.jpg") %>
8
+ # <% gallery.with_image(src: "mug-side.jpg", alt: "Mug, from the side") %>
9
+ # <% end %>
10
+ #
11
+ # The main view is a scroll-snap strip, so it swipes on a touch screen with no
12
+ # script involved. Previous and next wrap round, and arrow keys do the same.
13
+ # Only the first image loads with the page. `zoom_src:` and `thumbnail_src:` are
14
+ # optional, and worth giving: the zoomed image only downloads when someone opens
15
+ # it, so it can be much larger than `src:`.
16
+ #
17
+ # Clicking the image opens it by itself over the dimmed page, with zoom and close
18
+ # in the top corners and previous and next at the sides. There, clicking zooms in
19
+ # on that spot and clicking again zooms out; so do the buttons, the + and - keys,
20
+ # 0 to reset, and pinching a trackpad. A zoomed image pans by dragging or
21
+ # scrolling. Clicking outside it or pressing Escape closes it. The image grows
22
+ # out of its place on the page and returns there, and all the movement stops for
23
+ # anyone whose system asks for reduced motion.
24
+ #
25
+ # `zoom: :hover` adds what big shops do: moving a mouse over the image shows a
26
+ # lens on it and that part magnified beside it, `zoom_scale:` times over (2.5 by
27
+ # default). With no room beside it, the magnified part shows over the image. A
28
+ # click still opens the overlay, which is also what touch screens and the
29
+ # keyboard get, having no mouse to hover with.
30
+ #
31
+ # `aspect:` is :square, :landscape or :portrait, and `fit:` is :contain (the whole
32
+ # photo) or :cover (fills the frame, cropping). A single image gets no thumbnails
33
+ # or arrows.
34
+ class UI::Gallery < UI::Base
35
+ ASPECTS = { square: "aspect-square", landscape: "aspect-video", portrait: "aspect-[4/5]" }.freeze
36
+ FITS = { contain: "object-contain", cover: "object-cover" }.freeze
37
+ ZOOMS = %i[overlay hover].freeze
38
+
39
+ HIDE_SCROLLBAR = "[scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
40
+
41
+ # The lens and pane fade in and out, rather than blinking on and off with `hidden`.
42
+ HOVER_REVEAL = "pointer-events-none invisible absolute opacity-0 transition-[opacity,scale,visibility] duration-150 " \
43
+ "data-[shown]:visible data-[shown]:opacity-100 motion-reduce:transition-none"
44
+
45
+ # Always light on the dark backdrop, whatever the page's theme, ring included.
46
+ # The border keeps them in sight on a dark page, where the backdrop is black too.
47
+ OVERLAY_BUTTON = "rounded-full border border-white/25 bg-black/60 text-white hover:bg-black/80 hover:text-white " \
48
+ "focus:ring-white disabled:bg-black/60! disabled:text-white/50!"
49
+
50
+ renders_many :images, UI::Gallery::Image
51
+
52
+ def initialize(label: nil, aspect: :square, fit: :contain, zoom: :overlay, zoom_scale: 2.5, class: nil, **html_attrs)
53
+ @label = label
54
+ @aspect = ASPECTS.key?(aspect.to_sym) ? aspect.to_sym : :square
55
+ @fit = FITS.key?(fit.to_sym) ? fit.to_sym : :contain
56
+ @zoom = ZOOMS.include?(zoom.to_sym) ? zoom.to_sym : :overlay
57
+ @zoom_scale = zoom_scale
58
+ @user_class = binding.local_variable_get(:class)
59
+ @html_attrs = html_attrs
60
+ end
61
+
62
+ def render? = images.any?
63
+ def multiple? = images.size > 1
64
+ def hover_zoom? = @zoom == :hover
65
+
66
+ def root_attrs
67
+ attrs = @html_attrs.except(:data, :aria)
68
+ attrs[:role] = "group"
69
+ attrs[:class] = cn("flex w-full min-w-0 flex-col gap-3", @user_class)
70
+ attrs[:aria] = { label: label, roledescription: t("ui.gallery.roledescription", default: "gallery") }
71
+ .merge(@html_attrs[:aria] || {})
72
+ attrs[:data] = { slot: "gallery", controller: "ui--gallery", zoom: @zoom,
73
+ "ui--gallery-zoom-scale-value": @zoom_scale,
74
+ "ui--gallery-position-value": position_format,
75
+ action: "keydown@window->ui--gallery#keydown resize@window->ui--gallery#layout" }
76
+ .merge(@html_attrs[:data] || {})
77
+ attrs
78
+ end
79
+
80
+ def track_classes
81
+ cn("flex snap-x snap-mandatory overflow-x-auto rounded-lg bg-muted", HIDE_SCROLLBAR)
82
+ end
83
+
84
+ def slide_classes = cn("w-full shrink-0 snap-center", ASPECTS[@aspect])
85
+
86
+ def slide_button_attrs(image, index)
87
+ # One tab stop for the strip: the image showing. The rest are out of sight.
88
+ { type: "button", tabindex: index.zero? ? 0 : -1,
89
+ class: cn("group/slide relative block size-full rounded-lg transition-all focus:outline-none",
90
+ "focus:ring-2 focus:ring-inset focus:ring-primary", hover_zoom? ? "cursor-crosshair" : "cursor-zoom-in"),
91
+ aria: { label: phrase(t("ui.gallery.zoom", default: "Zoom image %{current}: %{alt}"), current: index + 1, alt: image.alt),
92
+ haspopup: "dialog" },
93
+ data: { action: slide_action, "ui--gallery-index-param": index } }
94
+ end
95
+
96
+ # Covers the window with nothing of its own: only the image and its buttons sit
97
+ # on the backdrop.
98
+ def dialog_classes
99
+ "m-0 size-full max-h-none max-w-none overflow-hidden bg-transparent p-0 outline-none backdrop:bg-black/80"
100
+ end
101
+
102
+ def viewport_classes
103
+ cn("group/viewport absolute inset-0 flex overflow-auto overscroll-contain", HIDE_SCROLLBAR)
104
+ end
105
+
106
+ def image_classes = cn("size-full", FITS[@fit])
107
+
108
+ # Only the first image loads with the page. The rest wait to be scrolled to.
109
+ def image_loading(index) = index.zero? ? :eager : :lazy
110
+
111
+ # The padding leaves room for the focus ring, and the margin takes it back, so
112
+ # the first thumbnail lines up with the edge of the image.
113
+ def thumbnail_strip_classes = cn("-mx-1 flex gap-2 overflow-x-auto p-1", HIDE_SCROLLBAR)
114
+
115
+ def thumbnail_attrs(index)
116
+ { type: "button",
117
+ class: cn("block size-16 shrink-0 overflow-hidden rounded-md border-2 border-transparent opacity-70 hover:opacity-100",
118
+ "aria-current:border-primary aria-current:opacity-100", UI::Base::FOCUS_RING),
119
+ aria: { label: phrase(t("ui.gallery.thumbnail", default: "Show image %{current}"), current: index + 1),
120
+ current: ("true" if index.zero?) }.compact,
121
+ data: { action: "ui--gallery#select", "ui--gallery-index-param": index, "ui--gallery-target": "thumbnail" } }
122
+ end
123
+
124
+ def nav_button_attrs(direction)
125
+ edge = direction == :previous ? "start-2" : "end-2"
126
+ { variant: :outline, size: :icon,
127
+ class: cn("absolute top-1/2 z-10 hidden -translate-y-1/2 rounded-full bg-background/90 sm:inline-flex", edge),
128
+ aria: { label: nav_label(direction) }, data: { action: "ui--gallery##{direction}" } }
129
+ end
130
+
131
+ def overlay_nav_attrs(direction)
132
+ edge = direction == :previous ? "start-4" : "end-4"
133
+ { variant: :ghost, size: :icon,
134
+ class: cn(OVERLAY_BUTTON, "absolute top-1/2 z-10 -translate-y-1/2", edge),
135
+ aria: { label: nav_label(direction) }, data: { action: "ui--gallery##{direction}" } }
136
+ end
137
+
138
+ def zoom_button_attrs(direction, label)
139
+ { variant: :ghost, size: :icon, class: OVERLAY_BUTTON,
140
+ aria: { label: t("ui.gallery.zoom_#{direction}", default: label) },
141
+ data: { action: "ui--gallery#zoom#{direction.to_s.capitalize}", "ui--gallery-target": "zoom#{direction.to_s.capitalize}" } }
142
+ end
143
+
144
+ def position_text = phrase(position_format, current: 1, total: images.size)
145
+
146
+ private
147
+
148
+ def nav_label(direction) = t("ui.gallery.#{direction}", default: "#{direction.to_s.capitalize} image")
149
+
150
+ def slide_action
151
+ return "ui--gallery#open" unless hover_zoom?
152
+
153
+ "ui--gallery#open pointerenter->ui--gallery#hover pointermove->ui--gallery#hover pointerleave->ui--gallery#hoverEnd"
154
+ end
155
+
156
+ def label = @label || t("ui.gallery.label", default: "Image gallery")
157
+ def position_format = t("ui.gallery.position", default: "Image %{current} of %{total}")
158
+
159
+ # Filled in here rather than by t(), so the controller can be handed the same
160
+ # text with its placeholders still in it.
161
+ def phrase(format, **values)
162
+ values.reduce(format) { |text, (key, value)| text.sub("%{#{key}}", value.to_s) }
163
+ end
164
+ end
@@ -0,0 +1,27 @@
1
+ <%# frozen_string_literal: true %>
2
+ <%= tag.kbd(**root_attrs) do %>
3
+ <% if written? %>
4
+ <%= tag.kbd(content, class: key_class) %>
5
+ <% else %>
6
+ <% steps.each_with_index do |combo, step| %>
7
+ <% if step.positive? %><span class="text-[0.85em] opacity-70"><%= t("ui.kbd.then", default: "then") %></span><% end %>
8
+ <span class="inline-flex items-center gap-0.5">
9
+ <% combo.each_with_index do |key, index| %>
10
+ <% if index.positive? %><span class="opacity-60 group-data-[platform=mac]/kbd:hidden" aria-hidden="true">+</span><% end %>
11
+ <% mac, mac_name, other, other_name = key_labels(key, combo) %>
12
+ <% linux, linux_name = linux_labels(key) %>
13
+ <% if linux %>
14
+ <%= key_tag(mac, mac_name, "hidden group-data-[platform=mac]/kbd:inline-flex") %>
15
+ <%= key_tag(other, other_name, "group-data-[platform=mac]/kbd:hidden group-data-[platform=linux]/kbd:hidden") %>
16
+ <%= key_tag(linux, linux_name, "hidden group-data-[platform=linux]/kbd:inline-flex") %>
17
+ <% elsif mac == other && mac_name == other_name %>
18
+ <%= key_tag(mac, mac_name) %>
19
+ <% else %>
20
+ <%= key_tag(mac, mac_name, "hidden group-data-[platform=mac]/kbd:inline-flex") %>
21
+ <%= key_tag(other, other_name, "group-data-[platform=mac]/kbd:hidden") %>
22
+ <% end %>
23
+ <% end %>
24
+ </span>
25
+ <% end %>
26
+ <% end %>
27
+ <% end %>
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ # A keyboard key, or a shortcut, written the way the reader's computer labels
4
+ # it. "mod" is ⌘ on a Mac and Ctrl elsewhere:
5
+ #
6
+ # <%= render(UI::Kbd.new("mod+k")) %> ⌘K, or Ctrl+K
7
+ # <%= render(UI::Kbd.new("shift+enter")) %> ⇧↵, or Shift+Enter
8
+ # <%= render(UI::Kbd.new(%w[g i])) %> G then I
9
+ # <%= render(UI::Kbd.new) { "Fn" } %> any key, as written
10
+ #
11
+ # It takes its colours from the text around it, so it reads the same in a
12
+ # paragraph, a menu, a tooltip or a filled button.
13
+ #
14
+ # Names: mod, cmd (meta), ctrl, alt (option), shift, enter, esc, tab, space,
15
+ # backspace, delete, up, down, left, right; anything else (k, /, F5) is shown
16
+ # as written. A letter is lowercase on its own (n, g then i), since that's what
17
+ # you type, and a capital with a modifier (⌘K), the way menus show it; Shift
18
+ # is always written out (⇧G), so a capital never means Shift. Screen readers
19
+ # hear the key's name, "Command", not the symbol.
20
+ #
21
+ # The Mac or not guess comes from the browser's user agent, so the page
22
+ # doesn't change once it's loaded; the controller corrects it if a cached
23
+ # page guessed for someone else.
24
+ #
25
+ # `bind: true` makes the shortcut press the button or link it sits in (a
26
+ # sequence can't be bound); `bind: "#save"` presses that element instead, or
27
+ # focuses it if it's a field, like "/" for search. It
28
+ # leaves typing alone, unless the shortcut uses mod, ctrl or alt, and the
29
+ # element gets aria-keyshortcuts so screen readers announce it.
30
+ class UI::Kbd < UI::Base
31
+ # name => [Mac symbol, Mac name, other label, other name]
32
+ KEYS = {
33
+ "mod" => [ "⌘", "Command", "Ctrl", "Control" ],
34
+ "cmd" => [ "⌘", "Command", "Win", "Windows" ],
35
+ "ctrl" => [ "⌃", "Control", "Ctrl", "Control" ],
36
+ "alt" => [ "⌥", "Option", "Alt", "Alt" ],
37
+ "shift" => [ "⇧", "Shift", "Shift", "Shift" ],
38
+ "enter" => [ "↵", "Return", "Enter", "Enter" ],
39
+ "esc" => [ "Esc", "Escape", "Esc", "Escape" ],
40
+ "tab" => [ "⇥", "Tab", "Tab", "Tab" ],
41
+ "space" => [ "Space", "Space", "Space", "Space" ],
42
+ "backspace" => [ "⌫", "Delete", "Backspace", "Backspace" ],
43
+ "delete" => [ "⌦", "Forward Delete", "Del", "Delete" ],
44
+ "up" => [ "↑", "Up Arrow", "↑", "Up Arrow" ],
45
+ "down" => [ "↓", "Down Arrow", "↓", "Down Arrow" ],
46
+ "left" => [ "←", "Left Arrow", "←", "Left Arrow" ],
47
+ "right" => [ "→", "Right Arrow", "→", "Right Arrow" ]
48
+ }.freeze
49
+
50
+ # Where Linux calls a key something else: the Windows key is Super there.
51
+ LINUX_KEYS = { "cmd" => [ "Super", "Super" ] }.freeze
52
+
53
+ ALIASES = { "command" => "cmd", "meta" => "cmd", "control" => "ctrl", "option" => "alt", "opt" => "alt",
54
+ "return" => "enter", "escape" => "esc", "del" => "delete", "arrowup" => "up",
55
+ "arrowdown" => "down", "arrowleft" => "left", "arrowright" => "right" }.freeze
56
+
57
+ SIZES = {
58
+ sm: "h-4 min-w-4 px-1 text-[10px]",
59
+ default: "h-5 min-w-5 px-1 text-xs",
60
+ lg: "h-6 min-w-6 px-1.5 text-sm"
61
+ }.freeze
62
+
63
+ KEY = "inline-flex items-center justify-center rounded-sm border border-current/15 bg-current/[0.07] " \
64
+ "font-sans font-medium leading-none tracking-normal whitespace-nowrap shadow-[0_1px_0] shadow-current/10"
65
+
66
+ APPLE = /Macintosh|Mac OS X|iPhone|iPad|iPod/
67
+ # A Linux desktop; Android and ChromeOS say Linux too, but don't call it Super.
68
+ LINUX = /Linux|X11/
69
+ NOT_LINUX = /Android|CrOS/
70
+
71
+ MODIFIERS = %w[mod cmd ctrl alt shift].freeze
72
+
73
+ def initialize(keys = nil, size: :default, bind: nil, class: nil, **html_attrs)
74
+ @keys = keys
75
+ @size = SIZES.key?(size.to_sym) ? size.to_sym : :default
76
+ @bind = bind
77
+ @user_class = binding.local_variable_get(:class)
78
+ @html_attrs = html_attrs
79
+ end
80
+
81
+ # One combo per step: "mod+k" is one step, %w[g i] two.
82
+ def steps
83
+ Array(@keys).map { |combo| combo.to_s.split("+").map(&:strip).reject(&:empty?) }.reject(&:empty?)
84
+ end
85
+
86
+ def written? = @keys.nil?
87
+
88
+ # mac, linux, or other (Windows and the rest), as the user agent suggests.
89
+ def platform
90
+ agent = request&.user_agent.to_s
91
+ return "mac" if agent.match?(APPLE)
92
+ return "linux" if agent.match?(LINUX) && !agent.match?(NOT_LINUX)
93
+
94
+ "other"
95
+ rescue StandardError
96
+ "other"
97
+ end
98
+
99
+ # [label, name] for a key Linux names differently, or nil.
100
+ def linux_labels(key) = LINUX_KEYS[ALIASES.fetch(key.downcase, key.downcase)]
101
+
102
+ # [mac label, mac name, other label, other name] for one key of a combo.
103
+ def key_labels(key, combo = [ key ])
104
+ name = ALIASES.fetch(key.downcase, key.downcase)
105
+ return KEYS[name] if KEYS.key?(name)
106
+
107
+ label = key.length == 1 ? (modified?(combo) ? key.upcase : key.downcase) : key
108
+ [ label, label, label, label ]
109
+ end
110
+
111
+ def key_class = cn(KEY, SIZES[@size])
112
+
113
+ def modified?(combo) = combo.any? { |key| MODIFIERS.include?(ALIASES.fetch(key.downcase, key.downcase)) }
114
+
115
+ # One key: the symbol shown, its name for screen readers. A key that's
116
+ # already a word (Esc, F5) is just the word.
117
+ def key_tag(label, name, extra = nil)
118
+ face = label == name ? label : safe_join([ tag.span(label, aria: { hidden: true }), tag.span(name, class: "sr-only") ])
119
+ tag.kbd(face, class: cn(key_class, extra))
120
+ end
121
+
122
+ def root_attrs
123
+ { **@html_attrs, class: cn("group/kbd inline-flex items-center gap-1 align-middle font-sans font-normal text-current/80", @user_class),
124
+ data: { slot: "kbd", platform: platform, controller: "ui--kbd", **bind_data, **(@html_attrs[:data] || {}) } }
125
+ end
126
+
127
+ private
128
+
129
+ def bind_data
130
+ return {} unless @bind && steps.size == 1
131
+
132
+ { "ui--kbd-keys-value": steps.first.join("+"), "ui--kbd-target-value": (@bind == true ? "" : @bind.to_s) }
133
+ end
134
+ end
@@ -0,0 +1,8 @@
1
+ <%# frozen_string_literal: true %>
2
+ <%= tag.div(**root_attrs) do %>
3
+ <% control = capture do %><%= render(group_component) do |g| %>
4
+ <% g.with_field(**field_options) %>
5
+ <% g.with_addon(align: unit_align) { unit } %>
6
+ <% end %><%= tag.input(**hidden_attrs) %><% end %>
7
+ <%= render(wrapper) { control } %>
8
+ <% end %>
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "securerandom"
5
+
6
+ # An amount of money, written the way people read it: grouped as it's typed,
7
+ # with the currency sign beside it and the paise or cents filled in when the
8
+ # field is left. The form gets the plain number.
9
+ #
10
+ # <%= render(UI::MoneyInput.new(name: "amount", value: 123456.5, unit: "₹", label: "Amount")) %>
11
+ # <%= render(UI::MoneyInput.new(form: f, field: :price)) %>
12
+ #
13
+ # The reader sees "1,23,456.50"; a hidden field posts "123456.50", so the
14
+ # controller never has to strip commas. Typing anything but digits and one
15
+ # decimal point is ignored, a pasted "₹1,23,456.789" is cleaned up, and no
16
+ # more decimals than `precision:` can be typed. It's a text field with the
17
+ # decimal keypad on phones, not type="number", which can't show grouping.
18
+ #
19
+ # The sign, separator, delimiter, precision and the sign's side come from the
20
+ # app's locale (`number.currency.format`), so each can be left out. `unit: "₹"`
21
+ # groups the Indian way (1,23,45,678) unless `grouping: :thousands` says
22
+ # otherwise; `grouping: :indian` asks for it with any sign. `precision: 0` is
23
+ # for whole amounts. Amounts can't be negative unless `negative: true`.
24
+ #
25
+ # ↑ and ↓ add or take away one whole unit, or `step:` (0.5, 100), and Shift
26
+ # ten of them. Each press fires `input` and `change`, as a number field does.
27
+ #
28
+ # It takes the same label, hint, error, size and form-builder options as
29
+ # UI::NumberInput. The visible field fires `input` and `change` as usual; read
30
+ # the amount from the hidden field (`[data-ui--money-input-target=value]`).
31
+ class UI::MoneyInput < UI::Base
32
+ SIZES = {
33
+ sm: "h-8 [&>input]:h-full [&>input]:text-xs",
34
+ default: "h-9",
35
+ lg: "h-12 [&>input]:h-full [&>input]:text-base"
36
+ }.freeze
37
+
38
+ GROUPINGS = %i[thousands indian].freeze
39
+
40
+ def initialize(name: nil, value: nil, form: nil, field: nil, unit: nil, precision: nil, separator: nil,
41
+ delimiter: nil, grouping: nil, negative: false, step: 1, size: :default, label: nil, hint: nil,
42
+ error: nil, required: false, disabled: false, placeholder: nil, class: nil, **html_attrs)
43
+ @name = name
44
+ @form = form
45
+ @field = field
46
+ @unit = unit || currency(:unit, "$")
47
+ @precision = Integer(precision || currency(:precision, 2))
48
+ @separator = separator || currency(:separator, ".")
49
+ @delimiter = delimiter || currency(:delimiter, ",")
50
+ @grouping = GROUPINGS.include?(grouping&.to_sym) ? grouping.to_sym : (@unit == "₹" ? :indian : :thousands)
51
+ @negative = negative
52
+ @step = step
53
+ @size = SIZES.key?(size.to_sym) ? size.to_sym : :default
54
+ @required = required
55
+ @disabled = disabled
56
+ @placeholder = placeholder
57
+ @user_class = binding.local_variable_get(:class)
58
+ @html_attrs = html_attrs
59
+ @input_id = html_attrs.delete(:id) || "ui-money-input-#{SecureRandom.hex(4)}"
60
+ @amount = amount(bound || value)
61
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error, required: required, disabled: disabled,
62
+ form: form, field: field, for: @input_id)
63
+ end
64
+
65
+ attr_reader :wrapper
66
+
67
+ def root_attrs
68
+ { class: cn("w-full", @user_class),
69
+ data: { slot: "money-input", controller: "ui--money-input", "ui--money-input-separator-value": @separator,
70
+ "ui--money-input-delimiter-value": @delimiter, "ui--money-input-precision-value": @precision,
71
+ "ui--money-input-grouping-value": @grouping, "ui--money-input-negative-value": @negative,
72
+ "ui--money-input-step-value": BigDecimal(@step.to_s).to_s("F").delete_suffix(".0") } }
73
+ end
74
+
75
+ def group_component
76
+ UI::InputGroup.new(class: cn("[&>input]:tabular-nums", SIZES[@size]),
77
+ data: { disabled: (true if @disabled) }.compact)
78
+ end
79
+
80
+ def field_options
81
+ data = @html_attrs[:data] || {}
82
+ action = [ "beforeinput->ui--money-input#guard keydown->ui--money-input#nudge input->ui--money-input#type " \
83
+ "blur->ui--money-input#finish", data[:action] ].compact.join(" ")
84
+ { type: :text, inputmode: "decimal", autocomplete: "off", spellcheck: false, value: shown,
85
+ placeholder: @placeholder || shown(BigDecimal(0)), required: @required, disabled: @disabled,
86
+ error: wrapper.error?, "aria-describedby": wrapper.describedby, **@html_attrs, id: @input_id,
87
+ data: data.merge("ui--money-input-target": "field", action: action) }.compact
88
+ end
89
+
90
+ def hidden_attrs
91
+ { type: "hidden", name: hidden_name, value: plain, disabled: (true if @disabled),
92
+ data: { "ui--money-input-target": "value" } }.compact
93
+ end
94
+
95
+ def unit = @unit
96
+
97
+ # The sign goes where the locale writes it: "%u%n" before, "%n %u" after.
98
+ def unit_align
99
+ pattern = currency(:format, "%u%n")
100
+ pattern.index("%u").to_i > pattern.index("%n").to_i ? "inline-end" : "inline-start"
101
+ end
102
+
103
+ # What the reader sees: "1,23,456.50".
104
+ def shown(amount = @amount) = written(amount, @separator) { |whole| group(whole) }
105
+
106
+ # What the form posts: "123456.50". Built from the digits, never a float.
107
+ def plain = written(@amount, ".").to_s
108
+
109
+ private
110
+
111
+ def written(amount, separator)
112
+ return nil if amount.nil?
113
+
114
+ whole, fraction = amount.abs.to_s("F").split(".")
115
+ text = block_given? ? yield(whole) : whole
116
+ text += separator + fraction.to_s.ljust(@precision, "0")[0, @precision] if @precision.positive?
117
+ amount.negative? ? "-#{text}" : text
118
+ end
119
+
120
+ def currency(key, default) = I18n.t("number.currency.format.#{key}", default: default)
121
+
122
+ def hidden_name
123
+ return "#{@form.object_name}[#{@field}]" if @form && @field
124
+
125
+ @name
126
+ end
127
+
128
+ def bound
129
+ return nil unless @form && @field && @form.object.respond_to?(@field)
130
+
131
+ @form.object.public_send(@field)
132
+ end
133
+
134
+ def amount(value)
135
+ return nil if value.blank?
136
+
137
+ number = value.is_a?(Numeric) ? BigDecimal(value.to_s) : BigDecimal(value.to_s.delete(@delimiter).tr(@separator, "."))
138
+ number = number.abs unless @negative
139
+ # round(0) gives an Integer; keep a BigDecimal.
140
+ BigDecimal(number.round(@precision).to_s)
141
+ rescue ArgumentError
142
+ nil
143
+ end
144
+
145
+ # 1234567 -> 1,234,567, or the Indian way, 12,34,567: the last three
146
+ # digits, then twos.
147
+ def group(digits)
148
+ return digits.gsub(/\B(?=(\d{3})+(?!\d))/, @delimiter) unless @grouping == :indian
149
+ return digits if digits.length <= 3
150
+
151
+ "#{digits[0...-3].gsub(/\B(?=(\d{2})+(?!\d))/, @delimiter)}#{@delimiter}#{digits[-3..]}"
152
+ end
153
+ end
@@ -0,0 +1,11 @@
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", class: UI::NumberInput::ADDON_CLASSES) do %>
5
+ <%= render(UI::Button.new(**step_button_options(:decrement))) do %><%= render(UI::Icon.new(name: "minus", size: icon_size)) %><% end %>
6
+ <% end %>
7
+ <% g.with_addon(align: "inline-end", class: UI::NumberInput::ADDON_CLASSES) do %>
8
+ <%= render(UI::Button.new(**step_button_options(:increment))) do %><%= render(UI::Icon.new(name: "plus", size: icon_size)) %><% end %>
9
+ <% end %>
10
+ <% end %><% end %>
11
+ <%= render(wrapper) { group } %>
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ # A number field with - and + buttons, for quantities and stock counts. It's a
6
+ # real <input type="number">, so typing, arrow keys, `min:`, `max:`, `step:` and
7
+ # form validation all work as usual, and it takes the same label, hint, error
8
+ # and form-builder options as UI::Input.
9
+ #
10
+ # <%= render(UI::NumberInput.new(form: f, field: :quantity, min: 1, max: 99, class: "w-32")) %>
11
+ # <%= render(UI::NumberInput.new(name: "weight", value: 1.5, step: 0.25, size: :lg)) %>
12
+ #
13
+ # Holding a button keeps counting. Each step fires `input`, and `change` fires
14
+ # once when the button is let go, so a form that submits on change sends one
15
+ # request however long the button is held. A button turns off at its limit.
16
+ #
17
+ # The buttons are left out of the tab order: arrow keys do the same job from
18
+ # the field, and tabbing along a row of quantities shouldn't stop three times.
19
+ # Screen readers can still reach them. `size: :lg` is for touch screens.
20
+ class UI::NumberInput < UI::Base
21
+ SIZES = {
22
+ sm: { group: "h-8 [&>input]:text-xs", button: "w-8", icon: :xs },
23
+ default: { group: "h-9", button: "w-9", icon: :sm },
24
+ lg: { group: "h-12 [&>input]:text-base", button: "w-12", icon: :default }
25
+ }.freeze
26
+
27
+ # Reaches the field from the group, because UI::InputGroup::Field sets its own
28
+ # classes. The browser's spinner goes, in favour of the buttons.
29
+ GROUP_CLASSES = "[&>input]:h-full [&>input]:min-w-0 [&>input]:px-1 [&>input]:text-center " \
30
+ "[&>input]:[appearance:textfield] [&>input::-webkit-inner-spin-button]:appearance-none " \
31
+ "[&>input::-webkit-outer-spin-button]:appearance-none"
32
+
33
+ # The buttons fill the group's height, rather than the addon's usual inset.
34
+ ADDON_CLASSES = "cursor-default self-stretch p-0 has-[>button]:ml-0 has-[>button]:mr-0"
35
+
36
+ STEP_BUTTON = {
37
+ decrement: { label: "Decrease", direction: "down", edge: "rounded-e-none border-e border-input" },
38
+ increment: { label: "Increase", direction: "up", edge: "rounded-s-none border-s border-input" }
39
+ }.freeze
40
+
41
+ def initialize(size: :default, min: nil, max: nil, step: nil, form: nil, field: nil, label: nil, hint: nil,
42
+ required: false, disabled: false, error: nil, class: nil, **html_attrs)
43
+ @size = SIZES.key?(size.to_sym) ? size.to_sym : :default
44
+ @min = min
45
+ @max = max
46
+ @step = step
47
+ @form = form
48
+ @field = field
49
+ @required = required
50
+ @disabled = disabled
51
+ # The <input> is wrapped by the group's border, so the label has to sit
52
+ # outside it; give the field a stable id for the label to point at.
53
+ @input_id = html_attrs[:id] || "ui-number-input-#{SecureRandom.hex(4)}"
54
+ @user_class = binding.local_variable_get(:class)
55
+ @html_attrs = html_attrs
56
+ @wrapper = UI::FormField.new(label: label, hint: hint, error: error,
57
+ required: required, disabled: disabled,
58
+ form: form, field: field, for: @input_id)
59
+ end
60
+
61
+ attr_reader :wrapper
62
+
63
+ def group_component
64
+ UI::InputGroup.new(class: cn(GROUP_CLASSES, SIZES[@size][:group], @user_class),
65
+ data: { controller: "ui--number-input" })
66
+ end
67
+
68
+ def field_options
69
+ { type: :number, form: @form, field: @field, label: false, required: @required, disabled: @disabled,
70
+ error: wrapper.error?, "aria-describedby": wrapper.describedby, min: @min, max: @max, step: @step,
71
+ inputmode: inputmode, **@html_attrs, id: @input_id, data: field_data }.compact
72
+ end
73
+
74
+ def step_button_options(kind)
75
+ button = STEP_BUTTON.fetch(kind)
76
+ { variant: :ghost, disabled: @disabled || @html_attrs[:readonly].present?, tabindex: -1,
77
+ # Lifted on focus, so the neighbouring field doesn't cover the kit's ring.
78
+ class: cn("relative h-full p-0 text-foreground select-none touch-manipulation focus:z-10 disabled:opacity-40",
79
+ SIZES[@size][:button], button[:edge]),
80
+ aria: { label: t("ui.number_input.#{kind}", default: button[:label]), controls: @input_id },
81
+ data: { "ui--number-input-target": kind.to_s, "ui--number-input-direction-param": button[:direction],
82
+ action: "pointerdown->ui--number-input#press click->ui--number-input#click" } }
83
+ end
84
+
85
+ def icon_size = SIZES[@size][:icon]
86
+
87
+ private
88
+
89
+ # The keypad a phone shows. Its number pad has no minus sign, so that's only
90
+ # for fields that can't go below zero.
91
+ def inputmode
92
+ return "decimal" if @step.to_s == "any" || @step.to_f % 1 != 0
93
+ return "numeric" if @min && @min.to_f >= 0
94
+
95
+ nil
96
+ end
97
+
98
+ def field_data
99
+ data = @html_attrs[:data] || {}
100
+ action = [ "input->ui--number-input#sync change->ui--number-input#sync", data[:action] ].compact.join(" ")
101
+ data.merge("ui--number-input-target": "input", action: action)
102
+ end
103
+ end