velvetkit 0.3.0 → 0.4.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 (175) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +80 -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/number_input.html.erb +11 -0
  24. data/registry/app/components/ui/number_input.rb +103 -0
  25. data/registry/app/components/ui/progress/segment.rb +25 -0
  26. data/registry/app/components/ui/progress.html.erb +25 -0
  27. data/registry/app/components/ui/progress.rb +182 -0
  28. data/registry/app/components/ui/select.html.erb +24 -5
  29. data/registry/app/components/ui/select.rb +44 -5
  30. data/registry/app/components/ui/timeline/item.html.erb +21 -0
  31. data/registry/app/components/ui/timeline/item.rb +145 -0
  32. data/registry/app/components/ui/timeline.html.erb +1 -0
  33. data/registry/app/components/ui/timeline.rb +42 -0
  34. data/registry/app/javascript/chart_js.ts +37 -0
  35. data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
  36. data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
  37. data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
  38. data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
  39. data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
  40. data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
  41. data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
  42. data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
  43. data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
  44. data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
  45. data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
  46. data/registry/app/javascript/vanilla_calendar.ts +15 -0
  47. data/registry/components.yml +19 -3
  48. data/registry/config/locales/velvetkit.en.yml +73 -0
  49. data/registry/icons/lucide/download.svg +17 -0
  50. data/registry/icons/lucide/file-plus.svg +18 -0
  51. data/registry/icons/lucide/file.svg +16 -0
  52. data/registry/icons/lucide/folder.svg +15 -0
  53. data/registry/icons/lucide/keyboard.svg +23 -0
  54. data/registry/icons/lucide/minus.svg +15 -0
  55. data/registry/icons/lucide/package.svg +18 -0
  56. data/registry/icons/lucide/pause.svg +16 -0
  57. data/registry/icons/lucide/percent.svg +17 -0
  58. data/registry/icons/lucide/printer.svg +17 -0
  59. data/registry/icons/lucide/receipt.svg +17 -0
  60. data/registry/icons/lucide/sun-moon.svg +19 -0
  61. data/registry/icons/lucide/truck.svg +19 -0
  62. data/registry/icons/lucide/user-plus.svg +18 -0
  63. data/registry/icons/lucide/zoom-in.svg +18 -0
  64. data/registry/icons/lucide/zoom-out.svg +17 -0
  65. data/registry/icons/phosphor/circle-half.svg +2 -0
  66. data/registry/icons/phosphor/download-simple.svg +2 -0
  67. data/registry/icons/phosphor/file-plus.svg +2 -0
  68. data/registry/icons/phosphor/file.svg +2 -0
  69. data/registry/icons/phosphor/folder.svg +2 -0
  70. data/registry/icons/phosphor/keyboard.svg +2 -0
  71. data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
  72. data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
  73. data/registry/icons/phosphor/minus.svg +2 -0
  74. data/registry/icons/phosphor/package.svg +2 -0
  75. data/registry/icons/phosphor/pause.svg +2 -0
  76. data/registry/icons/phosphor/percent.svg +2 -0
  77. data/registry/icons/phosphor/printer.svg +2 -0
  78. data/registry/icons/phosphor/receipt.svg +2 -0
  79. data/registry/icons/phosphor/truck.svg +2 -0
  80. data/registry/icons/phosphor/user-plus.svg +2 -0
  81. data/registry/icons/tabler/download.svg +18 -0
  82. data/registry/icons/tabler/file-plus.svg +19 -0
  83. data/registry/icons/tabler/file.svg +17 -0
  84. data/registry/icons/tabler/folder.svg +16 -0
  85. data/registry/icons/tabler/keyboard.svg +23 -0
  86. data/registry/icons/tabler/minus.svg +16 -0
  87. data/registry/icons/tabler/package.svg +20 -0
  88. data/registry/icons/tabler/percentage.svg +18 -0
  89. data/registry/icons/tabler/player-pause.svg +17 -0
  90. data/registry/icons/tabler/printer.svg +18 -0
  91. data/registry/icons/tabler/receipt.svg +16 -0
  92. data/registry/icons/tabler/sun-moon.svg +21 -0
  93. data/registry/icons/tabler/truck.svg +18 -0
  94. data/registry/icons/tabler/user-plus.svg +19 -0
  95. data/registry/icons/tabler/zoom-in.svg +19 -0
  96. data/registry/icons/tabler/zoom-out.svg +18 -0
  97. data/registry/icons.yml +16 -0
  98. data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
  99. data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
  100. data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
  101. data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
  102. data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
  103. data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
  104. data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
  105. data/registry/test/components/previews/ui/chart_preview.rb +46 -0
  106. data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
  107. data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
  108. data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
  109. data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
  110. data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
  111. data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
  112. data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
  113. data/registry/test/components/previews/ui/command_preview.rb +25 -0
  114. data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
  115. data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
  116. data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
  117. data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
  118. data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
  119. data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
  120. data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
  121. data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
  122. data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
  123. data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
  124. data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
  125. data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
  126. data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
  127. data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
  128. data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
  129. data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
  130. data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
  131. data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
  132. data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
  133. data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
  134. data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
  135. data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
  136. data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
  137. data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
  138. data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
  139. data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
  140. data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
  141. data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
  142. data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
  143. data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
  144. data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
  145. data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
  146. data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
  147. data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
  148. data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
  149. data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
  150. data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
  151. data/registry/test/components/previews/ui/progress_preview.rb +46 -0
  152. data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
  153. data/registry/test/components/previews/ui/select_preview.rb +5 -0
  154. data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
  155. data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
  156. data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
  157. data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
  158. data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
  159. data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
  160. data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
  161. data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
  162. data/registry/test/components/ui/chart_test.rb +187 -0
  163. data/registry/test/components/ui/command/group_test.rb +19 -0
  164. data/registry/test/components/ui/command/item_test.rb +47 -0
  165. data/registry/test/components/ui/command_test.rb +110 -0
  166. data/registry/test/components/ui/date_picker_test.rb +129 -111
  167. data/registry/test/components/ui/file_field_test.rb +105 -9
  168. data/registry/test/components/ui/gallery_test.rb +192 -0
  169. data/registry/test/components/ui/kbd_test.rb +150 -0
  170. data/registry/test/components/ui/number_input_test.rb +155 -0
  171. data/registry/test/components/ui/progress_test.rb +188 -0
  172. data/registry/test/components/ui/select_test.rb +62 -0
  173. data/registry/test/components/ui/timeline_test.rb +175 -0
  174. metadata +147 -2
  175. 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,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
@@ -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 %>