lostcoords-ui 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +287 -26
  3. data/app/assets/tailwind/lostcoords/ui/card.css +11 -5
  4. data/app/assets/tailwind/lostcoords/ui/collapsible.css +6 -0
  5. data/app/assets/tailwind/lostcoords/ui/components/dialog.css +2 -2
  6. data/app/assets/tailwind/lostcoords/ui/components/search.css +5 -0
  7. data/app/assets/tailwind/lostcoords/ui/components.css +1 -0
  8. data/app/assets/tailwind/lostcoords/ui/core.css +2 -2
  9. data/app/assets/tailwind/lostcoords/ui/popover.css +6 -4
  10. data/app/assets/tailwind/lostcoords/ui/search.css +96 -0
  11. data/app/assets/tailwind/lostcoords/ui/sheet.css +41 -8
  12. data/app/assets/tailwind/lostcoords/ui/theme.css +18 -0
  13. data/app/assets/tailwind/lostcoords/ui/toast.css +34 -4
  14. data/app/assets/tailwind/lostcoords/ui/typography.css +10 -0
  15. data/app/helpers/lostcoords/ui/badge_helper.rb +20 -12
  16. data/app/helpers/lostcoords/ui/button_helper.rb +63 -1
  17. data/app/helpers/lostcoords/ui/choice_helper.rb +9 -16
  18. data/app/helpers/lostcoords/ui/collapsible_helper.rb +23 -4
  19. data/app/helpers/lostcoords/ui/dialog_helper.rb +46 -11
  20. data/app/helpers/lostcoords/ui/form_helper.rb +20 -8
  21. data/app/helpers/lostcoords/ui/glass_helper.rb +6 -0
  22. data/app/helpers/lostcoords/ui/layout_helper.rb +13 -5
  23. data/app/helpers/lostcoords/ui/list_group_helper.rb +65 -0
  24. data/app/helpers/lostcoords/ui/menu_helper.rb +73 -13
  25. data/app/helpers/lostcoords/ui/navigation_helper.rb +57 -12
  26. data/app/helpers/lostcoords/ui/primitives_helper.rb +112 -34
  27. data/app/helpers/lostcoords/ui/render_attributes_helper.rb +2 -0
  28. data/app/helpers/lostcoords/ui/search_helper.rb +51 -0
  29. data/app/helpers/lostcoords/ui/sheet_helper.rb +20 -8
  30. data/app/helpers/lostcoords/ui/structures_helper.rb +105 -16
  31. data/app/helpers/lostcoords/ui/tailwind_helper.rb +1 -1
  32. data/app/helpers/lostcoords/ui/toast_helper.rb +11 -4
  33. data/app/helpers/lostcoords/ui/typography_helper.rb +5 -3
  34. data/app/javascript/lostcoords/ui/controllers/lc_menu_controller.js +5 -6
  35. data/app/javascript/lostcoords/ui/controllers/lc_nav_controller.js +79 -0
  36. data/app/javascript/lostcoords/ui/controllers/lc_search_controller.js +264 -0
  37. data/app/javascript/lostcoords/ui/controllers/lc_selection_controller.js +1 -6
  38. data/app/javascript/lostcoords/ui/controllers/lc_sheet_controller.js +29 -15
  39. data/app/javascript/lostcoords/ui/helpers/confirm.js +57 -0
  40. data/app/javascript/lostcoords/ui/helpers/floating_panel.js +9 -0
  41. data/app/javascript/lostcoords/ui/index.js +4 -2
  42. data/app/views/lostcoords/ui/components/_lc_collapsible.html.erb +3 -3
  43. data/app/views/lostcoords/ui/components/_lc_confirm_sheet.html.erb +14 -0
  44. data/app/views/lostcoords/ui/components/_lc_dialog.html.erb +24 -16
  45. data/app/views/lostcoords/ui/components/_lc_floating_panel.html.erb +11 -6
  46. data/app/views/lostcoords/ui/components/_lc_search.html.erb +25 -0
  47. data/config/locales/en.yml +11 -0
  48. data/config/locales/es.yml +11 -0
  49. data/config/locales/ja.yml +11 -0
  50. data/lib/lostcoords/ui/builders.rb +1 -1
  51. data/lib/lostcoords/ui/form_builder.rb +27 -12
  52. data/lib/lostcoords/ui/parts.rb +65 -8
  53. data/lib/lostcoords/ui/tokens.rb +3 -1
  54. data/lib/lostcoords/ui/version.rb +1 -1
  55. metadata +10 -2
  56. data/app/javascript/lostcoords/ui/controllers/lc_confirm_dialog_controller.js +0 -39
@@ -36,6 +36,8 @@ module Lostcoords::Ui
36
36
  end
37
37
  normalized[prefix] = values if values
38
38
  end
39
+ # Rails renders popover: true as popover="true", which browsers read as manual: no light dismiss.
40
+ normalized[:popover] = "auto" if normalized[:popover] == true
39
41
  normalized
40
42
  end
41
43
 
@@ -0,0 +1,51 @@
1
+ module Lostcoords::Ui
2
+ module SearchHelper
3
+ RESULT_DETAIL = "line-clamp-2 text-xs text-muted-foreground [&_mark]:bg-transparent [&_mark]:font-semibold [&_mark]:text-foreground"
4
+
5
+ # A search trigger that opens a full-screen search over the page. Attributes go to the trigger,
6
+ # which is what sits in your layout. Results come from one of three places:
7
+ #
8
+ # - src: the server renders them. As the user types, the field requests src with the query in
9
+ # param and shows the HTML it answers, built from lc_search_group and lc_search_result. Without
10
+ # JavaScript the field submits to src as a normal GET.
11
+ # - The block: rows rendered there are filtered in the browser by their text and keywords.
12
+ # - Your own code: handle lc-search:query, call preventDefault and fill event.detail.results.
13
+ #
14
+ # The block also renders what shows before anything is typed. Pass id: when a page has two searches,
15
+ # and shortcut: nil to drop the page shortcut.
16
+ def lc_search(label:, src: nil, param: "q", placeholder: label, trigger: lc_t("lostcoords.ui.search.trigger"), shortcut: "mod+k", id: "lc-search", **attributes, &block)
17
+ lc_label!(label, :label)
18
+ lc_label!(trigger, :trigger)
19
+ lc_label!(param, :param)
20
+ lc_label!(id, :id)
21
+ defaults = { class: "lc-search-trigger", aria: { label: label } }
22
+ data = { lc_search_target: "trigger", action: "lc-search#open", slot: "search-trigger" }
23
+ required = { type: "button", aria: { haspopup: "dialog", controls: id }, data: shortcut ? lc_shortcut_data(shortcut, data) : data }
24
+ render "lostcoords/ui/components/lc_search", id:, label:, src:, param:, placeholder:, trigger:, shortcut:,
25
+ attributes: lc_attributes(defaults, attributes, required), content: (capture(&block) if block)
26
+ end
27
+
28
+ # A titled group of results.
29
+ def lc_search_group(title, **attributes, &block)
30
+ lc_label!(title, :title)
31
+ defaults = { class: "flex flex-col py-related", aria: { label: title }, data: { slot: "search-group" } }
32
+ tag.section(**lc_attributes(defaults, attributes)) do
33
+ safe_join([ tag.h2(title, class: "px-heading pb-related font-mono text-xs font-medium uppercase tracking-widest text-muted-foreground", data: { slot: "search-group-title" }), (capture(&block) if block) ])
34
+ end
35
+ end
36
+
37
+ # One result: a link with an optional icon badge and a line of detail. keywords: adds words the
38
+ # browser filter matches without showing them. A block replaces the title and detail.
39
+ def lc_search_result(title, url, detail: nil, icon: nil, keywords: nil, **attributes, &block)
40
+ lc_label!(url, :url)
41
+ lc_label!(title, :title) unless block
42
+ defaults = { class: "lc-search-result group/result flex min-h-control-lg w-full items-center gap-heading rounded-2xl px-heading py-related text-start no-underline", data: { slot: "search-result", lc_search_target: "result", lc_search_keywords: keywords.presence } }
43
+ tag.a(**lc_attributes(defaults, attributes, { href: url })) do
44
+ body = block ? capture(&block) : tag.span(class: "flex min-w-0 flex-1 flex-col gap-tight") do
45
+ safe_join([ tag.span(title, class: "truncate text-label font-semibold", data: { slot: "search-result-title" }), tag.span(detail, class: RESULT_DETAIL, data: { slot: "search-result-detail" }) ])
46
+ end
47
+ safe_join([ (lc_icon_badge(icon: icon) if icon), body, lc_icon("arrow-turn-down-left", options: { class: "size-4 shrink-0 text-muted-foreground opacity-0 group-hover/result:opacity-100 group-focus-visible/result:opacity-100", "aria-hidden": true }) ].compact)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -1,16 +1,28 @@
1
1
  module Lostcoords::Ui
2
2
  module SheetHelper
3
- # Makes a <dialog> an iOS sheet on phones (lc-sheet controller), keeping the dialog's own
4
- # controllers. detents: "fit", "medium large" or "large".
3
+ SHEET_TAGS = %i[div dialog].freeze
5
4
  SHEET_DETENTS = [ "fit", "medium large", "large" ].freeze
5
+ SHEET_HANDLE = "grid min-h-7 w-full flex-none cursor-grab touch-none place-items-center active:cursor-grabbing sm:hidden"
6
6
 
7
- def lc_sheet_data(data = {}, detents: "fit")
7
+ # A panel that is an iOS sheet on phones, with the lc-sheet controller. tag: :dialog is a modal
8
+ # <dialog> (lc_dialog builds on it). A :div shows while [data-open] or, with popover:, :popover-open;
9
+ # from sm up it keeps your frame, or with anchored: true it is a popover under its trigger, opened by a
10
+ # FloatingPanel inside a positioned ancestor. detents: "fit", "medium large" or "large". handle: true
11
+ # adds a grab strip above the content; without one the sheet draws its grabber over it.
12
+ # A swipe down dispatches lc-sheet:dismiss; unless a listener cancels it, an open popover hides.
13
+ # With tag: :dialog, size: draws lc_dialog's modal frame at that width, for a dialog whose content
14
+ # brings its own header and labels the dialog with aria-label.
15
+ def lc_sheet(tag: :div, anchored: false, detents: "fit", handle: false, size: nil, **attributes, &block)
16
+ tag = lc_option!(:tag, tag, SHEET_TAGS)
8
17
  detents = lc_option!(:detents, detents, SHEET_DETENTS.map(&:to_sym)).to_s
9
- data = data.to_h.symbolize_keys
10
- data.merge(
11
- controller: [ data[:controller], "lc-sheet" ].compact.join(" "),
12
- lc_sheet_detents_value: detents
13
- )
18
+ raise ArgumentError, "anchored: true needs tag: :div" if anchored && tag == :dialog
19
+ raise ArgumentError, "size: needs tag: :dialog" if size && tag != :dialog
20
+
21
+ defaults = { data: { slot: "sheet", controller: "lc-sheet", lc_sheet_detents_value: detents } }
22
+ defaults[:class] = lc_tw("lc-sheet", ("lc-sheet-anchored" if anchored)) unless tag == :dialog
23
+ defaults[:class] = lc_dialog_class(size: size) if size
24
+ strip = self.tag.span(class: SHEET_HANDLE, aria: { hidden: true }, data: { slot: "sheet-handle", lc_sheet_target: "handle" }) if handle
25
+ content_tag(tag, safe_join([ strip, (capture(&block) if block) ].compact), **lc_attributes(defaults, attributes))
14
26
  end
15
27
  end
16
28
  end
@@ -20,43 +20,132 @@ module Lostcoords::Ui
20
20
 
21
21
  # Yields the control's attributes; splat them into the control with `**field`. error: takes a
22
22
  # message or a list of messages, such as `record.errors.full_messages_for(:name)`.
23
- def lc_field(label:, id:, hint: nil, error: nil, **attributes, &block)
23
+ # size: :sm is the compact field for dense forms, with a caption label closer to its control.
24
+ def lc_field(label:, id:, hint: nil, error: nil, size: :md, **attributes, &block)
24
25
  lc_label!(label, :label)
25
26
  lc_label!(id, :id)
26
27
  errors = Array(error).compact_blank
27
28
  descriptions = [ ("#{id}-hint" if hint.present?), ("#{id}-error" if errors.any?) ].compact
28
29
  control = { id: id, aria: { describedby: descriptions.presence&.join(" "), invalid: (true if errors.any?) } }
29
- tag.div(**lc_attributes({ class: lc_field_class, data: { slot: "field", invalid: (true if errors.any?) } }, attributes)) do
30
+ tag.div(**lc_attributes({ class: lc_field_class(size: size), data: { slot: "field", invalid: (true if errors.any?) } }, attributes)) do
30
31
  safe_join([
31
- tag.label(label, for: id, class: lc_field_label_class, data: { slot: "field-label" }),
32
+ tag.label(label, for: id, class: lc_field_label_class(size: size), data: { slot: "field-label" }),
32
33
  (capture(Parts::Field.new(self, id, control), &block) if block),
33
34
  *lc_field_messages(id, hint, errors)
34
35
  ].compact)
35
36
  end
36
37
  end
37
38
 
38
- # One choice from a few options, as a segmented control. Arrows move and select, like native radios,
39
- # so it is a radiogroup of radio buttons to assistive technology.
40
- def lc_toggle_group(label:, name: nil, value: nil, **attributes, &block)
39
+ # Names a group of controls that no single label can: toggles, a picker, several inputs. With an id,
40
+ # the fieldset's aria-describedby points at the hint and error, as lc_field does for its control.
41
+ # size: :sm matches lc_field's compact size. The legend's slot follows the fieldset's.
42
+ def lc_fieldset(legend:, id: nil, hint: nil, error: nil, size: :md, **attributes, &block)
43
+ lc_label!(legend, :legend)
44
+ errors = Array(error).compact_blank
45
+ described = [ ("#{id}-hint" if id && hint.present?), ("#{id}-error" if id && errors.any?) ].compact.presence&.join(" ")
46
+ defaults = { id: id, class: lc_field_class(size: size, extra: "m-0 min-w-0 border-0 p-0"), aria: { invalid: (true if errors.any?), describedby: described }, data: { slot: "fieldset", invalid: (true if errors.any?) } }
47
+ fieldset = lc_attributes(defaults, attributes)
48
+ tag.fieldset(**fieldset) do
49
+ safe_join([
50
+ tag.legend(legend, class: lc_field_label_class(size: size, extra: [ "p-0", ("mb-tight" if size.to_s == "sm") ]), data: { slot: "#{fieldset.dig(:data, :slot)}-legend" }),
51
+ (capture(&block) if block),
52
+ *lc_field_messages(id, hint, errors)
53
+ ].compact)
54
+ end
55
+ end
56
+
57
+ # One choice from a few options, as a segmented control. The group decides what its items render:
58
+ # name: makes native radios inside labels, which bring their own arrows, form value and reset;
59
+ # as: :nav makes links, the current one marked with aria-current; otherwise radio buttons whose
60
+ # selection lives in the lc-selection controller.
61
+ def lc_toggle_group(label:, name: nil, value: nil, as: :div, **attributes, &block)
41
62
  lc_label!(label, :label)
42
- defaults = { class: lc_toggle_group_class, role: "radiogroup", aria: { label: label }, data: { slot: "toggle-group", controller: "lc-selection", action: "click->lc-selection#select keydown->lc-selection#navigate", lc_selection_value_value: value } }
43
- tag.div(**lc_attributes(defaults, attributes, { role: "radiogroup", aria: { label: label } })) do
44
- safe_join([ (hidden_field_tag(name, value, data: { lc_selection_target: "input" }) if name), (capture(&block) if block) ].compact)
63
+ as = lc_option!(:as, as, %i[div nav])
64
+ raise ArgumentError, "a nav toggle group marks its current link; it takes no name: or value:" if as == :nav && (name || value)
65
+
66
+ mode = as == :nav ? :links : (name ? :radios : :buttons)
67
+ content = lc_within_toggle_group({ mode: mode, name: name, value: value&.to_s }) { capture(&block) if block }
68
+
69
+ defaults = { class: lc_toggle_group_class, data: { slot: "toggle-group" } }
70
+ case mode
71
+ when :links
72
+ tag.nav(content, **lc_attributes(defaults, attributes, { aria: { label: label } }))
73
+ when :radios
74
+ tag.div(content, **lc_attributes(defaults, attributes, { role: "radiogroup", aria: { label: label } }))
75
+ else
76
+ defaults[:data].merge!(controller: "lc-selection", action: "click->lc-selection#select keydown->lc-selection#navigate", lc_selection_value_value: value)
77
+ tag.div(content, **lc_attributes(defaults, attributes, { role: "radiogroup", aria: { label: label } }))
45
78
  end
46
79
  end
47
80
 
48
- def lc_toggle_group_item(label = nil, value: nil, selected: false, **attributes, &block)
81
+ # Takes href: and current: in a nav group, value: and checked: in a named group, and value: and
82
+ # selected: in a button group; arguments from another kind of group raise.
83
+ def lc_toggle_group_item(label = nil, value: nil, selected: nil, checked: nil, href: nil, current: nil, input: {}, **attributes, &block)
84
+ group = @_lc_toggle_group || { mode: :buttons }
85
+ given = { value: value, selected: selected, checked: checked, href: href, current: current }.compact.keys
86
+ allowed = { links: %i[href current], radios: %i[value checked], buttons: %i[value selected] }.fetch(group[:mode])
87
+ stray = given - allowed
88
+ raise ArgumentError, "#{stray.join(', ')} can't be used on an item of a #{group[:mode]} toggle group" if stray.any?
89
+ raise ArgumentError, "an item of a nav toggle group needs href:" if group[:mode] == :links && !href
90
+
91
+ content = block ? capture(&block) : label
92
+ defaults = { class: lc_toggle_group_item_class, data: { slot: "toggle-group-item" } }
93
+ return lc_toggle_link(content, href, current, defaults, attributes) if group[:mode] == :links
94
+
49
95
  value ||= label
50
96
  lc_label!(value, :value)
51
- defaults = { class: lc_toggle_group_item_class, aria: { checked: selected }, data: { slot: "toggle-group-item" } }
97
+ if group[:mode] == :radios
98
+ checked = group[:value] == value.to_s if checked.nil?
99
+ return lc_toggle_radio(content, group[:name], value, checked, input, defaults, attributes)
100
+ end
101
+
52
102
  required = { type: "button", role: "radio", data: { lc_selection_target: "option", value: value } }
53
- tag.button(block ? capture(&block) : label, **lc_attributes(defaults, attributes, required))
103
+ tag.button(content, **lc_attributes(defaults.merge(aria: { checked: selected || false }), attributes, required))
54
104
  end
55
105
 
56
- def lc_toggle(label = nil, active: false, ghost: false, size: :md, **attributes, &block)
57
- raise ArgumentError, "Use lc_button_link_to for navigation" if attributes.key?(:href) || attributes.key?("href")
58
- defaults = { class: lc_toggle_class(ghost: ghost, size: size), type: "button", aria: { pressed: active }, data: { slot: "toggle", controller: "lc-toggle", action: "click->lc-toggle#toggle" } }
59
- tag.button(block ? capture(&block) : label, **lc_attributes(defaults, attributes).merge(type: "button"))
106
+ # A button that flips its own aria-pressed. href: renders a link marked current when active, and
107
+ # name: a label around a radio checked when active. A caller that sets aria-pressed or
108
+ # aria-expanded owns the state, so the toggle drops its own controller.
109
+ def lc_toggle(label = nil, active: false, ghost: false, size: :md, href: nil, name: nil, value: nil, input: {}, **attributes, &block)
110
+ content = block ? capture(&block) : label
111
+ defaults = { class: lc_toggle_class(ghost: ghost, size: size), data: { slot: "toggle" } }
112
+ return lc_toggle_link(content, href, active, defaults, attributes) if href
113
+
114
+ if name
115
+ value ||= label
116
+ lc_label!(value, :value)
117
+ return lc_toggle_radio(content, name, value, active, input, defaults, attributes)
118
+ end
119
+
120
+ if lc_normalize_attributes(attributes).fetch(:aria, {}).slice(:pressed, :expanded).any?
121
+ raise ArgumentError, "pass active: or aria: { pressed: }, not both" if active
122
+ else
123
+ defaults[:aria] = { pressed: active }
124
+ defaults[:data].merge!(controller: "lc-toggle", action: "click->lc-toggle#toggle")
125
+ end
126
+ tag.button(content, **lc_attributes(defaults, attributes).merge(type: "button"))
60
127
  end
128
+
129
+ private
130
+ # Items read the enclosing group's mode, so a nested group restores the outer one.
131
+ def lc_within_toggle_group(group)
132
+ previous, @_lc_toggle_group = @_lc_toggle_group, group
133
+ yield
134
+ ensure
135
+ @_lc_toggle_group = previous
136
+ end
137
+
138
+ def lc_toggle_link(content, href, current, defaults, attributes)
139
+ raise ArgumentError, "a link can't be disabled; render it as a button instead" if attributes.key?(:disabled) || attributes.key?("disabled")
140
+ link_to(content, href, **lc_attributes(defaults.merge(aria: { current: ("true" if current) }), attributes))
141
+ end
142
+
143
+ def lc_toggle_radio(content, name, value, checked, input, defaults, attributes)
144
+ attributes = attributes.dup
145
+ input = input.merge(disabled: attributes.delete(:disabled)) if attributes.key?(:disabled)
146
+ tag.label(**lc_attributes(defaults, attributes)) do
147
+ safe_join([ radio_button_tag(name, value, checked, **lc_attributes({ class: "sr-only" }, input)), content ])
148
+ end
149
+ end
61
150
  end
62
151
  end
@@ -9,7 +9,7 @@ module Lostcoords::Ui
9
9
 
10
10
  # The theme's own scales, so class merging knows text-label is a size and gap-related a gap.
11
11
  MERGE_THEME = {
12
- "text" => %w[display title heading subheading body small label caption],
12
+ "text" => %w[display title heading subheading body small label caption eyebrow],
13
13
  "spacing" => %w[tight related heading content inset section gutter control-sm control-md control-lg],
14
14
  "container" => %w[page reading form],
15
15
  "radius" => %w[pill]
@@ -12,10 +12,17 @@ module Lostcoords::Ui
12
12
  end
13
13
  end
14
14
 
15
- # Fixed corner that holds toasts. Render it once in the layout and append toasts to it, from flash
16
- # or a Turbo Stream; it shows in the top layer, above dialogs and sticky chrome.
17
- def lc_toast_region(**attributes, &block)
18
- defaults = { class: "lc-toast-region", role: "status", aria: { live: "polite" }, data: { slot: "toast-region", controller: "lc-toast-region" } }
15
+ TOAST_PLACEMENTS = %i[bottom_end bottom_center top_end top_center].freeze
16
+
17
+ # Fixed edge that holds toasts. Render it once in the layout and append toasts to it, from flash
18
+ # or a Turbo Stream; it shows in the top layer, above dialogs and sticky chrome. mobile_placement:
19
+ # applies below the sm breakpoint. --lc-toast-offset sets the distance from the edges, and
20
+ # --lc-toast-offset-top or --lc-toast-offset-bottom one edge, such as below a sticky header.
21
+ def lc_toast_region(placement: :bottom_end, mobile_placement: nil, **attributes, &block)
22
+ placement = lc_option!(:placement, placement, TOAST_PLACEMENTS)
23
+ mobile_placement = lc_option!(:mobile_placement, mobile_placement, TOAST_PLACEMENTS) if mobile_placement
24
+ data = { slot: "toast-region", controller: "lc-toast-region", placement: placement.to_s.dasherize, mobile_placement: mobile_placement&.to_s&.dasherize }.compact
25
+ defaults = { class: "lc-toast-region", role: "status", aria: { live: "polite" }, data: data }
19
26
  tag.div(**lc_attributes(defaults, attributes)) { capture(&block) if block }
20
27
  end
21
28
  end
@@ -1,8 +1,9 @@
1
1
  module Lostcoords::Ui
2
2
  module TypographyHelper
3
3
  HEADING_SIZES = { display: "text-display", title: "text-title", heading: "text-heading", subheading: "text-subheading" }.freeze
4
- TEXT_SIZES = { body: "text-body", small: "text-small", label: "text-label", caption: "text-caption" }.freeze
4
+ TEXT_SIZES = { body: "text-body", small: "text-small", label: "text-label font-semibold", caption: "text-caption", eyebrow: "text-eyebrow lc-eyebrow" }.freeze
5
5
  TEXT_TONES = { default: "text-foreground", muted: "text-muted-foreground" }.freeze
6
+ TEXT_ELEMENTS = %i[p span div label legend figcaption dt dd h1 h2 h3 h4 h5 h6].freeze
6
7
  KBD_BASE = "lc-kbd inline-flex items-center rounded-md bg-tile px-1.5 font-sans text-caption leading-5 whitespace-nowrap text-muted-foreground"
7
8
  KBD_NAMES = { "mod" => "Ctrl", "ctrl" => "Ctrl", "alt" => "Alt", "shift" => "Shift", "meta" => "Meta", "escape" => "Esc", "enter" => "↵",
8
9
  "arrowup" => "↑", "arrowdown" => "↓", "arrowleft" => "←", "arrowright" => "→" }.freeze
@@ -29,8 +30,9 @@ module Lostcoords::Ui
29
30
  tag.public_send("h#{level}", content, **lc_attributes({ class: lc_heading_class(size: size, trim: trim) }, attributes), &block)
30
31
  end
31
32
 
32
- def lc_text(content = nil, size: :body, tone: :default, trim: false, **attributes, &block)
33
- tag.p(content, **lc_attributes({ class: lc_text_class(size: size, tone: tone, trim: trim) }, attributes), &block)
33
+ # as: picks the element, such as :span inside a row or :h3 for a heading set in a text role.
34
+ def lc_text(content = nil, size: :body, tone: :default, trim: false, as: :p, **attributes, &block)
35
+ tag.public_send(lc_option!(:as, as, TEXT_ELEMENTS), content, **lc_attributes({ class: lc_text_class(size: size, tone: tone, trim: trim) }, attributes), &block)
34
36
  end
35
37
 
36
38
  # A key hint, such as lc_kbd("mod+k"). "mod" renders Ctrl and reads ⌘ on Apple platforms once JS runs.
@@ -49,12 +49,11 @@ export default class extends FloatingPanel {
49
49
  if (event.detail === 0) focusFirstMenuItem(this.popoverTarget)
50
50
  }
51
51
 
52
- toggleFromRow(event) {
53
- if (this.popoverTarget.contains(event.target)) return
54
- if (event.target.closest("a, button, input, select, textarea, [data-lc-menu-ignore]")) return
55
-
56
- event.preventDefault()
57
- this.isOpen ? this.close() : this.open()
52
+ check(event) {
53
+ const item = event.currentTarget
54
+ const checked = item.getAttribute("aria-checked") !== "true"
55
+ item.setAttribute("aria-checked", String(checked))
56
+ this.dispatch("check", { target: item, detail: { checked } })
58
57
  }
59
58
 
60
59
  open() {
@@ -0,0 +1,79 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ // Each visit renders a new nav, so the indicator's last spot is kept per nav
4
+ // label and the new one slides from there to its current link.
5
+ const spots = new Map()
6
+
7
+ export default class extends Controller {
8
+ static targets = ["indicator", "link"]
9
+
10
+ connect() {
11
+ this.key = this.element.getAttribute("aria-label")
12
+ this.initialLink = this.currentLink
13
+ this.restore = () => this.select({ currentTarget: this.initialLink }, { animate: false })
14
+ document.addEventListener("turbo:before-cache", this.restore)
15
+
16
+ // The observer reports once on observe, after the first placement below.
17
+ let observed = false
18
+ this.observer = new ResizeObserver(() => {
19
+ if (observed) this.place({ animate: false })
20
+ observed = true
21
+ })
22
+ this.observer.observe(this.element)
23
+
24
+ const previous = spots.get(this.key)
25
+ if (previous) this.move(previous, { animate: false })
26
+ this.frame = requestAnimationFrame(() => this.place({ animate: Boolean(previous) }))
27
+ }
28
+
29
+ disconnect() {
30
+ cancelAnimationFrame(this.frame)
31
+ this.observer.disconnect()
32
+ document.removeEventListener("turbo:before-cache", this.restore)
33
+ }
34
+
35
+ select(event, { animate = true } = {}) {
36
+ if (event.button > 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
37
+
38
+ for (const link of this.linkTargets) {
39
+ if (link === event.currentTarget) link.setAttribute("aria-current", "page")
40
+ else link.removeAttribute("aria-current")
41
+ }
42
+ this.place({ animate })
43
+ }
44
+
45
+ get currentLink() {
46
+ return this.linkTargets.find((link) => link.getAttribute("aria-current") === "page")
47
+ }
48
+
49
+ place({ animate }) {
50
+ const link = this.currentLink
51
+ if (!link?.offsetParent) {
52
+ this.element.removeAttribute("data-indicator")
53
+ return
54
+ }
55
+
56
+ const rtl = getComputedStyle(this.element).direction === "rtl"
57
+ const spot = {
58
+ x: rtl ? link.offsetLeft + link.offsetWidth - this.element.clientWidth : link.offsetLeft,
59
+ y: link.offsetTop,
60
+ width: link.offsetWidth,
61
+ height: link.offsetHeight,
62
+ }
63
+ spots.set(this.key, spot)
64
+ this.move(spot, { animate })
65
+ }
66
+
67
+ move({ x, y, width, height }, { animate }) {
68
+ const style = this.indicatorTarget.style
69
+ style.transition = animate ? "" : "none"
70
+ style.translate = `${x}px ${y}px`
71
+ style.width = `${width}px`
72
+ style.height = `${height}px`
73
+ this.element.toggleAttribute("data-indicator", true)
74
+ if (!animate) {
75
+ this.indicatorTarget.offsetWidth
76
+ style.transition = ""
77
+ }
78
+ }
79
+ }