lostcoords-ui 0.12.2 → 0.14.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 (28) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +87 -21
  3. data/app/assets/tailwind/lostcoords/ui/components/menu.css +0 -1
  4. data/app/assets/tailwind/lostcoords/ui/dialog.css +7 -0
  5. data/app/assets/tailwind/lostcoords/ui/emoji-picker.css +0 -18
  6. data/app/assets/tailwind/lostcoords/ui/popover.css +10 -0
  7. data/app/assets/tailwind/lostcoords/ui/sheet.css +13 -55
  8. data/app/helpers/lostcoords/ui/card_helper.rb +1 -1
  9. data/app/helpers/lostcoords/ui/dialog_helper.rb +6 -6
  10. data/app/helpers/lostcoords/ui/list_group_helper.rb +24 -24
  11. data/app/helpers/lostcoords/ui/menu_helper.rb +31 -24
  12. data/app/helpers/lostcoords/ui/primitives_helper.rb +8 -15
  13. data/app/helpers/lostcoords/ui/sheet_helper.rb +17 -17
  14. data/app/helpers/lostcoords/ui/tailwind_helper.rb +8 -2
  15. data/app/javascript/lostcoords/ui/controllers/lc_hovercard_controller.js +3 -2
  16. data/app/javascript/lostcoords/ui/controllers/lc_menu_controller.js +14 -9
  17. data/app/javascript/lostcoords/ui/controllers/lc_search_controller.js +1 -1
  18. data/app/javascript/lostcoords/ui/controllers/lc_sheet_controller.js +3 -71
  19. data/app/javascript/lostcoords/ui/controllers/lc_tooltip_controller.js +3 -5
  20. data/app/javascript/lostcoords/ui/helpers/catalog_picker.js +1 -1
  21. data/app/javascript/lostcoords/ui/helpers/floating_panel.js +16 -14
  22. data/app/views/lostcoords/ui/components/_emoji_picker.html.erb +0 -2
  23. data/app/views/lostcoords/ui/components/_lc_confirm_sheet.html.erb +2 -2
  24. data/app/views/lostcoords/ui/components/_lc_dialog.html.erb +9 -6
  25. data/lib/lostcoords/ui/parts.rb +9 -0
  26. data/lib/lostcoords/ui/version.rb +1 -1
  27. metadata +1 -2
  28. data/app/views/lostcoords/ui/components/_lc_floating_panel.html.erb +0 -13
@@ -59,20 +59,19 @@ module Lostcoords::Ui
59
59
  end
60
60
 
61
61
  # trigger: :icon renders a quiet icon button named by label; rendered HTML becomes the content
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)
62
+ # of a custom trigger, also named by label. trigger_variant: and trigger_size: style the trigger.
63
+ def lc_menu(id:, label: lc_t("lostcoords.ui.menu.label"), align: :end, side: :bottom, trigger: :button, trigger_variant: :secondary, trigger_size: :md, icon: "ellipsis-vertical", trigger_attributes: {}, wrapper_attributes: {}, **attributes, &block)
64
+ default_trigger = lc_panel_trigger(label, trigger, icon, trigger_variant, trigger_size)
65
+ lc_panel(id: id, label: label, controller: "lc-menu", role: "menu", classes: lc_menu_popover_class(align: align, side: side), attributes: lc_attributes({ data: { slot: "menu" } }, attributes),
66
+ default_trigger: default_trigger, trigger_attributes: trigger_attributes, wrapper_attributes: wrapper_attributes, &block)
67
67
  end
68
68
 
69
- # Takes the same trigger options as lc_menu, including trigger: false.
70
- def lc_popover(id:, label: lc_t("lostcoords.ui.popover.trigger"), title: nil, align: :end, trigger_variant: :secondary, trigger: :button, icon: "ellipsis-vertical", trigger_attributes: {}, wrapper_attributes: {}, **attributes, &block)
71
- title = title.presence
72
- align = lc_option!(:align, align, MENU_POPOVER_ALIGN.keys)
73
- default_trigger = ->(wiring) { lc_panel_trigger(label, trigger, icon, trigger_variant, wiring) } unless trigger == false
74
- classes = lc_popover_class(align: align, extra: "data-open:flex flex-col gap-content p-inset z-(--lc-z-popover) min-w-64 max-w-[calc(100vw-2rem)]")
75
- lc_render_panel(id, label, title, "lc-popover", "dialog", classes, lc_attributes({ data: { slot: "popover" } }, attributes), default_trigger, trigger_attributes, wrapper_attributes, &block)
69
+ # Takes the same trigger and placement options as lc_menu, including trigger: false.
70
+ def lc_popover(id:, label: lc_t("lostcoords.ui.popover.trigger"), title: nil, align: :end, side: :bottom, trigger: :button, trigger_variant: :secondary, trigger_size: :md, icon: "ellipsis-vertical", trigger_attributes: {}, wrapper_attributes: {}, **attributes, &block)
71
+ default_trigger = lc_panel_trigger(label, trigger, icon, trigger_variant, trigger_size)
72
+ classes = lc_popover_class(align: align, side: side, extra: "data-open:flex flex-col gap-content p-inset z-(--lc-z-popover) min-w-64 max-w-[calc(100vw-2rem)]")
73
+ lc_panel(id: id, label: label, title: title.presence, controller: "lc-popover", role: "dialog", classes: classes, attributes: lc_attributes({ data: { slot: "popover" } }, attributes),
74
+ default_trigger: default_trigger, trigger_attributes: trigger_attributes, wrapper_attributes: wrapper_attributes, &block)
76
75
  end
77
76
 
78
77
  # icon: names a leading Heroicon; leading: takes other rendered content, such as an avatar, and is
@@ -119,23 +118,28 @@ module Lostcoords::Ui
119
118
  end
120
119
 
121
120
  private
122
- def lc_panel_trigger(label, trigger, icon, variant, attributes, size = :md)
121
+ # The default trigger as a callable that takes the panel's wiring, or nil for trigger: false.
122
+ def lc_panel_trigger(label, trigger, icon, variant, size)
123
+ return if trigger == false
124
+
125
+ size = lc_option!(:trigger_size, size, ButtonHelper::BUTTON_SIZES.keys)
126
+ variant = lc_option!(:trigger_variant, variant, ButtonHelper::BUTTON_VARIANTS.keys)
123
127
  custom = trigger.is_a?(String) && trigger.html_safe?
124
128
  kind = custom ? :custom : lc_option!(:trigger, trigger, MENU_TRIGGERS)
125
- case kind
126
- when :custom, :icon
129
+ lambda do |wiring|
130
+ next lc_button(label, variant: variant, size: size, **wiring) if kind == :button
131
+
127
132
  content = custom ? trigger : lc_icon(icon, options: { "aria-hidden": true })
128
133
  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" } }
129
- tag.button(content, **lc_attributes(icon_button, attributes))
130
- else
131
- lc_button(label, variant: variant, **attributes)
134
+ tag.button(content, **lc_attributes(icon_button, wiring))
132
135
  end
133
136
  end
134
137
 
135
- # The block runs before the default trigger renders, so a menu.trigger part can replace it.
136
- # trigger: false renders only the panel, for a caller that hosts the controller on an ancestor
137
- # and wires its own triggers.
138
- def lc_render_panel(id, label, title, controller, role, classes, attributes, default_trigger, trigger_attributes, wrapper_attributes, &block)
138
+ # A trigger and the panel it opens, in a wrapper that hosts the controller. The block runs
139
+ # before the default trigger renders, so a trigger part can replace it. Without a default
140
+ # trigger only the panel renders, for a caller that hosts the controller on an ancestor and
141
+ # wires its own triggers.
142
+ def lc_panel(id:, label:, controller:, role:, classes:, attributes:, default_trigger:, title: nil, trigger_attributes: {}, wrapper_attributes: {}, &block)
139
143
  lc_label!(id, :id)
140
144
  lc_label!(label, :label)
141
145
  standalone = default_trigger.nil?
@@ -149,8 +153,11 @@ module Lostcoords::Ui
149
153
  required = { id: id, role: role, aria: name, data: { "#{controller}_target" => "popover" } }
150
154
  required[:tabindex] = -1 if role == "dialog"
151
155
  merged = lc_attributes({ class: classes }, attributes, required)
152
- wrapper = lc_attributes({ class: "relative inline-flex" }, wrapper_attributes, { data: { controller: controller } }) unless standalone
153
- render "lostcoords/ui/components/lc_floating_panel", id: id, title: title, wrapper: wrapper, trigger: trigger, attributes: merged, content: content
156
+ heading = tag.h2(title, id: "#{id}-title", class: lc_heading_class(size: :subheading)) if title
157
+ panel = tag.div(safe_join([ heading, content ].compact), **merged)
158
+ return panel if standalone
159
+
160
+ tag.div(safe_join([ trigger, panel ]), **lc_attributes({ class: "relative inline-flex" }, wrapper_attributes, { data: { controller: controller } }))
154
161
  end
155
162
  end
156
163
  end
@@ -50,7 +50,6 @@ module Lostcoords::Ui
50
50
  tag.button(safe_join([ body, chevron ]), **lc_attributes(defaults, attributes))
51
51
  end
52
52
 
53
- # A block replaces the icon with custom content, such as an avatar or two stacked icons.
54
53
  # One pill with a primary action and a toggle on its inline end that opens a menu of the block's
55
54
  # lc_menu_option items. With href: the action is a link. label may be rendered HTML. action:,
56
55
  # toggle: and menu: add attributes to those parts; other attributes go on the wrapper.
@@ -60,8 +59,6 @@ module Lostcoords::Ui
60
59
  # <% end %>
61
60
  def lc_split_button(label, id:, href: nil, menu_label: lc_t("lostcoords.ui.menu.label"), variant: :primary, tone: :default, size: :md,
62
61
  icon: nil, icon_position: :start, block: false, align: :end, action: {}, toggle: {}, menu: {}, **attributes, &items)
63
- lc_label!(id, :id)
64
- lc_label!(menu_label, :menu_label)
65
62
  variant = lc_option!(:variant, variant, ButtonHelper::BUTTON_VARIANTS.keys)
66
63
  size = lc_option!(:size, size, ButtonHelper::BUTTON_SIZES.keys)
67
64
  button = { variant: variant, tone: tone, size: size, icon: icon, icon_position: icon_position }
@@ -72,20 +69,16 @@ module Lostcoords::Ui
72
69
  ButtonHelper::SPLIT_BUTTON_PART, ButtonHelper::SPLIT_BUTTON_TOGGLE, ButtonHelper::SPLIT_BUTTON_TOGGLE_WIDTHS.fetch(size),
73
70
  (ButtonHelper::SPLIT_BUTTON_DIVIDER unless variant == :outline)
74
71
  ))
75
- toggle = tag.button(lc_icon("chevron-down", variant: :mini, options: { "aria-hidden": true }), **lc_attributes(
76
- { type: "button", class: toggle_class, data: { slot: "split-button-toggle", action: "lc-menu#toggle" } }, toggle,
77
- { id: "#{id}-trigger", aria: { label: menu_label, haspopup: "menu", expanded: false, controls: id }, data: { lc_menu_target: "trigger" } }
78
- ))
79
-
80
- popover = tag.div(capture(&items), **lc_attributes(
81
- { class: lc_menu_popover_class(align: align), data: { slot: "menu" } }, menu,
82
- { id: id, role: "menu", aria: { labelledby: "#{id}-trigger" }, data: { lc_menu_target: "popover" } }
83
- ))
84
-
85
- defaults = { class: lc_tw(ButtonHelper::SPLIT_BUTTON_BASE, ("flex w-full" if block)), data: { controller: "lc-menu", slot: "split-button" } }
86
- tag.div(safe_join([ primary, toggle, popover ]), **lc_attributes(defaults, attributes))
72
+ halves = lambda do |wiring|
73
+ chevron = lc_icon("chevron-down", variant: :mini, options: { "aria-hidden": true })
74
+ safe_join([ primary, tag.button(chevron, **lc_attributes({ type: "button", class: toggle_class, aria: { label: menu_label }, data: { slot: "split-button-toggle" } }, wiring)) ])
75
+ end
76
+ wrapper = lc_attributes({ class: lc_tw(ButtonHelper::SPLIT_BUTTON_BASE, ("flex w-full" if block)), data: { controller: "lc-menu", slot: "split-button" } }, attributes)
77
+ lc_panel(id: id, label: menu_label, controller: "lc-menu", role: "menu", classes: lc_menu_popover_class(align: align), attributes: lc_attributes({ data: { slot: "menu" } }, menu),
78
+ default_trigger: halves, trigger_attributes: toggle, wrapper_attributes: wrapper, &items)
87
79
  end
88
80
 
81
+ # A block replaces the icon with custom content, such as an avatar or two stacked icons.
89
82
  def lc_icon_button(label, icon: nil, size: :md, icon_size: size, shape: :circle, variant: :ghost, tone: :default, tooltip: nil, **attributes, &content)
90
83
  attributes = lc_icon_button_attributes(label, size, icon_size, shape, variant, tone, { type: "button" }, attributes)
91
84
  lc_icon_button_tooltip(label, tooltip, attributes) { |tip| tag.button(lc_icon_button_content(icon, &content), **lc_attributes(attributes, tip)) }
@@ -1,34 +1,34 @@
1
1
  module Lostcoords::Ui
2
2
  module SheetHelper
3
- SHEET_TAGS = %i[div dialog].freeze
3
+ SHEET_ELEMENTS = %i[div dialog].freeze
4
4
  SHEET_DETENTS = [ "fit", "medium large", "large" ].freeze
5
5
  SHEET_SURFACES = %i[glass].freeze
6
- SHEET_HANDLE = "grid min-h-7 w-full flex-none cursor-grab touch-none place-items-center active:cursor-grabbing sm:hidden"
7
6
 
8
- # A panel that is an iOS sheet on phones, with the lc-sheet controller. tag: :dialog is a modal
9
- # <dialog> (lc_dialog builds on it). A :div shows while [data-open] or, with popover:, :popover-open;
10
- # from sm up it keeps your frame, or with anchored: true it is a popover under its trigger, opened by a
11
- # FloatingPanel inside a positioned ancestor. detents: "fit", "medium large" or "large". handle: true
12
- # adds a grab strip above the content; without one the sheet draws its grabber over it.
7
+ # A panel that is a sheet on phones, with the lc-sheet controller: it spans the screen's width and sits
8
+ # on its bottom edge. as: :dialog is a modal <dialog> (lc_dialog builds on it). A :div shows while
9
+ # [data-open] or, with popover:, :popover-open; from sm up it keeps your frame, or with anchored: true it
10
+ # is a popover under its trigger, opened by a FloatingPanel inside a positioned ancestor.
11
+ # detents: "fit" is a half sheet at its content's height, "large" fills the screen and "medium large"
12
+ # drags between half the screen and all of it.
13
13
  # A swipe down dispatches lc-sheet:dismiss; unless a listener cancels it, an open popover hides.
14
- # With tag: :dialog, size: draws lc_dialog's modal frame at that width, for a dialog whose content
14
+ # With as: :dialog, size: draws lc_dialog's modal frame at that width, for a dialog whose content
15
15
  # brings its own header and labels the dialog with aria-label.
16
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)
19
- tag = lc_option!(:tag, tag, SHEET_TAGS)
17
+ # Yields a builder whose header and footer parts stay pinned while the content scrolls, and whose close
18
+ # part is the ✕ at the header's start.
19
+ def lc_sheet(as: :div, anchored: false, detents: "fit", size: nil, surface: nil, **attributes, &block)
20
+ as = lc_option!(:as, as, SHEET_ELEMENTS)
20
21
  detents = lc_option!(:detents, detents, SHEET_DETENTS.map(&:to_sym)).to_s
21
22
  surface = lc_option!(:surface, surface, SHEET_SURFACES) unless surface.nil?
22
- raise ArgumentError, "anchored: true needs tag: :div" if anchored && tag == :dialog
23
- raise ArgumentError, "size: needs tag: :dialog" if size && tag != :dialog
24
- raise ArgumentError, "surface: :glass needs tag: :div" if surface && tag == :dialog
23
+ raise ArgumentError, "anchored: true needs as: :div" if anchored && as == :dialog
24
+ raise ArgumentError, "size: needs as: :dialog" if size && as != :dialog
25
+ raise ArgumentError, "surface: :glass needs as: :div" if surface && as == :dialog
25
26
 
26
27
  defaults = { data: { slot: "sheet", controller: "lc-sheet", lc_sheet_detents_value: detents } }
27
- defaults[:class] = lc_tw("lc-sheet", ("lc-sheet-anchored" if anchored), (lc_glass_class(shape: :picker) if surface == :glass)) unless tag == :dialog
28
+ defaults[:class] = lc_tw("lc-sheet", ("lc-sheet-anchored" if anchored), (lc_glass_class(shape: :picker) if surface == :glass)) unless as == :dialog
28
29
  defaults[:class] = lc_dialog_class(size: size) if size
29
- strip = self.tag.span(class: SHEET_HANDLE, aria: { hidden: true }, data: { slot: "sheet-handle", lc_sheet_target: "handle" }) if handle
30
30
  content = capture(Parts::Sheet.new(self), &block) if block
31
- content_tag(tag, safe_join([ strip, content ].compact), **lc_attributes(defaults, attributes))
31
+ content_tag(as, content, **lc_attributes(defaults, attributes))
32
32
  end
33
33
  end
34
34
  end
@@ -15,9 +15,15 @@ module Lostcoords::Ui
15
15
  "radius" => %w[pill]
16
16
  }.freeze
17
17
 
18
+ # One merger for the process. Its cache is thread-safe and keyed by the class string, so a class
19
+ # list merged in one request is a lookup in every later one; a merger per view would start each
20
+ # request cold.
21
+ def self.merger
22
+ @merger ||= TailwindMerge::Merger.new(config: { theme: MERGE_THEME, cache_size: 4096 })
23
+ end
24
+
18
25
  def lc_tw(*classes)
19
- @tailwind_merger ||= TailwindMerge::Merger.new(config: { theme: MERGE_THEME })
20
- @tailwind_merger.merge(classes.flatten.compact.join(" "))
26
+ TailwindHelper.merger.merge(classes.flatten.compact.join(" "))
21
27
  end
22
28
  end
23
29
  end
@@ -15,12 +15,10 @@ export default class extends Controller {
15
15
  static values = { src: String }
16
16
 
17
17
  connect() {
18
- this.stopPageLeave = onPageLeave(() => this.close({ immediate: true }))
19
18
  this.untab()
20
19
  }
21
20
 
22
21
  disconnect() {
23
- this.stopPageLeave?.()
24
22
  this.cancelTimers()
25
23
  this.close({ immediate: true })
26
24
  }
@@ -52,6 +50,7 @@ export default class extends Controller {
52
50
  }
53
51
  this.element.toggleAttribute("data-open", true)
54
52
  this.release = liftToTopLayer(this.contentTarget, this.triggerTarget)
53
+ this.stopPageLeave = onPageLeave(() => this.close({ immediate: true }))
55
54
  this.unregister = registerDismissable({
56
55
  type: "hovercard",
57
56
  element: this.element,
@@ -92,6 +91,8 @@ export default class extends Controller {
92
91
  this.element.toggleAttribute("data-open", false)
93
92
  this.release?.({ immediate })
94
93
  this.release = null
94
+ this.stopPageLeave?.()
95
+ this.stopPageLeave = null
95
96
  this.unregister?.()
96
97
  this.unregister = null
97
98
  }
@@ -12,25 +12,33 @@ export default class extends FloatingPanel {
12
12
  super.initialize()
13
13
  this.focusOutHandler = this.handleFocusOut.bind(this)
14
14
  this.pointerDownHandler = this.handlePointerDown.bind(this)
15
- this.keydownHandler = (event) => navigateMenu(event, this.popoverTarget)
16
- this.triggerKeydownHandler = this.handleTriggerKeydown.bind(this)
15
+ this.keydownHandler = this.handleKeydown.bind(this)
17
16
  this.focusOutSuppressed = false
18
17
  this.focusOutSuppressionTimer = null
19
18
  }
20
19
 
20
+ // Keys are heard on the host, so a trigger or panel that a morph replaces needs no listener of its own.
21
21
  connect() {
22
- super.connect()
23
- this.triggerTarget.addEventListener("keydown", this.triggerKeydownHandler)
22
+ this.element.addEventListener("keydown", this.keydownHandler)
24
23
  }
25
24
 
26
25
  disconnect() {
27
- this.triggerTarget.removeEventListener("keydown", this.triggerKeydownHandler)
26
+ this.element.removeEventListener("keydown", this.keydownHandler)
28
27
  this.clearFocusOutSuppression()
29
28
  super.disconnect()
30
29
  }
31
30
 
31
+ handleKeydown(event) {
32
+ if (!this.hasPopoverTarget) return
33
+ if (this.popoverTarget.contains(event.target)) {
34
+ if (this.isOpen) navigateMenu(event, this.popoverTarget)
35
+ } else if (this.triggerTargets.some((trigger) => trigger.contains(event.target))) {
36
+ this.openFromTrigger(event)
37
+ }
38
+ }
39
+
32
40
  // Arrow Down opens the menu on its first item, Arrow Up on its last.
33
- handleTriggerKeydown(event) {
41
+ openFromTrigger(event) {
34
42
  if (event.key !== "ArrowDown" && event.key !== "ArrowUp") return
35
43
  if (event.altKey || event.ctrlKey || event.metaKey) return
36
44
 
@@ -62,15 +70,12 @@ export default class extends FloatingPanel {
62
70
  super.open()
63
71
  this.element.addEventListener("focusout", this.focusOutHandler)
64
72
  this.element.addEventListener("pointerdown", this.pointerDownHandler)
65
- this.popoverTarget.addEventListener("keydown", this.keydownHandler)
66
73
  }
67
74
 
68
75
  release(options) {
69
76
  super.release(options)
70
77
  this.element.removeEventListener("focusout", this.focusOutHandler)
71
78
  this.element.removeEventListener("pointerdown", this.pointerDownHandler)
72
- if (this.hasPopoverTarget)
73
- this.popoverTarget.removeEventListener("keydown", this.keydownHandler)
74
79
  }
75
80
 
76
81
  handleFocusOut(event) {
@@ -74,7 +74,7 @@ export default class extends Controller {
74
74
  reset() {
75
75
  this.animations.forEach((animation) => animation.cancel())
76
76
  this.animations = []
77
- this.dialogTarget.close()
77
+ if (this.hasDialogTarget) this.dialogTarget.close()
78
78
  this.unregisterDialog?.()
79
79
  this.unregisterDialog = null
80
80
  this.closing = false
@@ -1,19 +1,15 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
- import { holdDocumentScroll } from "lostcoords/ui/helpers/dismiss_stack"
3
2
  import { SHEET_TIMING, sheetDuration } from "lostcoords/ui/helpers/sheet_motion"
4
3
  import { hideTopLayer, supportsTopLayer } from "lostcoords/ui/helpers/top_layer"
5
4
  import { isSheetViewport, prefersReducedMotion, TEXT_ENTRY } from "lostcoords/ui/helpers/viewport"
6
5
 
7
6
  const PROJECTION_MS = 180
8
7
  const MEDIUM_FRACTION = 0.5
9
- const PAGE_SCALE_LOSS = 0.06
10
8
  const NO_DRAG = "input[type=range], [data-lc-sheet-no-drag]"
11
- const HANDLE = "[data-lc-sheet-target~='handle']"
12
- const GRABBER_ZONE = 28
9
+ const DRAG_ZONE = 28
13
10
  const KEYBOARD_FREE = 0.5
14
11
 
15
12
  export default class extends Controller {
16
- static targets = ["handle"]
17
13
  static values = { detents: { type: String, default: "fit" } }
18
14
 
19
15
  connect() {
@@ -59,7 +55,6 @@ export default class extends Controller {
59
55
  this.stopMouseTracking()
60
56
  clearTimeout(this.settleTimer)
61
57
  clearTimeout(this.transitionTimer)
62
- this.releasePage()
63
58
  this.resetSurface()
64
59
  }
65
60
 
@@ -72,8 +67,6 @@ export default class extends Controller {
72
67
  this.detent = detents[0]
73
68
  if (detents.length > 1 || this.detent.large)
74
69
  this.element.style.blockSize = `${this.detent.height}px`
75
- this.setLargeness(this.detent.large ? 1 : 0)
76
- if (this.detent.large) this.animatePage(1)
77
70
  }
78
71
 
79
72
  // The keyboard rises over the sheet. Safari and in-app browsers shrink the
@@ -86,11 +79,7 @@ export default class extends Controller {
86
79
 
87
80
  closed() {
88
81
  this.gesture = null
89
- if (this.ownsPage) this.animatePage(0)
90
- this.later(() => {
91
- this.releasePage()
92
- this.resetSurface()
93
- })
82
+ this.later(() => this.resetSurface())
94
83
  }
95
84
 
96
85
  // Touch: decide on the first move whether the gesture is the sheet's or the
@@ -149,8 +138,7 @@ export default class extends Controller {
149
138
 
150
139
  onPointerDown(event) {
151
140
  if (!this.isSheet || event.pointerType !== "mouse" || event.button !== 0) return
152
- const fromTop = event.clientY - this.element.getBoundingClientRect().top
153
- if (!event.target.closest(HANDLE) && fromTop > GRABBER_ZONE) return
141
+ if (event.clientY - this.element.getBoundingClientRect().top > DRAG_ZONE) return
154
142
 
155
143
  this.begin(event.clientX, event.clientY, event.timeStamp, null)
156
144
  this.startDrag(event.clientY)
@@ -206,7 +194,6 @@ export default class extends Controller {
206
194
  gesture.startVisible = this.visibleHeight()
207
195
  this.element.style.transition = "none"
208
196
  this.element.style.willChange = "transform"
209
- this.pageTransition("0s")
210
197
 
211
198
  const field = document.activeElement
212
199
  if (this.element.contains(field) && field.matches("input, textarea, select")) field.blur()
@@ -240,7 +227,6 @@ export default class extends Controller {
240
227
  settle(detent, detents = this.detents()) {
241
228
  this.detent = detent
242
229
  this.transition()
243
- this.pageTransition()
244
230
  this.show(detent.height, detents)
245
231
  }
246
232
 
@@ -298,16 +284,6 @@ export default class extends Controller {
298
284
  style.transform = ""
299
285
  if (resizes || visible > largest) style.blockSize = `${visible}px`
300
286
  }
301
-
302
- if (!detents.some((detent) => detent.large)) return
303
- const clamp = (n) => Math.max(0, Math.min(1, n))
304
- if (detents.length > 1) {
305
- const p = clamp((visible - smallest) / (largest - smallest))
306
- this.setLargeness(p)
307
- this.setPage(p)
308
- } else {
309
- this.setPage(clamp(visible / largest))
310
- }
311
287
  }
312
288
 
313
289
  rubberBand(wanted, detents) {
@@ -341,49 +317,6 @@ export default class extends Controller {
341
317
  return parseFloat(getComputedStyle(this.element).maxBlockSize) || window.innerHeight
342
318
  }
343
319
 
344
- setLargeness(p) {
345
- this.element.style.setProperty("--lc-sheet-p", p.toFixed(3))
346
- }
347
-
348
- setPage(p) {
349
- const root = document.documentElement
350
- if (!this.ownsPage) {
351
- if (p <= 0) return
352
- this.releaseScroll = holdDocumentScroll()
353
- const lockedTop = -parseFloat(document.body.style.top || "0")
354
- root.style.setProperty("--lc-sheet-page-origin", `${window.scrollY + lockedTop}px`)
355
- root.dataset.lcSheetPage = ""
356
- this.ownsPage = true
357
- }
358
- root.style.setProperty("--lc-sheet-page-p", p.toFixed(3))
359
- root.style.setProperty("--lc-sheet-page-scale", (1 - PAGE_SCALE_LOSS * p).toFixed(4))
360
- }
361
-
362
- animatePage(p) {
363
- this.pageTransition()
364
- if (p > 0 && !this.ownsPage) {
365
- this.setPage(0.001)
366
- requestAnimationFrame(() => requestAnimationFrame(() => this.setPage(p)))
367
- } else {
368
- this.setPage(p)
369
- }
370
- }
371
-
372
- releasePage() {
373
- if (!this.ownsPage) return
374
- this.ownsPage = false
375
- const root = document.documentElement
376
- delete root.dataset.lcSheetPage
377
- for (const name of ["p", "scale", "origin", "transition"])
378
- root.style.removeProperty(`--lc-sheet-page-${name}`)
379
- this.releaseScroll()
380
- }
381
-
382
- pageTransition(value = prefersReducedMotion() ? "0s" : SHEET_TIMING) {
383
- if (this.ownsPage || value !== "0s")
384
- document.documentElement.style.setProperty("--lc-sheet-page-transition", value)
385
- }
386
-
387
320
  transition() {
388
321
  const timing = prefersReducedMotion() ? "0s" : SHEET_TIMING
389
322
  this.element.style.transition = `transform ${timing}, block-size ${timing}, display ${timing} allow-discrete, overlay ${timing} allow-discrete`
@@ -405,7 +338,6 @@ export default class extends Controller {
405
338
  style.transform = ""
406
339
  style.willChange = ""
407
340
  style.blockSize = ""
408
- style.removeProperty("--lc-sheet-p")
409
341
  style.removeProperty("--lc-sheet-floor")
410
342
  }
411
343
 
@@ -9,12 +9,7 @@ import { liftToTopLayer, onPageLeave } from "lostcoords/ui/helpers/top_layer"
9
9
  export default class extends Controller {
10
10
  static targets = ["content"]
11
11
 
12
- connect() {
13
- this.stopPageLeave = onPageLeave(() => this.close({ immediate: true }))
14
- }
15
-
16
12
  disconnect() {
17
- this.stopPageLeave?.()
18
13
  this.close({ immediate: true })
19
14
  }
20
15
 
@@ -24,6 +19,7 @@ export default class extends Controller {
24
19
 
25
20
  this.element.toggleAttribute("data-open", true)
26
21
  this.release = liftToTopLayer(this.contentTarget, this.element)
22
+ this.stopPageLeave = onPageLeave(() => this.close({ immediate: true }))
27
23
  this.unregister = registerDismissable({
28
24
  type: "tooltip",
29
25
  element: this.element,
@@ -48,6 +44,8 @@ export default class extends Controller {
48
44
  this.element.toggleAttribute("data-open", false)
49
45
  this.release?.({ immediate })
50
46
  this.release = null
47
+ this.stopPageLeave?.()
48
+ this.stopPageLeave = null
51
49
  this.unregister?.()
52
50
  this.unregister = null
53
51
  }
@@ -468,7 +468,7 @@ export class CatalogPicker extends FloatingPanel {
468
468
  }
469
469
 
470
470
  get catalogElement() {
471
- return this.bodyTarget.querySelector("[data-picker-catalog]")
471
+ return this.hasBodyTarget ? this.bodyTarget.querySelector("[data-picker-catalog]") : null
472
472
  }
473
473
 
474
474
  get optionSelector() {
@@ -4,6 +4,10 @@ import { liftToTopLayer, onPageLeave } from "lostcoords/ui/helpers/top_layer"
4
4
 
5
5
  const BEFORE_OPEN = "overlay:before-open"
6
6
 
7
+ // A panel listens to the document only while it is open, and its teardown (disconnect, a page
8
+ // leave) never reads a target. A Turbo morph moves a panel's id-carrying trigger and panel into
9
+ // another host before Stimulus disconnects the emptied one, so a target read there would throw
10
+ // and leave the rest of the teardown undone.
7
11
  export class FloatingPanel extends Controller {
8
12
  static targets = ["trigger", "popover"]
9
13
  static values = { lift: { type: Boolean, default: true } }
@@ -20,18 +24,10 @@ export class FloatingPanel extends Controller {
20
24
  this.closeOnOutsideClick = (event) => {
21
25
  if (!this.element.contains(event.target)) this.close()
22
26
  }
23
- this.closeForPageLeave = () => this.closeNow({ immediate: true })
24
- }
25
-
26
- connect() {
27
- document.addEventListener(BEFORE_OPEN, this.closeForOther)
28
- this.stopPageLeave = onPageLeave(this.closeForPageLeave)
29
27
  }
30
28
 
31
29
  disconnect() {
32
- document.removeEventListener(BEFORE_OPEN, this.closeForOther)
33
- this.stopPageLeave?.()
34
- this.release({ immediate: true })
30
+ this.closeNow({ immediate: true })
35
31
  }
36
32
 
37
33
  toggle(event) {
@@ -58,6 +54,8 @@ export class FloatingPanel extends Controller {
58
54
  if (this.lifts) this.releaseLift = liftToTopLayer(this.popoverTarget, this.opener)
59
55
  this.triggerTarget.setAttribute("aria-expanded", "true")
60
56
 
57
+ document.addEventListener(BEFORE_OPEN, this.closeForOther)
58
+ this.stopPageLeave = onPageLeave(() => this.closeNow({ immediate: true }))
61
59
  if (this.constructor.closesOnOutsideClick)
62
60
  document.addEventListener("click", this.closeOnOutsideClick)
63
61
  this.unregisterDismissable = registerDismissable({
@@ -74,11 +72,12 @@ export class FloatingPanel extends Controller {
74
72
  }
75
73
 
76
74
  closeNow({ immediate = false } = {}) {
77
- if (!this.isOpen) return
78
-
79
- this.hide()
75
+ const wasOpen = this.isOpen
76
+ if (wasOpen) this.hide()
80
77
  this.release({ immediate })
81
- this.triggerTarget.setAttribute("aria-expanded", "false")
78
+ if (!wasOpen) return
79
+
80
+ if (this.hasTriggerTarget) this.triggerTarget.setAttribute("aria-expanded", "false")
82
81
  this.afterClose()
83
82
  }
84
83
 
@@ -87,6 +86,9 @@ export class FloatingPanel extends Controller {
87
86
  release({ immediate = false } = {}) {
88
87
  this.releaseLift?.({ immediate })
89
88
  this.releaseLift = null
89
+ document.removeEventListener(BEFORE_OPEN, this.closeForOther)
90
+ this.stopPageLeave?.()
91
+ this.stopPageLeave = null
90
92
  document.removeEventListener("click", this.closeOnOutsideClick)
91
93
  this.unregisterDismissable?.()
92
94
  this.unregisterDismissable = null
@@ -103,7 +105,7 @@ export class FloatingPanel extends Controller {
103
105
  }
104
106
 
105
107
  get isOpen() {
106
- return this.popoverTarget.hasAttribute("data-open")
108
+ return this.hasPopoverTarget && this.popoverTarget.hasAttribute("data-open")
107
109
  }
108
110
 
109
111
  get lifts() {
@@ -48,8 +48,6 @@
48
48
  aria-label="<%= label %>"
49
49
  hidden
50
50
  >
51
- <span class="lc-emoji-picker__handle" data-lc-sheet-target="handle" aria-hidden="true"></span>
52
-
53
51
  <div class="lc-emoji-picker__search">
54
52
  <div class="lc-emoji-picker__search-field">
55
53
  <svg class="lc-emoji-picker__search-icon" viewBox="0 0 20 20" fill="none" aria-hidden="true">
@@ -1,6 +1,6 @@
1
1
  <%# locals: (id:, attributes:) %>
2
- <%= lc_sheet(tag: :dialog, **attributes) do %>
3
- <form method="dialog" class="flex flex-col gap-inset p-inset max-sm:pt-7" data-slot="confirm-sheet-content">
2
+ <%= lc_sheet(as: :dialog, **attributes) do %>
3
+ <form method="dialog" class="flex flex-col gap-inset p-inset" data-slot="confirm-sheet-content">
4
4
  <div class="flex flex-col gap-tight max-sm:text-center">
5
5
  <p id="<%= id %>-message" class="<%= lc_heading_class(size: :subheading) %>" data-slot="confirm-sheet-message"></p>
6
6
  <p id="<%= id %>-description" class="<%= lc_text_class(size: :small, tone: :muted) %>" data-slot="confirm-sheet-description" hidden></p>
@@ -1,14 +1,17 @@
1
1
  <%# locals: (id:, title:, description:, trigger:, rendered_trigger:, detents:, close_action:, close_label:, attributes:, actions:, content:) %>
2
- <% dialog = lc_sheet(tag: :dialog, detents: detents, **attributes) do %>
3
- <div class="flex min-h-0 flex-col gap-inset p-inset sm:h-full sm:max-h-[inherit]" data-slot="dialog-content">
4
- <header class="flex shrink-0 items-start justify-between gap-content" data-slot="dialog-header">
2
+ <%# On phones the header is the sheet's title bar: pinned, the ✕ at its start and the title centred. %>
3
+ <% dialog = lc_sheet(as: :dialog, detents: detents, **attributes) do |sheet| %>
4
+ <div class="flex min-h-0 flex-col gap-inset p-inset max-sm:pt-0 sm:h-full sm:max-h-[inherit]" data-slot="dialog-content">
5
+ <header class="flex shrink-0 items-start justify-between gap-content max-sm:sticky max-sm:top-0 max-sm:z-sticky max-sm:-mx-inset max-sm:min-h-14 max-sm:flex-col max-sm:items-center max-sm:justify-center max-sm:bg-(--lc-sheet-bg) max-sm:px-14 max-sm:py-2" data-slot="dialog-header">
5
6
  <div class="flex min-w-0 flex-col gap-tight">
6
7
  <%= title %>
7
8
  <%= description %>
8
9
  </div>
9
- <div class="flex shrink-0 items-center gap-related -me-related -mt-tight" data-slot="dialog-actions">
10
- <%= actions %>
11
- <button type="button" class="<%= lc_icon_button_class(size: :sm, shape: :circle) %>" aria-label="<%= close_label %>" data-action="<%= close_action %>" data-slot="dialog-dismiss"><%= lc_icon "x-mark", options: { "aria-hidden": true } %></button>
10
+ <div class="flex shrink-0 items-center gap-related -me-related -mt-tight max-sm:contents" data-slot="dialog-actions">
11
+ <% if actions.present? %>
12
+ <div class="contents max-sm:absolute max-sm:end-3 max-sm:top-0 max-sm:flex max-sm:h-14 max-sm:items-center max-sm:gap-related"><%= actions %></div>
13
+ <% end %>
14
+ <%= sheet.close close_label, size: :sm, data: { action: close_action, slot: "dialog-dismiss" } %>
12
15
  </div>
13
16
  </header>
14
17
  <%# Children keep their height, so a long body scrolls instead of squashing a fixed-height button. %>