velvetkit 0.3.0 → 0.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +109 -0
- data/README.md +5 -1
- data/lib/generators/velvetkit/add/add_generator.rb +58 -0
- data/lib/velvetkit/version.rb +4 -1
- data/lib/velvetkit.rb +7 -0
- data/registry/app/assets/tailwind/velvetkit.css +90 -155
- data/registry/app/components/ui/chart.html.erb +47 -0
- data/registry/app/components/ui/chart.rb +177 -0
- data/registry/app/components/ui/command/group.rb +27 -0
- data/registry/app/components/ui/command/item.rb +64 -0
- data/registry/app/components/ui/command.html.erb +41 -0
- data/registry/app/components/ui/command.rb +122 -0
- data/registry/app/components/ui/date_picker.html.erb +58 -64
- data/registry/app/components/ui/date_picker.rb +199 -75
- data/registry/app/components/ui/file_field.html.erb +14 -4
- data/registry/app/components/ui/file_field.rb +179 -28
- data/registry/app/components/ui/gallery/image.rb +20 -0
- data/registry/app/components/ui/gallery.html.erb +52 -0
- data/registry/app/components/ui/gallery.rb +164 -0
- data/registry/app/components/ui/kbd.html.erb +27 -0
- data/registry/app/components/ui/kbd.rb +134 -0
- data/registry/app/components/ui/money_input.html.erb +8 -0
- data/registry/app/components/ui/money_input.rb +153 -0
- data/registry/app/components/ui/number_input.html.erb +11 -0
- data/registry/app/components/ui/number_input.rb +103 -0
- data/registry/app/components/ui/progress/segment.rb +25 -0
- data/registry/app/components/ui/progress.html.erb +25 -0
- data/registry/app/components/ui/progress.rb +182 -0
- data/registry/app/components/ui/scan_input.html.erb +6 -0
- data/registry/app/components/ui/scan_input.rb +94 -0
- data/registry/app/components/ui/select.html.erb +24 -5
- data/registry/app/components/ui/select.rb +44 -5
- data/registry/app/components/ui/slider.html.erb +23 -0
- data/registry/app/components/ui/slider.rb +254 -0
- data/registry/app/components/ui/timeline/item.html.erb +21 -0
- data/registry/app/components/ui/timeline/item.rb +145 -0
- data/registry/app/components/ui/timeline.html.erb +1 -0
- data/registry/app/components/ui/timeline.rb +42 -0
- data/registry/app/javascript/chart_js.ts +37 -0
- data/registry/app/javascript/controllers/ui/_internal/date_format.ts +74 -0
- data/registry/app/javascript/controllers/ui/_internal/shortcut.ts +80 -0
- data/registry/app/javascript/controllers/ui/chart_controller.ts +585 -0
- data/registry/app/javascript/controllers/ui/command_controller.ts +454 -0
- data/registry/app/javascript/controllers/ui/date_picker_controller.ts +247 -82
- data/registry/app/javascript/controllers/ui/file_field_controller.ts +288 -16
- data/registry/app/javascript/controllers/ui/gallery_controller.ts +568 -0
- data/registry/app/javascript/controllers/ui/kbd_controller.ts +60 -0
- data/registry/app/javascript/controllers/ui/money_input_controller.ts +228 -0
- data/registry/app/javascript/controllers/ui/number_input_controller.ts +111 -0
- data/registry/app/javascript/controllers/ui/progress_controller.ts +87 -0
- data/registry/app/javascript/controllers/ui/scan_input_controller.ts +143 -0
- data/registry/app/javascript/controllers/ui/select_controller.ts +198 -8
- data/registry/app/javascript/controllers/ui/slider_controller.ts +216 -0
- data/registry/app/javascript/vanilla_calendar.ts +15 -0
- data/registry/components.yml +25 -3
- data/registry/config/locales/velvetkit.en.yml +79 -0
- data/registry/icons/lucide/download.svg +17 -0
- data/registry/icons/lucide/file-plus.svg +18 -0
- data/registry/icons/lucide/file.svg +16 -0
- data/registry/icons/lucide/folder.svg +15 -0
- data/registry/icons/lucide/keyboard.svg +23 -0
- data/registry/icons/lucide/minus.svg +15 -0
- data/registry/icons/lucide/package.svg +18 -0
- data/registry/icons/lucide/pause.svg +16 -0
- data/registry/icons/lucide/percent.svg +17 -0
- data/registry/icons/lucide/printer.svg +17 -0
- data/registry/icons/lucide/receipt.svg +17 -0
- data/registry/icons/lucide/scan-barcode.svg +21 -0
- data/registry/icons/lucide/sun-moon.svg +19 -0
- data/registry/icons/lucide/truck.svg +19 -0
- data/registry/icons/lucide/user-plus.svg +18 -0
- data/registry/icons/lucide/volume-2.svg +17 -0
- data/registry/icons/lucide/volume-x.svg +17 -0
- data/registry/icons/lucide/zoom-in.svg +18 -0
- data/registry/icons/lucide/zoom-out.svg +17 -0
- data/registry/icons/phosphor/barcode.svg +2 -0
- data/registry/icons/phosphor/circle-half.svg +2 -0
- data/registry/icons/phosphor/download-simple.svg +2 -0
- data/registry/icons/phosphor/file-plus.svg +2 -0
- data/registry/icons/phosphor/file.svg +2 -0
- data/registry/icons/phosphor/folder.svg +2 -0
- data/registry/icons/phosphor/keyboard.svg +2 -0
- data/registry/icons/phosphor/magnifying-glass-minus.svg +2 -0
- data/registry/icons/phosphor/magnifying-glass-plus.svg +2 -0
- data/registry/icons/phosphor/minus.svg +2 -0
- data/registry/icons/phosphor/package.svg +2 -0
- data/registry/icons/phosphor/pause.svg +2 -0
- data/registry/icons/phosphor/percent.svg +2 -0
- data/registry/icons/phosphor/printer.svg +2 -0
- data/registry/icons/phosphor/receipt.svg +2 -0
- data/registry/icons/phosphor/speaker-high.svg +2 -0
- data/registry/icons/phosphor/speaker-x.svg +2 -0
- data/registry/icons/phosphor/truck.svg +2 -0
- data/registry/icons/phosphor/user-plus.svg +2 -0
- data/registry/icons/tabler/barcode.svg +23 -0
- data/registry/icons/tabler/download.svg +18 -0
- data/registry/icons/tabler/file-plus.svg +19 -0
- data/registry/icons/tabler/file.svg +17 -0
- data/registry/icons/tabler/folder.svg +16 -0
- data/registry/icons/tabler/keyboard.svg +23 -0
- data/registry/icons/tabler/minus.svg +16 -0
- data/registry/icons/tabler/package.svg +20 -0
- data/registry/icons/tabler/percentage.svg +18 -0
- data/registry/icons/tabler/player-pause.svg +17 -0
- data/registry/icons/tabler/printer.svg +18 -0
- data/registry/icons/tabler/receipt.svg +16 -0
- data/registry/icons/tabler/sun-moon.svg +21 -0
- data/registry/icons/tabler/truck.svg +18 -0
- data/registry/icons/tabler/user-plus.svg +19 -0
- data/registry/icons/tabler/volume-3.svg +17 -0
- data/registry/icons/tabler/volume.svg +18 -0
- data/registry/icons/tabler/zoom-in.svg +19 -0
- data/registry/icons/tabler/zoom-out.svg +18 -0
- data/registry/icons.yml +19 -0
- data/registry/test/components/previews/ui/chart_preview/cash_flow.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/donut.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/margin.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/pie.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/sales.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/stock.html.erb +6 -0
- data/registry/test/components/previews/ui/chart_preview/takings.html.erb +8 -0
- data/registry/test/components/previews/ui/chart_preview.rb +46 -0
- data/registry/test/components/previews/ui/command_preview/_picked.html.erb +14 -0
- data/registry/test/components/previews/ui/command_preview/dialog.html.erb +24 -0
- data/registry/test/components/previews/ui/command_preview/inline.html.erb +16 -0
- data/registry/test/components/previews/ui/command_preview/playground.html.erb +15 -0
- data/registry/test/components/previews/ui/command_preview/server_search.html.erb +24 -0
- data/registry/test/components/previews/ui/command_preview/shortcuts.html.erb +12 -0
- data/registry/test/components/previews/ui/command_preview/till.html.erb +17 -0
- data/registry/test/components/previews/ui/command_preview.rb +25 -0
- data/registry/test/components/previews/ui/date_picker_preview/custom_presets.html.erb +6 -0
- data/registry/test/components/previews/ui/date_picker_preview/in_form.html.erb +11 -0
- data/registry/test/components/previews/ui/date_picker_preview/range.html.erb +5 -0
- data/registry/test/components/previews/ui/date_picker_preview/states.html.erb +4 -3
- data/registry/test/components/previews/ui/date_picker_preview.rb +26 -6
- data/registry/test/components/previews/ui/file_field_preview/multiple_files.html.erb +1 -1
- data/registry/test/components/previews/ui/file_field_preview/product_photos.html.erb +12 -0
- data/registry/test/components/previews/ui/file_field_preview/single_image.html.erb +4 -0
- data/registry/test/components/previews/ui/file_field_preview.rb +32 -5
- data/registry/test/components/previews/ui/gallery_preview/hover_zoom.html.erb +13 -0
- data/registry/test/components/previews/ui/gallery_preview/landscape.html.erb +5 -0
- data/registry/test/components/previews/ui/gallery_preview/product_page.html.erb +13 -0
- data/registry/test/components/previews/ui/gallery_preview/single.html.erb +3 -0
- data/registry/test/components/previews/ui/gallery_preview.rb +45 -0
- data/registry/test/components/previews/ui/kbd_preview/bound.html.erb +20 -0
- data/registry/test/components/previews/ui/kbd_preview/keys.html.erb +13 -0
- data/registry/test/components/previews/ui/kbd_preview/menu.html.erb +13 -0
- data/registry/test/components/previews/ui/kbd_preview/search_field.html.erb +8 -0
- data/registry/test/components/previews/ui/kbd_preview/shortcuts_list.html.erb +17 -0
- data/registry/test/components/previews/ui/kbd_preview/till.html.erb +23 -0
- data/registry/test/components/previews/ui/kbd_preview/tooltip_and_buttons.html.erb +14 -0
- data/registry/test/components/previews/ui/kbd_preview.rb +32 -0
- data/registry/test/components/previews/ui/money_input_preview/cash_tendered.html.erb +24 -0
- data/registry/test/components/previews/ui/money_input_preview/currencies.html.erb +7 -0
- data/registry/test/components/previews/ui/money_input_preview/invoice.html.erb +24 -0
- data/registry/test/components/previews/ui/money_input_preview/sizes_and_states.html.erb +9 -0
- data/registry/test/components/previews/ui/money_input_preview.rb +34 -0
- data/registry/test/components/previews/ui/number_input_preview/cart.html.erb +11 -0
- data/registry/test/components/previews/ui/number_input_preview/decimals.html.erb +4 -0
- data/registry/test/components/previews/ui/number_input_preview/sizes.html.erb +5 -0
- data/registry/test/components/previews/ui/number_input_preview/states.html.erb +7 -0
- data/registry/test/components/previews/ui/number_input_preview/stock_adjustment.html.erb +3 -0
- data/registry/test/components/previews/ui/number_input_preview.rb +36 -0
- data/registry/test/components/previews/ui/progress_preview/file_upload.html.erb +36 -0
- data/registry/test/components/previews/ui/progress_preview/free_shipping.html.erb +4 -0
- data/registry/test/components/previews/ui/progress_preview/indeterminate.html.erb +4 -0
- data/registry/test/components/previews/ui/progress_preview/sizes_and_tones.html.erb +8 -0
- data/registry/test/components/previews/ui/progress_preview/steps.html.erb +9 -0
- data/registry/test/components/previews/ui/progress_preview/storage.html.erb +8 -0
- data/registry/test/components/previews/ui/progress_preview/storage_by_category.html.erb +7 -0
- data/registry/test/components/previews/ui/progress_preview/tasks_by_status.html.erb +14 -0
- data/registry/test/components/previews/ui/progress_preview/traffic_by_source.html.erb +15 -0
- data/registry/test/components/previews/ui/progress_preview.rb +46 -0
- data/registry/test/components/previews/ui/scan_input_preview/_simulate.html.erb +20 -0
- data/registry/test/components/previews/ui/scan_input_preview/library_checkout.html.erb +30 -0
- data/registry/test/components/previews/ui/scan_input_preview/playground.html.erb +13 -0
- data/registry/test/components/previews/ui/scan_input_preview/stock_count.html.erb +24 -0
- data/registry/test/components/previews/ui/scan_input_preview/ticket_check_in.html.erb +34 -0
- data/registry/test/components/previews/ui/scan_input_preview/till.html.erb +9 -0
- data/registry/test/components/previews/ui/scan_input_preview.rb +33 -0
- data/registry/test/components/previews/ui/select_preview/server_search.html.erb +22 -0
- data/registry/test/components/previews/ui/select_preview.rb +5 -0
- data/registry/test/components/previews/ui/slider_preview/named_steps.html.erb +7 -0
- data/registry/test/components/previews/ui/slider_preview/opening_hours.html.erb +5 -0
- data/registry/test/components/previews/ui/slider_preview/price_range.html.erb +6 -0
- data/registry/test/components/previews/ui/slider_preview/sizes_and_states.html.erb +9 -0
- data/registry/test/components/previews/ui/slider_preview/text_size.html.erb +16 -0
- data/registry/test/components/previews/ui/slider_preview/tip.html.erb +22 -0
- data/registry/test/components/previews/ui/slider_preview/volume.html.erb +5 -0
- data/registry/test/components/previews/ui/slider_preview.rb +45 -0
- data/registry/test/components/previews/ui/timeline_preview/activity_log.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/colored.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/colored_by_meaning.html.erb +7 -0
- data/registry/test/components/previews/ui/timeline_preview/failed_delivery.html.erb +6 -0
- data/registry/test/components/previews/ui/timeline_preview/order_status.html.erb +9 -0
- data/registry/test/components/previews/ui/timeline_preview/real_dates.html.erb +5 -0
- data/registry/test/components/previews/ui/timeline_preview/stock_history.html.erb +6 -0
- data/registry/test/components/previews/ui/timeline_preview.rb +40 -0
- data/registry/test/components/ui/chart_test.rb +187 -0
- data/registry/test/components/ui/command/group_test.rb +19 -0
- data/registry/test/components/ui/command/item_test.rb +47 -0
- data/registry/test/components/ui/command_test.rb +110 -0
- data/registry/test/components/ui/date_picker_test.rb +129 -111
- data/registry/test/components/ui/file_field_test.rb +105 -9
- data/registry/test/components/ui/gallery_test.rb +192 -0
- data/registry/test/components/ui/kbd_test.rb +150 -0
- data/registry/test/components/ui/money_input_test.rb +149 -0
- data/registry/test/components/ui/number_input_test.rb +155 -0
- data/registry/test/components/ui/progress_test.rb +188 -0
- data/registry/test/components/ui/scan_input_test.rb +110 -0
- data/registry/test/components/ui/select_test.rb +62 -0
- data/registry/test/components/ui/slider_test.rb +190 -0
- data/registry/test/components/ui/timeline_test.rb +175 -0
- metadata +188 -2
- data/registry/app/javascript/cally.ts +0 -8
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
|
|
5
|
+
# A handle to drag along a track: volume, a zoom level, how often to be
|
|
6
|
+
# emailed. `range: true` gives it two, a low and a high, for filters like a
|
|
7
|
+
# price band or opening hours.
|
|
8
|
+
#
|
|
9
|
+
# <%= render(UI::Slider.new(name: "volume", value: 40, label: "Volume", value_text: "%{value}%", show_value: true)) %>
|
|
10
|
+
# <%= render(UI::Slider.new(form: f, field: :alert_at, min: 50, max: 100, step: 5)) %>
|
|
11
|
+
# <%= render(UI::Slider.new(range: true, label: "Price", min: 0, max: 500, step: 10,
|
|
12
|
+
# from: params[:min_price] || 0, to: params[:max_price] || 500,
|
|
13
|
+
# from_name: "min_price", to_name: "max_price", value_text: "$%{value}", show_value: true)) %>
|
|
14
|
+
# <%= render(UI::Slider.new(range: true, form: f, from_field: :opens_at, to_field: :closes_at, max: 24)) %>
|
|
15
|
+
#
|
|
16
|
+
# Each handle is the browser's own <input type="range">, so it submits with
|
|
17
|
+
# the form, the arrow keys, Page Up and Down, Home and End move it, and screen
|
|
18
|
+
# readers announce it as a slider. Pressing anywhere on the track moves the
|
|
19
|
+
# nearest handle there and drags it. The two handles of a range can meet but
|
|
20
|
+
# not pass each other.
|
|
21
|
+
#
|
|
22
|
+
# `value_text:` formats a value for the reader to see (with
|
|
23
|
+
# `show_value: true`) and for a screen reader to hear: "%{value}%",
|
|
24
|
+
# "$%{value}", "%{value}:00". A range shows both, as "$20 – $80".
|
|
25
|
+
#
|
|
26
|
+
# `marks:` labels points along the track: an array of values, or a hash of
|
|
27
|
+
# value => label. A labelled value is also what's shown and heard there, so
|
|
28
|
+
#
|
|
29
|
+
# <%= render(UI::Slider.new(name: "quality", min: 1, max: 3, value: 2, show_value: true, label: "Quality",
|
|
30
|
+
# marks: { 1 => "Low", 2 => "Medium", 3 => "High" })) %>
|
|
31
|
+
#
|
|
32
|
+
# says "Medium" rather than 2. Pressing a mark moves the nearest handle to it.
|
|
33
|
+
#
|
|
34
|
+
# `aria:` goes on the input, or on a range's group: with `label: false`, give
|
|
35
|
+
# it `aria: { label: "Volume" }` so it still has a name.
|
|
36
|
+
#
|
|
37
|
+
# It fires the inputs' own `input` while a handle moves and `change` when it's
|
|
38
|
+
# let go, so a filter form can submit on change. Setting a handle's value from
|
|
39
|
+
# JavaScript, dispatch `input` on it after, and the track follows.
|
|
40
|
+
class UI::Slider < UI::Base
|
|
41
|
+
SIZES = {
|
|
42
|
+
sm: { thumb: "1.375rem", thumb_height: "1rem", track: "h-1.5" },
|
|
43
|
+
default: { thumb: "1.75rem", thumb_height: "1.25rem", track: "h-2.5" },
|
|
44
|
+
lg: { thumb: "2.25rem", thumb_height: "1.625rem", track: "h-3.5" }
|
|
45
|
+
}.freeze
|
|
46
|
+
|
|
47
|
+
# The input is there for the keyboard, the form and screen readers, and
|
|
48
|
+
# can't be seen. Its thumb could only sit on a step, so the knob is drawn
|
|
49
|
+
# separately: it follows the pointer freely while dragged, and glides onto
|
|
50
|
+
# the value otherwise. Pointer events go to the track (see the controller).
|
|
51
|
+
INPUT = "peer absolute inset-0 m-0 h-(--thumb-h) w-full cursor-[inherit] opacity-0 pointer-events-none"
|
|
52
|
+
|
|
53
|
+
# A white pill lifted by a shadow. The keyboard's focus and a drag each put
|
|
54
|
+
# a ring around it, outside its edge.
|
|
55
|
+
KNOB = "pointer-events-none absolute top-1/2 h-(--thumb-h) w-(--thumb) -translate-y-1/2 rounded-full bg-white " \
|
|
56
|
+
"shadow-[0_1px_3px_rgb(0_0_0/0.3),0_0_0_0.5px_rgb(0_0_0/0.08)] " \
|
|
57
|
+
"transition-[inset-inline-start,box-shadow] duration-150 ease-out motion-reduce:transition-none " \
|
|
58
|
+
"group-data-dragging/slider:transition-[box-shadow] " \
|
|
59
|
+
"peer-focus-visible:ring-3 peer-focus-visible:ring-ring/50 " \
|
|
60
|
+
"peer-data-active:shadow-none peer-data-active:ring-4 peer-data-active:ring-foreground/15"
|
|
61
|
+
|
|
62
|
+
# The fill glides with the knob, and keeps up with it during a drag.
|
|
63
|
+
FILL = "absolute inset-y-0 bg-primary transition-[inset-inline-start,width] duration-150 ease-out " \
|
|
64
|
+
"motion-reduce:transition-none group-data-dragging/slider:transition-none"
|
|
65
|
+
|
|
66
|
+
# The ends sit flush with the track's ends; the rest centre on their value.
|
|
67
|
+
MARK_PLACES = { 0.0 => { shift: "", style: "inset-inline-start: 0px" },
|
|
68
|
+
1.0 => { shift: "-translate-x-full rtl:translate-x-full", style: "inset-inline-start: 100%" } }.freeze
|
|
69
|
+
|
|
70
|
+
# Where a value sits along the track: the thumb's centre goes from half a
|
|
71
|
+
# thumb in at one end to half a thumb in at the other.
|
|
72
|
+
def self.at(fraction) = "calc(var(--thumb) / 2 + (100% - var(--thumb)) * #{fraction})"
|
|
73
|
+
|
|
74
|
+
def initialize(name: nil, value: nil, min: 0, max: 100, step: 1, form: nil, field: nil,
|
|
75
|
+
range: false, from: nil, to: nil, from_name: "from", to_name: "to", from_field: nil, to_field: nil,
|
|
76
|
+
label: nil, hint: nil, error: nil, value_text: nil, show_value: false, marks: nil,
|
|
77
|
+
size: :default, disabled: false, class: nil, **html_attrs)
|
|
78
|
+
@min = min.to_f
|
|
79
|
+
@max = [ max.to_f, @min ].max
|
|
80
|
+
@step = step.to_f.positive? ? step.to_f : 1.0
|
|
81
|
+
@form = form
|
|
82
|
+
@range = range
|
|
83
|
+
@label = label
|
|
84
|
+
@value_text = value_text
|
|
85
|
+
@show_value = show_value
|
|
86
|
+
@marks = marks.is_a?(Hash) ? marks.transform_keys(&:to_f) : Array(marks).to_h { |m| [ m.to_f, nil ] }
|
|
87
|
+
@size = SIZES.key?(size.to_sym) ? size.to_sym : :default
|
|
88
|
+
@disabled = disabled
|
|
89
|
+
@user_class = binding.local_variable_get(:class)
|
|
90
|
+
@aria = html_attrs.delete(:aria) || {}
|
|
91
|
+
@html_attrs = html_attrs
|
|
92
|
+
@id = html_attrs.delete(:id) || "ui-slider-#{SecureRandom.hex(4)}"
|
|
93
|
+
@fields = handles(name: name, value: value, field: field, from: from, to: to, from_name: from_name,
|
|
94
|
+
to_name: to_name, from_field: from_field, to_field: to_field)
|
|
95
|
+
@wrapper = UI::FormField.new(label: false, hint: hint, error: error, form: form,
|
|
96
|
+
field: range ? from_field : field, for: @id)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
attr_reader :wrapper, :fields
|
|
100
|
+
|
|
101
|
+
def range? = @range
|
|
102
|
+
def disabled? = @disabled
|
|
103
|
+
def show_value? = @show_value
|
|
104
|
+
|
|
105
|
+
def label_id = "#{@id}-label"
|
|
106
|
+
|
|
107
|
+
def label_text
|
|
108
|
+
return nil if @label == false
|
|
109
|
+
return @label if @label.present?
|
|
110
|
+
|
|
111
|
+
field = @fields.first[:field]
|
|
112
|
+
return nil unless @form && field
|
|
113
|
+
|
|
114
|
+
@form.object.class.try(:human_attribute_name, field) || field.to_s.humanize
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def root_attrs
|
|
118
|
+
{ **@html_attrs, class: cn("group/slider w-full space-y-2", disabled? && "opacity-50", @user_class),
|
|
119
|
+
role: ("group" if range?), aria: ({ labelledby: (label_id if label_text), **@aria }.compact if range?),
|
|
120
|
+
style: "--thumb: #{SIZES[@size][:thumb]}; --thumb-h: #{SIZES[@size][:thumb_height]}; --from: #{number(fraction(lower))}; --to: #{number(fraction(upper))}",
|
|
121
|
+
data: { slot: "slider", controller: "ui--slider", "ui--slider-format-value": text_format,
|
|
122
|
+
"ui--slider-range-format-value": range_format, "ui--slider-labels-value": labels.to_json,
|
|
123
|
+
**(@html_attrs[:data] || {}) } }.compact
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# Grows from the start of the track to the value, or between a range's two.
|
|
127
|
+
def fill_style
|
|
128
|
+
start = range? ? self.class.at("var(--from)") : "0px"
|
|
129
|
+
width = range? ? "calc((100% - var(--thumb)) * (var(--to) - var(--from)))" : self.class.at("var(--to)")
|
|
130
|
+
"inset-inline-start: #{start}; width: #{width}"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def track_attrs
|
|
134
|
+
{ class: cn("relative h-(--thumb-h) touch-pan-y select-none", disabled? ? "cursor-not-allowed" : "cursor-pointer"),
|
|
135
|
+
data: { "ui--slider-target": "track",
|
|
136
|
+
action: ("pointerdown->ui--slider#press" unless disabled?) } }.compact
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
# The knob's start edge: its centre is where self.at puts the value.
|
|
140
|
+
def knob_style(handle)
|
|
141
|
+
fraction = range? && handle.equal?(@fields.first) ? "var(--from)" : "var(--to)"
|
|
142
|
+
"inset-inline-start: calc((100% - var(--thumb)) * #{fraction})"
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def track_class = cn("absolute inset-x-0 top-1/2 -translate-y-1/2 overflow-hidden rounded-full bg-foreground/10",
|
|
146
|
+
SIZES[@size][:track])
|
|
147
|
+
|
|
148
|
+
def input_attrs(handle)
|
|
149
|
+
{ type: "range", id: handle[:id], min: number(@min), max: number(@max), step: number(@step),
|
|
150
|
+
value: number(handle[:value]), disabled: (true if disabled?), class: INPUT,
|
|
151
|
+
"aria-valuetext": value_text(handle[:value]), "aria-label": handle_name(handle),
|
|
152
|
+
"aria-describedby": wrapper.describedby, "aria-invalid": ("true" if wrapper.error?),
|
|
153
|
+
data: { "ui--slider-target": "input", action: "input->ui--slider#sync" },
|
|
154
|
+
aria: (@aria unless range?) }.compact
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# Renders the bound field through the form builder, so it takes the model's
|
|
158
|
+
# name, id prefix and value the way a Rails form expects.
|
|
159
|
+
def render_input(handle)
|
|
160
|
+
attrs = input_attrs(handle)
|
|
161
|
+
if @form && handle[:field]
|
|
162
|
+
@form.range_field(handle[:field], **attrs.except(:type, :value), value: attrs[:value])
|
|
163
|
+
else
|
|
164
|
+
tag.input(**attrs, name: handle[:name])
|
|
165
|
+
end
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
def output_text = range? ? range_text(lower, upper) : value_text(upper)
|
|
169
|
+
|
|
170
|
+
def header? = label_text.present? || show_value?
|
|
171
|
+
|
|
172
|
+
def marks? = @marks.any?
|
|
173
|
+
|
|
174
|
+
# Each mark's label and where it sits.
|
|
175
|
+
def marks
|
|
176
|
+
@marks.filter_map do |value, label|
|
|
177
|
+
next unless value.between?(@min, @max)
|
|
178
|
+
|
|
179
|
+
at = fraction(value)
|
|
180
|
+
{ label: label.presence || format_value(value), value: number(value), **MARK_PLACES.fetch(at.to_f) { centred(at) } }
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def centred(at) = { shift: "-translate-x-1/2 rtl:translate-x-1/2", style: "inset-inline-start: #{self.class.at(at)}" }
|
|
185
|
+
|
|
186
|
+
def mark_attrs(mark)
|
|
187
|
+
{ class: cn("absolute top-0 whitespace-nowrap", mark[:shift], !disabled? && "cursor-pointer hover:text-foreground"),
|
|
188
|
+
style: mark[:style],
|
|
189
|
+
data: { action: ("click->ui--slider#pick" unless disabled?), "ui--slider-value-param": mark[:value] }.compact }
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def value_text(value)
|
|
193
|
+
labels[number(value)] || text_format.gsub("%{value}", format_value(value))
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
private
|
|
197
|
+
|
|
198
|
+
def handles(name:, value:, field:, from:, to:, from_name:, to_name:, from_field:, to_field:)
|
|
199
|
+
if @range
|
|
200
|
+
low = snap(clamp(bound(from_field) || from || @min))
|
|
201
|
+
high = snap(clamp(bound(to_field) || to || @max))
|
|
202
|
+
low, high = high, low if low > high
|
|
203
|
+
[ { id: @id, name: from_name, field: from_field, value: low, aria_label: :from },
|
|
204
|
+
{ id: "#{@id}-to", name: to_name, field: to_field, value: high,
|
|
205
|
+
aria_label: :to } ]
|
|
206
|
+
else
|
|
207
|
+
# With no value, the middle, as the browser would.
|
|
208
|
+
start = bound(field) || value || (@min + ((@max - @min) / 2))
|
|
209
|
+
[ { id: @id, name: name, field: field, value: snap(clamp(start)), aria_label: nil } ]
|
|
210
|
+
end
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# A range's handles are named for screen readers; a single one has the label.
|
|
214
|
+
def handle_name(handle)
|
|
215
|
+
return nil unless handle[:aria_label]
|
|
216
|
+
|
|
217
|
+
t("ui.slider.#{handle[:aria_label]}", default: handle[:aria_label] == :from ? "Minimum" : "Maximum")
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def lower = range? ? @fields.first[:value] : @min
|
|
221
|
+
def upper = @fields.last[:value]
|
|
222
|
+
|
|
223
|
+
def bound(field)
|
|
224
|
+
return nil unless @form && field && @form.object.respond_to?(field)
|
|
225
|
+
|
|
226
|
+
@form.object.public_send(field).presence
|
|
227
|
+
end
|
|
228
|
+
|
|
229
|
+
def clamp(value) = value.to_f.clamp(@min, @max)
|
|
230
|
+
|
|
231
|
+
# To the nearest step from min, the way the browser rounds a range input.
|
|
232
|
+
def snap(value) = (@min + (((value - @min) / @step).round * @step)).clamp(@min, @max).round(decimals)
|
|
233
|
+
|
|
234
|
+
def fraction(value)
|
|
235
|
+
return 0 if @max == @min
|
|
236
|
+
|
|
237
|
+
((value - @min) / (@max - @min)).round(4)
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def text_format = @value_text || "%{value}"
|
|
241
|
+
|
|
242
|
+
def range_format = t("ui.slider.range_text", default: "%{from} – %{to}")
|
|
243
|
+
|
|
244
|
+
def range_text(low, high) = range_format.gsub("%{from}", value_text(low)).gsub("%{to}", value_text(high))
|
|
245
|
+
|
|
246
|
+
def labels = @marks.each_with_object({}) { |(value, label), all| all[number(value)] = label if label.present? }
|
|
247
|
+
|
|
248
|
+
def format_value(value) = number(value.to_f.round(decimals))
|
|
249
|
+
|
|
250
|
+
def decimals = number(@step)[/\.(\d+)\z/, 1].to_s.length
|
|
251
|
+
|
|
252
|
+
# 3, not 3.0; 2.5 stays 2.5.
|
|
253
|
+
def number(n) = (n.to_f % 1).zero? ? n.to_i.to_s : n.to_f.to_s
|
|
254
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
<%= tag.li(**item_attrs) do %>
|
|
2
|
+
<%= tag.span class: marker_classes, aria: { hidden: true } do %>
|
|
3
|
+
<% if !dot? && marker_icon %><%= render(UI::Icon.new(name: marker_icon, size: :sm)) %><% end %>
|
|
4
|
+
<% if !dot? && !marker_icon && status == :current %><%= tag.span class: "size-2 rounded-full bg-current" %><% end %>
|
|
5
|
+
<% end %>
|
|
6
|
+
<% if connector? %><%= tag.span class: connector_classes, aria: { hidden: true } %><% end %>
|
|
7
|
+
<%= tag.div class: text_classes do %>
|
|
8
|
+
<%= tag.div class: "flex flex-wrap items-baseline justify-between gap-x-3 gap-y-0.5" do %>
|
|
9
|
+
<%= tag.span class: title_classes do %><%= title %><%= tag.span ", #{status_text}", class: "sr-only" %><% end %>
|
|
10
|
+
<% if time? %>
|
|
11
|
+
<% if date? %>
|
|
12
|
+
<%= tag.time time_text, datetime: @time.iso8601, class: "text-xs text-muted-foreground" %>
|
|
13
|
+
<% else %>
|
|
14
|
+
<%= tag.span time_text, class: "text-xs text-muted-foreground" %>
|
|
15
|
+
<% end %>
|
|
16
|
+
<% end %>
|
|
17
|
+
<% end %>
|
|
18
|
+
<% if description? %><%= tag.p description, class: "text-sm text-muted-foreground" %><% end %>
|
|
19
|
+
<% if content? %><%= tag.div content, class: "mt-1.5 text-sm text-foreground" %><% end %>
|
|
20
|
+
<% end %>
|
|
21
|
+
<% end %>
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# One entry in a UI::Timeline: its marker, its title, when it happened, and the
|
|
4
|
+
# line on to the next entry. Its block is shown under the title, for anything
|
|
5
|
+
# more than `description:` (a link, a note, a badge).
|
|
6
|
+
#
|
|
7
|
+
# `status:` is :complete (the default, since most entries record something that
|
|
8
|
+
# has happened), :current, :upcoming or :error. `icon:` draws an icon in place
|
|
9
|
+
# of the status's own mark; a :dot timeline draws no icons.
|
|
10
|
+
#
|
|
11
|
+
# In a :colored timeline each circle takes the next colour in COLORS, or the one
|
|
12
|
+
# `color:` names, for colours that mean something (:emerald for stock received,
|
|
13
|
+
# :rose for written off). :upcoming and :error keep their own look, so a failure
|
|
14
|
+
# always reads as one.
|
|
15
|
+
#
|
|
16
|
+
# `time:` is a Time or Date, shown with the locale's :long format inside a
|
|
17
|
+
# <time> element; `time_label:` changes the words but keeps the machine-readable
|
|
18
|
+
# date ("2 hours ago"). A String is shown as it is, for what isn't a date yet
|
|
19
|
+
# ("Expected Friday").
|
|
20
|
+
class UI::Timeline::Item < UI::Base
|
|
21
|
+
STATUSES = %i[complete current upcoming error].freeze
|
|
22
|
+
|
|
23
|
+
ICONS = { complete: "check", error: "x" }.freeze
|
|
24
|
+
|
|
25
|
+
# Written out in full, status by status, so Tailwind finds every class.
|
|
26
|
+
MARKER = {
|
|
27
|
+
default: {
|
|
28
|
+
complete: "size-8 bg-primary text-primary-foreground",
|
|
29
|
+
current: "size-8 border-2 border-primary bg-background text-primary ring-4 ring-primary/15",
|
|
30
|
+
upcoming: "size-8 bg-muted text-muted-foreground",
|
|
31
|
+
error: "size-8 bg-destructive text-white ring-4 ring-destructive/15"
|
|
32
|
+
},
|
|
33
|
+
dot: {
|
|
34
|
+
complete: "mt-1 size-3 bg-primary",
|
|
35
|
+
current: "mt-1 size-3 border-2 border-primary bg-background ring-4 ring-primary/15",
|
|
36
|
+
upcoming: "mt-1 size-3 bg-muted-foreground/30",
|
|
37
|
+
error: "mt-1 size-3 bg-destructive ring-4 ring-destructive/15"
|
|
38
|
+
}
|
|
39
|
+
}.freeze
|
|
40
|
+
|
|
41
|
+
# Each colour's circle, then the ring it wears as the current item.
|
|
42
|
+
COLORS = {
|
|
43
|
+
blue: [ "bg-blue-600 text-white", "ring-4 ring-blue-600/20" ],
|
|
44
|
+
emerald: [ "bg-emerald-600 text-white", "ring-4 ring-emerald-600/20" ],
|
|
45
|
+
amber: [ "bg-amber-600 text-white", "ring-4 ring-amber-600/20" ],
|
|
46
|
+
violet: [ "bg-violet-600 text-white", "ring-4 ring-violet-600/20" ],
|
|
47
|
+
rose: [ "bg-rose-600 text-white", "ring-4 ring-rose-600/20" ],
|
|
48
|
+
teal: [ "bg-teal-600 text-white", "ring-4 ring-teal-600/20" ]
|
|
49
|
+
}.freeze
|
|
50
|
+
|
|
51
|
+
TITLE = {
|
|
52
|
+
complete: "text-foreground",
|
|
53
|
+
current: "text-foreground",
|
|
54
|
+
upcoming: "text-muted-foreground",
|
|
55
|
+
error: "text-destructive"
|
|
56
|
+
}.freeze
|
|
57
|
+
|
|
58
|
+
STATUS_TEXT = {
|
|
59
|
+
complete: "Completed",
|
|
60
|
+
current: "In progress",
|
|
61
|
+
upcoming: "Not started",
|
|
62
|
+
error: "Failed"
|
|
63
|
+
}.freeze
|
|
64
|
+
|
|
65
|
+
attr_reader :status, :title, :description, :icon
|
|
66
|
+
|
|
67
|
+
def initialize(title:, description: nil, time: nil, time_label: nil, status: :complete, icon: nil,
|
|
68
|
+
color: nil, class: nil, **html_attrs)
|
|
69
|
+
@title = title
|
|
70
|
+
@description = description
|
|
71
|
+
@time = time
|
|
72
|
+
@time_label = time_label
|
|
73
|
+
@status = STATUSES.include?(status&.to_sym) ? status.to_sym : :complete
|
|
74
|
+
@icon = icon
|
|
75
|
+
@color = color&.to_sym
|
|
76
|
+
@user_class = binding.local_variable_get(:class)
|
|
77
|
+
@html_attrs = html_attrs
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def place(variant:, index:, last:)
|
|
81
|
+
@variant = variant
|
|
82
|
+
@index = index
|
|
83
|
+
@last = last
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def dot? = @variant == :dot
|
|
87
|
+
def colored? = @variant == :colored && %i[complete current].include?(@status)
|
|
88
|
+
def color = COLORS.key?(@color) ? @color : COLORS.keys[@index % COLORS.size]
|
|
89
|
+
def connector? = !@last
|
|
90
|
+
def description? = @description.present?
|
|
91
|
+
def time? = @time.present?
|
|
92
|
+
def date? = @time.respond_to?(:iso8601)
|
|
93
|
+
def marker_icon = @icon || ICONS[@status]
|
|
94
|
+
|
|
95
|
+
def time_text
|
|
96
|
+
return @time_label if @time_label.present?
|
|
97
|
+
|
|
98
|
+
date? ? I18n.l(@time, format: :long) : @time.to_s
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
def item_attrs
|
|
102
|
+
attrs = @html_attrs.except(:data, :aria)
|
|
103
|
+
attrs[:class] = cn("relative flex min-w-0 gap-3", { "pb-6" => connector? }, @user_class)
|
|
104
|
+
attrs[:aria] = { current: ("step" if @status == :current) }.compact.merge(@html_attrs[:aria] || {})
|
|
105
|
+
attrs[:data] = { slot: "timeline-item", status: @status }.merge(@html_attrs[:data] || {})
|
|
106
|
+
attrs
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def marker_classes
|
|
110
|
+
# A circle is taller than a line of text. Its overhang doesn't count towards
|
|
111
|
+
# the item's height, so the text alone sets the gap to the next item, however
|
|
112
|
+
# much text there is. The last item has nothing below to overhang into.
|
|
113
|
+
cn("relative z-10 flex shrink-0 items-center justify-center rounded-full", marker_look,
|
|
114
|
+
{ "-mb-2" => !dot? && connector? })
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
def marker_look
|
|
118
|
+
return MARKER[dot? ? :dot : :default][@status] unless colored?
|
|
119
|
+
|
|
120
|
+
fill, ring = COLORS[color]
|
|
121
|
+
cn("size-8", fill, (ring if @status == :current))
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# Unbroken from this marker to the next one, drawn in while the entry is done
|
|
125
|
+
# (a plain line between colours, which would clash with any one of them).
|
|
126
|
+
# A dot sits 0.25rem down its item, so the line runs that far into the next.
|
|
127
|
+
# Placed from the start edge so it follows the marker in a right-to-left page.
|
|
128
|
+
def connector_classes
|
|
129
|
+
cn("absolute w-0.5",
|
|
130
|
+
dot? ? "start-[calc(0.375rem-1px)] top-4 -bottom-1" : "start-[calc(1rem-1px)] top-8 bottom-0",
|
|
131
|
+
@status == :complete && @variant != :colored ? "bg-primary" : "bg-border")
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def text_classes
|
|
135
|
+
cn("flex min-w-0 flex-1 flex-col gap-0.5", { "pt-1" => !dot? })
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def title_classes
|
|
139
|
+
cn("text-sm font-medium", TITLE[@status])
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def status_text
|
|
143
|
+
t("ui.timeline.#{@status}", default: STATUS_TEXT[@status])
|
|
144
|
+
end
|
|
145
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<%= tag.ol(**root_attrs) do %><% items.each do |item| %><%= item %><% end %><% end %>
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# What happened, and what's still to come, as a list down the page: an order's
|
|
4
|
+
# progress, a product's stock history, an account's activity. UI::Stepper is
|
|
5
|
+
# for moving someone through a form; this only records.
|
|
6
|
+
#
|
|
7
|
+
# <%= render(UI::Timeline.new(label: "Order history")) do |timeline| %>
|
|
8
|
+
# <% timeline.with_item(title: "Paid", time: order.paid_at, icon: "credit-card") %>
|
|
9
|
+
# <% timeline.with_item(title: "Shipped", status: :current) do %>
|
|
10
|
+
# <%= link_to "Track parcel", order.tracking_url %>
|
|
11
|
+
# <% end %>
|
|
12
|
+
# <% timeline.with_item(title: "Delivered", status: :upcoming, time: "Expected Friday") %>
|
|
13
|
+
# <% end %>
|
|
14
|
+
#
|
|
15
|
+
# Items show in the order they're given, so newest-first is up to the caller.
|
|
16
|
+
# `variant: :default` draws a circle holding the item's icon or a mark for its
|
|
17
|
+
# status, `:dot` a small dot, for a long log, and `:colored` the same circles in
|
|
18
|
+
# a different colour each. Each item's options are on UI::Timeline::Item.
|
|
19
|
+
class UI::Timeline < UI::Base
|
|
20
|
+
VARIANTS = %i[default dot colored].freeze
|
|
21
|
+
|
|
22
|
+
renders_many :items, UI::Timeline::Item
|
|
23
|
+
|
|
24
|
+
def initialize(variant: :default, label: nil, class: nil, **html_attrs)
|
|
25
|
+
@variant = VARIANTS.include?(variant.to_sym) ? variant.to_sym : :default
|
|
26
|
+
@label = label
|
|
27
|
+
@user_class = binding.local_variable_get(:class)
|
|
28
|
+
@html_attrs = html_attrs
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def before_render
|
|
32
|
+
items.each_with_index { |item, index| item.place(variant: @variant, index: index, last: index == items.size - 1) }
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def root_attrs
|
|
36
|
+
attrs = @html_attrs.except(:data, :aria)
|
|
37
|
+
attrs[:class] = cn("flex w-full flex-col", @user_class)
|
|
38
|
+
attrs[:aria] = { label: @label || t("ui.timeline.label", default: "Timeline") }.merge(@html_attrs[:aria] || {})
|
|
39
|
+
attrs[:data] = { slot: "timeline", variant: @variant }.merge(@html_attrs[:data] || {})
|
|
40
|
+
attrs
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// Chart.js, built as its own entry point rather than bundled into
|
|
2
|
+
// application.js, so a page without a chart never downloads it. Only the line,
|
|
3
|
+
// bar, pie and doughnut parts are registered, a fraction of the full build.
|
|
4
|
+
//
|
|
5
|
+
// Same shape as ag_grid.ts and cally.ts, and for the same reasons: a bun
|
|
6
|
+
// code-split chunk can't be used because Propshaft serves digested filenames,
|
|
7
|
+
// and the URL is handed to the Stimulus controller from Ruby.
|
|
8
|
+
import {
|
|
9
|
+
ArcElement,
|
|
10
|
+
BarController,
|
|
11
|
+
BarElement,
|
|
12
|
+
CategoryScale,
|
|
13
|
+
Chart,
|
|
14
|
+
DoughnutController,
|
|
15
|
+
LinearScale,
|
|
16
|
+
LineController,
|
|
17
|
+
LineElement,
|
|
18
|
+
PieController,
|
|
19
|
+
PointElement,
|
|
20
|
+
Tooltip,
|
|
21
|
+
} from 'chart.js';
|
|
22
|
+
|
|
23
|
+
Chart.register(
|
|
24
|
+
ArcElement,
|
|
25
|
+
BarController,
|
|
26
|
+
BarElement,
|
|
27
|
+
CategoryScale,
|
|
28
|
+
DoughnutController,
|
|
29
|
+
LinearScale,
|
|
30
|
+
LineController,
|
|
31
|
+
LineElement,
|
|
32
|
+
PieController,
|
|
33
|
+
PointElement,
|
|
34
|
+
Tooltip,
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export { Chart };
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Typed dates in the locale's order, and ISO dates for the form.
|
|
2
|
+
//
|
|
3
|
+
// The order ("DMY") and separator come from Ruby, which derives them from
|
|
4
|
+
// date.formats.typed in the locale, so one string governs what a date field
|
|
5
|
+
// accepts, shows and hints at.
|
|
6
|
+
const ISO = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
7
|
+
// Any of / - . or a space between the parts: people type what is in front of
|
|
8
|
+
// them, and being strict about the separator only creates work.
|
|
9
|
+
const PARTS = /^(\d{1,4})[/\-. ](\d{1,4})[/\-. ](\d{1,4})$/;
|
|
10
|
+
const SEPARATORS = /[/\-. ]/;
|
|
11
|
+
const LENGTH: Record<string, number> = { D: 2, M: 2, Y: 4 };
|
|
12
|
+
|
|
13
|
+
// The locale's order in, ISO out. Rejects 31/02 rather than rolling it into
|
|
14
|
+
// the 3rd of March, which is what Date does and how a wrong date gets stored
|
|
15
|
+
// without anyone seeing an error.
|
|
16
|
+
export function toIso(typed: string, order: string): string | null {
|
|
17
|
+
const match = PARTS.exec(typed.trim());
|
|
18
|
+
if (!match) return null;
|
|
19
|
+
|
|
20
|
+
const parts = order.split('');
|
|
21
|
+
const day = match[parts.indexOf('D') + 1];
|
|
22
|
+
const month = match[parts.indexOf('M') + 1];
|
|
23
|
+
const year = match[parts.indexOf('Y') + 1];
|
|
24
|
+
if (!day || !month || !year || year.length !== 4) return null;
|
|
25
|
+
|
|
26
|
+
const iso = `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`;
|
|
27
|
+
const date = new Date(`${iso}T00:00:00`);
|
|
28
|
+
if (Number.isNaN(date.getTime())) return null;
|
|
29
|
+
|
|
30
|
+
// Round-tripped, because Date accepts the 31st of February and quietly
|
|
31
|
+
// returns the 3rd of March.
|
|
32
|
+
return date.getDate() === Number(day) && date.getMonth() + 1 === Number(month) ? iso : null;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function toTyped(iso: string, order: string, separator: string): string {
|
|
36
|
+
const match = ISO.exec((iso ?? '').trim());
|
|
37
|
+
if (!match) return '';
|
|
38
|
+
|
|
39
|
+
const [, year, month, day] = match;
|
|
40
|
+
const parts: Record<string, string> = { D: day, M: month, Y: year };
|
|
41
|
+
return order.split('').map((key) => parts[key]).join(separator);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// What a date field holds as it's typed: digits only, in the locale's order,
|
|
45
|
+
// the separator put in as each part fills. A separator typed early ends the
|
|
46
|
+
// part, so "1/" becomes "01/". A pasted ISO date is turned round. `growing`
|
|
47
|
+
// is false while deleting, so backspacing over a separator isn't undone.
|
|
48
|
+
export function maskDate(raw: string, order: string, separator: string, growing = true): string {
|
|
49
|
+
if (ISO.test(raw.trim())) return toTyped(raw.trim(), order, separator);
|
|
50
|
+
|
|
51
|
+
const keys = order.split('');
|
|
52
|
+
const parts: string[] = [];
|
|
53
|
+
let current = '';
|
|
54
|
+
for (const char of raw) {
|
|
55
|
+
if (parts.length === keys.length) break;
|
|
56
|
+
|
|
57
|
+
const key = keys[parts.length];
|
|
58
|
+
if (/\d/.test(char)) {
|
|
59
|
+
current += char;
|
|
60
|
+
if (current.length === LENGTH[key]) {
|
|
61
|
+
parts.push(current);
|
|
62
|
+
current = '';
|
|
63
|
+
}
|
|
64
|
+
} else if (SEPARATORS.test(char) && current !== '' && key !== 'Y') {
|
|
65
|
+
parts.push(current.padStart(LENGTH[key], '0'));
|
|
66
|
+
current = '';
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
let out = parts.join(separator);
|
|
71
|
+
if (current) out += (parts.length ? separator : '') + current;
|
|
72
|
+
else if (parts.length > 0 && parts.length < keys.length && (growing || SEPARATORS.test(raw.slice(-1)))) out += separator;
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Keyboard shortcuts written the way UI::Kbd writes them: "mod+k",
|
|
2
|
+
// "shift+enter", "alt+c", "/". "mod" is ⌘ on a Mac and Ctrl elsewhere.
|
|
3
|
+
const MODIFIERS = ['mod', 'cmd', 'ctrl', 'alt', 'shift'] as const;
|
|
4
|
+
type Modifier = (typeof MODIFIERS)[number];
|
|
5
|
+
|
|
6
|
+
const ALIASES: Record<string, string> = {
|
|
7
|
+
command: 'cmd', meta: 'cmd', control: 'ctrl', option: 'alt', opt: 'alt', return: 'enter',
|
|
8
|
+
escape: 'esc', del: 'delete', arrowup: 'up', arrowdown: 'down', arrowleft: 'left', arrowright: 'right',
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// What KeyboardEvent.key says for the named keys.
|
|
12
|
+
const EVENT_KEYS: Record<string, string> = {
|
|
13
|
+
enter: 'enter', esc: 'escape', tab: 'tab', space: ' ', backspace: 'backspace', delete: 'delete',
|
|
14
|
+
up: 'arrowup', down: 'arrowdown', left: 'arrowleft', right: 'arrowright',
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// aria-keyshortcuts spells keys the way KeyboardEvent.key does.
|
|
18
|
+
const ARIA_NAMES: Record<string, string> = {
|
|
19
|
+
cmd: 'Meta', ctrl: 'Control', alt: 'Alt', shift: 'Shift', enter: 'Enter', esc: 'Escape', tab: 'Tab',
|
|
20
|
+
space: 'Space', backspace: 'Backspace', delete: 'Delete', up: 'ArrowUp', down: 'ArrowDown',
|
|
21
|
+
left: 'ArrowLeft', right: 'ArrowRight',
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export type Shortcut = { mods: Set<Modifier>; key: string };
|
|
25
|
+
|
|
26
|
+
const platformName = () => {
|
|
27
|
+
const nav = navigator as Navigator & { userAgentData?: { platform?: string } };
|
|
28
|
+
return nav.userAgentData?.platform || navigator.platform || navigator.userAgent;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const isApple = () => /mac|iphone|ipad|ipod/i.test(platformName());
|
|
32
|
+
|
|
33
|
+
// What to label keys for: a Mac, a Linux desktop (where the Windows key is
|
|
34
|
+
// Super), or anything else. Android and ChromeOS report Linux too.
|
|
35
|
+
export const keyboardPlatform = (): 'mac' | 'linux' | 'other' => {
|
|
36
|
+
if (isApple()) return 'mac';
|
|
37
|
+
const name = platformName();
|
|
38
|
+
const agent = navigator.userAgent;
|
|
39
|
+
if (/linux|x11/i.test(name) && !/android|cros/i.test(agent)) return 'linux';
|
|
40
|
+
return 'other';
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const parseShortcut = (keys: string): Shortcut => {
|
|
44
|
+
const names = keys.split('+').map((part) => part.trim().toLowerCase()).filter(Boolean).map((part) => ALIASES[part] ?? part);
|
|
45
|
+
const isMod = (n: string): n is Modifier => (MODIFIERS as readonly string[]).includes(n);
|
|
46
|
+
return { mods: new Set(names.filter(isMod)), key: names.filter((n) => !isMod(n)).pop() ?? '' };
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Modifiers have to match exactly, so Ctrl+S isn't ⌘S on a Mac.
|
|
50
|
+
export const matchesShortcut = (event: KeyboardEvent, { mods, key }: Shortcut, apple = isApple()) => {
|
|
51
|
+
const wantMeta = mods.has('cmd') || (mods.has('mod') && apple);
|
|
52
|
+
const wantCtrl = mods.has('ctrl') || (mods.has('mod') && !apple);
|
|
53
|
+
if (event.metaKey !== wantMeta || event.ctrlKey !== wantCtrl || event.altKey !== mods.has('alt')) return false;
|
|
54
|
+
|
|
55
|
+
// Letters and digits by their place on the keyboard: Option on a Mac turns
|
|
56
|
+
// "k" into "˚". Shift has to match for these.
|
|
57
|
+
if (/^[a-z]$/.test(key)) return event.shiftKey === mods.has('shift') && event.code === `Key${key.toUpperCase()}`;
|
|
58
|
+
if (/^[0-9]$/.test(key)) return event.shiftKey === mods.has('shift') && event.code === `Digit${key}`;
|
|
59
|
+
|
|
60
|
+
const named = EVENT_KEYS[key];
|
|
61
|
+
if (named) return event.shiftKey === mods.has('shift') && event.key.toLowerCase() === named;
|
|
62
|
+
// A symbol, like "?" or "/": the character typed, whatever it took to type it.
|
|
63
|
+
return event.key.toLowerCase() === key;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// A shortcut with mod, ctrl or alt still works while typing in a field; a
|
|
67
|
+
// bare key is typing.
|
|
68
|
+
export const usableWhileTyping = ({ mods }: Shortcut) => mods.has('mod') || mods.has('ctrl') || mods.has('alt') || mods.has('cmd');
|
|
69
|
+
|
|
70
|
+
export const isTyping = (target: EventTarget | null) => {
|
|
71
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
72
|
+
return target.isContentEditable ||
|
|
73
|
+
target.matches('input:not([type="checkbox"], [type="radio"], [type="button"], [type="submit"]), textarea, select');
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const ariaShortcut = ({ mods, key }: Shortcut, apple = isApple()) => {
|
|
77
|
+
const names = [...mods].map((mod) => (mod === 'mod' ? (apple ? 'Meta' : 'Control') : ARIA_NAMES[mod]));
|
|
78
|
+
names.push(ARIA_NAMES[key] ?? (key.length === 1 ? key.toUpperCase() : key));
|
|
79
|
+
return names.join('+');
|
|
80
|
+
};
|