phlex-reactive 0.19.3 → 0.19.4

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.
@@ -248,6 +248,19 @@ export { registerReactiveStreamRender as registerReactiveDismiss, registerReacti
248
248
  // the new content needs, in both entries.
249
249
  const STREAMED = "reactive:streamed"
250
250
 
251
+ // --- reactive_listnav highlight mode (issue #492) ---
252
+ // Per trigger, the query its last sync saw (a changed one puts a highlight:
253
+ // :first highlight back on the first option), and the counter for an option
254
+ // id aria-activedescendant can name.
255
+ const listnavQueries = new WeakMap()
256
+ let listnavIds = 0
257
+
258
+ // A keydown an IME is composing with (its Enter commits the composition, its
259
+ // arrows move among the candidates): never a listnav or tags key.
260
+ function imeComposing(event) {
261
+ return !!event?.isComposing || event?.keyCode === 229
262
+ }
263
+
251
264
  function streamIntoRoots(event) {
252
265
  const detail = event.detail
253
266
  const render = detail?.render
@@ -2725,7 +2738,7 @@ function bindingsNeeded(root) {
2725
2738
  ) ||
2726
2739
  (root.querySelectorAll?.("[data-reactive-show-field], [data-reactive-show]") ?? []).length > 0 ||
2727
2740
  root.querySelector?.(
2728
- "[data-reactive-nested-json], [data-reactive-nested-list], [data-reactive-confirm-when-param], [data-reactive-enable], [data-reactive-select-all], [data-reactive-count], [data-reactive-editable], [data-reactive-media], [data-reactive-css-vars]",
2741
+ "[data-reactive-nested-json], [data-reactive-nested-list], [data-reactive-confirm-when-param], [data-reactive-enable], [data-reactive-select-all], [data-reactive-count], [data-reactive-editable], [data-reactive-media], [data-reactive-css-vars], [data-reactive-listnav-highlight-param]",
2729
2742
  ) != null
2730
2743
  )
2731
2744
  }
@@ -3608,6 +3621,8 @@ export default class extends Controller {
3608
3621
  (name, args) => this.#diagnoseZeroTargets(`client op "${name}"`, args),
3609
3622
  ),
3610
3623
  listnavOptions: (event) => this.#listnavOptions(event),
3624
+ // After a filter or tags pass changed which options show (issue #492).
3625
+ syncListnav: () => this.#syncListnav(),
3611
3626
  collectFields: () => this.#collectFields(),
3612
3627
  // A reactive_editable's value and its last server-rendered text (#351),
3613
3628
  // and whether the engine has contenteditable="plaintext-only" (#355).
@@ -4254,7 +4269,7 @@ export default class extends Controller {
4254
4269
  // would have. tagsAdd keeps its two composition guards (an Enter that
4255
4270
  // listnav already took, or is about to take) in the same tick.
4256
4271
  tagsAdd(event) {
4257
- if (event?.defaultPrevented) return
4272
+ if (event?.defaultPrevented || imeComposing(event)) return
4258
4273
  if (this.#listnavOptions(event).some((el) => el.hasAttribute?.("data-reactive-highlighted"))) return
4259
4274
  return __SPLIT__
4260
4275
  ? this.#bindingsAction("tagsAdd", event)
@@ -4332,27 +4347,42 @@ export default class extends Controller {
4332
4347
  this.#listnavMove(event, (current, length) => (current < 0 ? length - 1 : (current - 1 + length) % length))
4333
4348
  }
4334
4349
 
4350
+ // Home/End. In highlight mode (issue #492, bound with highlight: :first only)
4351
+ // a plain one moves the caret while the query is filled; ctrl always moves.
4335
4352
  listnavFirst(event) {
4336
- this.#listnavMove(event, () => 0)
4353
+ if (!this.#listnavCaret(event)) this.#listnavMove(event, () => 0)
4337
4354
  }
4338
4355
 
4339
4356
  listnavLast(event) {
4340
- this.#listnavMove(event, (_current, length) => length - 1)
4357
+ if (!this.#listnavCaret(event)) this.#listnavMove(event, (_current, length) => length - 1)
4341
4358
  }
4342
4359
 
4343
4360
  // Enter: activate the highlighted option (fires its reactive select). No-op if
4344
4361
  // nothing is highlighted, and in that case DON'T preventDefault — Enter falls
4345
- // through (there's no selection to make).
4362
+ // through (there's no selection to make). Under highlight: :first (issue #492)
4363
+ // a highlight that went away (its group was hidden) is put back first.
4364
+ // meta/ctrl+Enter clicks with that modifier held, so a link option opens as a
4365
+ // modified click does; a button ignores it.
4346
4366
  listnavPick(event) {
4347
- const options = this.#listnavOptions(event)
4348
- const current = options.findIndex((el) => el.hasAttribute("data-reactive-highlighted"))
4349
- if (current < 0) return
4367
+ if (imeComposing(event)) return
4368
+ const highlightedOf = () => this.#listnavOptions(event).find((el) => el.hasAttribute("data-reactive-highlighted"))
4369
+ let chosen = highlightedOf()
4370
+ if (!chosen && this.#listnavTrigger(event).getAttribute?.("data-reactive-listnav-highlight-param") === "first") {
4371
+ this.#syncListnavTrigger(this.#listnavTrigger(event))
4372
+ chosen = highlightedOf()
4373
+ }
4374
+ if (!chosen) return
4350
4375
  event.preventDefault()
4351
- options[current].click()
4376
+ if (event.metaKey || event.ctrlKey) {
4377
+ chosen.dispatchEvent(
4378
+ new MouseEvent("click", { metaKey: event.metaKey, ctrlKey: event.ctrlKey, bubbles: true, cancelable: true }),
4379
+ )
4380
+ } else chosen.click()
4352
4381
  }
4353
4382
 
4354
4383
  listnavClose(event) {
4355
- for (const el of this.#listnavOptions(event)) el.removeAttribute("data-reactive-highlighted")
4384
+ if (imeComposing(event)) return
4385
+ this.#listnavHighlight(this.#listnavTrigger(event), this.#listnavAll(event))
4356
4386
  }
4357
4387
 
4358
4388
  // Move among THIS root's options: `pick(current, length)` returns the next
@@ -4362,8 +4392,9 @@ export default class extends Controller {
4362
4392
  // (issue #271) focuses the chosen item, current being the item that is or
4363
4393
  // contains document.activeElement.
4364
4394
  #listnavMove(event, pick) {
4365
- const options = this.#listnavOptions(event)
4366
- if (!options.length) return
4395
+ const all = this.#listnavAll(event)
4396
+ const options = this.#listnavOptions(event, all)
4397
+ if (!options.length || imeComposing(event)) return
4367
4398
  event.preventDefault()
4368
4399
 
4369
4400
  const focusMode = this.#listnavFocusMode(event)
@@ -4373,19 +4404,84 @@ export default class extends Controller {
4373
4404
  : options.findIndex((el) => el.hasAttribute("data-reactive-highlighted"))
4374
4405
  const chosen = options[pick(current, options.length)]
4375
4406
 
4376
- if (focusMode) {
4377
- chosen.focus?.()
4378
- } else {
4379
- for (const el of options) el.removeAttribute("data-reactive-highlighted")
4380
- chosen.setAttribute("data-reactive-highlighted", "true")
4381
- }
4407
+ if (focusMode) chosen.focus?.()
4408
+ else this.#listnavHighlight(this.#listnavTrigger(event), all, chosen)
4382
4409
  chosen.scrollIntoView?.({ block: "nearest" })
4383
4410
  }
4384
4411
 
4412
+ // The listnav trigger: the element the keyboard filters ride on (the search
4413
+ // input), read from the event; the root for a directly-invoked call.
4414
+ #listnavTrigger(event) {
4415
+ return event?.currentTarget ?? event?.target ?? this.element
4416
+ }
4417
+
4385
4418
  // Focus mode is read off the TRIGGER like the option selector (issue #271).
4386
4419
  #listnavFocusMode(event) {
4387
- const trigger = event?.currentTarget ?? event?.target ?? this.element
4388
- return trigger.getAttribute?.("data-reactive-listnav-focus-param") === "true"
4420
+ return this.#listnavTrigger(event).getAttribute?.("data-reactive-listnav-focus-param") === "true"
4421
+ }
4422
+
4423
+ // Whether a plain Home/End belongs to the caret (issue #492): highlight mode,
4424
+ // no ctrl, and a filled query.
4425
+ #listnavCaret(event) {
4426
+ return !this.#listnavFocusMode(event) && !event.ctrlKey && !!this.#listnavTrigger(event).value
4427
+ }
4428
+
4429
+ // Highlight `chosen` (none: clear) among `all` — every option the selector
4430
+ // matches in this root, the hidden ones too, so an option hidden while
4431
+ // highlighted cannot come back as a second highlight. Under highlight:
4432
+ // :first (issue #492) the highlight is announced: aria-selected="true" on the
4433
+ // option, unless it already carries an aria-selected the client did not
4434
+ // write (marked data-reactive-aria-selected, so a Turbo snapshot or a morph
4435
+ // carries the mark with the attribute), and its id (generated when it has
4436
+ // none) as the trigger's aria-activedescendant. Both go with it. Without
4437
+ // highlight: no ARIA is written or removed: an app may own both (a pick list
4438
+ // marks its pick aria-selected, issue #442).
4439
+ #listnavHighlight(trigger, all, chosen) {
4440
+ const aria = trigger.getAttribute?.("data-reactive-listnav-highlight-param") === "first"
4441
+ for (const el of all) {
4442
+ if (el === chosen) continue
4443
+ if (el.hasAttribute("data-reactive-highlighted")) el.removeAttribute("data-reactive-highlighted")
4444
+ if (aria && el.hasAttribute("data-reactive-aria-selected")) {
4445
+ el.removeAttribute("aria-selected")
4446
+ el.removeAttribute("data-reactive-aria-selected")
4447
+ }
4448
+ }
4449
+ chosen?.setAttribute("data-reactive-highlighted", "true")
4450
+ if (!aria) return
4451
+ if (!chosen) return trigger.removeAttribute("aria-activedescendant")
4452
+ if (!chosen.hasAttribute("aria-selected")) {
4453
+ chosen.setAttribute("aria-selected", "true")
4454
+ chosen.setAttribute("data-reactive-aria-selected", "")
4455
+ }
4456
+ chosen.id ||= `reactive-option-${++listnavIds}`
4457
+ trigger.setAttribute("aria-activedescendant", chosen.id)
4458
+ }
4459
+
4460
+ // Bring every highlight: :first listnav trigger this root owns in line with
4461
+ // its options (issue #492). The bindings feature calls it (core.syncListnav):
4462
+ // after a filter or tags pass, at connect, after a morph and after a stream
4463
+ // into the root. (A plain listnav needs none: the filter and tags passes
4464
+ // drop a hidden option's highlight themselves, and it writes no ARIA.)
4465
+ #syncListnav() {
4466
+ // A root without one (every other bindings root asks too) pays this probe.
4467
+ const selector = '[data-reactive-listnav-highlight-param="first"]'
4468
+ if (!this.element.querySelector?.(selector)) return
4469
+ const owns = this.#ownershipFilter()
4470
+ for (const trigger of this.element.querySelectorAll(selector)) if (owns(trigger)) this.#syncListnavTrigger(trigger)
4471
+ }
4472
+
4473
+ // One highlight: :first trigger: a highlight that is no longer navigable
4474
+ // goes, with its ARIA, and the first navigable option takes it when none
4475
+ // has it, and when the query changed since the last sync.
4476
+ #syncListnavTrigger(trigger) {
4477
+ const event = { currentTarget: trigger }
4478
+ const all = this.#listnavAll(event)
4479
+ const options = this.#listnavOptions(event, all)
4480
+ const query = trigger.value ?? ""
4481
+ let chosen = options.find((el) => el.hasAttribute("data-reactive-highlighted"))
4482
+ if (listnavQueries.has(trigger) && listnavQueries.get(trigger) !== query) chosen = undefined
4483
+ listnavQueries.set(trigger, query)
4484
+ this.#listnavHighlight(trigger, all, chosen ?? options[0])
4389
4485
  }
4390
4486
 
4391
4487
  // The option elements this root owns (skips nested reactive roots, issue #15),
@@ -4397,15 +4493,31 @@ export default class extends Controller {
4397
4493
  // no-nested-root case it is a constant true, skipping a closest() walk per
4398
4494
  // option. Hidden options are excluded (issue #163): a reactive_filter (or any
4399
4495
  // `hidden` toggle) removes a row from the keyboard path too, so an Arrow can't
4400
- // highlight — and Enter can't pick — an invisible option.
4401
- #listnavOptions(event) {
4402
- const trigger = event?.currentTarget ?? event?.target ?? this.element
4496
+ // highlight — and Enter can't pick — an invisible option. So is an option
4497
+ // under a hidden ancestor up to the root (issue #492: a group a reactive_show
4498
+ // hid) or under a hidden root: a walk of parentElement up to the root and
4499
+ // the root itself, cheaper than a probe for any hidden element, which is a
4500
+ // full traversal of the root.
4501
+ // `all` is #listnavAll's list when the caller has it already.
4502
+ #listnavOptions(event, all = this.#listnavAll(event)) {
4503
+ const root = this.element
4504
+ return all.filter((el) => {
4505
+ for (let node = el; node; node = node === root ? null : node.parentElement) {
4506
+ if (node.hidden) return false
4507
+ }
4508
+ return true
4509
+ })
4510
+ }
4511
+
4512
+ // Every option the selector matches in this root, the hidden ones too (the
4513
+ // highlight write clears them as well).
4514
+ #listnavAll(event) {
4515
+ const trigger = this.#listnavTrigger(event)
4403
4516
  const selector =
4404
4517
  trigger.getAttribute?.("data-reactive-listnav-option-param") ??
4405
4518
  this.element.getAttribute("data-reactive-listnav-option-param")
4406
4519
  if (!selector) return []
4407
- const owns = this.#ownershipFilter()
4408
- return Array.from(this.element.querySelectorAll(selector)).filter((el) => !el.hidden && owns(el))
4520
+ return Array.from(this.element.querySelectorAll(selector)).filter(this.#ownershipFilter())
4409
4521
  }
4410
4522
 
4411
4523
  // Enqueue the action — debounced if a debounce window is set, else immediately.
@@ -294,6 +294,24 @@ module Phlex
294
294
  "keydown.end->reactive#listnavLast"
295
295
  ].freeze
296
296
 
297
+ # highlight: :first (issue #492): the arrows, Enter with or without
298
+ # meta/ctrl held (Stimulus compares all four modifiers, so each is its
299
+ # own filter; a held one opens a link option as a modified click does),
300
+ # and Home/End with or without ctrl (the client lets a plain one move
301
+ # the caret while the query is filled). No Escape: the highlight stays,
302
+ # and closing is the caller's own binding.
303
+ LISTNAV_HIGHLIGHT_ACTIONS = [
304
+ "keydown.down->reactive#listnavNext",
305
+ "keydown.up->reactive#listnavPrev",
306
+ "keydown.enter->reactive#listnavPick",
307
+ "keydown.meta+enter->reactive#listnavPick",
308
+ "keydown.ctrl+enter->reactive#listnavPick",
309
+ "keydown.home->reactive#listnavFirst",
310
+ "keydown.ctrl+home->reactive#listnavFirst",
311
+ "keydown.end->reactive#listnavLast",
312
+ "keydown.ctrl+end->reactive#listnavLast"
313
+ ].freeze
314
+
297
315
  # Event modifiers (issue #80) — window:, once:, outside:, throttle: are
298
316
  # RESERVED keyword names on on() (no longer usable as free action params):
299
317
  #
@@ -944,14 +962,30 @@ module Phlex
944
962
  # Escape is yours to bind (on_client("keydown.esc", ...) composes via mix):
945
963
  #
946
964
  # ul(role: "menu", **mix(reactive_listnav(focus: true), data: { testid: "menu" }))
947
- def reactive_listnav(option_selector = nil, focus: false)
965
+ #
966
+ # `highlight: :first` (issue #492) keeps the first visible option
967
+ # highlighted whenever none is — at connect, after a filter pass, after
968
+ # a morph or a stream into the root — so "type, Enter" picks the top
969
+ # match. A highlight the user moved stays while its option is visible; a
970
+ # changed query (or a morph that re-renders the options) puts it back on
971
+ # the first. Home/End move it while the query is empty (ctrl+Home/End
972
+ # always), meta/ctrl+Enter activates the option with that modifier
973
+ # held, and Escape is not bound. The highlight is announced
974
+ # (aria-selected on the option, aria-activedescendant on the input):
975
+ #
976
+ # input(name: "q", role: "combobox", **reactive_listnav(highlight: :first))
977
+ def reactive_listnav(option_selector = nil, focus: false, highlight: nil)
978
+ highlight = listnav_highlight!(highlight, focus)
948
979
  option_selector ||= focus ? "[role=menuitem]" : "[role=option]"
980
+ actions = LISTNAV_FOCUS_ACTIONS if focus
981
+ actions ||= highlight ? LISTNAV_HIGHLIGHT_ACTIONS : LISTNAV_ACTIONS
949
982
  data = {
950
- action: (focus ? LISTNAV_FOCUS_ACTIONS : LISTNAV_ACTIONS).join(" "),
983
+ action: actions.join(" "),
951
984
  reactive_listnav_option_param: filter_selector!(:selector, option_selector)
952
985
  }
953
986
  # STRING "true" — Phlex renders a boolean true as a valueless attribute.
954
987
  data[:reactive_listnav_focus_param] = "true" if focus
988
+ data[:reactive_listnav_highlight_param] = "first" if highlight
955
989
  { data: }
956
990
  end
957
991
 
@@ -1617,6 +1651,20 @@ module Phlex
1617
1651
  "reactive_filter match: must be one of #{FILTER_MATCH_MODES.join(", ")}, got #{match.inspect}"
1618
1652
  end
1619
1653
 
1654
+ # reactive_listnav's highlight: (issue #492): nil, or :first as a Symbol
1655
+ # or a String. Anything else raises at render, and so does :first with
1656
+ # focus: true — roving focus moves real focus, it has no highlight.
1657
+ def listnav_highlight!(highlight, focus)
1658
+ return nil if highlight.nil?
1659
+
1660
+ unless (highlight.is_a?(Symbol) || highlight.is_a?(String)) && highlight.to_sym == :first
1661
+ raise ArgumentError, "reactive_listnav highlight: must be :first, got #{highlight.inspect}"
1662
+ end
1663
+ raise ArgumentError, "reactive_listnav highlight: :first cannot combine with focus: true" if focus
1664
+
1665
+ :first
1666
+ end
1667
+
1620
1668
  # A reactive_filter/reactive_listnav selector, validated non-blank and
1621
1669
  # stringified (issue #163). A blank selector is a dead binding — the
1622
1670
  # client would silently match nothing — so it fails loudly at render,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Reactive
5
- VERSION = "0.19.3"
5
+ VERSION = "0.19.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-reactive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.19.3
4
+ version: 0.19.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mikael Henriksson