velvetkit 0.4.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 (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -2
  3. data/lib/velvetkit/version.rb +1 -1
  4. data/registry/app/components/ui/money_input.html.erb +8 -0
  5. data/registry/app/components/ui/money_input.rb +153 -0
  6. data/registry/app/components/ui/scan_input.html.erb +6 -0
  7. data/registry/app/components/ui/scan_input.rb +94 -0
  8. data/registry/app/components/ui/slider.html.erb +23 -0
  9. data/registry/app/components/ui/slider.rb +254 -0
  10. data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
  11. data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
  12. data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
  13. data/registry/components.yml +6 -0
  14. data/registry/config/locales/velvetkit.en.yml +6 -0
  15. data/registry/icons/lucide/scan-barcode.svg +21 -0
  16. data/registry/icons/lucide/volume-2.svg +17 -0
  17. data/registry/icons/lucide/volume-x.svg +17 -0
  18. data/registry/icons/phosphor/barcode.svg +2 -0
  19. data/registry/icons/phosphor/speaker-high.svg +2 -0
  20. data/registry/icons/phosphor/speaker-x.svg +2 -0
  21. data/registry/icons/tabler/barcode.svg +23 -0
  22. data/registry/icons/tabler/volume-3.svg +17 -0
  23. data/registry/icons/tabler/volume.svg +18 -0
  24. data/registry/icons.yml +3 -0
  25. data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
  26. data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
  27. data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
  28. data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
  29. data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
  30. data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
  31. data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
  32. data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
  33. data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
  34. data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
  35. data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
  36. data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
  37. data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
  38. data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
  39. data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
  40. data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
  41. data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
  42. data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
  43. data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
  44. data/registry/test/components/previews/ui/slider_preview.rb +45 -0
  45. data/registry/test/components/ui/money_input_test.rb +149 -0
  46. data/registry/test/components/ui/scan_input_test.rb +110 -0
  47. data/registry/test/components/ui/slider_test.rb +190 -0
  48. metadata +42 -1
@@ -0,0 +1,17 @@
1
+ <!-- @license lucide-static v0.545.0 - ISC -->
2
+ <svg
3
+ class="lucide lucide-volume-x"
4
+ xmlns="http://www.w3.org/2000/svg"
5
+ width="24"
6
+ height="24"
7
+ viewBox="0 0 24 24"
8
+ fill="none"
9
+ stroke="currentColor"
10
+ stroke-width="2"
11
+ stroke-linecap="round"
12
+ stroke-linejoin="round"
13
+ >
14
+ <path d="M11 4.702a.705.705 0 0 0-1.203-.498L6.413 7.587A1.4 1.4 0 0 1 5.416 8H3a1 1 0 0 0-1 1v6a1 1 0 0 0 1 1h2.416a1.4 1.4 0 0 1 .997.413l3.383 3.384A.705.705 0 0 0 11 19.298z" />
15
+ <line x1="22" x2="16" y1="9" y2="15" />
16
+ <line x1="16" x2="22" y1="9" y2="15" />
17
+ </svg>
@@ -0,0 +1,2 @@
1
+ <!-- @license @phosphor-icons/core v2.1.1 - MIT -->
2
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M232,48V88a8,8,0,0,1-16,0V56H184a8,8,0,0,1,0-16h40A8,8,0,0,1,232,48ZM72,200H40V168a8,8,0,0,0-16,0v40a8,8,0,0,0,8,8H72a8,8,0,0,0,0-16Zm152-40a8,8,0,0,0-8,8v32H184a8,8,0,0,0,0,16h40a8,8,0,0,0,8-8V168A8,8,0,0,0,224,160ZM32,96a8,8,0,0,0,8-8V56H72a8,8,0,0,0,0-16H32a8,8,0,0,0-8,8V88A8,8,0,0,0,32,96ZM80,80a8,8,0,0,0-8,8v80a8,8,0,0,0,16,0V88A8,8,0,0,0,80,80Zm104,88V88a8,8,0,0,0-16,0v80a8,8,0,0,0,16,0ZM144,80a8,8,0,0,0-8,8v80a8,8,0,0,0,16,0V88A8,8,0,0,0,144,80Zm-32,0a8,8,0,0,0-8,8v80a8,8,0,0,0,16,0V88A8,8,0,0,0,112,80Z"/></svg>
@@ -0,0 +1,2 @@
1
+ <!-- @license @phosphor-icons/core v2.1.1 - MIT -->
2
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M155.51,24.81a8,8,0,0,0-8.42.88L77.25,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H77.25l69.84,54.31A8,8,0,0,0,160,224V32A8,8,0,0,0,155.51,24.81ZM32,96H72v64H32ZM144,207.64,88,164.09V91.91l56-43.55Zm54-106.08a40,40,0,0,1,0,52.88,8,8,0,0,1-12-10.58,24,24,0,0,0,0-31.72,8,8,0,0,1,12-10.58ZM248,128a79.9,79.9,0,0,1-20.37,53.34,8,8,0,0,1-11.92-10.67,64,64,0,0,0,0-85.33,8,8,0,1,1,11.92-10.67A79.83,79.83,0,0,1,248,128Z"/></svg>
@@ -0,0 +1,2 @@
1
+ <!-- @license @phosphor-icons/core v2.1.1 - MIT -->
2
+ <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 256 256" fill="currentColor"><path d="M155.51,24.81a8,8,0,0,0-8.42.88L77.25,80H32A16,16,0,0,0,16,96v64a16,16,0,0,0,16,16H77.25l69.84,54.31A8,8,0,0,0,160,224V32A8,8,0,0,0,155.51,24.81ZM32,96H72v64H32ZM144,207.64,88,164.09V91.91l56-43.55Zm101.66-61.3a8,8,0,0,1-11.32,11.32L216,139.31l-18.34,18.35a8,8,0,0,1-11.32-11.32L204.69,128l-18.35-18.34a8,8,0,0,1,11.32-11.32L216,116.69l18.34-18.35a8,8,0,0,1,11.32,11.32L227.31,128Z"/></svg>
@@ -0,0 +1,23 @@
1
+ <!-- @license @tabler/icons v3.50.0 - MIT -->
2
+ <svg
3
+ xmlns="http://www.w3.org/2000/svg"
4
+ width="24"
5
+ height="24"
6
+ viewBox="0 0 24 24"
7
+ fill="none"
8
+ stroke="currentColor"
9
+ stroke-width="2"
10
+ stroke-linecap="round"
11
+ stroke-linejoin="round"
12
+ class="icon icon-tabler icons-tabler-outline icon-tabler-barcode"
13
+ >
14
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
15
+ <path d="M4 7v-1a2 2 0 0 1 2 -2h2" />
16
+ <path d="M4 17v1a2 2 0 0 0 2 2h2" />
17
+ <path d="M16 4h2a2 2 0 0 1 2 2v1" />
18
+ <path d="M16 20h2a2 2 0 0 0 2 -2v-1" />
19
+ <path d="M5 11h1v2h-1l0 -2" />
20
+ <path d="M10 11l0 2" />
21
+ <path d="M14 11h1v2h-1l0 -2" />
22
+ <path d="M19 11l0 2" />
23
+ </svg>
@@ -0,0 +1,17 @@
1
+ <!-- @license @tabler/icons v3.50.0 - MIT -->
2
+ <svg
3
+ xmlns="http://www.w3.org/2000/svg"
4
+ width="24"
5
+ height="24"
6
+ viewBox="0 0 24 24"
7
+ fill="none"
8
+ stroke="currentColor"
9
+ stroke-width="2"
10
+ stroke-linecap="round"
11
+ stroke-linejoin="round"
12
+ class="icon icon-tabler icons-tabler-outline icon-tabler-volume-3"
13
+ >
14
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
15
+ <path d="M6 15h-2a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1h2l3.5 -4.5a.8 .8 0 0 1 1.5 .5v14a.8 .8 0 0 1 -1.5 .5l-3.5 -4.5" />
16
+ <path d="M16 10l4 4m0 -4l-4 4" />
17
+ </svg>
@@ -0,0 +1,18 @@
1
+ <!-- @license @tabler/icons v3.50.0 - MIT -->
2
+ <svg
3
+ xmlns="http://www.w3.org/2000/svg"
4
+ width="24"
5
+ height="24"
6
+ viewBox="0 0 24 24"
7
+ fill="none"
8
+ stroke="currentColor"
9
+ stroke-width="2"
10
+ stroke-linecap="round"
11
+ stroke-linejoin="round"
12
+ class="icon icon-tabler icons-tabler-outline icon-tabler-volume"
13
+ >
14
+ <path stroke="none" d="M0 0h24v24H0z" fill="none" />
15
+ <path d="M15 8a5 5 0 0 1 0 8" />
16
+ <path d="M17.7 5a9 9 0 0 1 0 14" />
17
+ <path d="M6 15h-2a1 1 0 0 1 -1 -1v-4a1 1 0 0 1 1 -1h2l3.5 -4.5a.8 .8 0 0 1 1.5 .5v14a.8 .8 0 0 1 -1.5 .5l-3.5 -4.5" />
18
+ </svg>
data/registry/icons.yml CHANGED
@@ -70,6 +70,7 @@ qr-code: { tabler: qrcode, phosphor: qr-code }
70
70
  receipt: { tabler: receipt, phosphor: receipt }
71
71
  rocket: { tabler: rocket, phosphor: rocket }
72
72
  rotate-cw: { tabler: rotate-clockwise, phosphor: arrow-clockwise }
73
+ scan-barcode: { tabler: barcode, phosphor: barcode }
73
74
  search: { tabler: search, phosphor: magnifying-glass }
74
75
  settings: { tabler: settings, phosphor: gear }
75
76
  shield: { tabler: shield, phosphor: shield }
@@ -84,6 +85,8 @@ upload: { tabler: upload, phosphor: upload-simple }
84
85
  user: { tabler: user, phosphor: user }
85
86
  user-plus: { tabler: user-plus, phosphor: user-plus }
86
87
  users: { tabler: users, phosphor: users }
88
+ volume-2: { tabler: volume, phosphor: speaker-high }
89
+ volume-x: { tabler: volume-3, phosphor: speaker-x }
87
90
  workflow: { tabler: sitemap, phosphor: flow-arrow }
88
91
  x: { tabler: x, phosphor: x }
89
92
  zoom-in: { tabler: zoom-in, phosphor: magnifying-glass-plus }
@@ -0,0 +1,24 @@
1
+ <div id="money-cash-demo" class="max-w-xs space-y-4 rounded-lg border p-5">
2
+ <div class="flex items-baseline justify-between">
3
+ <span class="text-sm text-muted-foreground">To pay</span>
4
+ <span class="text-2xl font-semibold tabular-nums">₹1,847.00</span>
5
+ </div>
6
+ <%= render(UI::MoneyInput.new(name: "tendered", unit: "₹", size: :lg, label: "Cash received", autofocus: true)) %>
7
+ <div class="flex items-baseline justify-between text-sm">
8
+ <span class="text-muted-foreground">Change</span>
9
+ <span class="font-medium tabular-nums" data-demo="change" role="status">—</span>
10
+ </div>
11
+ </div>
12
+ <script>
13
+ (() => {
14
+ const root = document.getElementById("money-cash-demo");
15
+ const due = 1847;
16
+ root.addEventListener("input", () => {
17
+ const given = Number(root.querySelector('input[type=hidden][name="tendered"]').value || 0);
18
+ const out = root.querySelector('[data-demo="change"]');
19
+ if (!given) out.textContent = "—";
20
+ else if (given < due) out.textContent = "₹" + (due - given).toLocaleString("en-IN", { minimumFractionDigits: 2 }) + " short";
21
+ else out.textContent = "₹" + (given - due).toLocaleString("en-IN", { minimumFractionDigits: 2 });
22
+ });
23
+ })();
24
+ </script>
@@ -0,0 +1,7 @@
1
+ <div class="grid max-w-xs gap-4">
2
+ <%= render(UI::MoneyInput.new(name: "inr", unit: "₹", value: 12345678.9, label: "Rupees, grouped in lakhs and crores")) %>
3
+ <%= render(UI::MoneyInput.new(name: "usd", unit: "$", value: 12345678.9, label: "Dollars, grouped in thousands")) %>
4
+ <%= render(UI::MoneyInput.new(name: "eur", unit: "€", value: 1234.5, separator: ",", delimiter: ".", label: "Euros, with a decimal comma")) %>
5
+ <%= render(UI::MoneyInput.new(name: "jpy", unit: "¥", value: 15800, precision: 0, label: "Yen, no decimals")) %>
6
+ <p class="text-sm text-muted-foreground">Each posts the plain number, like <code>12345678.90</code>. The sign, separators and decimals default to the app's locale.</p>
7
+ </div>
@@ -0,0 +1,24 @@
1
+ <%# Reads the hidden fields, which hold plain numbers, so there's no comma or full stop to strip. %>
2
+ <div id="money-invoice-demo" class="grid max-w-sm gap-4">
3
+ <%= render(UI::MoneyInput.new(name: "fee", unit: "€", separator: ",", delimiter: ".", value: 8500, label: "Design work")) %>
4
+ <%= render(UI::MoneyInput.new(name: "expenses", unit: "€", separator: ",", delimiter: ".", value: 1245.75, label: "Expenses",
5
+ hint: "Travel and printing, at cost.")) %>
6
+ <dl class="grid grid-cols-2 gap-y-1 border-t pt-3 text-sm">
7
+ <dt class="text-muted-foreground">VAT at 20% on the work</dt><dd class="text-end tabular-nums" data-demo="vat"></dd>
8
+ <dt class="font-medium">Total</dt><dd class="text-end font-medium tabular-nums" data-demo="total"></dd>
9
+ </dl>
10
+ </div>
11
+ <script>
12
+ (() => {
13
+ const root = document.getElementById("money-invoice-demo");
14
+ const money = (n) => "€" + n.toLocaleString("de-DE", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
15
+ const amount = (name) => Number(root.querySelector(`input[type=hidden][name="${name}"]`).value || 0);
16
+ const total = () => {
17
+ const vat = amount("fee") * 0.2;
18
+ root.querySelector('[data-demo="vat"]').textContent = money(vat);
19
+ root.querySelector('[data-demo="total"]').textContent = money(amount("fee") + amount("expenses") + vat);
20
+ };
21
+ root.addEventListener("input", total);
22
+ total();
23
+ })();
24
+ </script>
@@ -0,0 +1,9 @@
1
+ <div class="grid max-w-xs gap-4">
2
+ <%= render(UI::MoneyInput.new(name: "small", unit: "$", value: 499, size: :sm, label: "Small")) %>
3
+ <%= render(UI::MoneyInput.new(name: "default", unit: "$", value: 4999, label: "Default")) %>
4
+ <%= render(UI::MoneyInput.new(name: "large", unit: "$", value: 49999, size: :lg, label: "Large, for touch screens")) %>
5
+ <%= render(UI::MoneyInput.new(name: "empty", unit: "$", label: "Empty, required", required: true, hint: "Cents are filled in when you leave the field.")) %>
6
+ <%= render(UI::MoneyInput.new(name: "refund", unit: "$", value: -1200, negative: true, label: "A refund, below zero allowed")) %>
7
+ <%= render(UI::MoneyInput.new(name: "disabled", unit: "$", value: 2500, label: "Disabled", disabled: true)) %>
8
+ <%= render(UI::MoneyInput.new(name: "invalid", unit: "$", value: 0, label: "With an error", error: "Must be more than $0")) %>
9
+ </div>
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UI::MoneyInputPreview < ViewComponent::Preview
4
+ # @label Playground
5
+ # @param unit text
6
+ # @param value text
7
+ # @param precision number "Decimals: 2 for paise or cents, 0 for whole amounts"
8
+ # @param grouping select { choices: ["", thousands, indian] } "Blank: Indian for ₹, thousands otherwise. Try ₹ as the unit"
9
+ # @param negative toggle
10
+ # @param size select { choices: [sm, default, lg] }
11
+ # @param label text
12
+ # @param hint text
13
+ # @param disabled toggle
14
+ # @param error text
15
+ def playground(unit: "$", value: "123456.5", precision: 2, grouping: "", negative: false, size: :default,
16
+ label: "Amount", hint: "", disabled: false, error: "")
17
+ render(UI::MoneyInput.new(name: "amount", unit: unit, value: value.presence, precision: precision,
18
+ grouping: grouping.presence, negative: negative, size: size.to_sym,
19
+ label: label.presence, hint: hint.presence, disabled: disabled,
20
+ error: error.presence, class: "max-w-xs"))
21
+ end
22
+
23
+ # @label Currencies
24
+ def currencies; render_with_template; end
25
+
26
+ # @label Sizes and States
27
+ def sizes_and_states; render_with_template; end
28
+
29
+ # @label Invoice Total, Following Live
30
+ def invoice; render_with_template; end
31
+
32
+ # @label Cash and Change at a Till
33
+ def cash_tendered; render_with_template; end
34
+ end
@@ -0,0 +1,20 @@
1
+ <%# What a scanner does: types the code fast, then presses the suffix key. For trying the demos without one. %>
2
+ <div class="flex flex-wrap items-center gap-2 text-sm">
3
+ <span class="text-muted-foreground">Simulate a scan:</span>
4
+ <% codes.each do |code, label| %>
5
+ <%= render(UI::Button.new(variant: :outline, size: :sm, data: { simulate: code, key: local_assigns.fetch(:key, "Enter") })) { label } %>
6
+ <% end %>
7
+ </div>
8
+ <script>
9
+ (() => {
10
+ const root = document.currentScript.previousElementSibling;
11
+ root.addEventListener("click", (event) => {
12
+ const button = event.target.closest("[data-simulate]");
13
+ if (!button) return;
14
+ const field = root.closest("[data-demo]").querySelector("[data-ui--scan-input-target=field]");
15
+ field.focus();
16
+ field.value = button.dataset.simulate;
17
+ field.dispatchEvent(new KeyboardEvent("keydown", { key: button.dataset.key, bubbles: true, cancelable: true }));
18
+ });
19
+ })();
20
+ </script>
@@ -0,0 +1,30 @@
1
+ <%# Handled in the page: the scan event is cancelled, so nothing is submitted. %>
2
+ <div class="grid max-w-md gap-4" data-demo>
3
+ <%= render(UI::ScanInput.new(name: "isbn", label: "Scan each book's barcode", placeholder: "ISBN")) %>
4
+ <%= render "ui/scan_input_preview/simulate", codes: { "9780141439518" => "Pride and Prejudice", "9780262033848" => "Introduction to Algorithms", "9780000000000" => "Unknown" } %>
5
+ <p class="text-sm" role="status" data-out></p>
6
+ <ol class="divide-y rounded-md border text-sm" data-list></ol>
7
+ <p class="text-sm text-muted-foreground"><span data-count>0</span> books, due back in 14 days.</p>
8
+ </div>
9
+ <script>
10
+ (() => {
11
+ const root = document.currentScript.previousElementSibling;
12
+ const books = { "9780141439518": "Pride and Prejudice", "9780262033848": "Introduction to Algorithms", "9780316769488": "The Catcher in the Rye" };
13
+ const list = root.querySelector("[data-list]");
14
+ const say = (text) => (root.querySelector("[data-out]").textContent = text);
15
+ root.addEventListener("ui--scan-input:scan", (event) => {
16
+ event.preventDefault();
17
+ const { code } = event.detail;
18
+ const title = books[code];
19
+ if (!title) return say(`No book with the ISBN ${code}.`);
20
+ if (list.querySelector(`[data-isbn="${code}"]`)) return say(`${title} is already on this loan.`);
21
+ const item = document.createElement("li");
22
+ item.className = "px-3 py-2";
23
+ item.dataset.isbn = code;
24
+ item.textContent = title;
25
+ list.append(item);
26
+ root.querySelector("[data-count]").textContent = list.children.length;
27
+ say(`Added ${title}.`);
28
+ });
29
+ })();
30
+ </script>
@@ -0,0 +1,13 @@
1
+ <div class="grid max-w-sm gap-4" data-demo>
2
+ <%= render(UI::ScanInput.new(name: "code", submit: false, **options)) %>
3
+ <%= render "ui/scan_input_preview/simulate", codes: { "4006381333931" => "EAN-13", "TICKET-7F3K-0192" => "QR ticket" }, key: options[:suffix] == :tab ? "Tab" : "Enter" %>
4
+ <p class="text-sm text-muted-foreground" role="status" data-demo-out>Nothing scanned yet.</p>
5
+ </div>
6
+ <script>
7
+ (() => {
8
+ const root = document.currentScript.previousElementSibling;
9
+ root.addEventListener("ui--scan-input:scan", (event) => {
10
+ root.querySelector("[data-demo-out]").textContent = `Scanned ${event.detail.code}`;
11
+ });
12
+ })();
13
+ </script>
@@ -0,0 +1,24 @@
1
+ <%# capture: true sends keys typed with no field focused into this one, so a scan lands even after clicking elsewhere. %>
2
+ <div class="grid max-w-md gap-4" data-demo>
3
+ <%= render(UI::ScanInput.new(name: "sku", label: "Count shelf B-12", capture: true,
4
+ hint: "Click anywhere on the page, then scan: the code still lands here.")) %>
5
+ <%= render "ui/scan_input_preview/simulate", codes: { "SKU-1042" => "Cable ties", "SKU-2210" => "Hex bolts", "SKU-3307" => "Wall plugs" } %>
6
+ <table class="text-sm">
7
+ <thead><tr class="text-start text-muted-foreground"><th class="py-1 text-start font-normal">Item</th><th class="py-1 text-end font-normal">Counted</th><th class="py-1 text-end font-normal">Expected</th></tr></thead>
8
+ <tbody data-rows>
9
+ <tr data-sku="SKU-1042"><td class="py-1">Cable ties</td><td class="py-1 text-end tabular-nums" data-n>0</td><td class="py-1 text-end tabular-nums">3</td></tr>
10
+ <tr data-sku="SKU-2210"><td class="py-1">Hex bolts</td><td class="py-1 text-end tabular-nums" data-n>0</td><td class="py-1 text-end tabular-nums">2</td></tr>
11
+ <tr data-sku="SKU-3307"><td class="py-1">Wall plugs</td><td class="py-1 text-end tabular-nums" data-n>0</td><td class="py-1 text-end tabular-nums">4</td></tr>
12
+ </tbody>
13
+ </table>
14
+ </div>
15
+ <script>
16
+ (() => {
17
+ const root = document.currentScript.previousElementSibling;
18
+ root.addEventListener("ui--scan-input:scan", (event) => {
19
+ event.preventDefault();
20
+ const cell = root.querySelector(`tr[data-sku="${event.detail.code}"] [data-n]`);
21
+ if (cell) cell.textContent = Number(cell.textContent) + 1;
22
+ });
23
+ })();
24
+ </script>
@@ -0,0 +1,34 @@
1
+ <%# keyboard: false keeps a handheld's on-screen keyboard down; its trigger button does the typing. %>
2
+ <div class="grid max-w-sm gap-4" data-demo>
3
+ <%= render(UI::ScanInput.new(name: "ticket", label: "Check in", placeholder: "Scan a ticket's QR code", size: :lg, keyboard: false)) %>
4
+ <%= render "ui/scan_input_preview/simulate", codes: { "TKT-A-0192" => "Valid", "TKT-A-0007" => "Already used", "TKT-Z-9999" => "Not for today" } %>
5
+ <div class="rounded-md border p-4 text-center" role="status" data-out>
6
+ <p class="text-sm text-muted-foreground">Waiting for a ticket</p>
7
+ </div>
8
+ </div>
9
+ <script>
10
+ (() => {
11
+ const root = document.currentScript.previousElementSibling;
12
+ const used = new Set(["TKT-A-0007"]);
13
+ const out = root.querySelector("[data-out]");
14
+ const show = (title, line, tone) => {
15
+ out.className = `rounded-md border p-4 text-center ${tone}`;
16
+ out.innerHTML = "";
17
+ const strong = document.createElement("p");
18
+ strong.className = "text-lg font-semibold";
19
+ strong.textContent = title;
20
+ const small = document.createElement("p");
21
+ small.className = "text-sm";
22
+ small.textContent = line;
23
+ out.append(strong, small);
24
+ };
25
+ root.addEventListener("ui--scan-input:scan", (event) => {
26
+ event.preventDefault();
27
+ const { code } = event.detail;
28
+ if (!code.startsWith("TKT-A-")) return show("Not for today", `${code} is for another day.`, "border-amber-500/50 bg-amber-500/10");
29
+ if (used.has(code)) return show("Already checked in", `${code} was used at 18:42.`, "border-destructive/50 bg-destructive/10");
30
+ used.add(code);
31
+ show("Welcome", `${code}, general admission.`, "border-emerald-500/50 bg-emerald-500/10");
32
+ });
33
+ })();
34
+ </script>
@@ -0,0 +1,9 @@
1
+ <%# A real form: each code is posted, and the server answers with a Turbo Stream adding the line. The demo server answers slowly, so quick scans queue. %>
2
+ <div class="grid max-w-md gap-4" data-demo>
3
+ <%= form_with url: "/demo/scans", class: "contents", data: { demo_live: true } do %>
4
+ <%= render(UI::ScanInput.new(name: "code", label: "Scan items", size: :lg)) %>
5
+ <% end %>
6
+ <%= render "ui/scan_input_preview/simulate", codes: { "8901030865278" => "Rice", "8901058851298" => "Noodles", "8901719110016" => "Dal", "1111" => "Unknown" } %>
7
+ <p class="text-sm text-destructive" id="scan-demo-status" role="status"></p>
8
+ <ul id="scan-demo-lines" class="divide-y rounded-md border px-3 text-sm"></ul>
9
+ </div>
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UI::ScanInputPreview < ViewComponent::Preview
4
+ # @label Playground
5
+ # @param label text
6
+ # @param placeholder text
7
+ # @param size select { choices: [sm, default, lg] }
8
+ # @param suffix select { choices: [enter, tab] } "The key the scanner ends a code with"
9
+ # @param keyboard toggle "Off keeps a phone's on-screen keyboard down"
10
+ # @param capture toggle "Keys typed anywhere on the page go into the field"
11
+ # @param disabled toggle
12
+ # @param hint text
13
+ # @param error text
14
+ def playground(label: "Scan an item", placeholder: "", size: :default, suffix: :enter, keyboard: true,
15
+ capture: false, disabled: false, hint: "", error: "")
16
+ render_with_template(locals: { options: {
17
+ label: label.presence, placeholder: placeholder.presence, size: size.to_sym, suffix: suffix.to_sym,
18
+ keyboard: keyboard, capture: capture, disabled: disabled, hint: hint.presence, error: error.presence
19
+ } })
20
+ end
21
+
22
+ # @label Library Checkout
23
+ def library_checkout; render_with_template; end
24
+
25
+ # @label Stock Count, Scanning Anywhere on the Page
26
+ def stock_count; render_with_template; end
27
+
28
+ # @label Ticket Check-in on a Handheld
29
+ def ticket_check_in; render_with_template; end
30
+
31
+ # @label Till, Sent to the Server
32
+ def till; render_with_template; end
33
+ end
@@ -0,0 +1,7 @@
1
+ <div class="grid max-w-sm gap-8">
2
+ <%= render(UI::Slider.new(name: "digest", min: 0, max: 3, value: 1, label: "Email me about comments", show_value: true,
3
+ marks: { 0 => "Never", 1 => "Weekly", 2 => "Daily", 3 => "Right away" })) %>
4
+ <%= render(UI::Slider.new(name: "quality", min: 1, max: 3, value: 2, label: "Video quality", show_value: true,
5
+ marks: { 1 => "Low", 2 => "Medium", 3 => "High" },
6
+ hint: "Higher uses more data.")) %>
7
+ </div>
@@ -0,0 +1,5 @@
1
+ <div class="max-w-md">
2
+ <%= render(UI::Slider.new(range: true, label: "Open", min: 0, max: 24, from: 9, to: 17, from_name: "opens_at",
3
+ to_name: "closes_at", value_text: "%{value}:00", show_value: true,
4
+ marks: { 0 => "Midnight", 6 => "6:00", 12 => "Noon", 18 => "18:00", 24 => "Midnight" })) %>
5
+ </div>
@@ -0,0 +1,6 @@
1
+ <%# A filter form submits on change, which fires once a handle is let go, not on every step of a drag. %>
2
+ <form class="max-w-sm space-y-3" onsubmit="event.preventDefault()">
3
+ <%= render(UI::Slider.new(range: true, label: "Price", min: 0, max: 500, step: 10, from: 40, to: 260,
4
+ from_name: "min_price", to_name: "max_price", value_text: "$%{value}", show_value: true)) %>
5
+ <p class="text-sm text-muted-foreground">Handles meet but don't pass each other. Press the track to grab the nearest.</p>
6
+ </form>
@@ -0,0 +1,9 @@
1
+ <div class="grid max-w-sm gap-6">
2
+ <%= render(UI::Slider.new(name: "small", value: 30, size: :sm, label: "Small", show_value: true)) %>
3
+ <%= render(UI::Slider.new(name: "default", value: 50, label: "Default", show_value: true)) %>
4
+ <%= render(UI::Slider.new(name: "large", value: 70, size: :lg, label: "Large, for touch screens", show_value: true)) %>
5
+ <%= render(UI::Slider.new(name: "disabled", value: 40, label: "Disabled", disabled: true, show_value: true)) %>
6
+ <%= render(UI::Slider.new(name: "alert_at", value: 30, min: 50, max: 100, step: 5, label: "Warn me when storage reaches",
7
+ value_text: "%{value}%", show_value: true, hint: "We'll email you once a day until there's room.",
8
+ error: "Must be at least 50%")) %>
9
+ </div>
@@ -0,0 +1,16 @@
1
+ <div id="slider-text-size-demo" class="grid max-w-md gap-4">
2
+ <%= render(UI::Slider.new(name: "text_size", min: 12, max: 24, value: 16, label: "Text size", value_text: "%{value}px",
3
+ show_value: true, marks: [ 12, 16, 20, 24 ])) %>
4
+ <p class="rounded-md border p-4" data-demo="sample" style="font-size: 16px">
5
+ The quick brown fox jumps over the lazy dog.
6
+ </p>
7
+ </div>
8
+ <script>
9
+ (() => {
10
+ const root = document.getElementById("slider-text-size-demo");
11
+ const sample = root.querySelector('[data-demo="sample"]');
12
+ root.addEventListener("input", (event) => {
13
+ if (event.target.name === "text_size") sample.style.fontSize = `${event.target.value}px`;
14
+ });
15
+ })();
16
+ </script>
@@ -0,0 +1,22 @@
1
+ <div id="slider-tip-demo" class="max-w-xs space-y-5 rounded-lg border p-5">
2
+ <div class="text-center">
3
+ <p class="text-sm text-muted-foreground">Add a tip</p>
4
+ <p class="text-3xl font-semibold tabular-nums" data-demo="total">$48.30</p>
5
+ <p class="text-sm text-muted-foreground" data-demo="breakdown">$42.00 + $6.30 tip</p>
6
+ </div>
7
+ <%= render(UI::Slider.new(name: "tip_percent", min: 0, max: 25, step: 5, value: 15, size: :lg, label: false,
8
+ aria: { label: "Tip" }, value_text: "%{value}%",
9
+ marks: { 0 => "None", 10 => "10%", 15 => "15%", 20 => "20%", 25 => "25%" })) %>
10
+ </div>
11
+ <script>
12
+ (() => {
13
+ const root = document.getElementById("slider-tip-demo");
14
+ const bill = 42;
15
+ const money = (n) => `$${n.toFixed(2)}`;
16
+ root.addEventListener("input", (event) => {
17
+ const tip = bill * Number(event.target.value) / 100;
18
+ root.querySelector('[data-demo="total"]').textContent = money(bill + tip);
19
+ root.querySelector('[data-demo="breakdown"]').textContent = tip ? `${money(bill)} + ${money(tip)} tip` : "No tip";
20
+ });
21
+ })();
22
+ </script>
@@ -0,0 +1,5 @@
1
+ <div class="flex max-w-xs items-center gap-3">
2
+ <%= render(UI::Icon.new(name: "volume-x", size: :sm, class: "shrink-0 text-muted-foreground")) %>
3
+ <%= render(UI::Slider.new(name: "volume", value: 65, label: false, aria: { label: "Volume" }, value_text: "%{value}%")) %>
4
+ <%= render(UI::Icon.new(name: "volume-2", size: :sm, class: "shrink-0 text-muted-foreground")) %>
5
+ </div>
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UI::SliderPreview < ViewComponent::Preview
4
+ # @label Playground
5
+ # @param range toggle "Two handles, a low and a high"
6
+ # @param value number
7
+ # @param min number
8
+ # @param max number
9
+ # @param step number
10
+ # @param label text
11
+ # @param show_value toggle
12
+ # @param value_text text "Can use %{value}"
13
+ # @param size select { choices: [sm, default, lg] }
14
+ # @param disabled toggle
15
+ # @param hint text
16
+ # @param error text
17
+ def playground(range: false, value: 40, min: 0, max: 100, step: 1, label: "Volume", show_value: true,
18
+ value_text: "%{value}%", size: :default, disabled: false, hint: "", error: "")
19
+ render(UI::Slider.new(name: "volume", range: range, value: value, from: value, to: max, min: min, max: max,
20
+ step: step, label: label.presence, show_value: show_value, value_text: value_text.presence,
21
+ size: size.to_sym, disabled: disabled, hint: hint.presence, error: error.presence,
22
+ class: "max-w-sm"))
23
+ end
24
+
25
+ # @label Sizes and States
26
+ def sizes_and_states; render_with_template; end
27
+
28
+ # @label Volume, with Icons
29
+ def volume; render_with_template; end
30
+
31
+ # @label Price Range Filter
32
+ def price_range; render_with_template; end
33
+
34
+ # @label Opening Hours, with Marks
35
+ def opening_hours; render_with_template; end
36
+
37
+ # @label Named Steps
38
+ def named_steps; render_with_template; end
39
+
40
+ # @label Text Size, Following Live
41
+ def text_size; render_with_template; end
42
+
43
+ # @label Tip on a Card Reader
44
+ def tip; render_with_template; end
45
+ end