velvetkit 0.4.0 → 0.4.2

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 (84) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +47 -2
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/card/action.html.erb +1 -0
  5. data/registry/app/components/ui/card/action.rb +14 -0
  6. data/registry/app/components/ui/card/content.html.erb +1 -1
  7. data/registry/app/components/ui/card/content.rb +4 -2
  8. data/registry/app/components/ui/card/description.html.erb +1 -1
  9. data/registry/app/components/ui/card/description.rb +4 -2
  10. data/registry/app/components/ui/card/footer.html.erb +1 -1
  11. data/registry/app/components/ui/card/footer.rb +4 -2
  12. data/registry/app/components/ui/card/header.html.erb +1 -1
  13. data/registry/app/components/ui/card/header.rb +9 -2
  14. data/registry/app/components/ui/card/title.html.erb +1 -1
  15. data/registry/app/components/ui/card/title.rb +4 -2
  16. data/registry/app/components/ui/card.html.erb +1 -1
  17. data/registry/app/components/ui/card.rb +49 -3
  18. data/registry/app/components/ui/money_input.html.erb +8 -0
  19. data/registry/app/components/ui/money_input.rb +153 -0
  20. data/registry/app/components/ui/scan_input.html.erb +6 -0
  21. data/registry/app/components/ui/scan_input.rb +94 -0
  22. data/registry/app/components/ui/slider.html.erb +23 -0
  23. data/registry/app/components/ui/slider.rb +254 -0
  24. data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
  25. data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
  26. data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
  27. data/registry/components.yml +7 -1
  28. data/registry/config/locales/velvetkit.en.yml +6 -0
  29. data/registry/icons/lucide/scan-barcode.svg +21 -0
  30. data/registry/icons/lucide/volume-2.svg +17 -0
  31. data/registry/icons/lucide/volume-x.svg +17 -0
  32. data/registry/icons/phosphor/barcode.svg +2 -0
  33. data/registry/icons/phosphor/speaker-high.svg +2 -0
  34. data/registry/icons/phosphor/speaker-x.svg +2 -0
  35. data/registry/icons/tabler/barcode.svg +23 -0
  36. data/registry/icons/tabler/volume-3.svg +17 -0
  37. data/registry/icons/tabler/volume.svg +18 -0
  38. data/registry/icons.yml +3 -0
  39. data/registry/test/components/previews/ui/card_preview/edge_to_edge.html.erb +19 -0
  40. data/registry/test/components/previews/ui/card_preview/image.html.erb +12 -0
  41. data/registry/test/components/previews/ui/card_preview/just_content.html.erb +10 -0
  42. data/registry/test/components/previews/ui/card_preview/open_bill.html.erb +18 -0
  43. data/registry/test/components/previews/ui/card_preview/playground.html.erb +17 -0
  44. data/registry/test/components/previews/ui/card_preview/settings_form.html.erb +15 -24
  45. data/registry/test/components/previews/ui/card_preview/sign_in.html.erb +23 -0
  46. data/registry/test/components/previews/ui/card_preview/small.html.erb +17 -0
  47. data/registry/test/components/previews/ui/card_preview/spacing.html.erb +13 -0
  48. data/registry/test/components/previews/ui/card_preview/stats.html.erb +11 -0
  49. data/registry/test/components/previews/ui/card_preview/team.html.erb +21 -0
  50. data/registry/test/components/previews/ui/card_preview.rb +42 -28
  51. data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
  52. data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
  53. data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
  54. data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
  55. data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
  56. data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
  57. data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
  58. data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
  59. data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
  60. data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
  61. data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
  62. data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
  63. data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
  64. data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
  65. data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
  66. data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
  67. data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
  68. data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
  69. data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
  70. data/registry/test/components/previews/ui/slider_preview.rb +45 -0
  71. data/registry/test/components/ui/card/action_test.rb +19 -0
  72. data/registry/test/components/ui/card/content_test.rb +1 -11
  73. data/registry/test/components/ui/card/footer_test.rb +3 -2
  74. data/registry/test/components/ui/card/header_test.rb +3 -4
  75. data/registry/test/components/ui/card/title_test.rb +3 -4
  76. data/registry/test/components/ui/card_test.rb +63 -2
  77. data/registry/test/components/ui/money_input_test.rb +149 -0
  78. data/registry/test/components/ui/scan_input_test.rb +110 -0
  79. data/registry/test/components/ui/slider_test.rb +190 -0
  80. metadata +55 -5
  81. data/registry/test/components/previews/ui/card_preview/complete.html.erb +0 -31
  82. data/registry/test/components/previews/ui/card_preview/default.html.erb +0 -7
  83. data/registry/test/components/previews/ui/card_preview/with_footer.html.erb +0 -13
  84. data/registry/test/components/previews/ui/card_preview/with_header.html.erb +0 -11
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1946e018660d08bfab63d5e0a9b9e00e2e41a138bacf878e66832c0eebb0400b
4
- data.tar.gz: 7b5ae9a20372f619449b42520d0f1d643b9b8d6094437d00715c8faebb883029
3
+ metadata.gz: 72694a7eeade1ead01f0db952b0945196c102cbdab7e500988ac98cf286fd4c8
4
+ data.tar.gz: aab42bfc198a73982c20b53754596ff1cf905b9f211479656ab9ecc5cde5d1c3
5
5
  SHA512:
6
- metadata.gz: e4cb6bacd3d8e0ccc231e5d655ac639d40df556e49d7b02f1aa0feb5d2ccb8f46aa2a99a08853f26ceb789e920d5f4a5e779c29e3a816c28296547a6b6549200
7
- data.tar.gz: 7624a9d18314e65a4c43a883c9e42b1ac0291ba84b8c5bccb1641a594311384112933abd158ed412877b4b2252f971853ecc5b4d5fbbcbcf000dfe0aaa74aa52
6
+ metadata.gz: 0401f8a0d91f4186852e0f5431efa7ba15ba6578124ba1ab31de40f79df4fb4cf0019b5665d2ad8844f5051a0220b03775a4c3f7a2f9cec49ac6a7af497b7bdd
7
+ data.tar.gz: c0be11bcf1a941922e3b50b8cb813858bf8daf04c8246e44f1d73cc0472b8401808e95f5ea5b0662b626debd85121762e549802f42069d7ee441af142bc04d9b
data/CHANGELOG.md CHANGED
@@ -1,6 +1,40 @@
1
1
  # Changelog
2
2
 
3
- ## Unreleased
3
+ ## 0.4.2 — 2026-11-09
4
+
5
+ Changes:
6
+
7
+ - `Card` is redesigned: a soft ring instead of a border and shadow, tighter
8
+ padding, a smaller title, and a tinted footer band. It gains
9
+ `header.with_action` for a button or badge at the top right,
10
+ `card.with_image` for a picture edge to edge across the top, `size: :sm`,
11
+ and `spacing:`. The examples are new.
12
+
13
+ Upgrading an app: re-run `velvetkit:add card`. Cards now have 16px of padding
14
+ where they had 24px; `spacing: 6` gets the old padding back. If you colour a
15
+ card's edge with a `border-*` class, use `ring-*` instead, and remove any
16
+ `pt-6` added to the first part of a card, which the card now handles. A card
17
+ with no parts, just a block, keeps its own padding as before.
18
+
19
+ ## 0.4.1 — 2026-11-09
20
+
21
+ New components:
22
+
23
+ - `Slider`: a handle to drag for a value, or two for a range such as a price
24
+ band, with value text, labelled marks and form binding. The knob follows
25
+ the pointer freely and the value keeps to the step.
26
+ - `MoneyInput`: an amount of money, grouped as it's typed (1,23,456.78 for
27
+ rupees, 123,456.78 otherwise) with its currency sign, decimals filled in
28
+ when the field is left, and the plain number (123456.78) posted in a
29
+ hidden field. The sign, separators and decimals follow the app's locale.
30
+ ↑ and ↓ step by one unit, or `step:`, and Shift by ten.
31
+ - `ScanInput`: a field for barcode and QR scanners. Enter (or Tab) submits
32
+ the form with the code and empties the field, codes scanned while one is
33
+ still being sent wait their turn, and it keeps focus when focus falls to
34
+ the page. A `scan` event lets the page handle codes itself; `capture:`,
35
+ `keyboard: false` and `suffix: :tab` suit scan-only pages and handhelds.
36
+
37
+ ## 0.4.0 — 2026-11-08
4
38
 
5
39
  New components:
6
40
 
@@ -13,6 +47,15 @@ New components:
13
47
  - `Chart`: line, bar, stacked bar, pie and donut charts on Chart.js, with a
14
48
  legend, tooltips, keyboard reading, a table of the numbers, and animation.
15
49
  The docs site has a Charts section with a page for each.
50
+ - `Progress`: a bar for an upload, an import or storage used, with a value or
51
+ sliding while there's none yet, in tones, and split into coloured segments
52
+ with a legend. Setting its value attribute from JavaScript moves it live.
53
+ - `Kbd`: a key or shortcut, shown as each reader's computer labels it (⌘ on a
54
+ Mac, Ctrl on Windows, Super on Linux), with `bind:` to press a button or
55
+ focus a field when the shortcut is pressed.
56
+ - `Command`: a search box over pages, actions and records, inline or opened
57
+ with ⌘K, ranking by best match, with groups, shortcuts, and server search
58
+ with `url:`.
16
59
 
17
60
  Changes:
18
61
 
@@ -34,6 +77,7 @@ Changes:
34
77
  JavaScript build: `bun.config.js`, `webpack.config.js`, or a build script
35
78
  that lists `application.js`. Before, they were copied but never built, so
36
79
  those components failed to load their library.
80
+ - The docs site's search is the kit's own `Command`.
37
81
 
38
82
  Upgrading an app: re-run `velvetkit:add` for `date_picker`, `select` and
39
83
  `file_field`, and copy the new `--chart-*` values into your `velvetkit.css`.
@@ -41,7 +85,8 @@ Upgrading an app: re-run `velvetkit:add` for `date_picker`, `select` and
41
85
  `app/javascript/vanilla_calendar.ts` as its own entry point; `file_field`
42
86
  needs `@rails/activestorage`. If you added `chart_js.ts`,
43
87
  `vanilla_calendar.ts` or `ag_grid.ts` to your build by hand, `add` leaves
44
- it as it is.
88
+ it as it is. `progress` needs the `velvetkit-progress` keyframes from the new
89
+ `velvetkit.css`; without them a bar with no value doesn't move.
45
90
 
46
91
  - Fix: Rails 8.1.4.1, for security advisories in Active Storage and Action Pack.
47
92
  - Fix: the docs site shows a component added to the catalog without a server
@@ -4,5 +4,5 @@ module Velvetkit
4
4
  # Not the released version: the release workflow takes that from the tag
5
5
  # (v0.3.0 publishes 0.3.0) and writes it here on the runner only, so this
6
6
  # file can stay behind. See .github/workflows/release.yml.
7
- VERSION = "0.4.0"
7
+ VERSION = "0.4.2"
8
8
  end
@@ -0,0 +1 @@
1
+ <%= tag.div(**root_attrs) do %><%= content %><% end %>
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ # What goes at the top right of a card's header: a button, a badge, a link.
4
+ class UI::Card::Action < UI::Base
5
+ def initialize(class: nil, **html_attrs)
6
+ @user_class = binding.local_variable_get(:class)
7
+ @html_attrs = html_attrs
8
+ end
9
+
10
+ def root_attrs
11
+ { **@html_attrs, class: cn("col-start-2 row-span-2 row-start-1 self-start justify-self-end", @user_class),
12
+ data: { slot: "card-action", **(@html_attrs[:data] || {}) } }
13
+ end
14
+ end
@@ -1 +1 @@
1
- <%= tag.div class: classes, **@html_attrs do %><%= content %><% end %>
1
+ <%= tag.div(**root_attrs) do %><%= content %><% end %>
@@ -6,7 +6,9 @@ class UI::Card::Content < UI::Base
6
6
  @html_attrs = html_attrs
7
7
  end
8
8
 
9
- def classes
10
- cn("p-6 pt-0 first:pt-6", @user_class)
9
+ def root_attrs
10
+ { **@html_attrs, class: cn("px-(--card-spacing)",
11
+ @user_class),
12
+ data: { slot: "card-content", **(@html_attrs[:data] || {}) } }
11
13
  end
12
14
  end
@@ -1 +1 @@
1
- <%= tag.p class: classes, **@html_attrs do %><%= content %><% end %>
1
+ <%= tag.p(**root_attrs) do %><%= content %><% end %>
@@ -6,7 +6,9 @@ class UI::Card::Description < UI::Base
6
6
  @html_attrs = html_attrs
7
7
  end
8
8
 
9
- def classes
10
- cn("text-sm text-muted-foreground", @user_class)
9
+ def root_attrs
10
+ { **@html_attrs, class: cn("text-sm text-muted-foreground",
11
+ @user_class),
12
+ data: { slot: "card-description", **(@html_attrs[:data] || {}) } }
11
13
  end
12
14
  end
@@ -1 +1 @@
1
- <%= tag.div class: classes, **@html_attrs do %><%= content %><% end %>
1
+ <%= tag.div(**root_attrs) do %><%= content %><% end %>
@@ -6,7 +6,9 @@ class UI::Card::Footer < UI::Base
6
6
  @html_attrs = html_attrs
7
7
  end
8
8
 
9
- def classes
10
- cn("flex items-center p-6 pt-0", @user_class)
9
+ def root_attrs
10
+ { **@html_attrs, class: cn("flex items-center rounded-b-xl border-t bg-muted/50 p-(--card-spacing)",
11
+ @user_class),
12
+ data: { slot: "card-footer", **(@html_attrs[:data] || {}) } }
11
13
  end
12
14
  end
@@ -1 +1 @@
1
- <%= tag.div class: classes, **@html_attrs do %><% if title? %><%= title %><% end %><% if description? %><%= description %><% end %><%= content %><% end %>
1
+ <%= tag.div(**root_attrs) do %><% if title? %><%= title %><% end %><% if description? %><%= description %><% end %><% if action? %><%= action %><% end %><%= content %><% end %>
@@ -1,15 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class UI::Card::Header < UI::Base
4
+ # Two columns once there's an action, which then spans the title and
5
+ # description rows.
6
+ CLASSES = "grid auto-rows-min items-start gap-1 rounded-t-xl px-(--card-spacing) " \
7
+ "has-data-[slot=card-action]:grid-cols-[1fr_auto] [.border-b]:pb-(--card-spacing)"
8
+
4
9
  renders_one :title, UI::Card::Title
5
10
  renders_one :description, UI::Card::Description
11
+ renders_one :action, UI::Card::Action
6
12
 
7
13
  def initialize(class: nil, **html_attrs)
8
14
  @user_class = binding.local_variable_get(:class)
9
15
  @html_attrs = html_attrs
10
16
  end
11
17
 
12
- def classes
13
- cn("flex flex-col gap-1.5 p-6", @user_class)
18
+ def root_attrs
19
+ { **@html_attrs, class: cn(CLASSES, @user_class),
20
+ data: { slot: "card-header", **(@html_attrs[:data] || {}) } }
14
21
  end
15
22
  end
@@ -1 +1 @@
1
- <%= tag.h3 class: classes, **@html_attrs do %><%= content %><% end %>
1
+ <%= tag.h3(**root_attrs) do %><%= content %><% end %>
@@ -6,7 +6,9 @@ class UI::Card::Title < UI::Base
6
6
  @html_attrs = html_attrs
7
7
  end
8
8
 
9
- def classes
10
- cn("text-lg font-semibold leading-none tracking-tight", @user_class)
9
+ def root_attrs
10
+ { **@html_attrs, class: cn("text-base leading-snug font-medium group-data-[size=sm]/card:text-sm",
11
+ @user_class),
12
+ data: { slot: "card-title", **(@html_attrs[:data] || {}) } }
11
13
  end
12
14
  end
@@ -1 +1 @@
1
- <%= tag.div class: classes, **@html_attrs do %><%= header if header? %><%= card_content if card_content? %><%= footer if footer? %><%= content %><% end %>
1
+ <%= tag.div(**root_attrs) do %><%= image if image? %><%= header if header? %><%= card_content if card_content? %><%= footer if footer? %><%= content %><% end %>
@@ -1,18 +1,64 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # A surface that groups one thing: a form, a stat, a person, an event.
4
+ #
5
+ # <%= render(UI::Card.new) do |card| %>
6
+ # <% card.with_header do |header| %>
7
+ # <% header.with_title { "Team" } %>
8
+ # <% header.with_description { "Who can see this project." } %>
9
+ # <% header.with_action { render(UI::Button.new(size: :sm)) { "Invite" } } %>
10
+ # <% end %>
11
+ # <% card.with_content { ... } %>
12
+ # <% card.with_footer { ... } %>
13
+ # <% end %>
14
+ #
15
+ # `with_action` sits at the top right of the header. `with_image(src:, alt:)`
16
+ # runs edge to edge across the top. The footer is a tinted band along the
17
+ # bottom.
18
+ #
19
+ # `size: :sm` is tighter, with a smaller title. `spacing:` sets the gap between
20
+ # the parts and their inset, in Tailwind's steps (4 is 16px, 6 is 24px). Inside
21
+ # the card it's the CSS variable --card-spacing, so content can reach the
22
+ # edges with `-mx-(--card-spacing)` and stay lined up with the rest.
23
+ #
24
+ # A card with no parts, just a block, is a bare surface with no padding of its
25
+ # own: `render(UI::Card.new(class: "p-6")) { ... }`.
26
+ #
27
+ # It doesn't clip its contents, so a menu or date picker opened inside isn't
28
+ # cut off at the card's edge.
3
29
  class UI::Card < UI::Base
30
+ SPACING = { default: 4, sm: 3 }.freeze
31
+
32
+ # Only once the card has parts. `^=` matches card-header, card-content and
33
+ # so on, as direct children, which a block of the app's own never is.
34
+ # The footer and image reach the card's edge; those two are `!` because
35
+ # Tailwind's order for these variants would otherwise let py win.
36
+ CLASSES = "group/card rounded-xl bg-card text-card-foreground ring-1 ring-foreground/10 " \
37
+ "has-[>[data-slot^=card-]]:flex has-[>[data-slot^=card-]]:flex-col " \
38
+ "has-[>[data-slot^=card-]]:gap-(--card-spacing) has-[>[data-slot^=card-]]:py-(--card-spacing) " \
39
+ "has-[>[data-slot^=card-]]:text-sm has-[>[data-slot=card-footer]]:pb-0! " \
40
+ "has-[>[data-slot=card-image]:first-child]:pt-0!"
41
+
42
+ renders_one :image, lambda { |src:, alt:, class: nil, **attrs|
43
+ UI::Image.new(src: src, alt: alt, rounded: :none, data: { slot: "card-image" },
44
+ class: cn("aspect-video w-full rounded-t-xl object-cover", binding.local_variable_get(:class)), **attrs)
45
+ }
4
46
  renders_one :header, UI::Card::Header
5
47
  renders_one :card_content, UI::Card::Content
6
48
  renders_one :footer, UI::Card::Footer
7
49
 
8
50
  alias_method :with_content, :with_card_content
9
51
 
10
- def initialize(class: nil, **html_attrs)
52
+ def initialize(size: :default, spacing: nil, class: nil, **html_attrs)
53
+ @size = SPACING.key?(size.to_sym) ? size.to_sym : :default
54
+ @spacing = spacing || SPACING[@size]
11
55
  @user_class = binding.local_variable_get(:class)
12
56
  @html_attrs = html_attrs
13
57
  end
14
58
 
15
- def classes
16
- cn("rounded-xl border bg-card text-card-foreground shadow", @user_class)
59
+ def root_attrs
60
+ { **@html_attrs, class: cn(CLASSES, @user_class),
61
+ style: [ "--card-spacing: calc(var(--spacing) * #{@spacing})", @html_attrs[:style] ].compact.join("; "),
62
+ data: { slot: "card", size: @size, **(@html_attrs[:data] || {}) } }
17
63
  end
18
64
  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,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
@@ -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 %>