lostcoords-ui 0.6.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 +296 -27
  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 -56
  10. data/app/assets/tailwind/lostcoords/ui/search.css +96 -0
  11. data/app/assets/tailwind/lostcoords/ui/sheet.css +42 -69
  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 +23 -10
  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 +106 -15
  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 +3 -8
  38. data/app/javascript/lostcoords/ui/controllers/lc_sheet_controller.js +42 -16
  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 +59 -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
@@ -1,77 +1,137 @@
1
1
  module Lostcoords::Ui
2
2
  module PrimitivesHelper
3
- def lc_button(content = nil, variant: :primary, size: :md, icon: nil, icon_position: :start, **attributes, &block)
4
- tag.button(lc_button_content(content, icon, icon_position, &block), **lc_button_attributes(variant, size, { type: "button" }, attributes))
3
+ def lc_button(content = nil, variant: :primary, tone: :default, size: :md, block: false, icon: nil, icon_position: :start, **attributes, &content_block)
4
+ tag.button(lc_button_content(content, icon, icon_position, &content_block), **lc_button_attributes(variant, tone, size, block, { type: "button" }, attributes))
5
5
  end
6
6
 
7
- def lc_button_link_to(content = nil, url = nil, variant: :primary, size: :md, icon: nil, icon_position: :start, **attributes, &block)
8
- attributes = lc_button_attributes(variant, size, {}, attributes)
7
+ def lc_button_link_to(content = nil, url = nil, variant: :primary, tone: :default, size: :md, block: false, icon: nil, icon_position: :start, **attributes, &content_block)
8
+ attributes = lc_button_attributes(variant, tone, size, block, {}, attributes)
9
9
  raise ArgumentError, "Links cannot be disabled; use lc_button for a disabled control" if attributes[:disabled]
10
10
 
11
- if block
11
+ if content_block
12
12
  url ||= content
13
- content = capture(&block)
13
+ content = capture(&content_block)
14
14
  end
15
15
 
16
16
  link_to(lc_button_content(content, icon, icon_position), url, attributes)
17
17
  end
18
18
 
19
- def lc_button_to(content = nil, url = nil, variant: :primary, size: :md, icon: nil, icon_position: :start, **attributes, &block)
20
- if block
19
+ def lc_button_to(content = nil, url = nil, variant: :primary, tone: :default, size: :md, block: false, icon: nil, icon_position: :start, **attributes, &content_block)
20
+ if content_block
21
21
  url ||= content
22
- content = capture(&block)
22
+ content = capture(&content_block)
23
23
  end
24
24
 
25
- button_to(lc_button_content(content, icon, icon_position), url, lc_button_attributes(variant, size, {}, attributes))
25
+ button_to(lc_button_content(content, icon, icon_position), url, lc_button_attributes(variant, tone, size, block, {}, attributes))
26
26
  end
27
27
 
28
- def lc_icon_button(label, icon:, size: :md, shape: :circle, **attributes)
29
- size = lc_option!(:size, size, ButtonHelper::ICON_BUTTON_SIZES.keys)
30
- shape = lc_option!(:shape, shape, ButtonHelper::ICON_BUTTON_SHAPES.keys)
31
- defaults = { type: "button", class: lc_icon_button_class(size: size, shape: shape), aria: { label: label }, data: { slot: "icon-button", size: size } }
32
- attributes = lc_attributes(defaults, attributes)
33
- accessible_label = attributes.fetch(:"aria-label", attributes.dig(:aria, :label))
34
- raise ArgumentError, "lc_icon_button requires a nonblank accessible label" if label.blank? || accessible_label.blank?
28
+ # A button styled as a select for opening a menu or picker. A string label truncates; a block
29
+ # renders its own content, such as an avatar and a label. The chevron is appended either way.
30
+ def lc_select_trigger(label = nil, size: :md, block: false, **attributes, &content)
31
+ size = lc_option!(:size, size, TailwindHelper::CONTROL_SIZES.keys)
32
+ body = content ? capture(&content) : tag.span(label, class: "min-w-0 truncate")
33
+ chevron = lc_icon("chevron-down", variant: :mini, options: { "aria-hidden": true, "data-icon": "inline-end" })
34
+ defaults = { type: "button", class: lc_select_trigger_class(size: size, block: block), data: { slot: "select-trigger", size: size } }
35
+ tag.button(safe_join([ body, chevron ]), **lc_attributes(defaults, attributes))
36
+ end
35
37
 
36
- tag.button(**attributes) do
37
- lc_icon(icon, options: { "aria-hidden": true })
38
- end
38
+ # A block replaces the icon with custom content, such as an avatar or two stacked icons.
39
+ # One pill with a primary action and a toggle on its inline end that opens a menu of the block's
40
+ # lc_menu_option items. With href: the action is a link. label may be rendered HTML. action:,
41
+ # toggle: and menu: add attributes to those parts; other attributes go on the wrapper.
42
+ #
43
+ # <%= lc_split_button "Add expense", id: "add", href: new_expense_path, icon: "plus", menu_label: "More ways to add" do %>
44
+ # <%= lc_menu_option "Import a CSV", icon: "arrow-down-tray" %>
45
+ # <% end %>
46
+ def lc_split_button(label, id:, href: nil, menu_label: lc_t("lostcoords.ui.menu.label"), variant: :primary, tone: :default, size: :md,
47
+ icon: nil, icon_position: :start, block: false, align: :end, action: {}, toggle: {}, menu: {}, **attributes, &items)
48
+ lc_label!(id, :id)
49
+ lc_label!(menu_label, :menu_label)
50
+ variant = lc_option!(:variant, variant, ButtonHelper::BUTTON_VARIANTS.keys)
51
+ size = lc_option!(:size, size, ButtonHelper::BUTTON_SIZES.keys)
52
+ button = { variant: variant, tone: tone, size: size, icon: icon, icon_position: icon_position }
53
+ action = lc_attributes({ class: lc_tw(ButtonHelper::SPLIT_BUTTON_PART, ButtonHelper::SPLIT_BUTTON_ACTION) }, action)
54
+ primary = href ? lc_button_link_to(label, href, **button, **action) : lc_button(label, **button, **action)
55
+
56
+ toggle_class = lc_button_class(variant: variant, tone: tone, size: size, extra: lc_tw(
57
+ ButtonHelper::SPLIT_BUTTON_PART, ButtonHelper::SPLIT_BUTTON_TOGGLE, ButtonHelper::SPLIT_BUTTON_TOGGLE_WIDTHS.fetch(size),
58
+ (ButtonHelper::SPLIT_BUTTON_DIVIDER unless variant == :outline)
59
+ ))
60
+ toggle = tag.button(lc_icon("chevron-down", variant: :mini, options: { "aria-hidden": true }), **lc_attributes(
61
+ { type: "button", class: toggle_class, data: { slot: "split-button-toggle", action: "lc-menu#toggle" } }, toggle,
62
+ { id: "#{id}-trigger", aria: { label: menu_label, haspopup: "menu", expanded: false, controls: id }, data: { lc_menu_target: "trigger" } }
63
+ ))
64
+
65
+ popover = tag.div(capture(&items), **lc_attributes(
66
+ { class: lc_menu_popover_class(align: align), data: { slot: "menu" } }, menu,
67
+ { id: id, role: "menu", aria: { labelledby: "#{id}-trigger" }, data: { lc_menu_target: "popover" } }
68
+ ))
69
+
70
+ defaults = { class: lc_tw(ButtonHelper::SPLIT_BUTTON_BASE, ("flex w-full" if block)), data: { controller: "lc-menu", slot: "split-button" } }
71
+ tag.div(safe_join([ primary, toggle, popover ]), **lc_attributes(defaults, attributes))
72
+ end
73
+
74
+ def lc_icon_button(label, icon: nil, size: :md, shape: :circle, **attributes, &content)
75
+ attributes = lc_icon_button_attributes(label, size, shape, { type: "button" }, attributes)
76
+ tag.button(lc_icon_button_content(icon, &content), **attributes)
77
+ end
78
+
79
+ def lc_icon_button_link_to(label, url, icon: nil, size: :md, shape: :circle, **attributes, &content)
80
+ attributes = lc_icon_button_attributes(label, size, shape, {}, attributes)
81
+ raise ArgumentError, "Links cannot be disabled; use lc_icon_button for a disabled control" if attributes[:disabled]
82
+
83
+ link_to(lc_icon_button_content(icon, &content), url, attributes)
84
+ end
85
+
86
+ def lc_icon_button_to(label, url, icon: nil, size: :md, shape: :circle, **attributes, &content)
87
+ button_to(lc_icon_button_content(icon, &content), url, lc_icon_button_attributes(label, size, shape, {}, attributes))
39
88
  end
40
89
 
41
- def lc_badge(content = nil, tone: :neutral, **attributes, &block)
90
+ def lc_badge(content = nil, tone: :neutral, shape: :rounded, **attributes, &block)
42
91
  tone = lc_option!(:tone, tone, BadgeHelper::BADGE_TONES.keys)
43
- tag.span(content, **lc_attributes({ class: lc_badge_class(tone: tone), data: { slot: "badge", tone: tone } }, attributes), &block)
92
+ tag.span(content, **lc_attributes({ class: lc_badge_class(tone: tone, shape: shape), data: { slot: "badge", tone: tone } }, attributes), &block)
44
93
  end
45
94
 
46
- def lc_icon_badge(icon:, size: :md, shape: :square, tone: :neutral, **attributes)
95
+ # Pass icon: for a library icon, or content or a block for an emoji or a custom glyph.
96
+ def lc_icon_badge(content = nil, icon: nil, size: :md, shape: :square, tone: :neutral, **attributes, &block)
97
+ raise ArgumentError, "Pass icon:, content or a block, not more than one" if [ icon, content, block ].compact.size > 1
98
+
47
99
  tag.span(**lc_attributes({ class: lc_icon_badge_class(size: size, shape: shape, tone: tone), data: { slot: "icon-badge" } }, attributes)) do
48
- lc_icon(icon, options: { class: "size-[1em]", "aria-hidden": true })
100
+ icon ? lc_icon(icon, options: { class: "size-[1em]", "aria-hidden": true }) : block ? capture(&block) : content
49
101
  end
50
102
  end
51
103
 
52
- def lc_avatar(content = nil, src: nil, alt: nil, size: :md, ink: false, linked: false, **attributes, &block)
104
+ # as: :button makes the avatar itself the control, such as a picker's trigger.
105
+ def lc_avatar(content = nil, src: nil, alt: nil, size: :md, ink: false, linked: false, as: :span, **attributes, &block)
53
106
  size = lc_option!(:size, size, BadgeHelper::AVATAR_SIZES.keys)
107
+ as = lc_option!(:as, as, %i[span button])
54
108
  defaults = { class: lc_avatar_class(size: size, ink: ink, linked: linked), data: { slot: "avatar" } }
55
109
  if src
56
110
  tag.img(**lc_attributes(defaults.merge(src: src, alt: alt.to_s, class: lc_tw(defaults[:class], "object-cover")), attributes))
111
+ elsif as == :button
112
+ defaults.merge!(type: "button", class: lc_tw(defaults[:class], "cursor-pointer border-0 p-0"))
113
+ tag.button(content, **lc_attributes(defaults, attributes), &block)
57
114
  else
58
115
  tag.span(content, **lc_attributes(defaults, attributes), &block)
59
116
  end
60
117
  end
61
118
 
62
- def lc_avatar_stack(compact: false, **attributes, &block)
119
+ # row: takes attributes for the inner row that overlaps the avatars, such as a controller target.
120
+ def lc_avatar_stack(compact: false, row: {}, **attributes, &block)
63
121
  tag.span(**lc_attributes({ class: lc_avatar_stack_class(compact: compact), data: { slot: "avatar-stack" } }, attributes)) do
64
- tag.span(class: lc_avatar_stack_row_class, &block)
122
+ tag.span(**lc_attributes({ class: lc_avatar_stack_row_class }, row), &block)
65
123
  end
66
124
  end
67
125
 
126
+ CARD_ELEMENTS = %i[div section article aside li form].freeze
127
+
68
128
  # Yields a card builder with header, title, description, content and footer parts. The card stacks
69
- # its children with the same inset it pads with.
70
- def lc_card(href: nil, interactive: false, padding: :md, **attributes, &block)
71
- defaults = { class: lc_card_class(interactive: interactive || !href.nil?, padding: padding, extra: "flex flex-col gap-inset"), data: { slot: "card" } }
129
+ # its children with the same inset it pads with; an unpadded card's children meet edge to edge.
130
+ def lc_card(href: nil, interactive: false, padding: :md, as: :div, **attributes, &block)
131
+ defaults = { class: lc_card_class(interactive: interactive || !href.nil?, padding: padding, extra: [ "flex flex-col", ("gap-inset" unless padding.to_s == "none") ]), data: { slot: "card" } }
72
132
  content = capture(Parts::Card.new(self), &block) if block
73
133
  if href.nil?
74
- tag.div(content, **lc_attributes(defaults, attributes))
134
+ tag.public_send(lc_option!(:as, as, CARD_ELEMENTS), content, **lc_attributes(defaults, attributes))
75
135
  else
76
136
  defaults.merge!(href: href, class: lc_tw(defaults[:class], "no-underline text-inherit"))
77
137
  tag.a(content, **lc_attributes(defaults, attributes))
@@ -122,9 +182,27 @@ module Lostcoords::Ui
122
182
  safe_join(position == :start ? [ glyph, content ] : [ content, glyph ])
123
183
  end
124
184
 
125
- def lc_button_attributes(variant, size, defaults, attributes)
126
- data = { slot: "button", variant: lc_option!(:variant, variant, ButtonHelper::BUTTON_VARIANTS.keys), size: lc_option!(:size, size, ButtonHelper::BUTTON_SIZES.keys) }
127
- lc_attributes(defaults.merge(class: lc_button_class(variant: variant, size: size), data: data), attributes)
185
+ def lc_button_attributes(variant, tone, size, block, defaults, attributes)
186
+ tone = lc_option!(:tone, tone, ButtonHelper::BUTTON_TONES.keys)
187
+ data = { slot: "button", variant: lc_option!(:variant, variant, ButtonHelper::BUTTON_VARIANTS.keys), tone: (tone unless tone == :default), size: lc_option!(:size, size, ButtonHelper::BUTTON_SIZES.keys) }
188
+ lc_attributes(defaults.merge(class: lc_button_class(variant: variant, tone: tone, size: size, block: block), data: data), attributes)
189
+ end
190
+
191
+ def lc_icon_button_attributes(label, size, shape, defaults, attributes)
192
+ size = lc_option!(:size, size, ButtonHelper::ICON_BUTTON_SIZES.keys)
193
+ shape = lc_option!(:shape, shape, ButtonHelper::ICON_BUTTON_SHAPES.keys)
194
+ defaults = defaults.merge(class: lc_icon_button_class(size: size, shape: shape), aria: { label: label }, data: { slot: "icon-button", size: size })
195
+ attributes = lc_attributes(defaults, attributes)
196
+ accessible_label = attributes.fetch(:"aria-label", attributes.dig(:aria, :label))
197
+ raise ArgumentError, "lc_icon_button requires a nonblank accessible label" if label.blank? || accessible_label.blank?
198
+
199
+ attributes
200
+ end
201
+
202
+ def lc_icon_button_content(icon, &content)
203
+ raise ArgumentError, "Pass icon: or a block" unless icon || content
204
+
205
+ content ? capture(&content) : lc_icon(icon, options: { "aria-hidden": true })
128
206
  end
129
207
  end
130
208
  end
@@ -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,41 +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
- 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)
39
62
  lc_label!(label, :label)
40
- defaults = { class: lc_toggle_group_class, role: "group", aria: { label: label }, data: { slot: "toggle-group", controller: "lc-selection", action: "click->lc-selection#select keydown->lc-selection#navigate", lc_selection_value_value: value } }
41
- tag.div(**lc_attributes(defaults, attributes, { role: "group", aria: { label: label } })) do
42
- 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 } }))
43
78
  end
44
79
  end
45
80
 
46
- 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
+
47
95
  value ||= label
48
96
  lc_label!(value, :value)
49
- defaults = { class: lc_toggle_group_item_class, type: "button", aria: { pressed: selected }, data: { slot: "toggle-group-item", lc_selection_target: "option", value: value } }
50
- required = { type: "button", data: { lc_selection_target: "option", value: value } }
51
- tag.button(block ? capture(&block) : label, **lc_attributes(defaults, attributes, required))
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
+
102
+ required = { type: "button", role: "radio", data: { lc_selection_target: "option", value: value } }
103
+ tag.button(content, **lc_attributes(defaults.merge(aria: { checked: selected || false }), attributes, required))
52
104
  end
53
105
 
54
- def lc_toggle(label = nil, active: false, ghost: false, size: :md, **attributes, &block)
55
- raise ArgumentError, "Use lc_button_link_to for navigation" if attributes.key?(:href) || attributes.key?("href")
56
- 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" } }
57
- 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"))
58
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
59
150
  end
60
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
+ }