lostcoords-ui 0.14.13 → 0.14.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 729b5ab883cb06c1a4b067c3b0581b3743341a414dd18daf08c0af2b0a407eca
4
- data.tar.gz: 1fbe2ad618bc983a2f3ad4b5d86a5bdf6c2481755ec81161351d762118b38a5c
3
+ metadata.gz: dd77dbe9de01d08ee1a49ee39764c0e4de02307ea2be5a2f7eebe000aca2b0c4
4
+ data.tar.gz: 80ca196ac506c0a6410dc7b1d761517c4935b855f24b729d153f468bc752b1b8
5
5
  SHA512:
6
- metadata.gz: ac70042419af37f4ea4b5a5bc6c5d9e835420ac953d8b8225b31a6fff792952cb9b4f54a58075152e2cab7f795f5a771dbea737417d2691efaa83925aa766173
7
- data.tar.gz: 2cbf0a9ee9680c60c46bac6b88b82853308de0c36375b1b90b5d90bb4c26b22547c06d4740b1e015d71bdc71d4609770271181246c750496f0f3acfd00a6faa2
6
+ metadata.gz: 7458a749f77b100455d279d532b5ab6933aa7c6b10bedf399ed6910e84dbf813baec8058bd3c2e73b722c20e6f2362f2810cc657886f7c2e661bde49cdfbbe1a
7
+ data.tar.gz: '0324502035085506805b9c0fd65c985404061a52f3cf9a115851954a6b9e66cd01e34d49073da26a674381bae30c78ced352d3f21747b2139a9245aea67e2968'
data/README.md CHANGED
@@ -170,6 +170,22 @@ Omit `only` to register every library controller. Unknown identifiers raise befo
170
170
  registered, and repeated calls on the same application keep later overrides, so register defaults
171
171
  before application overrides.
172
172
 
173
+ ## Changes in 0.14.14
174
+
175
+ Nothing needs an edit; a host that waits on a dialog's animations after `close()` before clearing up can
176
+ use `closeDialog` instead. Added:
177
+
178
+ - **`closeDialog(dialog)`** in `lostcoords/ui/helpers/dialog` closes a dialog and settles once it has left.
179
+ In Safari `close()` now returns while the dialog is still open for its exit, so code that checked
180
+ `dialog.open` after its animations could find it open.
181
+ - **The `lc-light-dismiss` controller** comes with every `popover: true` or `popover: "auto"` an lc builder
182
+ renders. It closes the popover itself on a tap outside, Escape or a `popovertarget` hide, with its exit:
183
+ Safari dropped a native popover in the frame the browser closed it.
184
+ - **`data-lc-popover-close`** on a link or button inside such a popover closes it, with its exit, so an
185
+ in-page link in a menu needs no controller of its own.
186
+ - **`hidePopoverAfterExit(popover)`** in `lostcoords/ui/helpers/top_layer` hides a popover from your code
187
+ with its exit.
188
+
173
189
  ## Changes in 0.14.13
174
190
 
175
191
  Nothing needs an edit. Fixed:
@@ -673,7 +689,10 @@ your own controls with the `lc-disabled:` and `lc-pending:` variants, which tell
673
689
 
674
690
  Use native attributes for application behavior, `class:` to merge additional Tailwind utilities, and
675
691
  theme tokens to restyle the library. `popover: true` renders `popover="auto"`, as a bare `popover`
676
- attribute does. `lc_field` yields the input's `id` and validation attributes so
692
+ attribute does, with the `lc-light-dismiss` controller: it makes the popover manual once it connects and
693
+ closes it itself on a tap outside, Escape, a `popovertarget` button that hides it, or a click on an
694
+ element inside marked `data-lc-popover-close`, so its exit plays in Safari too. Without JavaScript it stays
695
+ a native auto popover. `lc_field` yields the input's `id` and validation attributes so
677
696
  labels, hints and errors stay connected. Linked cards use `lc_card(href: ...)`; cards containing
678
697
  buttons or fields remain ordinary containers.
679
698
 
@@ -1189,7 +1208,9 @@ it, and the page stops scrolling behind it.
1189
1208
  `registerDialog(dialog, { close, priority, restoreFocus, lockScroll })`: `close` may return `false` to keep the dialog open, such as while it asks to discard changes; `priority` ranks it against panels open in
1190
1209
  it (a dialog is 10, a panel 20); `restoreFocus` runs after a close; `lockScroll: false` leaves the page
1191
1210
  scrollable. It returns the unregister function and ends when the dialog closes; a click on the dialog's own
1192
- padding does not close it.
1211
+ padding does not close it. `closeDialog(dialog, returnValue)` closes it and returns a promise that settles
1212
+ once it has left, closed and with its exit over; clear up after a dialog (empty its frame, remove it) when
1213
+ that settles, not when `close()` returns, since Safari keeps a dialog open while its exit plays.
1193
1214
 
1194
1215
  ```erb
1195
1216
  <button type="button" popovertarget="filters">Filters</button>
@@ -1197,7 +1218,9 @@ padding does not close it.
1197
1218
  data: { action: "lc-sheet:dismiss->filters#reset" } do %>…<% end %>
1198
1219
  ```
1199
1220
 
1200
- `lc-popover-shell` gives a native `[popover]` menu the same enter and exit as FloatingPanel panels. Set
1221
+ `lc-popover-shell` gives a native `[popover]` menu the same enter and exit as FloatingPanel panels;
1222
+ `hidePopoverAfterExit(popover)` from `lostcoords/ui/helpers/top_layer` hides one from your own code with
1223
+ its exit, as `lc-light-dismiss` does. Set
1201
1224
  `--lc-popover-from` to change the closed pose and an `origin-*` utility for where it grows from.
1202
1225
 
1203
1226
  `lc_hovercard` previews what a link points to, such as a profile over a name, with the `lc-hovercard`
@@ -5,7 +5,7 @@
5
5
  @import "./theme.css";
6
6
  @source "../../../../helpers/lostcoords/ui/{avatar_badge,badge,button,card,choice,copy_button,description_list,feedback,file_drop,form,glass,icon,layout,list_group,navigation,primitives,render_attributes,structures,tailwind,translation,typography}_helper.rb";
7
7
  @source "../../../../javascript/lostcoords/ui/helpers/*.js";
8
- @source "../../../../javascript/lostcoords/ui/controllers/lc_{char_counter,copy,kbd,nav,scroll_keeper,selection,shortcut,tabs,toc,toggle}_controller.js";
8
+ @source "../../../../javascript/lostcoords/ui/controllers/lc_{char_counter,copy,kbd,light_dismiss,nav,scroll_keeper,selection,shortcut,tabs,toc,toggle}_controller.js";
9
9
  @source "../../../../../lib/lostcoords/ui/parts.rb";
10
10
 
11
11
  @import "./glass.css";
@@ -1,7 +1,8 @@
1
1
  /* Closed is the exit pose, so closing animates too; allow-discrete holds
2
2
  display/overlay until it ends. Open is FloatingPanel's [data-open] or a
3
3
  native [popover]'s :popover-open; a panel lifted to the top layer
4
- ([data-lc-lifted]) follows [data-open] alone. --lc-popover-from sets the closed pose;
4
+ ([data-lc-lifted]) follows [data-open] alone, and a popover playing its
5
+ exit before it hides ([data-closing], see hidePopoverAfterExit) is closed. --lc-popover-from sets the closed pose;
5
6
  origin-* utilities set where it grows from. */
6
7
  @utility lc-popover-shell {
7
8
  opacity: 0;
@@ -13,7 +14,7 @@
13
14
  display var(--lc-motion-feedback) allow-discrete,
14
15
  overlay var(--lc-motion-feedback) allow-discrete;
15
16
 
16
- &:is([data-open], :popover-open:not([data-lc-lifted])) {
17
+ &:is([data-open], :popover-open:not([data-lc-lifted])):not([data-closing]) {
17
18
  opacity: 1;
18
19
  transform: translateY(0) scale(1);
19
20
  pointer-events: auto;
@@ -29,7 +30,7 @@
29
30
  /* stylelint-disable-next-line no-invalid-position-declaration -- an @utility body nests like a rule */
30
31
  transform: none;
31
32
 
32
- &:is([data-open], :popover-open:not([data-lc-lifted])) {
33
+ &:is([data-open], :popover-open:not([data-lc-lifted])):not([data-closing]) {
33
34
  @starting-style { transform: none; }
34
35
  }
35
36
  }
@@ -65,9 +65,9 @@
65
65
  @starting-style { transform: translateY(var(--lc-sheet-offscreen)); }
66
66
  }
67
67
 
68
- /* Where `overlay` can't transition, lc-sheet holds a closing dialog open
69
- in its exit pose until the slide ends. */
70
- &[open][data-closing] { transform: translateY(var(--lc-sheet-offscreen)); }
68
+ /* Where `overlay` can't transition, a closing dialog or popover stays
69
+ open in its exit pose until the slide ends. */
70
+ &:is([open], :popover-open)[data-closing] { transform: translateY(var(--lc-sheet-offscreen)); }
71
71
 
72
72
  &::backdrop {
73
73
  background: var(--lc-scrim-sheet);
@@ -85,7 +85,7 @@
85
85
  @starting-style { opacity: 0; }
86
86
  }
87
87
 
88
- &[open][data-closing]::backdrop { opacity: 0; }
88
+ &:is([open], :popover-open)[data-closing]::backdrop { opacity: 0; }
89
89
 
90
90
  /* The sheet appears in place; lc-sheet's own slides already skip under reduced motion. */
91
91
  @media (prefers-reduced-motion: reduce) {
@@ -20,7 +20,16 @@ module Lostcoords::Ui
20
20
  merged[:data] ||= {}
21
21
  merged[:data][key] = [ defaults[:data][key], attributes.dig(:data, key) ].join(" ").split.uniq.join(" ")
22
22
  end
23
- merged
23
+ lc_light_dismiss(merged)
24
+ end
25
+
26
+ # An auto popover's closes are lc-light-dismiss's, so its exit plays in Safari too.
27
+ def lc_light_dismiss(attributes)
28
+ return attributes unless attributes[:popover] == "auto"
29
+
30
+ data = attributes[:data].is_a?(Hash) ? attributes[:data] : {}
31
+ controller = [ data[:controller], "lc-light-dismiss" ].compact.join(" ").split.uniq.join(" ")
32
+ attributes.merge(data: data.merge(controller: controller))
24
33
  end
25
34
 
26
35
  def lc_normalize_attributes(attributes)
@@ -0,0 +1,72 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
3
+ import { hidePopoverAfterExit } from "lostcoords/ui/helpers/top_layer"
4
+
5
+ // A popover the gem rendered as popover="auto", made manual when it connects so that every close is
6
+ // the gem's and plays the exit, which Safari skips when the browser closes a popover itself: a tap
7
+ // outside, Escape, a popovertarget button that hides it and a [data-lc-popover-close] element in it.
8
+ // Without JavaScript it stays a native auto popover.
9
+ export default class extends Controller {
10
+ connect() {
11
+ if (!this.element.matches(":popover-open")) this.element.popover = "manual"
12
+ this.onToggle = (event) => (event.newState === "open" ? this.opened() : this.closed())
13
+ this.onPointerDown = this.onPointerDown.bind(this)
14
+ this.onClick = this.onClick.bind(this)
15
+ this.element.addEventListener("toggle", this.onToggle)
16
+ if (this.element.matches(":popover-open")) this.opened()
17
+ }
18
+
19
+ disconnect() {
20
+ this.element.removeEventListener("toggle", this.onToggle)
21
+ this.closed()
22
+ }
23
+
24
+ hide() {
25
+ return hidePopoverAfterExit(this.element)
26
+ }
27
+
28
+ opened() {
29
+ this.element.removeAttribute("data-closing")
30
+ if (this.unregister) return
31
+ document.addEventListener("pointerdown", this.onPointerDown, true)
32
+ document.addEventListener("click", this.onClick, true)
33
+ this.unregister = registerDismissable({
34
+ element: this.element,
35
+ type: "popover",
36
+ close: () => this.hide(),
37
+ isOpen: () => this.element.matches(":popover-open:not([data-closing])"),
38
+ })
39
+ }
40
+
41
+ closed() {
42
+ document.removeEventListener("pointerdown", this.onPointerDown, true)
43
+ document.removeEventListener("click", this.onClick, true)
44
+ this.unregister?.()
45
+ this.unregister = null
46
+ }
47
+
48
+ // The popover's own buttons toggle it on click, so a press on one is not a press outside.
49
+ onPointerDown(event) {
50
+ if (this.element.contains(event.target) || this.invoker(event.target)) return
51
+ this.hide()
52
+ }
53
+
54
+ onClick(event) {
55
+ const invoker = this.invoker(event.target)
56
+ if (invoker) {
57
+ if (invoker.getAttribute("popovertargetaction") === "show") return
58
+ event.preventDefault()
59
+ this.hide()
60
+ } else if (
61
+ event.target.closest?.("[data-lc-popover-close]") &&
62
+ this.element.contains(event.target)
63
+ ) {
64
+ this.hide()
65
+ }
66
+ }
67
+
68
+ invoker(target) {
69
+ const button = target.closest?.("[popovertarget]")
70
+ return button?.popoverTargetElement === this.element ? button : null
71
+ }
72
+ }
@@ -30,6 +30,17 @@ export function showDialog(dialog) {
30
30
  dialog.focus({ preventScroll: true })
31
31
  }
32
32
 
33
+ // Closes a dialog and settles once it has left: closed, with its exit transition over. Where the
34
+ // browser can't transition a closing dialog (Safari), lc-sheet holds it open for its exit and closes
35
+ // it when that ends, so code that clears up after a dialog waits for this, not for close().
36
+ export function closeDialog(dialog, returnValue) {
37
+ if (!dialog.open) return Promise.resolve()
38
+ const closed = new Promise((resolve) => dialog.addEventListener("close", resolve, { once: true }))
39
+ dialog.close(returnValue)
40
+ const exits = dialog.getAnimations().map((animation) => animation.finished)
41
+ return Promise.allSettled([dialog.open ? closed : null, ...exits]).then(() => {})
42
+ }
43
+
33
44
  // Joins a <dialog> to the dismiss stack: Escape, the backdrop and a swipe call close, which may
34
45
  // return false to keep it open, such as while it asks whether to discard changes. priority ranks
35
46
  // it against panels open in it (a dialog is 10, a panel 20); restoreFocus runs after a close;
@@ -36,6 +36,28 @@ export function hideTopLayer(element) {
36
36
  if (supportsTopLayer() && element.matches(":popover-open")) element.hidePopover()
37
37
  }
38
38
 
39
+ // Hides a popover once its exit has played, and settles when it is hidden. Where `overlay` can't
40
+ // transition, the popover stays open in its exit pose ([data-closing]) until the exit ends.
41
+ export function hidePopoverAfterExit(element) {
42
+ if (!supportsTopLayer() || !element.matches(":popover-open")) return Promise.resolve()
43
+ if (EXITS_NATIVELY) {
44
+ element.hidePopover()
45
+ return exitOf(element)
46
+ }
47
+ if (element.hasAttribute("data-closing")) return exitOf(element)
48
+
49
+ element.toggleAttribute("data-closing", true)
50
+ return exitOf(element).then(() => {
51
+ // Shown again meanwhile: the exit was called off.
52
+ if (!element.hasAttribute("data-closing")) return
53
+ element.removeAttribute("data-closing")
54
+ hideTopLayer(element)
55
+ })
56
+ }
57
+
58
+ const exitOf = (element) =>
59
+ Promise.allSettled(element.getAnimations().map((animation) => animation.finished)).then(() => {})
60
+
39
61
  // Calls close before Turbo snapshots or leaves the page, so a cached page never
40
62
  // keeps an open overlay. Returns a function that removes the listeners.
41
63
  export function onPageLeave(close) {
@@ -8,6 +8,7 @@ import Dock from "lostcoords/ui/controllers/lc_dock_controller"
8
8
  import EmojiPicker from "lostcoords/ui/controllers/lc_emoji_picker_controller"
9
9
  import Hovercard from "lostcoords/ui/controllers/lc_hovercard_controller"
10
10
  import Kbd from "lostcoords/ui/controllers/lc_kbd_controller"
11
+ import LightDismiss from "lostcoords/ui/controllers/lc_light_dismiss_controller"
11
12
  import Menu from "lostcoords/ui/controllers/lc_menu_controller"
12
13
  import Nav from "lostcoords/ui/controllers/lc_nav_controller"
13
14
  import Popover from "lostcoords/ui/controllers/lc_popover_controller"
@@ -35,6 +36,7 @@ const controllers = {
35
36
  "lc-emoji-picker": EmojiPicker,
36
37
  "lc-hovercard": Hovercard,
37
38
  "lc-kbd": Kbd,
39
+ "lc-light-dismiss": LightDismiss,
38
40
  "lc-menu": Menu,
39
41
  "lc-nav": Nav,
40
42
  "lc-popover": Popover,
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.14.13"
3
+ VERSION = "0.14.14"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lostcoords-ui
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.14.13
4
+ version: 0.14.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lost Coordinates
@@ -171,6 +171,7 @@ files:
171
171
  - app/javascript/lostcoords/ui/controllers/lc_emoji_picker_controller.js
172
172
  - app/javascript/lostcoords/ui/controllers/lc_hovercard_controller.js
173
173
  - app/javascript/lostcoords/ui/controllers/lc_kbd_controller.js
174
+ - app/javascript/lostcoords/ui/controllers/lc_light_dismiss_controller.js
174
175
  - app/javascript/lostcoords/ui/controllers/lc_menu_controller.js
175
176
  - app/javascript/lostcoords/ui/controllers/lc_nav_controller.js
176
177
  - app/javascript/lostcoords/ui/controllers/lc_popover_controller.js