lostcoords-ui 0.14.5 → 0.14.6

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: e061ef0f5e56280a6a307f83c8973c5ee67f559e51fad6c204dbb0e4a1f14b06
4
- data.tar.gz: eb9d51da41159c64e095c9a2b278062056c1551f942a99b8db284e08f2faf09a
3
+ metadata.gz: b9905bd1837028339420ab4601d23e1499a6a9ba355b694707fa79284dad63fd
4
+ data.tar.gz: 7f53b12ddc748f8aa39d820af1a278b07d9cdcbafca0eef0392258352f3eacdf
5
5
  SHA512:
6
- metadata.gz: 5b5bfe64afe74ec39952deb44ba4e61b81bac7ea1ced30bd5e8e49ba712cc30df155f62385515ca837eeadf117a1e1ebf3ec70654714eac8baa6db1b604d01fb
7
- data.tar.gz: c23667fe2803b558ffddc30e92f1195bacfa833c74d19461b42d9d66a5a45f866ab8d9f1c9b17d0fd263dd10ffb4f9449d057ff65e884bc7f86814ba8126db97
6
+ metadata.gz: 923ff077a975fd90c19daf1a186a1a334516dacb0351e89fb1094a13016b86068d21e0f3e5cabd52c70c72d5d3a3f58e97909dc6b299d3de2c0f950c394c4f5e
7
+ data.tar.gz: 73ed37232a4ac32ecf122f81df0e039bc8b9040f6812a343fa79f25e214a90d2255c41ae35c08c577590c2d5f458fa35e836624f7a50c9d82719fa185bdec007
data/README.md CHANGED
@@ -170,6 +170,17 @@ 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.6
174
+
175
+ Nothing needs an edit. Fixed:
176
+
177
+ - **Focus returns to the pressed control in Safari.** Safari on a Mac doesn't focus a button or link on
178
+ click, so the browser had nothing to hand focus back to when a dialog closed and it fell to the page.
179
+ `registerDialog` now remembers what was pressed and returns focus there when closing leaves it nowhere.
180
+ - **`showContextDialog` opens through `showDialog`**, so a dialog anchored to a menu takes focus itself, as
181
+ every other dialog does, instead of ringing its first control. The confirm sheet no longer repeats the
182
+ step.
183
+
173
184
  ## Changes in 0.14.5
174
185
 
175
186
  One edit, only for a host that skips `base.css`:
@@ -1152,7 +1163,9 @@ Every interactive component works from the keyboard without extra code:
1152
1163
  The focus ring follows how the person is moving. Focus that comes back from a closed dialog, sheet, menu
1153
1164
  or popover shows its ring only after Tab or an arrow key moved focus; Escape, Enter in a field and typing
1154
1165
  don't count, so a dialog opened with the mouse and closed with Escape leaves no ring on its button. A panel
1155
- closed from inside, such as by choosing a menu item, returns focus to what opened it. A controller that
1166
+ closed from inside, such as by choosing a menu item, returns focus to what opened it, and a dialog
1167
+ closed with nothing focused before it, as after a click in Safari, returns focus to the control that was
1168
+ pressed. A controller that
1156
1169
  moves focus back itself calls `settleFocus()` from `lostcoords/ui/helpers/focus` afterwards for the same
1157
1170
  rule. Hosts that skip `base.css` hide the ring on `[data-lc-focus-quiet]:focus-visible`.
1158
1171
 
@@ -50,8 +50,5 @@ export function ask(
50
50
  dialog.style.removeProperty("--lc-dialog-origin")
51
51
  showContextDialog(dialog, anchor)
52
52
  registerDialog(dialog, { lockScroll: false })
53
- // showModal focuses Cancel, and a confirm raised while typing would hand it the field's focus ring.
54
- // The sheet itself takes focus; Tab still reaches both buttons.
55
- dialog.focus({ preventScroll: true })
56
53
  })
57
54
  }
@@ -1,10 +1,11 @@
1
+ import { showDialog } from "lostcoords/ui/helpers/dialog"
1
2
  import { isSheetViewport } from "lostcoords/ui/helpers/viewport"
2
3
 
3
4
  const EDGE = 16
4
5
 
5
6
  export function showContextDialog(dialog, menu) {
6
7
  delete dialog.dataset.anchored
7
- if (!dialog.open) dialog.showModal()
8
+ showDialog(dialog)
8
9
 
9
10
  const rect = menu?.getBoundingClientRect()
10
11
  const corner = rect?.height
@@ -1,5 +1,5 @@
1
1
  import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
2
- import { settleFocus } from "lostcoords/ui/helpers/focus"
2
+ import { currentOpener, returnFocus, settleFocus } from "lostcoords/ui/helpers/focus"
3
3
 
4
4
  const registrations = new WeakMap()
5
5
 
@@ -42,6 +42,8 @@ export function registerDialog(
42
42
  const existing = registrations.get(dialog)
43
43
  if (existing) return existing
44
44
 
45
+ const opener = currentOpener(dialog)
46
+
45
47
  const unregisterDismissable = registerDismissable({
46
48
  element: dialog,
47
49
  type: "lc-dialog",
@@ -64,10 +66,12 @@ export function registerDialog(
64
66
  if (!inside) close(event)
65
67
  }
66
68
  // A dialog closed and shown again in the same task fires close while it is already open again.
67
- // By now the browser has handed focus back to what had it before the dialog opened.
69
+ // By now the browser has handed focus back to what had it before the dialog opened, if
70
+ // anything had.
68
71
  const unregisterOnClose = () => {
69
72
  if (dialog.open) return
70
73
  unregister()
74
+ returnFocus(opener, dialog)
71
75
  settleFocus()
72
76
  }
73
77
  const unregister = () => {
@@ -5,11 +5,14 @@ import { isEditable } from "lostcoords/ui/helpers/roving_focus"
5
5
  // focus back to its opener with a ring nobody asked for. Only moving focus by key counts here.
6
6
  const MOVES = new Set(["Tab", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End"])
7
7
  const QUIET = "data-lc-focus-quiet"
8
+ const CONTROL = "a[href], button, summary, [tabindex]"
8
9
  let byKeyboard = false
10
+ let pressed = null
9
11
 
10
12
  document.addEventListener(
11
13
  "keydown",
12
14
  (event) => {
15
+ pressed = null
13
16
  if (event.key === "Tab" || (MOVES.has(event.key) && !isEditable(event.target)))
14
17
  byKeyboard = true
15
18
  },
@@ -17,8 +20,9 @@ document.addEventListener(
17
20
  )
18
21
  document.addEventListener(
19
22
  "pointerdown",
20
- () => {
23
+ (event) => {
21
24
  byKeyboard = false
25
+ pressed = event.target.closest?.(CONTROL) ?? null
22
26
  },
23
27
  true,
24
28
  )
@@ -27,6 +31,23 @@ document.addEventListener("turbo:before-morph-attribute", (event) => {
27
31
  if (event.detail.attributeName === QUIET) event.preventDefault()
28
32
  })
29
33
 
34
+ // What opened whatever is opening now: the element with focus, or the control just pressed.
35
+ // Safari on a Mac doesn't focus a button or link on click, so there the browser has nothing to
36
+ // hand focus back to when a dialog closes.
37
+ export function currentOpener(overlay) {
38
+ const active = document.activeElement
39
+ if (active && active !== document.body && !overlay.contains(active)) return active
40
+ return pressed && !overlay.contains(pressed) ? pressed : null
41
+ }
42
+
43
+ // Focus goes back to opener when closing overlay left it nowhere: on the body, or still inside.
44
+ export function returnFocus(opener, overlay) {
45
+ const active = document.activeElement
46
+ const lost = !active || active === document.body || overlay.contains(active)
47
+ if (lost && opener?.isConnected && opener.getClientRects().length > 0)
48
+ opener.focus({ preventScroll: true })
49
+ }
50
+
30
51
  // Call after focus has come back from a closed dialog, menu or panel. Unless the person is moving
31
52
  // focus by keyboard, the element that has it keeps it without the ring until focus moves on.
32
53
  // A text field always shows its ring, as it does on a click.
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.14.5"
3
+ VERSION = "0.14.6"
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.5
4
+ version: 0.14.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lost Coordinates