lostcoords-ui 0.14.3 → 0.14.5

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: 27c237af634ec60537ff964703faf62608340f24f83d6ee9224f80c0b9649620
4
- data.tar.gz: 7cf3f448f87b9ed7ec8bb6d670378c59d0320fa673ba7badc64bb9dcbf9f6f7e
3
+ metadata.gz: e061ef0f5e56280a6a307f83c8973c5ee67f559e51fad6c204dbb0e4a1f14b06
4
+ data.tar.gz: eb9d51da41159c64e095c9a2b278062056c1551f942a99b8db284e08f2faf09a
5
5
  SHA512:
6
- metadata.gz: 1fab12510a1d750e90711815773c613aeaed71eea35d679894a6370b22aaebadee9b84e665894136444f8b43f7668f7e1d632f5abc808767a23c73fe177ba6c4
7
- data.tar.gz: b2c5ac6c41c1d8eeebc05d5de53e6c358d20b5ee0882c11c7cebedc4bb8e1bc56fc96ba08a62157a1e1b48a742540b467a4063d23826761585c63ce6d0b09fe0
6
+ metadata.gz: 5b5bfe64afe74ec39952deb44ba4e61b81bac7ea1ced30bd5e8e49ba712cc30df155f62385515ca837eeadf117a1e1ebf3ec70654714eac8baa6db1b604d01fb
7
+ data.tar.gz: c23667fe2803b558ffddc30e92f1195bacfa833c74d19461b42d9d66a5a45f866ab8d9f1c9b17d0fd263dd10ffb4f9449d057ff65e884bc7f86814ba8126db97
data/README.md CHANGED
@@ -170,6 +170,36 @@ 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.5
174
+
175
+ One edit, only for a host that skips `base.css`:
176
+
177
+ - **The focus ring is drawn in one place, `lc-focus-ring`.** Fields and the emoji picker's search drew their
178
+ own copy as a box shadow, and a linked list row drew a third with a different offset. They now take the
179
+ shared ring: `base.css` applies it to whatever holds keyboard focus, and the list row applies the same
180
+ utility while its link has focus. A host with its own base styles gets its own focus style on fields; to
181
+ keep the library's, add `:is(input, textarea, select):focus-visible { @apply lc-focus-ring; }`.
182
+
183
+ Faster:
184
+
185
+ - **The emoji catalog loads when someone reaches for the picker** (pointer, touch or focus), not with every
186
+ page that has one. It is every emoji there is, about 900KB of markup; opening the picker waits for the
187
+ same request.
188
+
189
+ ## Changes in 0.14.4
190
+
191
+ Nothing needs an edit. Focus behaves more like the platform:
192
+
193
+ - **Returned focus shows its ring only for keyboard users.** Browsers ring whatever is focused after a key
194
+ press, and Escape, Enter in a field and typing are key presses, so a dialog opened with the mouse and
195
+ closed with Escape left a ring on its button. Focus that comes back from a closed dialog, sheet, menu,
196
+ popover or search now shows the ring only after Tab or an arrow key moved focus. A controller that moves
197
+ focus back itself calls the new **`settleFocus()`** from `lostcoords/ui/helpers/focus`.
198
+ - **A panel closed from inside returns focus to what opened it.** Choosing a menu item or an emoji left
199
+ focus on a hidden element, so it fell to the page.
200
+ - **The emoji picker's search halo reads `--ring`** like every other field, not a `--focus-halo` variable
201
+ the library never defined.
202
+
173
203
  ## Changes in 0.14.3
174
204
 
175
205
  Nothing needs an edit, and one look changes:
@@ -1119,6 +1149,18 @@ Every interactive component works from the keyboard without extra code:
1119
1149
  | Emoji picker | Arrow Down from the search enters the grid, arrows move through it by row, Arrow Up from the top row returns to the search, and Enter in the search picks the best match. The grid is one Tab stop. |
1120
1150
  | Dialogs, sheets, popovers, menus, tooltips | Escape closes the topmost one and returns focus to what opened it; an open tooltip closes before the menu or dialog around it. Escape in a search field with text clears it first. |
1121
1151
 
1152
+ The focus ring follows how the person is moving. Focus that comes back from a closed dialog, sheet, menu
1153
+ or popover shows its ring only after Tab or an arrow key moved focus; Escape, Enter in a field and typing
1154
+ 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
1156
+ moves focus back itself calls `settleFocus()` from `lostcoords/ui/helpers/focus` afterwards for the same
1157
+ rule. Hosts that skip `base.css` hide the ring on `[data-lc-focus-quiet]:focus-visible`.
1158
+
1159
+ The ring itself is the `lc-focus-ring` utility: two units of `--ring` hugging the element's own radius.
1160
+ `base.css` draws it on every focusable element, so a component never restates an outline. One whose ring
1161
+ belongs on another element applies the utility through a variant, as a linked list row does with
1162
+ `has-[[data-slot=list-link]:focus-visible]:lc-focus-ring`.
1163
+
1122
1164
  Page-wide shortcuts focus a field or press a button. Merge `lc_shortcut_data` into the element's data and
1123
1165
  show the keys with `lc_kbd`:
1124
1166
 
@@ -24,13 +24,14 @@
24
24
 
25
25
  :where([popover]) { color: inherit; }
26
26
 
27
- :is(a, button, input, textarea, select, summary, [tabindex]:not(dialog)):focus-visible,
27
+ /* data-lc-focus-quiet: focus handed back by a closed dialog or panel while the person is not
28
+ on the keyboard (helpers/focus). */
29
+ :is(a, button, input, textarea, select, summary, [tabindex]:not(dialog)):focus-visible:not([data-lc-focus-quiet]),
28
30
  label:has(input.sr-only:focus-visible, + input.sr-only:focus-visible) {
29
- outline: calc(var(--spacing) * 0.5) solid var(--ring, color-mix(in oklab, var(--primary) 50%, transparent));
30
- outline-offset: 0;
31
+ @apply lc-focus-ring;
31
32
  }
32
33
 
33
- dialog:focus-visible { outline: none; }
34
+ dialog:focus-visible, [data-lc-focus-quiet]:focus-visible { outline: none; }
34
35
 
35
36
  :where(code):not(:where(pre code)) {
36
37
  @apply lc-inline-code;
@@ -172,11 +172,6 @@
172
172
  -webkit-appearance: none;
173
173
  appearance: none;
174
174
  }
175
-
176
- &:focus {
177
- outline: none;
178
- box-shadow: var(--focus-halo, 0 0 0 2px color-mix(in oklab, var(--primary) 50%, transparent));
179
- }
180
175
  }
181
176
 
182
177
  /* ---- Skin-tone control ------------------------------------------- */
@@ -5,6 +5,14 @@
5
5
  @custom-variant lc-disabled (&:disabled:not([aria-busy="true"]));
6
6
  @custom-variant lc-pending (&:disabled[aria-busy="true"]);
7
7
 
8
+ /* The focus ring, drawn here and nowhere else. base.css puts it on whatever holds keyboard
9
+ focus; a component whose ring belongs on another element, such as a row around its link,
10
+ applies it through a variant. */
11
+ @utility lc-focus-ring {
12
+ outline: calc(var(--spacing) * 0.5) solid var(--ring, color-mix(in oklab, var(--primary) 50%, transparent));
13
+ outline-offset: 0;
14
+ }
15
+
8
16
  /* A control gives a little as it is pressed, unless it is disabled. */
9
17
  @utility lc-press {
10
18
  @media (prefers-reduced-motion: no-preference) {
@@ -36,9 +36,8 @@ module Lostcoords::Ui
36
36
  "w-full max-w-full min-w-0 " \
37
37
  "px-3 " \
38
38
  "font-sans text-base bg-(--lc-field-bg,var(--input)) text-foreground placeholder:text-muted-foreground " \
39
- "border border-transparent rounded-xl lc-squircle outline-none " \
39
+ "border border-transparent rounded-xl lc-squircle " \
40
40
  "transition-[border-color,box-shadow] duration-(--lc-motion-quick) ease-(--lc-ease-out) " \
41
- "focus:shadow-[0_0_0_calc(var(--spacing)*0.5)_var(--ring,color-mix(in_oklab,var(--primary)_50%,transparent))] " \
42
41
  "aria-invalid:border-destructive aria-invalid:shadow-[0_0_0_3px_color-mix(in_oklab,var(--destructive)_16%,transparent)]"
43
42
 
44
43
  # Text and nothing else: for a control inside a row that shows focus itself, such as lc_list_field.
@@ -14,8 +14,7 @@ module Lostcoords::Ui
14
14
  # A linked li or div row: the title's link covers the row, the row's own controls sit above it,
15
15
  # and the row shows the link's focus ring.
16
16
  LIST_ROW_LINKED =
17
- "relative has-[[data-slot=list-link]:focus-visible]:outline-2 has-[[data-slot=list-link]:focus-visible]:outline-offset-2 " \
18
- "has-[[data-slot=list-link]:focus-visible]:outline-(--ring,color-mix(in_oklab,var(--primary)_50%,transparent)) " \
17
+ "relative has-[[data-slot=list-link]:focus-visible]:lc-focus-ring " \
19
18
  "[&_:is(a[href],button,input,select,textarea,summary,[tabindex]):not([data-slot=list-link])]:relative " \
20
19
  "[&_:is(a[href],button,input,select,textarea,summary,[tabindex]):not([data-slot=list-link])]:z-raised"
21
20
  LIST_LINK = "text-inherit no-underline focus-visible:outline-none after:absolute after:inset-0 after:rounded-[inherit] after:content-['']"
@@ -1,11 +1,13 @@
1
1
  import { CatalogPicker } from "lostcoords/ui/helpers/catalog_picker"
2
2
  import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
3
+ import { settleFocus } from "lostcoords/ui/helpers/focus"
3
4
  import { menuItems, navigateMenu } from "lostcoords/ui/helpers/menu_keys"
4
5
  import { navigate } from "lostcoords/ui/helpers/roving_focus"
5
6
  import { prefersReducedMotion } from "lostcoords/ui/helpers/viewport"
6
7
 
7
8
  const TONE_MODIFIERS = ["", "\u{1F3FB}", "\u{1F3FC}", "\u{1F3FD}", "\u{1F3FE}", "\u{1F3FF}"]
8
9
  const TONE_STORAGE_KEY = "lostcoords_ui:lc-emoji-picker:tone"
10
+ const REACH = ["pointerenter", "touchstart", "focusin"]
9
11
 
10
12
  function markCurrent(element, current) {
11
13
  element.setAttribute("aria-current", String(current))
@@ -35,10 +37,15 @@ export default class extends CatalogPicker {
35
37
  super.connect()
36
38
  this.tone = this.readTone()
37
39
  this.syncTones()
38
- this.preloadCatalog()
40
+ // The catalog is every emoji there is. It loads once someone reaches for the picker, not
41
+ // with each page that has one: opening it waits for the same request.
42
+ this.reach = () => this.fetchCatalog()
43
+ for (const type of REACH)
44
+ this.element.addEventListener(type, this.reach, { once: true, passive: true })
39
45
  }
40
46
 
41
47
  disconnect() {
48
+ for (const type of REACH) this.element.removeEventListener(type, this.reach)
42
49
  this.unregisterTones?.()
43
50
  this.clearPopover()
44
51
  super.disconnect()
@@ -202,7 +209,10 @@ export default class extends CatalogPicker {
202
209
  this.unregisterTones = null
203
210
  const hadFocus = this.toneMenuTarget.contains(document.activeElement)
204
211
  this.toneMenuTarget.hidden = true
205
- if (hadFocus) this.toneToggleTarget.focus({ preventScroll: true })
212
+ if (hadFocus) {
213
+ this.toneToggleTarget.focus({ preventScroll: true })
214
+ settleFocus()
215
+ }
206
216
  this.toneToggleTarget.setAttribute("aria-expanded", "false")
207
217
  }
208
218
 
@@ -1,5 +1,6 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
2
  import { registerDialog } from "lostcoords/ui/helpers/dialog"
3
+ import { settleFocus } from "lostcoords/ui/helpers/focus"
3
4
  import { prefersReducedMotion } from "lostcoords/ui/helpers/viewport"
4
5
 
5
6
  // Full-screen search. Each query dispatches lc-search:query with { query, results }; a listener
@@ -85,6 +86,7 @@ export default class extends Controller {
85
86
  this.reset()
86
87
  this.returnFocus?.focus({ preventScroll: true })
87
88
  this.returnFocus = null
89
+ settleFocus()
88
90
  }
89
91
 
90
92
  // A click on the backdrop closes search, and so does choosing a result, which matters for
@@ -1,4 +1,5 @@
1
1
  import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
2
+ import { settleFocus } from "lostcoords/ui/helpers/focus"
2
3
 
3
4
  const registrations = new WeakMap()
4
5
 
@@ -63,8 +64,11 @@ export function registerDialog(
63
64
  if (!inside) close(event)
64
65
  }
65
66
  // 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.
66
68
  const unregisterOnClose = () => {
67
- if (!dialog.open) unregister()
69
+ if (dialog.open) return
70
+ unregister()
71
+ settleFocus()
68
72
  }
69
73
  const unregister = () => {
70
74
  if (registrations.get(dialog) !== unregister) return
@@ -1,3 +1,5 @@
1
+ import { settleFocus } from "lostcoords/ui/helpers/focus"
2
+
1
3
  const stack = []
2
4
  let nextOrder = 0
3
5
  let lockedScrollY = 0
@@ -83,6 +85,7 @@ function closeEntry(entry, event) {
83
85
  if (closed === false) return
84
86
 
85
87
  entry.restoreFocus?.()
88
+ settleFocus()
86
89
  }
87
90
 
88
91
  function topDismissable(scope = topModal()?.element) {
@@ -1,5 +1,6 @@
1
1
  import { Controller } from "@hotwired/stimulus"
2
2
  import { registerDismissable } from "lostcoords/ui/helpers/dismiss_stack"
3
+ import { settleFocus } from "lostcoords/ui/helpers/focus"
3
4
  import { liftToTopLayer, onPageLeave } from "lostcoords/ui/helpers/top_layer"
4
5
 
5
6
  const BEFORE_OPEN = "overlay:before-open"
@@ -73,10 +74,18 @@ export class FloatingPanel extends Controller {
73
74
 
74
75
  closeNow({ immediate = false } = {}) {
75
76
  const wasOpen = this.isOpen
77
+ // Focus left on a hidden item would fall to the body: a panel closed from inside, such as by
78
+ // choosing a menu item, hands focus back to what opened it.
79
+ const heldFocus = wasOpen && !immediate && this.popoverTarget.contains(document.activeElement)
76
80
  if (wasOpen) this.hide()
77
81
  this.release({ immediate })
78
82
  if (!wasOpen) return
79
83
 
84
+ if (heldFocus) {
85
+ this.focusOnClose?.focus({ preventScroll: true })
86
+ settleFocus()
87
+ }
88
+
80
89
  if (this.hasTriggerTarget) this.triggerTarget.setAttribute("aria-expanded", "false")
81
90
  this.afterClose()
82
91
  }
@@ -0,0 +1,39 @@
1
+ import { isEditable } from "lostcoords/ui/helpers/roving_focus"
2
+
3
+ // Browsers show the focus ring on anything focused after a key press, and Escape, Enter in a
4
+ // field and typing are key presses: a dialog opened with the mouse and closed with Escape hands
5
+ // focus back to its opener with a ring nobody asked for. Only moving focus by key counts here.
6
+ const MOVES = new Set(["Tab", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End"])
7
+ const QUIET = "data-lc-focus-quiet"
8
+ let byKeyboard = false
9
+
10
+ document.addEventListener(
11
+ "keydown",
12
+ (event) => {
13
+ if (event.key === "Tab" || (MOVES.has(event.key) && !isEditable(event.target)))
14
+ byKeyboard = true
15
+ },
16
+ true,
17
+ )
18
+ document.addEventListener(
19
+ "pointerdown",
20
+ () => {
21
+ byKeyboard = false
22
+ },
23
+ true,
24
+ )
25
+ // A same-URL refresh morphs the page against server HTML, which never carries the mark.
26
+ document.addEventListener("turbo:before-morph-attribute", (event) => {
27
+ if (event.detail.attributeName === QUIET) event.preventDefault()
28
+ })
29
+
30
+ // Call after focus has come back from a closed dialog, menu or panel. Unless the person is moving
31
+ // focus by keyboard, the element that has it keeps it without the ring until focus moves on.
32
+ // A text field always shows its ring, as it does on a click.
33
+ export function settleFocus() {
34
+ const element = document.activeElement
35
+ if (byKeyboard || !element || element === document.body || isEditable(element)) return
36
+
37
+ element.setAttribute(QUIET, "")
38
+ element.addEventListener("blur", () => element.removeAttribute(QUIET), { once: true })
39
+ }
@@ -1,5 +1,5 @@
1
1
  module Lostcoords
2
2
  module Ui
3
- VERSION = "0.14.3"
3
+ VERSION = "0.14.5"
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.3
4
+ version: 0.14.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lost Coordinates
@@ -192,6 +192,7 @@ files:
192
192
  - app/javascript/lostcoords/ui/helpers/dialog.js
193
193
  - app/javascript/lostcoords/ui/helpers/dismiss_stack.js
194
194
  - app/javascript/lostcoords/ui/helpers/floating_panel.js
195
+ - app/javascript/lostcoords/ui/helpers/focus.js
195
196
  - app/javascript/lostcoords/ui/helpers/menu_keys.js
196
197
  - app/javascript/lostcoords/ui/helpers/roving_focus.js
197
198
  - app/javascript/lostcoords/ui/helpers/sheet_motion.js