lostcoords-ui 0.10.0 → 0.12.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 (33) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +155 -23
  3. data/app/assets/tailwind/lostcoords/ui/base.css +1 -1
  4. data/app/assets/tailwind/lostcoords/ui/components/hovercard.css +4 -0
  5. data/app/assets/tailwind/lostcoords/ui/components.css +1 -0
  6. data/app/assets/tailwind/lostcoords/ui/core.css +2 -2
  7. data/app/assets/tailwind/lostcoords/ui/emoji-picker.css +1 -1
  8. data/app/assets/tailwind/lostcoords/ui/hovercard.css +47 -0
  9. data/app/helpers/lostcoords/ui/avatar_badge_helper.rb +43 -0
  10. data/app/helpers/lostcoords/ui/badge_helper.rb +20 -2
  11. data/app/helpers/lostcoords/ui/button_helper.rb +16 -4
  12. data/app/helpers/lostcoords/ui/card_helper.rb +3 -1
  13. data/app/helpers/lostcoords/ui/choice_helper.rb +1 -2
  14. data/app/helpers/lostcoords/ui/emoji_picker_helper.rb +5 -1
  15. data/app/helpers/lostcoords/ui/file_drop_helper.rb +32 -0
  16. data/app/helpers/lostcoords/ui/form_helper.rb +18 -4
  17. data/app/helpers/lostcoords/ui/hovercard_helper.rb +31 -0
  18. data/app/helpers/lostcoords/ui/list_group_helper.rb +38 -3
  19. data/app/helpers/lostcoords/ui/menu_helper.rb +23 -14
  20. data/app/helpers/lostcoords/ui/primitives_helper.rb +20 -12
  21. data/app/helpers/lostcoords/ui/render_attributes_helper.rb +1 -1
  22. data/app/helpers/lostcoords/ui/sheet_helper.rb +9 -3
  23. data/app/helpers/lostcoords/ui/structures_helper.rb +37 -17
  24. data/app/helpers/lostcoords/ui/typography_helper.rb +4 -2
  25. data/app/javascript/lostcoords/ui/controllers/lc_hovercard_controller.js +104 -0
  26. data/app/javascript/lostcoords/ui/helpers/dialog.js +27 -4
  27. data/app/javascript/lostcoords/ui/helpers/dismiss_stack.js +1 -1
  28. data/app/javascript/lostcoords/ui/index.js +2 -0
  29. data/app/views/lostcoords/ui/components/_lc_dialog.html.erb +2 -1
  30. data/lib/lostcoords/ui/form_builder.rb +7 -0
  31. data/lib/lostcoords/ui/parts.rb +15 -0
  32. data/lib/lostcoords/ui/version.rb +1 -1
  33. metadata +7 -1
@@ -8,9 +8,13 @@ module Lostcoords::Ui
8
8
 
9
9
  # popover_class paints the desktop popover: the gem leaves it unpainted so the host can match
10
10
  # its floating surfaces. picker_class and trigger_class style the wrapper and its button.
11
- def lc_emoji_picker(form:, field:, label:, catalog_url:, value: nil, quick: [], recent: [], recent_scope: nil, auto_submit: false, picker_class: nil, trigger_class: nil, popover_class: nil, **attributes)
11
+ # popover_size: :lg widens the desktop popover for a roomier grid.
12
+ POPOVER_SIZES = { md: nil, lg: "[--lc-emoji-picker-popover-width:28rem]" }.freeze
13
+
14
+ def lc_emoji_picker(form:, field:, label:, catalog_url:, value: nil, quick: [], recent: [], recent_scope: nil, auto_submit: false, popover_size: :md, picker_class: nil, trigger_class: nil, popover_class: nil, **attributes)
12
15
  lc_label!(label, :label)
13
16
  lc_label!(catalog_url, :catalog_url)
17
+ picker_class = lc_tw(POPOVER_SIZES.fetch(lc_option!(:popover_size, popover_size, POPOVER_SIZES.keys)), picker_class)
14
18
  render "lostcoords/ui/components/emoji_picker", form: form, field: field, label: label, catalog_url: catalog_url, value: value, quick: quick, recent: recent, recent_scope: recent_scope, auto_submit: auto_submit,
15
19
  picker_class: picker_class, trigger_class: trigger_class, popover_class: popover_class, attributes: attributes
16
20
  end
@@ -0,0 +1,32 @@
1
+ module Lostcoords::Ui
2
+ module FileDropHelper
3
+ FILE_DROP =
4
+ "grid min-h-45 cursor-pointer place-items-center content-center gap-2 rounded-2xl border-2 border-dashed border-border bg-muted p-4 text-center " \
5
+ "transition-[background-color,border-color,box-shadow] duration-150 ease-out hover:border-primary hover:bg-accent hover:shadow-(--lc-shadow-sm) sm:p-6"
6
+
7
+ def lc_file_drop_class(extra: nil)
8
+ lc_tw(FILE_DROP, extra)
9
+ end
10
+
11
+ # A dashed drop zone: a label around a visually hidden file input, so a click or a dropped file reaches the
12
+ # input. name: builds the input with accept:, multiple: and input: attributes, such as a controller target;
13
+ # a block renders your own input instead (form.file_drop passes the form's), and must give it class "sr-only".
14
+ # button: adds a decorative button inside the label, which the label already names.
15
+ def lc_file_drop(name: nil, title:, hint: nil, button: nil, icon: "arrow-up-tray", accept: nil, multiple: false, input: {}, **attributes, &block)
16
+ lc_label!(title, :title)
17
+ raise ArgumentError, "Pass name: or a block that renders the file input, not both" if name && block
18
+ raise ArgumentError, "lc_file_drop needs name: or a block that renders the file input" unless name || block
19
+
20
+ field = block ? capture(&block) : file_field_tag(name, **lc_attributes({ class: "sr-only", accept: accept, multiple: multiple }, input))
21
+ tag.label(**lc_attributes({ class: lc_file_drop_class, data: { slot: "file-drop" } }, attributes)) do
22
+ safe_join([
23
+ lc_icon_badge(icon: icon, size: :lg, shape: :circle, tone: :primary, class: "mb-2", aria: { hidden: true }),
24
+ tag.span(title, class: lc_heading_class(size: :subheading), data: { slot: "file-drop-title" }),
25
+ (lc_text(hint, as: :span, size: :small, tone: :muted, data: { slot: "file-drop-hint" }) if hint.present?),
26
+ (lc_button(button, as: :span, size: :sm, class: "mt-2 pointer-events-none", aria: { hidden: true }) if button.present?),
27
+ field
28
+ ].compact)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -20,6 +20,18 @@ module Lostcoords::Ui
20
20
 
21
21
  TOGGLE_GROUP_ITEM_ACTIVE = "lc-toggle-group-item-active bg-background text-foreground hover:text-foreground"
22
22
 
23
+ # :stacked puts each item's icon over its label; :adaptive makes the group full width and stacks
24
+ # its items only while the group itself is narrow.
25
+ TOGGLE_GROUP_LAYOUTS = { inline: nil, stacked: nil, adaptive: "@container/lc-toggle flex w-full" }.freeze
26
+ TOGGLE_GROUP_ITEM_LAYOUTS = {
27
+ inline: nil,
28
+ stacked: "flex-col h-auto min-h-control-sm gap-0.5 px-1.5 py-1.5 text-caption whitespace-normal text-center text-balance [&_svg]:size-5",
29
+ adaptive: "@max-sm/lc-toggle:flex-col @max-sm/lc-toggle:h-auto @max-sm/lc-toggle:gap-0.5 @max-sm/lc-toggle:px-1.5 " \
30
+ "@max-sm/lc-toggle:py-1.5 @max-sm/lc-toggle:text-caption @max-sm/lc-toggle:[&_svg]:size-5"
31
+ }.freeze
32
+ # A track wider than its container scrolls sideways, its edges fading while there is more to see.
33
+ TOGGLE_GROUP_SCROLL = "lc-scroll-fade-x max-w-full overflow-x-auto [scrollbar-width:none] *:shrink-0"
34
+
23
35
  INPUT_BASE =
24
36
  "w-full max-w-full min-w-0 " \
25
37
  "px-3 " \
@@ -55,12 +67,14 @@ module Lostcoords::Ui
55
67
  form_with(**options, builder: Lostcoords::Ui::FormBuilder, &block)
56
68
  end
57
69
 
58
- def lc_toggle_group_class(extra: nil)
59
- lc_tw(TOGGLE_GROUP_BASE, extra)
70
+ def lc_toggle_group_class(layout: :inline, scroll: false, extra: nil)
71
+ layout = lc_option!(:layout, layout, TOGGLE_GROUP_LAYOUTS.keys)
72
+ lc_tw(TOGGLE_GROUP_BASE, TOGGLE_GROUP_LAYOUTS.fetch(layout), (TOGGLE_GROUP_SCROLL if scroll), extra)
60
73
  end
61
74
 
62
- def lc_toggle_group_item_class(active: false, extra: nil)
63
- lc_tw(TOGGLE_GROUP_ITEM_BASE, (TOGGLE_GROUP_ITEM_ACTIVE if active), extra)
75
+ def lc_toggle_group_item_class(active: false, layout: :inline, extra: nil)
76
+ layout = lc_option!(:layout, layout, TOGGLE_GROUP_ITEM_LAYOUTS.keys)
77
+ lc_tw(TOGGLE_GROUP_ITEM_BASE, TOGGLE_GROUP_ITEM_LAYOUTS.fetch(layout), (TOGGLE_GROUP_ITEM_ACTIVE if active), extra)
64
78
  end
65
79
 
66
80
  def lc_input_class(size: :md, plain: false, extra: nil)
@@ -0,0 +1,31 @@
1
+ module Lostcoords::Ui
2
+ module HovercardHelper
3
+ HOVERCARD_SIDES = %i[bottom top].freeze
4
+
5
+ # A preview card for what a link points to, such as a profile over a name. It opens after a short
6
+ # hover or on keyboard focus and stays open while the pointer is on it.
7
+ #
8
+ # Yields attributes for the trigger; splat them with `**card` on the link. Pass the card as content:,
9
+ # or src: to load it as HTML from your endpoint the first time it opens, so a long list of triggers
10
+ # costs nothing until one is hovered. The card is a preview: it is hidden from assistive technology
11
+ # and its links are out of the tab order, so everything in it must also be reachable from the page
12
+ # the trigger opens. Inside a paragraph, keep content: to phrasing elements such as spans; HTML that
13
+ # src: loads can hold anything.
14
+ def lc_hovercard(src: nil, content: nil, side: :bottom, **attributes, &block)
15
+ raise ArgumentError, "Pass src: or content:, not both" if src && content
16
+ raise ArgumentError, "Pass src: or content:" unless src || content
17
+ raise ArgumentError, "lc_hovercard needs a block that renders its trigger" unless block
18
+ lc_label!(src, :src) if src
19
+ side = lc_option!(:side, side, HOVERCARD_SIDES)
20
+ trigger = capture({ data: { lc_hovercard_target: "trigger" } }, &block)
21
+ body = content || tag.span(class: "flex flex-col gap-related", data: { slot: "hovercard-loading" }) do
22
+ safe_join([ lc_skeleton(class: "h-10 w-10 rounded-full"), lc_skeleton, lc_skeleton(class: "h-4 w-2/3") ])
23
+ end
24
+ data = { slot: "hovercard", side: side, controller: "lc-hovercard", lc_hovercard_src_value: src,
25
+ action: "mouseenter->lc-hovercard#enter focusin->lc-hovercard#enter mouseleave->lc-hovercard#leave focusout->lc-hovercard#leave" }.compact
26
+ tag.span(**lc_attributes({ class: "lc-hovercard relative inline-flex min-w-0", data: data }, attributes)) do
27
+ safe_join([ trigger, tag.span(body, class: lc_glass_class(shape: :menu, extra: "lc-hovercard__content lc-squircle"), aria: { hidden: true }, data: { slot: "hovercard-content", lc_hovercard_target: "content", loaded: (true unless src) }) ])
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,6 +1,11 @@
1
1
  module Lostcoords::Ui
2
2
  module ListGroupHelper
3
- LIST_ROW = "lc-list-row min-w-0 bg-card text-card-foreground first:rounded-t-3xl last:rounded-b-3xl lc-squircle"
3
+ LIST_ROW = "lc-list-row min-w-0 bg-card text-card-foreground lc-squircle"
4
+ # The group rounds its first and last shown children, whatever they are: a row, a field, or a
5
+ # collapsible of rows. A hidden row leaves the rounding to its next shown sibling.
6
+ LIST_GROUP =
7
+ "m-0 grid min-w-0 list-none gap-0.5 p-0 " \
8
+ "[&>:not([hidden]):not(:not([hidden])~*)]:rounded-t-3xl [&>:not([hidden]):not(:has(~:not([hidden])))]:rounded-b-3xl"
4
9
  LIST_ITEM = "flex min-h-16 min-w-0 items-center gap-heading px-content py-heading text-start sm:px-inset"
5
10
  LIST_ROW_PADDING = "px-content py-heading sm:px-inset"
6
11
  LIST_ICON = "grid size-10 shrink-0 place-items-center rounded-full bg-tile [&>svg:not([class*='size-'])]:size-4.5"
@@ -14,6 +19,12 @@ module Lostcoords::Ui
14
19
  "[&_:is(a[href],button,input,select,textarea,summary,[tabindex]):not([data-slot=list-link])]:relative " \
15
20
  "[&_:is(a[href],button,input,select,textarea,summary,[tabindex]):not([data-slot=list-link])]:z-raised"
16
21
  LIST_LINK = "text-inherit no-underline focus-visible:outline-none after:absolute after:inset-0 after:rounded-[inherit] after:content-['']"
22
+ # A label row around a hidden radio: the checked row fills, bolds and shows a trailing check, as
23
+ # a menuitemradio does. The global focus halo rings the row while its radio has focus.
24
+ LIST_ROW_CHOICE =
25
+ "group/choice flex items-center gap-heading cursor-pointer transition-colors duration-(--lc-motion-quick) " \
26
+ "hover:bg-accent has-checked:bg-primary-soft has-checked:font-semibold has-disabled:cursor-not-allowed has-disabled:opacity-45"
27
+ LIST_ROW_CHOICE_CHECK = "ms-auto size-4 shrink-0 text-primary invisible group-has-checked/choice:visible"
17
28
  LIST_FIELD_TAGS = %i[div fieldset span].freeze
18
29
  LIST_FIELD = "@container px-content py-2.5 transition-colors duration-(--lc-motion-quick) has-[[data-plain]:focus]:bg-accent"
19
30
  LIST_FIELD_GRID = "grid min-h-9 grid-cols-[1.25rem_minmax(0,1fr)] items-center gap-x-heading gap-y-related @md:grid-cols-[8rem_minmax(0,1fr)]"
@@ -41,7 +52,7 @@ module Lostcoords::Ui
41
52
  # first and last rows round.
42
53
  def lc_list_group(tag: :div, **attributes, &block)
43
54
  tag = lc_option!(:tag, tag, %i[div ul])
44
- content_tag(tag, (capture(&block) if block), **lc_attributes({ class: "m-0 grid min-w-0 list-none gap-0.5 p-0", data: { slot: "list-group" } }, attributes))
55
+ content_tag(tag, (capture(&block) if block), **lc_attributes({ class: LIST_GROUP, data: { slot: "list-group" } }, attributes))
45
56
  end
46
57
 
47
58
  # With a title, the row lays out icon:, the title and subtitle, then the block as its trailing
@@ -49,7 +60,13 @@ module Lostcoords::Ui
49
60
  # content that pads itself, such as an lc_list_item button. href: makes the row a link. On an li
50
61
  # or div row, the title is the link and covers the row, so the block can hold buttons and links
51
62
  # of its own; a click elsewhere in the row follows the title.
52
- def lc_list_row(title = nil, subtitle: nil, icon: nil, href: nil, tag: nil, padded: true, **attributes, &block)
63
+ # choice: :radio makes the row one choice of a radio group: a label around a hidden radio named
64
+ # name: with value:, then the title or block, then a check shown while checked:. input: adds
65
+ # attributes to the radio.
66
+ def lc_list_row(title = nil, subtitle: nil, icon: nil, href: nil, tag: nil, padded: true, choice: nil, name: nil, value: nil, checked: false, input: {}, **attributes, &block)
67
+ return lc_list_choice_row(title, choice, name, value, checked, input, attributes, &block) if choice
68
+ raise ArgumentError, "name:, value:, checked: and input: belong to a choice: row" if name || value || checked || input.any?
69
+
53
70
  tag = lc_option!(:tag, tag || (href ? :a : :div), LIST_ROW_TAGS)
54
71
  titled = href && tag != :a
55
72
  raise ArgumentError, "href: needs tag: :a, :li or :div" if titled && !%i[li div].include?(tag)
@@ -63,6 +80,24 @@ module Lostcoords::Ui
63
80
  content_tag(tag, (capture(&block) if block), **lc_attributes(defaults.merge(class: classes), attributes))
64
81
  end
65
82
 
83
+ private def lc_list_choice_row(title, choice, name, value, checked, input, attributes, &block)
84
+ lc_option!(:choice, choice, %i[radio])
85
+ lc_label!(name, :name)
86
+ raise ArgumentError, "a choice row needs value:" if value.nil?
87
+ raise ArgumentError, "a choice row takes a title or a block, not both" if title.present? && block
88
+
89
+ attributes = attributes.dup
90
+ input = input.merge(disabled: attributes.delete(:disabled)) if attributes.key?(:disabled)
91
+ content = block ? capture(&block) : content_tag(:span, title, class: "min-w-0", data: { slot: "list-title" })
92
+ content_tag(:label, **lc_attributes({ class: lc_tw(LIST_ROW, LIST_ROW_PADDING, LIST_ROW_CHOICE), data: { slot: "list-row", choice: "radio" } }, attributes)) do
93
+ safe_join([
94
+ radio_button_tag(name, value, checked, **lc_attributes({ class: "sr-only" }, input)),
95
+ content,
96
+ lc_icon("check", variant: :mini, options: { class: LIST_ROW_CHOICE_CHECK, "aria-hidden": true })
97
+ ])
98
+ end
99
+ end
100
+
66
101
  # A form row: a label column, then the block as the control. In a narrow row the label folds to
67
102
  # icon: alone and stays the accessible name. id: makes the label a <label> for that control;
68
103
  # tag: :fieldset names a group of controls with a legend. surface: false drops the row's panel,
@@ -15,6 +15,12 @@ module Lostcoords::Ui
15
15
  center: "start-1/2 end-auto -translate-x-1/2"
16
16
  }.freeze
17
17
 
18
+ # Below the trigger by default; :top opens upward, for a menu near the bottom of the screen.
19
+ MENU_POPOVER_SIDES = {
20
+ bottom: "",
21
+ top: "top-auto bottom-[calc(100%+--spacing(1))] [--lc-popover-from:translateY(--spacing(1))_scale(0.98)]"
22
+ }.freeze
23
+
18
24
  MENU_OPTION_BASE =
19
25
  "lc-menu__item lc-press [--lc-press-scale:0.98] group/option grid col-span-full grid-cols-[auto_minmax(0,1fr)_auto] items-center gap-x-3 w-full " \
20
26
  "min-h-control-md px-3 py-1.5 rounded-2xl lc-squircle border-none bg-transparent text-start font-sans text-label text-foreground " \
@@ -38,12 +44,14 @@ module Lostcoords::Ui
38
44
  MENU_GROUP_BASE = "col-span-full grid grid-cols-subgrid gap-y-px"
39
45
  MENU_GROUP_LABEL = "col-span-full px-3 pt-2 pb-1"
40
46
 
41
- def lc_popover_class(align: :end, extra: nil)
42
- lc_glass_class(shape: :menu, extra: lc_tw(POPOVER_BASE, MENU_POPOVER_ALIGN.fetch(lc_option!(:align, align, MENU_POPOVER_ALIGN.keys)), extra))
47
+ def lc_popover_class(align: :end, side: :bottom, extra: nil)
48
+ align = MENU_POPOVER_ALIGN.fetch(lc_option!(:align, align, MENU_POPOVER_ALIGN.keys))
49
+ side = MENU_POPOVER_SIDES.fetch(lc_option!(:side, side, MENU_POPOVER_SIDES.keys))
50
+ lc_glass_class(shape: :menu, extra: lc_tw(POPOVER_BASE, align, side, extra))
43
51
  end
44
52
 
45
- def lc_menu_popover_class(align: :end, extra: nil)
46
- lc_popover_class(align: align, extra: lc_tw(MENU_POPOVER_BASE, extra))
53
+ def lc_menu_popover_class(align: :end, side: :bottom, extra: nil)
54
+ lc_popover_class(align: align, side: side, extra: lc_tw(MENU_POPOVER_BASE, extra))
47
55
  end
48
56
 
49
57
  def lc_menu_option_class(tone: :default, extra: nil)
@@ -51,11 +59,11 @@ module Lostcoords::Ui
51
59
  end
52
60
 
53
61
  # trigger: :icon renders a quiet icon button named by label; rendered HTML becomes the content
54
- # of a custom trigger, also named by label.
55
- def lc_menu(id:, label: lc_t("lostcoords.ui.menu.label"), align: :end, trigger: :button, icon: "ellipsis-vertical", trigger_attributes: {}, wrapper_attributes: {}, **attributes, &block)
56
- align = lc_option!(:align, align, MENU_POPOVER_ALIGN.keys)
57
- default_trigger = ->(wiring) { lc_panel_trigger(label, trigger, icon, :secondary, wiring) } unless trigger == false
58
- lc_render_panel(id, label, nil, "lc-menu", "menu", lc_menu_popover_class(align: align), lc_attributes({ data: { slot: "menu" } }, attributes), default_trigger, trigger_attributes, wrapper_attributes, &block)
62
+ # of a custom trigger, also named by label. trigger_size: sizes either one.
63
+ def lc_menu(id:, label: lc_t("lostcoords.ui.menu.label"), align: :end, side: :bottom, trigger: :button, trigger_size: :md, icon: "ellipsis-vertical", trigger_attributes: {}, wrapper_attributes: {}, **attributes, &block)
64
+ trigger_size = lc_option!(:trigger_size, trigger_size, ButtonHelper::ICON_BUTTON_SIZES.keys)
65
+ default_trigger = ->(wiring) { lc_panel_trigger(label, trigger, icon, :secondary, wiring, trigger_size) } unless trigger == false
66
+ lc_render_panel(id, label, nil, "lc-menu", "menu", lc_menu_popover_class(align: align, side: side), lc_attributes({ data: { slot: "menu" } }, attributes), default_trigger, trigger_attributes, wrapper_attributes, &block)
59
67
  end
60
68
 
61
69
  # Takes the same trigger options as lc_menu, including trigger: false.
@@ -70,8 +78,9 @@ module Lostcoords::Ui
70
78
  # icon: names a leading Heroicon; leading: takes other rendered content, such as an avatar, and is
71
79
  # hidden from assistive technology so the label stays the name. checked: true or false makes a
72
80
  # menuitemradio with a check mark. checkbox: true makes a menuitemcheckbox that flips on click and
73
- # leaves the menu open. method: renders a button_to form for href.
74
- def lc_menu_option(label = nil, href: nil, tone: :default, icon: nil, leading: nil, hint: nil, checked: nil, checkbox: false, method: nil, params: {}, form: {}, **attributes, &block)
81
+ # leaves the menu open. method: renders a button_to form for href, with form_attributes: on the
82
+ # form. form: is the button's own attribute, naming a form elsewhere on the page to submit.
83
+ def lc_menu_option(label = nil, href: nil, tone: :default, icon: nil, leading: nil, hint: nil, checked: nil, checkbox: false, method: nil, params: {}, form_attributes: {}, **attributes, &block)
75
84
  tone = lc_option!(:tone, tone, MENU_OPTION_TONES.keys)
76
85
  raise ArgumentError, "Pass icon: or leading:, not both" if icon && leading
77
86
  raise ArgumentError, "checked: must be true, false or nil" unless [ true, false, nil ].include?(checked)
@@ -91,7 +100,7 @@ module Lostcoords::Ui
91
100
  defaults[:aria] = { checked: checked } unless checked.nil?
92
101
 
93
102
  if method
94
- button_to(content, href, **lc_attributes(defaults, attributes), method: method, params: params, form: lc_attributes({ class: "contents" }, form))
103
+ button_to(content, href, **lc_attributes(defaults, attributes), method: method, params: params, form: lc_attributes({ class: "contents" }, form_attributes))
95
104
  elsif href
96
105
  raise ArgumentError, "Links cannot be disabled; use a menu button instead" if attributes[:disabled] || attributes["disabled"]
97
106
 
@@ -110,13 +119,13 @@ module Lostcoords::Ui
110
119
  end
111
120
 
112
121
  private
113
- def lc_panel_trigger(label, trigger, icon, variant, attributes)
122
+ def lc_panel_trigger(label, trigger, icon, variant, attributes, size = :md)
114
123
  custom = trigger.is_a?(String) && trigger.html_safe?
115
124
  kind = custom ? :custom : lc_option!(:trigger, trigger, MENU_TRIGGERS)
116
125
  case kind
117
126
  when :custom, :icon
118
127
  content = custom ? trigger : lc_icon(icon, options: { "aria-hidden": true })
119
- icon_button = { type: "button", class: lc_icon_button_class(shape: :circle, size: :md, extra: ButtonHelper::ICON_BUTTON_QUIET), aria: { label: label }, data: { slot: "icon-button" } }
128
+ icon_button = { type: "button", class: lc_icon_button_class(shape: :circle, size: size, extra: ButtonHelper::ICON_BUTTON_QUIET), aria: { label: label }, data: { slot: "icon-button" } }
120
129
  tag.button(content, **lc_attributes(icon_button, attributes))
121
130
  else
122
131
  lc_button(label, variant: variant, **attributes)
@@ -37,7 +37,7 @@ module Lostcoords::Ui
37
37
  content = capture(&content_block)
38
38
  end
39
39
 
40
- button_to(lc_button_content(content, icon, icon_position), url, lc_button_attributes(variant, tone, size, block, {}, attributes))
40
+ button_to(lc_button_content(content, icon, icon_position), url, lc_button_attributes(variant, tone, size, block, {}, lc_button_form(attributes)))
41
41
  end
42
42
 
43
43
  # A button styled as a select for opening a menu or picker. A string label truncates; a block
@@ -86,21 +86,21 @@ module Lostcoords::Ui
86
86
  tag.div(safe_join([ primary, toggle, popover ]), **lc_attributes(defaults, attributes))
87
87
  end
88
88
 
89
- def lc_icon_button(label, icon: nil, size: :md, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
90
- attributes = lc_icon_button_attributes(label, size, shape, variant, tone, { type: "button" }, attributes)
89
+ def lc_icon_button(label, icon: nil, size: :md, icon_size: size, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
90
+ attributes = lc_icon_button_attributes(label, size, icon_size, shape, variant, tone, { type: "button" }, attributes)
91
91
  lc_icon_button_tooltip(label, tooltip, attributes) { |tip| tag.button(lc_icon_button_content(icon, &content), **lc_attributes(attributes, tip)) }
92
92
  end
93
93
 
94
- def lc_icon_button_link_to(label, url, icon: nil, size: :md, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
95
- attributes = lc_icon_button_attributes(label, size, shape, variant, tone, {}, attributes)
94
+ def lc_icon_button_link_to(label, url, icon: nil, size: :md, icon_size: size, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
95
+ attributes = lc_icon_button_attributes(label, size, icon_size, shape, variant, tone, {}, attributes)
96
96
  raise ArgumentError, "Links cannot be disabled; use lc_icon_button for a disabled control" if attributes[:disabled]
97
97
 
98
98
  lc_icon_button_tooltip(label, tooltip, attributes) { |tip| link_to(lc_icon_button_content(icon, &content), url, lc_attributes(attributes, tip)) }
99
99
  end
100
100
 
101
- def lc_icon_button_to(label, url, icon: nil, size: :md, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
102
- attributes = lc_icon_button_attributes(label, size, shape, variant, tone, {}, attributes)
103
- lc_icon_button_tooltip(label, tooltip, attributes) { |tip| button_to(lc_icon_button_content(icon, &content), url, lc_attributes(attributes, tip)) }
101
+ def lc_icon_button_to(label, url, icon: nil, size: :md, icon_size: size, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
102
+ attributes = lc_icon_button_attributes(label, size, icon_size, shape, variant, tone, {}, attributes)
103
+ lc_icon_button_tooltip(label, tooltip, attributes) { |tip| button_to(lc_icon_button_content(icon, &content), url, lc_attributes(lc_button_form(attributes), tip)) }
104
104
  end
105
105
 
106
106
  def lc_badge(content = nil, tone: :neutral, shape: :rounded, **attributes, &block)
@@ -133,8 +133,9 @@ module Lostcoords::Ui
133
133
  end
134
134
 
135
135
  # row: takes attributes for the inner row that overlaps the avatars, such as a controller target.
136
- def lc_avatar_stack(compact: false, row: {}, **attributes, &block)
137
- tag.span(**lc_attributes({ class: lc_avatar_stack_class(compact: compact), data: { slot: "avatar-stack" } }, attributes)) do
136
+ # size: pins every avatar in the stack to one lc_avatar size; overlap: :sm tightens it for a line of text.
137
+ def lc_avatar_stack(compact: false, size: :md, overlap: :md, row: {}, **attributes, &block)
138
+ tag.span(**lc_attributes({ class: lc_avatar_stack_class(compact: compact, size: size, overlap: overlap), data: { slot: "avatar-stack" } }, attributes)) do
138
139
  tag.span(**lc_attributes({ class: lc_avatar_stack_row_class }, row), &block)
139
140
  end
140
141
  end
@@ -222,11 +223,11 @@ module Lostcoords::Ui
222
223
  lc_attributes(defaults.merge(class: lc_button_class(variant: variant, tone: tone, size: size, block: block), data: data), attributes)
223
224
  end
224
225
 
225
- def lc_icon_button_attributes(label, size, shape, variant, tone, defaults, attributes)
226
+ def lc_icon_button_attributes(label, size, icon_size, shape, variant, tone, defaults, attributes)
226
227
  size = lc_option!(:size, size, ButtonHelper::ICON_BUTTON_SIZES.keys)
227
228
  shape = lc_option!(:shape, shape, ButtonHelper::ICON_BUTTON_SHAPES.keys)
228
229
  data = { slot: "icon-button", size: size, variant: (variant.to_sym unless variant.to_sym == :ghost), tone: (tone.to_sym unless tone.to_sym == :default) }.compact
229
- defaults = defaults.merge(class: lc_icon_button_class(size: size, shape: shape, variant: variant, tone: tone), aria: { label: label }, data: data)
230
+ defaults = defaults.merge(class: lc_icon_button_class(size: size, icon_size: icon_size, shape: shape, variant: variant, tone: tone), aria: { label: label }, data: data)
230
231
  attributes = lc_attributes(defaults, attributes)
231
232
  accessible_label = attributes.fetch(:"aria-label", attributes.dig(:aria, :label))
232
233
  raise ArgumentError, "lc_icon_button requires a nonblank accessible label" if label.blank? || accessible_label.blank?
@@ -244,6 +245,13 @@ module Lostcoords::Ui
244
245
  lc_tooltip(text, id: id) { |tip| yield(tip) }
245
246
  end
246
247
 
248
+ # button_to's form is a box of its own; contents lets the button sit in the caller's row or grid.
249
+ def lc_button_form(attributes)
250
+ attributes = attributes.dup
251
+ form = attributes.delete(:form) || attributes.delete("form") || {}
252
+ attributes.merge(form: lc_attributes({ class: "contents" }, form))
253
+ end
254
+
247
255
  def lc_icon_button_content(icon, &content)
248
256
  raise ArgumentError, "Pass icon: or a block" unless icon || content
249
257
 
@@ -55,7 +55,7 @@ module Lostcoords::Ui
55
55
  end
56
56
 
57
57
  def lc_option!(name, value, allowed)
58
- normalized = value.is_a?(Integer) ? value : (value.to_sym if value.is_a?(String) || value.is_a?(Symbol))
58
+ normalized = value.is_a?(Numeric) ? value : (value.to_sym if value.is_a?(String) || value.is_a?(Symbol))
59
59
  return normalized if allowed.include?(normalized)
60
60
 
61
61
  raise ArgumentError, "Invalid #{name}: #{value.inspect}. Expected one of: #{allowed.map(&:inspect).join(', ')}"
@@ -2,6 +2,7 @@ module Lostcoords::Ui
2
2
  module SheetHelper
3
3
  SHEET_TAGS = %i[div dialog].freeze
4
4
  SHEET_DETENTS = [ "fit", "medium large", "large" ].freeze
5
+ SHEET_SURFACES = %i[glass].freeze
5
6
  SHEET_HANDLE = "grid min-h-7 w-full flex-none cursor-grab touch-none place-items-center active:cursor-grabbing sm:hidden"
6
7
 
7
8
  # A panel that is an iOS sheet on phones, with the lc-sheet controller. tag: :dialog is a modal
@@ -12,17 +13,22 @@ module Lostcoords::Ui
12
13
  # A swipe down dispatches lc-sheet:dismiss; unless a listener cancels it, an open popover hides.
13
14
  # With tag: :dialog, size: draws lc_dialog's modal frame at that width, for a dialog whose content
14
15
  # 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
+ # surface: :glass draws a :div in the translucent menu material, a menu on desktop and a sheet on phones.
17
+ # Yields a builder whose header and footer parts stay pinned while the content scrolls.
18
+ def lc_sheet(tag: :div, anchored: false, detents: "fit", handle: false, size: nil, surface: nil, **attributes, &block)
16
19
  tag = lc_option!(:tag, tag, SHEET_TAGS)
17
20
  detents = lc_option!(:detents, detents, SHEET_DETENTS.map(&:to_sym)).to_s
21
+ surface = lc_option!(:surface, surface, SHEET_SURFACES) unless surface.nil?
18
22
  raise ArgumentError, "anchored: true needs tag: :div" if anchored && tag == :dialog
19
23
  raise ArgumentError, "size: needs tag: :dialog" if size && tag != :dialog
24
+ raise ArgumentError, "surface: :glass needs tag: :div" if surface && tag == :dialog
20
25
 
21
26
  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
27
+ defaults[:class] = lc_tw("lc-sheet", ("lc-sheet-anchored" if anchored), (lc_glass_class(shape: :picker) if surface == :glass)) unless tag == :dialog
23
28
  defaults[:class] = lc_dialog_class(size: size) if size
24
29
  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))
30
+ content = capture(Parts::Sheet.new(self), &block) if block
31
+ content_tag(tag, safe_join([ strip, content ].compact), **lc_attributes(defaults, attributes))
26
32
  end
27
33
  end
28
34
  end
@@ -3,10 +3,21 @@ module Lostcoords::Ui
3
3
  INPUT_TYPES = %i[text search email password tel url number date time datetime-local month week].freeze
4
4
 
5
5
  # plain: true drops the fill, border, padding and height, for a control in an lc_list_field row.
6
- def lc_input(name: nil, value: nil, type: :text, size: :md, plain: false, **attributes)
6
+ INPUT_ICON = "pointer-events-none absolute start-3.5 size-4.5 text-muted-foreground"
7
+
8
+ # icon: draws a leading icon inside the field, such as a magnifying glass on a search field; the field
9
+ # comes wrapped in a span then, and class: and the other attributes still reach the <input>.
10
+ def lc_input(name: nil, value: nil, type: :text, size: :md, plain: false, icon: nil, **attributes)
7
11
  type = lc_option!(:type, type, INPUT_TYPES)
12
+ raise ArgumentError, "icon: needs a field; plain: true has no room for one" if icon && plain
13
+
8
14
  data = { slot: "input", plain: (true if plain) }.compact
9
- tag.input(**lc_attributes({ type: type, name: name, value: value, class: lc_input_class(size: size, plain: plain), data: data }, attributes))
15
+ input = tag.input(**lc_attributes({ type: type, name: name, value: value, class: lc_input_class(size: size, plain: plain, extra: ("ps-10" if icon)), data: data }, attributes))
16
+ return input unless icon
17
+
18
+ tag.span(class: "relative flex min-w-0 items-center", data: { slot: "input-group" }) do
19
+ safe_join([ lc_icon(icon, options: { class: INPUT_ICON, "aria-hidden": true }), input ])
20
+ end
10
21
  end
11
22
 
12
23
  def lc_textarea(content = nil, name: nil, **attributes, &block)
@@ -97,18 +108,21 @@ module Lostcoords::Ui
97
108
  # as: :nav makes links, the current one marked with aria-current; otherwise radio buttons whose
98
109
  # selection lives in the lc-selection controller.
99
110
  # variant: :pills lays the items out as wrapping toggle pills, sized by size:, instead of a segmented track.
100
- def lc_toggle_group(label:, name: nil, value: nil, as: :div, variant: :segmented, size: :md, **attributes, &block)
111
+ # layout: and scroll: shape the segmented track; see lc_toggle_group_class.
112
+ def lc_toggle_group(label:, name: nil, value: nil, as: :div, variant: :segmented, size: :md, layout: :inline, scroll: false, **attributes, &block)
101
113
  lc_label!(label, :label)
102
114
  as = lc_option!(:as, as, %i[div nav])
103
115
  variant = lc_option!(:variant, variant, %i[segmented pills])
104
116
  size = lc_option!(:size, size, ButtonHelper::BUTTON_SIZES.keys)
117
+ layout = lc_option!(:layout, layout, FormHelper::TOGGLE_GROUP_LAYOUTS.keys)
105
118
  raise ArgumentError, "size: sizes pills; a segmented toggle group has one size" if variant == :segmented && size != :md
119
+ raise ArgumentError, "layout: and scroll: shape a segmented track; pills wrap" if variant == :pills && (layout != :inline || scroll)
106
120
  raise ArgumentError, "a nav toggle group marks its current link; it takes no name: or value:" if as == :nav && (name || value)
107
121
 
108
122
  mode = as == :nav ? :links : (name ? :radios : :buttons)
109
- content = lc_within_toggle_group({ mode: mode, name: name, value: value&.to_s, variant: variant, size: size }) { capture(&block) if block }
123
+ content = lc_within_toggle_group({ mode: mode, name: name, value: value&.to_s, variant: variant, size: size, layout: layout }) { capture(&block) if block }
110
124
 
111
- defaults = { class: (variant == :pills ? lc_inline_class : lc_toggle_group_class), data: { slot: "toggle-group" } }
125
+ defaults = { class: (variant == :pills ? lc_inline_class : lc_toggle_group_class(layout: layout, scroll: scroll)), data: { slot: "toggle-group" } }
112
126
  case mode
113
127
  when :links
114
128
  tag.nav(content, **lc_attributes(defaults, attributes, { aria: { label: label } }))
@@ -121,17 +135,19 @@ module Lostcoords::Ui
121
135
  end
122
136
 
123
137
  # Takes href: and current: in a nav group, value: and checked: in a named group, and value: and
124
- # selected: in a button group; arguments from another kind of group raise.
125
- def lc_toggle_group_item(label = nil, value: nil, selected: nil, checked: nil, href: nil, current: nil, input: {}, **attributes, &block)
126
- group = @_lc_toggle_group || { mode: :buttons, variant: :segmented }
138
+ # selected: in a button group; arguments from another kind of group raise. icon: and icon_only:
139
+ # work as on lc_toggle.
140
+ def lc_toggle_group_item(label = nil, value: nil, selected: nil, checked: nil, href: nil, current: nil, input: {}, icon: nil, icon_only: false, **attributes, &block)
141
+ group = @_lc_toggle_group || { mode: :buttons, variant: :segmented, layout: :inline }
127
142
  given = { value: value, selected: selected, checked: checked, href: href, current: current }.compact.keys
128
143
  allowed = { links: %i[href current], radios: %i[value checked], buttons: %i[value selected] }.fetch(group[:mode])
129
144
  stray = given - allowed
130
145
  raise ArgumentError, "#{stray.join(', ')} can't be used on an item of a #{group[:mode]} toggle group" if stray.any?
131
146
  raise ArgumentError, "an item of a nav toggle group needs href:" if group[:mode] == :links && !href
132
147
 
133
- content = block ? capture(&block) : label
134
- defaults = { class: (group[:variant] == :pills ? lc_toggle_class(size: group[:size]) : lc_toggle_group_item_class), data: { slot: "toggle-group-item" } }
148
+ content = lc_toggle_content(label, icon, icon_only, &block)
149
+ item_class = group[:variant] == :pills ? lc_toggle_class(size: group[:size]) : lc_toggle_group_item_class(layout: group[:layout] || :inline)
150
+ defaults = { class: lc_tw(item_class, (TOGGLE_ICON_ONLY if icon_only)), data: { slot: "toggle-group-item", icon_only: (true if icon_only) }.compact }
135
151
  return lc_toggle_link(content, href, current, defaults, attributes) if group[:mode] == :links
136
152
 
137
153
  value ||= label
@@ -154,13 +170,7 @@ module Lostcoords::Ui
154
170
  # the label stays as its accessible name. tooltip: true shows the label on hover and focus, and
155
171
  # tooltip: "text" describes the toggle with other text, as on lc_icon_button.
156
172
  def lc_toggle(label = nil, active: false, ghost: false, size: :md, href: nil, name: nil, value: nil, input: {}, icon: nil, icon_only: false, tooltip: nil, **attributes, &block)
157
- raise ArgumentError, "icon_only: true needs icon: and a label, not a block" if icon_only && (icon.nil? || label.blank? || block)
158
-
159
- content = block ? capture(&block) : label
160
- if icon
161
- glyph = lc_icon(icon, options: { "aria-hidden": true, "data-icon": ("inline-start" unless icon_only) }.compact)
162
- content = safe_join([ glyph, (icon_only ? tag.span(label, class: "sr-only") : content) ])
163
- end
173
+ content = lc_toggle_content(label, icon, icon_only, &block)
164
174
  defaults = { class: lc_toggle_class(ghost: ghost, size: size, extra: (TOGGLE_ICON_ONLY if icon_only)), data: { slot: "toggle", icon_only: (true if icon_only) }.compact }
165
175
  lc_icon_button_tooltip(label, tooltip, attributes) do |tip|
166
176
  attributes = lc_attributes(attributes, tip)
@@ -183,6 +193,16 @@ module Lostcoords::Ui
183
193
  end
184
194
 
185
195
  private
196
+ def lc_toggle_content(label, icon, icon_only, &block)
197
+ raise ArgumentError, "icon_only: true needs icon: and a label, not a block" if icon_only && (icon.nil? || label.blank? || block)
198
+
199
+ content = block ? capture(&block) : label
200
+ return content unless icon
201
+
202
+ glyph = lc_icon(icon, options: { "aria-hidden": true, "data-icon": ("inline-start" unless icon_only) }.compact)
203
+ safe_join([ glyph, (icon_only ? tag.span(label, class: "sr-only") : content) ])
204
+ end
205
+
186
206
  # Items read the enclosing group's mode, so a nested group restores the outer one.
187
207
  def lc_within_toggle_group(group)
188
208
  previous, @_lc_toggle_group = @_lc_toggle_group, group
@@ -26,7 +26,8 @@ module Lostcoords::Ui
26
26
 
27
27
  # anchor: true links the heading to itself, so readers can copy a link to it; it needs id:.
28
28
  # anchor: "section-id" links it to an element around it instead, such as its <section>.
29
- def lc_heading(content = nil, level: 2, size: :heading, trim: false, anchor: false, **attributes, &block)
29
+ # as: :span or :div sets text in a heading role without making it a heading, such as an amount in a row.
30
+ def lc_heading(content = nil, level: 2, size: :heading, trim: false, anchor: false, as: nil, **attributes, &block)
30
31
  raise ArgumentError, "Invalid level: #{level.inspect}. Expected an integer from 1 to 6" unless (1..6).include?(level) && level.is_a?(Integer)
31
32
 
32
33
  target = anchor == true ? attributes[:id].to_s : anchor.presence&.to_s
@@ -34,7 +35,8 @@ module Lostcoords::Ui
34
35
 
35
36
  content = capture(&block) if block
36
37
  content = tag.a(content, href: "##{target}", class: "lc-anchor", data: { slot: "heading-anchor" }) if target
37
- tag.public_send("h#{level}", content, **lc_attributes({ class: lc_heading_class(size: size, trim: trim, extra: ("lc-anchor-target" if anchor == true)) }, attributes))
38
+ element = as ? lc_option!(:as, as, %i[span div]) : "h#{level}"
39
+ tag.public_send(element, content, **lc_attributes({ class: lc_heading_class(size: size, trim: trim, extra: ("lc-anchor-target" if anchor == true)) }, attributes))
38
40
  end
39
41
 
40
42
  # as: picks the element, such as :span inside a row or :h3 for a heading set in a text role.