{"version":3,"file":"chunk-grid.mjs","names":[],"sources":["../../src/components/Listbox/grid.ts"],"sourcesContent":["//\n// Copyright 2026 DXOS.org\n//\n\nimport { type KeyboardEvent, createContext, useEffect } from 'react';\n\n/**\n * The ARIA grid pattern for list rows (AUDIT §6 point 19): the listbox is the one tab stop and zag moves its highlight;\n * ArrowRight enters the highlighted row, focusing its first control; Tab and Shift+Tab move between the row's controls;\n * Escape or ArrowLeft returns to the list. Row controls stay out of the tab order until a row is entered.\n */\n\nconst CONTROL_SELECTOR = [\n  'button',\n  'a[href]',\n  'input:not([type=\"hidden\"])',\n  'select',\n  'textarea',\n  '[contenteditable]:not([contenteditable=\"false\"])',\n  '[tabindex]',\n].join(', ');\n\nconst ROW_SELECTOR = '[role=\"option\"]';\nconst EDITABLE_PREVIEW_SELECTOR = '[data-scope=\"editable\"][data-part=\"preview\"]';\n\n/** The row (`option`) holding `element` in this list, not in a list nested in a row's detail. */\nconst rowOf = (list: HTMLElement, element: Element): HTMLElement | null => {\n  const row = element.closest<HTMLElement>(ROW_SELECTOR);\n  return row && row.closest('[role=\"listbox\"]') === list ? row : null;\n};\n\nconst isEditable = (element: Element) =>\n  element instanceof HTMLTextAreaElement ||\n  (element instanceof HTMLInputElement && !['checkbox', 'radio', 'button', 'submit', 'reset'].includes(element.type)) ||\n  (element instanceof HTMLElement && element.isContentEditable);\n\n/** The focusable controls of a row, in DOM order. */\nexport const rowControls = (row: Element): HTMLElement[] =>\n  Array.from(row.querySelectorAll<HTMLElement>(CONTROL_SELECTOR)).filter(\n    (control) =>\n      !control.hidden &&\n      !control.hasAttribute('disabled') &&\n      control.getAttribute('aria-hidden') !== 'true' &&\n      control.closest(ROW_SELECTOR) === row,\n  );\n\n/** Whether a pointer event started on one of a row's controls rather than on the row itself. */\nexport const isFromControl = (row: HTMLElement, target: EventTarget | null) => {\n  if (!(target instanceof Element) || target === row) {\n    return false;\n  }\n  const control = target.closest(CONTROL_SELECTOR);\n  // A row's text edited in place (an Editable preview) is the row's for a pointer: a press selects the row and leaves the\n  // list its keys; a double-click, its pencil, or Enter once the row is entered still opens it.\n  if (control?.matches(EDITABLE_PREVIEW_SELECTOR)) {\n    return false;\n  }\n  return control !== null && control !== row && row.contains(control);\n};\n\n/**\n * Keeps every row control of `list` out of the tab order (`tabindex=-1`), including controls that mount later, so the\n * list is a single tab stop. Programmatic focus still reaches them.\n */\nexport const useRowTabStops = (list: HTMLElement | null) => {\n  useEffect(() => {\n    if (!list) {\n      return;\n    }\n    const apply = () => {\n      for (const row of list.querySelectorAll<HTMLElement>(ROW_SELECTOR)) {\n        if (row.closest('[role=\"listbox\"]') !== list) {\n          continue;\n        }\n        for (const control of rowControls(row)) {\n          if (control.getAttribute('tabindex') !== '-1') {\n            control.setAttribute('tabindex', '-1');\n          }\n        }\n      }\n    };\n    apply();\n    const observer = new MutationObserver(apply);\n    observer.observe(list, { subtree: true, childList: true, attributes: true, attributeFilter: ['tabindex'] });\n    return () => observer.disconnect();\n  }, [list]);\n};\n\n/**\n * Handles a keydown on the list before zag does. Returns `true` when zag must not see the event: the key was handled\n * here, or it came from inside a row or a row's detail, where keys belong to the focused control.\n */\nexport const handleGridKeyDown = (event: KeyboardEvent<HTMLElement>): boolean => {\n  const list = event.currentTarget;\n  const target = event.target;\n  if (!(target instanceof HTMLElement)) {\n    return false;\n  }\n\n  if (target === list) {\n    if (event.key !== 'ArrowRight') {\n      return false;\n    }\n    // The active descendant: zag marks it `data-highlighted` only while it tracks focus as visible.\n    const active = list.ownerDocument.getElementById(list.getAttribute('aria-activedescendant') ?? '');\n    const row = active && rowOf(list, active) === active ? active : null;\n    const [control] = row ? rowControls(row) : [];\n    if (control) {\n      event.preventDefault();\n      control.focus();\n    }\n    return true;\n  }\n\n  const row = rowOf(list, target);\n  if (!row || event.defaultPrevented) {\n    return true;\n  }\n  if (event.key === 'Escape' || (event.key === 'ArrowLeft' && !isEditable(target))) {\n    event.preventDefault();\n    list.focus();\n    return true;\n  }\n  if (event.key === 'Tab') {\n    const controls = rowControls(row);\n    const index = controls.findIndex((control) => control === target || control.contains(target));\n    if (controls.length > 0) {\n      event.preventDefault();\n      const next = (index + (event.shiftKey ? -1 : 1) + controls.length) % controls.length;\n      controls[next].focus();\n    }\n    return true;\n  }\n  return true;\n};\n\n/** The id of the row's `ItemText`, so a row control (a Remove button, a disclosure caret) can name itself by it. */\nexport type RowContextValue = { textId: string };\n\nexport const RowContext = createContext<RowContextValue | undefined>(undefined);\n"],"mappings":";;;;;;;AAYA,IAAM,mBAAmB;CACvB;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC,CAAC,KAAK,IAAI;AAEX,IAAM,eAAe;AACrB,IAAM,4BAA4B;;AAGlC,IAAM,SAAS,MAAmB,YAAyC;CACzE,MAAM,MAAM,QAAQ,QAAqB,YAAY;CACrD,OAAO,OAAO,IAAI,QAAQ,oBAAkB,MAAM,OAAO,MAAM;AACjE;AAEA,IAAM,cAAc,YAClB,mBAAmB,uBAClB,mBAAmB,oBAAoB,CAAC;CAAC;CAAY;CAAS;CAAU;CAAU;AAAO,CAAC,CAAC,SAAS,QAAQ,IAAI,KAChH,mBAAmB,eAAe,QAAQ;;AAG7C,IAAa,eAAe,QAC1B,MAAM,KAAK,IAAI,iBAA8B,gBAAgB,CAAC,CAAC,CAAC,QAC7D,YACC,CAAC,QAAQ,UACT,CAAC,QAAQ,aAAa,UAAU,KAChC,QAAQ,aAAa,aAAa,MAAM,UACxC,QAAQ,QAAQ,YAAY,MAAM,GACtC;;AAGF,IAAa,iBAAiB,KAAkB,WAA+B;CAC7E,IAAI,EAAE,kBAAkB,YAAY,WAAW,KAC7C,OAAO;CAET,MAAM,UAAU,OAAO,QAAQ,gBAAgB;CAG/C,IAAI,SAAS,QAAQ,yBAAyB,GAC5C,OAAO;CAET,OAAO,YAAY,QAAQ,YAAY,OAAO,IAAI,SAAS,OAAO;AACpE;;;;;AAMA,IAAa,kBAAkB,SAA6B;CAC1D,gBAAgB;EACd,IAAI,CAAC,MACH;EA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