@vielzeug/focus 3.0.0 → 26.10.0

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.
@@ -0,0 +1,2 @@
1
+ var FocusError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},FocusConfigError=class extends FocusError{};exports.FocusConfigError=FocusConfigError,exports.FocusError=FocusError;
2
+ //# sourceMappingURL=errors.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.cjs","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all focus errors. Use `instanceof FocusError` to catch any focus-originated error. */\nexport class FocusError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when navigation options are invalid, such as a non-positive typeahead delay or a key assigned to two actions. */\nexport class FocusConfigError extends FocusError {}\n"],"mappings":"AACA,IAAa,WAAb,cAAgC,KAAM,CACpC,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,iBAAb,cAAsC,UAAW,CAAC"}
@@ -0,0 +1,8 @@
1
+ /** Base class for all focus errors. Use `instanceof FocusError` to catch any focus-originated error. */
2
+ export declare class FocusError extends Error {
3
+ constructor(message: string, opts?: ErrorOptions);
4
+ }
5
+ /** Thrown when navigation options are invalid, such as a non-positive typeahead delay or a key assigned to two actions. */
6
+ export declare class FocusConfigError extends FocusError {
7
+ }
8
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA,wGAAwG;AACxG,qBAAa,UAAW,SAAQ,KAAK;gBACvB,OAAO,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,YAAY;CAKjD;AAED,2HAA2H;AAC3H,qBAAa,gBAAiB,SAAQ,UAAU;CAAG"}
package/dist/errors.js ADDED
@@ -0,0 +1,2 @@
1
+ var FocusError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},FocusConfigError=class extends FocusError{};export{FocusConfigError,FocusError};
2
+ //# sourceMappingURL=errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.js","names":[],"sources":["../src/errors.ts"],"sourcesContent":["/** Base class for all focus errors. Use `instanceof FocusError` to catch any focus-originated error. */\nexport class FocusError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when navigation options are invalid, such as a non-positive typeahead delay or a key assigned to two actions. */\nexport class FocusConfigError extends FocusError {}\n"],"mappings":"AACA,IAAa,WAAb,cAAgC,KAAM,CACpC,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,iBAAb,cAAsC,UAAW,CAAC"}
package/dist/focus.cjs CHANGED
@@ -1,2 +1,2 @@
1
- Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowRight`],prev:[`ArrowUp`,`ArrowLeft`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowRight`],prev:[`ArrowLeft`]},vertical:{first:[`Home`],last:[`End`],next:[`ArrowDown`],prev:[`ArrowUp`]}},t={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowLeft`],prev:[`ArrowUp`,`ArrowRight`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowLeft`],prev:[`ArrowRight`]},vertical:e.vertical},n=500,r=(e,t,n)=>{for(let r=t;r<e.length;r++)if(n(e[r],r))return r;return-1},i=(e,t,n)=>{for(let r=t;r>=0;r--)if(n(e[r],r))return r;return-1},a=e=>{if(e===void 0)return n;if(!Number.isFinite(e)||e<=0)throw RangeError(`Typeahead delay must be a positive finite number`);return e},o=n=>{let o=a(n.typeahead?.delayMs),s=new Map;for(let e of[`next`,`prev`,`first`,`last`])for(let t of n.keys?.[e]??[]){let n=s.get(t);if(n&&n!==e)throw RangeError(`Key "${t}" is assigned to both ${n} and ${e}`);s.set(t,e)}let c=-1,l=``,u=0,d=(e,t)=>n.isItemDisabled?.(e,t)??!1,f=(e,t)=>t>=0&&t<e.length&&!d(e[t],t),p=e=>(f(e,c)||(c=-1),c),m=()=>{l=``,u=0},h=(e,t,n,r)=>f(e,t)?(c=t,Object.freeze({action:n,event:r,index:t,item:e[t]})):(c=-1,null),g=(e,t,n)=>n===`forward`?r(e,t,(e,t)=>!d(e,t)):i(e,t,(e,t)=>!d(e,t)),_=(e,t,r)=>{let i=p(e);if(!e.length)return c=-1,null;let a=i<0?t===`forward`?0:e.length-1:t===`forward`?i+1:i-1,o=g(e,a,t),s=t===`forward`?`next`:`prev`;if(o>=0)return h(e,o,s,r);if(n.loop){let n=t===`forward`?0:e.length-1,i=g(e,n,t);if(i>=0)return h(e,i,s,r)}return null},v=(e,t,n)=>(m(),p(e),e.length?t===`first`?h(e,g(e,0,`forward`),t,n):t===`last`?h(e,g(e,e.length-1,`backward`),t,n):_(e,t===`next`?`forward`:`backward`,n):(c=-1,null)),y=(e,t,r)=>{let i=p(e),a=i<0?0:r?i:i+1;for(let r=0;r<e.length;r++){let i=(a+r)%e.length,o=e[i];if(!d(o,i)&&n.typeahead?.getLabel(o,i).toLocaleLowerCase().startsWith(t))return i}return-1},b=(e,t)=>{if(!n.typeahead||e.key.length!==1||e.ctrlKey||e.altKey||e.metaKey)return null;let r=performance.now(),i=e.key.toLocaleLowerCase();r-u>=o&&(l=``),u=r;let a=l+i;l=a.length>1&&[...a].every(e=>e===i)?i:a;let s=y(t,l,l.length>1);return s<0&&l.length>1&&(l=i,s=y(t,l,!1)),s<0?null:h(t,s,`typeahead`,e)},x=e=>{let t=n.getItems();if(p(t),m(),!Number.isInteger(e)||e<0||!t.length)return c=-1,c;let r=Math.min(e,t.length-1);return f(t,r)?(c=r,c):(c=-1,c)},S=e=>v(n.getItems(),e),C=()=>{let e=n.getItems(),t=p(e);return t>=0?e[t]:void 0},w=()=>{c=-1,m()},T=()=>p(n.getItems()),E=(e,t)=>typeof e==`function`?e():e??t,D=r=>{let i=n.keys,a=(E(n.direction,`ltr`)===`rtl`?t:e)[E(n.orientation,`vertical`)];for(let e of[`next`,`prev`,`first`,`last`])if((i?.[e]??a[e]).includes(r))return e};return{getActiveItem:C,getIndex:T,handleKeydown:e=>{if(e.defaultPrevented||e.isComposing||E(n.disabled,!1))return null;let t=n.getItems();p(t);let r=D(e.key);if(r){let n=v(t,r,e);return e.preventDefault(),Object.freeze({change:n,handled:!0})}let i=b(e,t);return i?(n.typeahead?.preventDefault&&e.preventDefault(),Object.freeze({change:i,handled:!0})):null},navigate:S,reset:w,set:x}},s=e=>typeof e==`function`?e():e,c=e=>{let t=e.activeElement;for(;t instanceof HTMLElement&&t.shadowRoot?.activeElement;)t=t.shadowRoot.activeElement;return t},l=e=>e.isConnected,u=e=>e instanceof HTMLElement&&`disabled`in e&&!!e.disabled,d=e=>e.closest(`[inert]`)?!0:e instanceof HTMLElement&&e.inert,f=e=>l(e)&&!u(e)&&!d(e),p=(e,t)=>{try{return e.focus({preventScroll:t}),c(e.ownerDocument)===e}catch{return!1}},m=(e,t={})=>{let n;try{n=s(e)}catch{n=null}if(n&&f(n)&&p(n,t.preventScroll))return!0;if(!t.fallback)return!1;let r;try{r=s(t.fallback)}catch{return!1}return!r||!f(r)?!1:p(r,t.preventScroll)},h=(e={})=>{let t=c(document),n=!e.signal?.aborted;n||(t=null);let r=()=>{n=!1,t=null};return e.signal?.aborted||e.signal?.addEventListener(`abort`,r,{once:!0}),()=>{if(!n)return!1;n=!1,e.signal?.removeEventListener(`abort`,r);let i=t;return t=null,i?m(i,{fallback:e.fallback,preventScroll:e.preventScroll}):e.fallback?m(e.fallback,{preventScroll:e.preventScroll}):!1}};exports.captureFocus=h,exports.createListNavigation=o,exports.restoreFocus=m;
1
+ Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});let e=require("@vielzeug/keymap");var FocusError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},FocusConfigError=class extends FocusError{},t={first:[`Home`],last:[`End`],next:[`ArrowRight`],nextRow:[`ArrowDown`],prev:[`ArrowLeft`],prevRow:[`ArrowUp`]},n={...t,next:[`ArrowLeft`],prev:[`ArrowRight`]},r=[`first`,`last`,`next`,`nextRow`,`prev`,`prevRow`],resolve=(e,t)=>typeof e==`function`?e():e??t,createGridNavigation=i=>{let a=new Map;for(let e of r)for(let t of i.keys?.[e]??[]){let n=a.get(t);if(n&&n!==e)throw new FocusConfigError(`Key "${t}" is assigned to both ${n} and ${e}`);a.set(t,e)}let o=-1,resolveColumns=()=>{let e=resolve(i.columns,1);if(!Number.isInteger(e)||e<1)throw new FocusConfigError(`columns must resolve to a positive integer, got: ${e}`);return e},activeIndex=e=>{let t=i.getActiveIndex?.();return t!==void 0&&(o=t>=0&&t<e.length?t:-1),o},commitIndex=(e,t,n,r)=>(o=t,Object.freeze({action:n,event:r,index:t,item:e[t]})),navigateWithItems=(e,t,n)=>{if(!e.length)return o=-1,null;if(t===`first`)return commitIndex(e,0,t,n);if(t===`last`)return commitIndex(e,e.length-1,t,n);let r=resolveColumns(),a=t===`next`?1:t===`prev`?-1:t===`nextRow`?r:-r,s=activeIndex(e),c=s<0?a>0?0:e.length-1:s+a;if(i.loop)return commitIndex(e,(c%e.length+e.length)%e.length,t,n);let l=Math.min(e.length-1,Math.max(0,c));return l===s?null:commitIndex(e,l,t,n)},resolveKeyAction=a=>{let o=resolve(i.direction,`ltr`)===`rtl`?n:t;for(let t of r)if((i.keys?.[t]??o[t]).some(t=>(0,e.matchKey)(a,t)))return t},handleKeydown=e=>{if(e.defaultPrevented||e.isComposing||resolve(i.disabled,!1))return null;let t=resolveKeyAction(e);if(!t)return null;let n=navigateWithItems(i.getItems(),t,e);return e.preventDefault(),Object.freeze({change:n,handled:!0})},navigate=e=>navigateWithItems(i.getItems(),e),set=e=>{let t=i.getItems();if(!Number.isInteger(e)||e<0||!t.length){o=-1;return}o=Math.min(e,t.length-1)},reset=()=>{o=-1},getIndex=()=>activeIndex(i.getItems()),getActiveItem=()=>{let e=i.getItems(),t=activeIndex(e);return t>=0?e[t]:void 0};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}},i={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowRight`],prev:[`ArrowUp`,`ArrowLeft`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowRight`],prev:[`ArrowLeft`]},vertical:{first:[`Home`],last:[`End`],next:[`ArrowDown`],prev:[`ArrowUp`]}},a={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowLeft`],prev:[`ArrowUp`,`ArrowRight`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowLeft`],prev:[`ArrowRight`]},vertical:i.vertical},o=500,findForward=(e,t,n)=>{for(let r=t;r<e.length;r++)if(n(e[r],r))return r;return-1},findBackward=(e,t,n)=>{for(let r=t;r>=0;r--)if(n(e[r],r))return r;return-1},resolveTypeaheadDelay=e=>{if(e===void 0)return o;if(!Number.isFinite(e)||e<=0)throw new FocusConfigError(`Typeahead delay must be a positive finite number`);return e},createListNavigation=t=>{let n=resolveTypeaheadDelay(t.typeahead?.delayMs),r=new Map;for(let e of[`next`,`prev`,`first`,`last`])for(let n of t.keys?.[e]??[]){let t=r.get(n);if(t&&t!==e)throw new FocusConfigError(`Key "${n}" is assigned to both ${t} and ${e}`);r.set(n,e)}let o=-1,s=``,c=0,isItemDisabled=(e,n)=>t.isItemDisabled?.(e,n)??!1,isUsableIndex=(e,t)=>t>=0&&t<e.length&&!isItemDisabled(e[t],t),normalizeIndex=e=>(isUsableIndex(e,o)||(o=-1),o),resetTypeahead=()=>{s=``,c=0},commitIndex=(e,t,n,r)=>isUsableIndex(e,t)?(o=t,Object.freeze({action:n,event:r,index:t,item:e[t]})):(o=-1,null),findEnabledIndex=(e,t,n)=>n===`forward`?findForward(e,t,(e,t)=>!isItemDisabled(e,t)):findBackward(e,t,(e,t)=>!isItemDisabled(e,t)),move=(e,n,r)=>{let i=normalizeIndex(e);if(!e.length)return o=-1,null;let a=i<0?n===`forward`?0:e.length-1:n===`forward`?i+1:i-1,s=findEnabledIndex(e,a,n),c=n===`forward`?`next`:`prev`;if(s>=0)return commitIndex(e,s,c,r);if(t.loop){let t=n===`forward`?0:e.length-1,i=findEnabledIndex(e,t,n);if(i>=0)return commitIndex(e,i,c,r)}return null},navigateWithItems=(e,t,n)=>(resetTypeahead(),normalizeIndex(e),e.length?t===`first`?commitIndex(e,findEnabledIndex(e,0,`forward`),t,n):t===`last`?commitIndex(e,findEnabledIndex(e,e.length-1,`backward`),t,n):move(e,t===`next`?`forward`:`backward`,n):(o=-1,null)),findTypeaheadMatch=(e,n,r)=>{let i=normalizeIndex(e),a=i<0?0:r?i:i+1;for(let r=0;r<e.length;r++){let i=(a+r)%e.length,o=e[i];if(!isItemDisabled(o,i)&&t.typeahead?.getLabel(o,i).toLocaleLowerCase().startsWith(n))return i}return-1},handleTypeahead=(e,r)=>{if(!t.typeahead||e.key.length!==1||e.ctrlKey||e.altKey||e.metaKey)return null;let i=performance.now(),a=e.key.toLocaleLowerCase();i-c>=n&&(s=``),c=i;let o=s+a;s=o.length>1&&[...o].every(e=>e===a)?a:o;let l=findTypeaheadMatch(r,s,s.length>1);return l<0&&s.length>1&&(s=a,l=findTypeaheadMatch(r,s,!1)),l<0?null:commitIndex(r,l,`typeahead`,e)},set=e=>{let n=t.getItems();if(normalizeIndex(n),resetTypeahead(),!Number.isInteger(e)||e<0||!n.length)return o=-1,o;let r=Math.min(e,n.length-1);return isUsableIndex(n,r)?(o=r,o):(o=-1,o)},navigate=e=>navigateWithItems(t.getItems(),e),getActiveItem=()=>{let e=t.getItems(),n=normalizeIndex(e);return n>=0?e[n]:void 0},reset=()=>{o=-1,resetTypeahead()},getIndex=()=>normalizeIndex(t.getItems()),resolve=(e,t)=>typeof e==`function`?e():e??t,resolveKeyAction=n=>{let r=t.keys,o=(resolve(t.direction,`ltr`)===`rtl`?a:i)[resolve(t.orientation,`vertical`)];for(let t of[`next`,`prev`,`first`,`last`])if((r?.[t]??o[t]).some(t=>(0,e.matchKey)(n,t)))return t},handleKeydown=e=>{if(e.defaultPrevented||e.isComposing||resolve(t.disabled,!1))return null;let n=t.getItems();normalizeIndex(n);let r=resolveKeyAction(e);if(r){let t=navigateWithItems(n,r,e);return e.preventDefault(),Object.freeze({change:t,handled:!0})}let i=handleTypeahead(e,n);return i?(t.typeahead?.preventDefault&&e.preventDefault(),Object.freeze({change:i,handled:!0})):null};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}},resolveTarget=e=>typeof e==`function`?e():e,getDeepActiveElement=e=>{let t=e.activeElement;for(;t instanceof HTMLElement&&t.shadowRoot?.activeElement;)t=t.shadowRoot.activeElement;return t},isConnected=e=>e.isConnected,isDisabled=e=>e instanceof HTMLElement&&`disabled`in e&&!!e.disabled,isInert=e=>e.closest(`[inert]`)?!0:e instanceof HTMLElement&&e.inert,canRestoreTo=e=>isConnected(e)&&!isDisabled(e)&&!isInert(e),focusAndVerify=(e,t)=>{try{return e.focus({preventScroll:t}),getDeepActiveElement(e.ownerDocument)===e}catch{return!1}},restoreFocus=(e,t={})=>{let n;try{n=resolveTarget(e)}catch{n=null}if(n&&canRestoreTo(n)&&focusAndVerify(n,t.preventScroll))return!0;if(!t.fallback)return!1;let r;try{r=resolveTarget(t.fallback)}catch{return!1}return!r||!canRestoreTo(r)?!1:focusAndVerify(r,t.preventScroll)},rescueFocus=(e,t={})=>{let n=getDeepActiveElement(document);return n&&n!==document.body?!1:restoreFocus(e,t)},captureFocus=(e={})=>{let t=getDeepActiveElement(document),n=!e.signal?.aborted;n||(t=null);let cancel=()=>{n=!1,t=null};return e.signal?.aborted||e.signal?.addEventListener(`abort`,cancel,{once:!0}),()=>{if(!n)return!1;n=!1,e.signal?.removeEventListener(`abort`,cancel);let r=t;return t=null,r?restoreFocus(r,{fallback:e.fallback,preventScroll:e.preventScroll}):e.fallback?restoreFocus(e.fallback,{preventScroll:e.preventScroll}):!1}};exports.FocusConfigError=FocusConfigError,exports.FocusError=FocusError,exports.captureFocus=captureFocus,exports.createGridNavigation=createGridNavigation,exports.createListNavigation=createListNavigation,exports.rescueFocus=rescueFocus,exports.restoreFocus=restoreFocus;
2
2
  //# sourceMappingURL=focus.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"focus.cjs","names":[],"sources":["../src/list-navigation.ts","../src/restore-focus.ts"],"sourcesContent":["export type ListNavigationAction = 'first' | 'last' | 'next' | 'prev';\nexport type ListKeyAction = ListNavigationAction | 'typeahead';\n\nexport type ListNavigationChange<T> = {\n readonly action: ListKeyAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type ListKeyResult<T> = {\n readonly change: ListNavigationChange<T> | null;\n readonly handled: true;\n};\n\nexport type ListNavigationTypeaheadOptions<T> = {\n delayMs?: number;\n getLabel: (item: T, index: number) => string;\n preventDefault?: boolean;\n};\n\nexport type MaybeGetter<T> = T | (() => T);\n\nexport type ListNavigationOptions<T> = {\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n getItems: () => readonly T[];\n isItemDisabled?: (item: T, index: number) => boolean;\n keys?: Partial<Record<ListNavigationAction, readonly string[]>>;\n loop?: boolean;\n orientation?: MaybeGetter<'both' | 'horizontal' | 'vertical'>;\n typeahead?: ListNavigationTypeaheadOptions<T>;\n};\n\nexport type ListNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): ListKeyResult<T> | null;\n navigate(action: ListNavigationAction): ListNavigationChange<T> | null;\n reset(): void;\n set(index: number): number;\n};\n\nconst DEFAULT_KEYS: Record<'both' | 'horizontal' | 'vertical', Record<ListNavigationAction, readonly string[]>> = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowRight'], prev: ['ArrowUp', 'ArrowLeft'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowRight'], prev: ['ArrowLeft'] },\n vertical: { first: ['Home'], last: ['End'], next: ['ArrowDown'], prev: ['ArrowUp'] },\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowLeft'], prev: ['ArrowUp', 'ArrowRight'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowLeft'], prev: ['ArrowRight'] },\n vertical: DEFAULT_KEYS.vertical,\n};\n\nconst DEFAULT_TYPEAHEAD_DELAY_MS = 500;\n\nconst findForward = <T>(items: readonly T[], start: number, predicate: (item: T, index: number) => boolean): number => {\n for (let index = start; index < items.length; index++) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst findBackward = <T>(\n items: readonly T[],\n start: number,\n predicate: (item: T, index: number) => boolean,\n): number => {\n for (let index = start; index >= 0; index--) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst resolveTypeaheadDelay = (delay: number | undefined): number => {\n if (delay === undefined) return DEFAULT_TYPEAHEAD_DELAY_MS;\n if (!Number.isFinite(delay) || delay <= 0) throw new RangeError('Typeahead delay must be a positive finite number');\n return delay;\n};\n\nexport const createListNavigation = <T>(options: ListNavigationOptions<T>): ListNavigation<T> => {\n const typeaheadDelay = resolveTypeaheadDelay(options.typeahead?.delayMs);\n const owners = new Map<string, ListNavigationAction>();\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action) throw new RangeError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n let typeaheadBuffer = '';\n let lastTypeaheadAt = 0;\n\n const isItemDisabled = (item: T, itemIndex: number): boolean => options.isItemDisabled?.(item, itemIndex) ?? false;\n\n const isUsableIndex = (items: readonly T[], itemIndex: number): boolean =>\n itemIndex >= 0 && itemIndex < items.length && !isItemDisabled(items[itemIndex], itemIndex);\n\n const normalizeIndex = (items: readonly T[]): number => {\n if (!isUsableIndex(items, index)) index = -1;\n\n return index;\n };\n\n const resetTypeahead = (): void => {\n typeaheadBuffer = '';\n lastTypeaheadAt = 0;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: ListKeyAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n if (!isUsableIndex(items, nextIndex)) {\n index = -1;\n\n return null;\n }\n\n index = nextIndex;\n\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const findEnabledIndex = (items: readonly T[], start: number, direction: 'forward' | 'backward'): number => {\n if (direction === 'forward')\n return findForward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n\n return findBackward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n };\n\n const move = (\n items: readonly T[],\n direction: 'forward' | 'backward',\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n const current = normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n const start =\n current < 0\n ? direction === 'forward'\n ? 0\n : items.length - 1\n : direction === 'forward'\n ? current + 1\n : current - 1;\n\n const nextIndex = findEnabledIndex(items, start, direction);\n const action: ListNavigationAction = direction === 'forward' ? 'next' : 'prev';\n\n if (nextIndex >= 0) return commitIndex(items, nextIndex, action, event);\n\n if (options.loop) {\n const wrapStart = direction === 'forward' ? 0 : items.length - 1;\n const wrapped = findEnabledIndex(items, wrapStart, direction);\n\n if (wrapped >= 0) return commitIndex(items, wrapped, action, event);\n }\n\n return null;\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: ListNavigationAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n resetTypeahead();\n normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n if (action === 'first') {\n return commitIndex(items, findEnabledIndex(items, 0, 'forward'), action, event);\n }\n\n if (action === 'last') {\n return commitIndex(items, findEnabledIndex(items, items.length - 1, 'backward'), action, event);\n }\n\n return move(items, action === 'next' ? 'forward' : 'backward', event);\n };\n\n const findTypeaheadMatch = (items: readonly T[], search: string, includeCurrent: boolean): number => {\n const current = normalizeIndex(items);\n const start = current < 0 ? 0 : includeCurrent ? current : current + 1;\n\n for (let offset = 0; offset < items.length; offset++) {\n const itemIndex = (start + offset) % items.length;\n const item = items[itemIndex];\n\n if (isItemDisabled(item, itemIndex)) continue;\n\n if (options.typeahead?.getLabel(item, itemIndex).toLocaleLowerCase().startsWith(search)) {\n return itemIndex;\n }\n }\n\n return -1;\n };\n\n const handleTypeahead = (event: KeyboardEvent, items: readonly T[]): ListNavigationChange<T> | null => {\n if (!options.typeahead || event.key.length !== 1 || event.ctrlKey || event.altKey || event.metaKey) return null;\n\n const now = performance.now();\n const key = event.key.toLocaleLowerCase();\n\n if (now - lastTypeaheadAt >= typeaheadDelay) typeaheadBuffer = '';\n\n lastTypeaheadAt = now;\n\n const candidate = typeaheadBuffer + key;\n const repeatedCharacter = candidate.length > 1 && [...candidate].every((character) => character === key);\n typeaheadBuffer = repeatedCharacter ? key : candidate;\n\n let nextIndex = findTypeaheadMatch(items, typeaheadBuffer, typeaheadBuffer.length > 1);\n\n if (nextIndex < 0 && typeaheadBuffer.length > 1) {\n typeaheadBuffer = key;\n nextIndex = findTypeaheadMatch(items, typeaheadBuffer, false);\n }\n\n if (nextIndex < 0) return null;\n\n return commitIndex(items, nextIndex, 'typeahead', event);\n };\n\n const set = (nextIndex: number): number => {\n const items = options.getItems();\n normalizeIndex(items);\n resetTypeahead();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n\n return index;\n }\n\n const clamped = Math.min(nextIndex, items.length - 1);\n\n if (!isUsableIndex(items, clamped)) {\n index = -1;\n\n return index;\n }\n\n index = clamped;\n\n return index;\n };\n\n const navigate = (action: ListNavigationAction): ListNavigationChange<T> | null => {\n return navigateWithItems(options.getItems(), action);\n };\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = normalizeIndex(items);\n\n return current >= 0 ? items[current] : undefined;\n };\n\n const reset = (): void => {\n index = -1;\n resetTypeahead();\n };\n\n const getIndex = (): number => {\n return normalizeIndex(options.getItems());\n };\n\n const resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n const resolveKeyAction = (eventKey: string): ListNavigationAction | undefined => {\n const keys = options.keys;\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n const defaults = keyTable[resolve(options.orientation, 'vertical')];\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n if ((keys?.[action] ?? defaults[action]).includes(eventKey)) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): ListKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const items = options.getItems();\n normalizeIndex(items);\n\n const action = resolveKeyAction(event.key);\n\n if (action) {\n const change = navigateWithItems(items, action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n }\n\n const change = handleTypeahead(event, items);\n if (!change) return null;\n if (options.typeahead?.preventDefault) event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","export type FocusableElement = HTMLElement | SVGElement;\nexport type FocusTarget = FocusableElement | null | undefined | (() => FocusableElement | null | undefined);\n\nexport type RestoreFocusOptions = {\n fallback?: FocusTarget;\n preventScroll?: boolean;\n};\n\nexport type CaptureFocusOptions = RestoreFocusOptions & {\n signal?: AbortSignal;\n};\n\nexport type FocusRestorer = () => boolean;\n\nconst resolveTarget = (target: FocusTarget): FocusableElement | null | undefined => {\n return typeof target === 'function' ? target() : target;\n};\n\nconst getDeepActiveElement = (rootDocument: Document): Element | null => {\n let active: Element | null = rootDocument.activeElement;\n\n while (active instanceof HTMLElement && active.shadowRoot?.activeElement) {\n active = active.shadowRoot.activeElement;\n }\n\n return active;\n};\n\nconst isConnected = (target: FocusableElement): boolean => {\n // `isConnected` pierces shadow boundaries (shadow-including root is the document);\n // `ownerDocument.contains` does NOT — it would reject elements inside shadow roots.\n return target.isConnected;\n};\n\nconst isDisabled = (target: FocusableElement): boolean => {\n return target instanceof HTMLElement && 'disabled' in target && Boolean((target as HTMLInputElement).disabled);\n};\n\nconst isInert = (target: FocusableElement): boolean => {\n if (target.closest('[inert]')) return true;\n\n return target instanceof HTMLElement && target.inert;\n};\n\nconst canRestoreTo = (target: FocusableElement): boolean => {\n return isConnected(target) && !isDisabled(target) && !isInert(target);\n};\n\nconst focusAndVerify = (target: FocusableElement, preventScroll: boolean | undefined): boolean => {\n try {\n target.focus({ preventScroll });\n return getDeepActiveElement(target.ownerDocument) === target;\n } catch {\n return false;\n }\n};\n\nexport const restoreFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n let next: FocusableElement | null | undefined;\n try {\n next = resolveTarget(target);\n } catch {\n next = null;\n }\n\n if (next && canRestoreTo(next) && focusAndVerify(next, options.preventScroll)) {\n return true;\n }\n\n if (!options.fallback) return false;\n\n let fallback: FocusableElement | null | undefined;\n try {\n fallback = resolveTarget(options.fallback);\n } catch {\n return false;\n }\n\n if (!fallback || !canRestoreTo(fallback)) return false;\n\n return focusAndVerify(fallback, options.preventScroll);\n};\n\nexport const captureFocus = (options: CaptureFocusOptions = {}): FocusRestorer => {\n let captured = getDeepActiveElement(document) as FocusableElement | null;\n let available = !options.signal?.aborted;\n\n if (!available) captured = null;\n\n const cancel = (): void => {\n available = false;\n captured = null;\n };\n\n if (!options.signal?.aborted) {\n options.signal?.addEventListener('abort', cancel, { once: true });\n }\n\n return () => {\n if (!available) return false;\n\n available = false;\n options.signal?.removeEventListener('abort', cancel);\n\n const target = captured;\n captured = null;\n\n if (target) {\n return restoreFocus(target, {\n fallback: options.fallback,\n preventScroll: options.preventScroll,\n });\n }\n\n if (options.fallback) {\n return restoreFocus(options.fallback, {\n preventScroll: options.preventScroll,\n });\n }\n\n return false;\n };\n};\n"],"mappings":"mEA2CA,IAAM,EAA4G,CAChH,KAAM,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAa,YAAY,EAAG,KAAM,CAAC,UAAW,WAAW,CAAE,EAC1G,WAAY,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAY,EAAG,KAAM,CAAC,WAAW,CAAE,EACxF,SAAU,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,WAAW,EAAG,KAAM,CAAC,SAAS,CAAE,CACrF,EAEM,EAAwC,CAC5C,KAAM,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAa,WAAW,EAAG,KAAM,CAAC,UAAW,YAAY,CAAE,EAC1G,WAAY,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,WAAW,EAAG,KAAM,CAAC,YAAY,CAAE,EACxF,SAAU,EAAa,QACzB,EAEM,EAA6B,IAE7B,GAAkB,EAAqB,EAAe,IAA2D,CACrH,IAAK,IAAI,EAAQ,EAAO,EAAQ,EAAM,OAAQ,IAC5C,GAAI,EAAU,EAAM,GAAQ,CAAK,EAAG,OAAO,EAG7C,MAAO,EACT,EAEM,GACJ,EACA,EACA,IACW,CACX,IAAK,IAAI,EAAQ,EAAO,GAAS,E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ACN,MAAO,EACT,CACF,EAEa,GAAgB,EAAqB,EAA+B,CAAC,IAAe,CAC/F,IAAI,EACJ,GAAI,CACF,EAAO,EAAc,CAAM,CAC7B,MAAQ,CACN,EAAO,IACT,CAEA,GAAI,GAAQ,EAAa,CAAI,GAAK,EAAe,EAAM,EAAQ,aAAa,EAC1E,MAAO,GAGT,GAAI,CAAC,EAAQ,SAAU,MAAO,GAE9B,IAAI,EACJ,GAAI,CACF,EAAW,EAAc,EAAQ,QAAQ,CAC3C,MAAQ,CACN,MAAO,EACT,CAIA,MAFI,CAAC,GAAY,CAAC,EAAa,CAAQ,EAAU,GAE1C,EAAe,EAAU,EAAQ,aAAa,CACvD,EAEa,GAAgB,EAA+B,CAAC,IAAqB,CAChF,IAAI,EAAW,EAAqB,QAAQ,EACxC,EAAY,CAAC,EAAQ,QAAQ,QAE5B,IAAW,EAAW,MAE3B,IAAM,MAAqB,CACzB,EAAY,GACZ,EAAW,IACb,EAMA,OAJK,EAAQ,QAAQ,SACnB,EAAQ,QAAQ,iBAAiB,QAAS,EAAQ,CAAE,KAAM,EAAK,CAAC,MAGrD,CACX,GAAI,CAAC,EAAW,MAAO,GAEvB,EAAY,GACZ,EAAQ,QAAQ,oBAAoB,QAAS,CAAM,EAEnD,IAAM,EAAS,EAgBf,MAfA,GAAW,KAEP,EACK,EAAa,EAAQ,CAC1B,SAAU,EAAQ,SAClB,cAAe,EAAQ,aACzB,CAAC,EAGC,EAAQ,SACH,EAAa,EAAQ,SAAU,CACpC,cAAe,EAAQ,aACzB,CAAC,EAGI,EACT,CACF"}
1
+ {"version":3,"file":"focus.cjs","names":[],"sources":["../src/errors.ts","../src/grid-navigation.ts","../src/list-navigation.ts","../src/restore-focus.ts"],"sourcesContent":["/** Base class for all focus errors. Use `instanceof FocusError` to catch any focus-originated error. */\nexport class FocusError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when navigation options are invalid, such as a non-positive typeahead delay or a key assigned to two actions. */\nexport class FocusConfigError extends FocusError {}\n","import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\nimport type { MaybeGetter } from './list-navigation.js';\n\nexport type GridNavigationAction = 'first' | 'last' | 'next' | 'nextRow' | 'prev' | 'prevRow';\n\nexport type GridNavigationChange<T> = {\n readonly action: GridNavigationAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type GridKeyResult<T> = {\n readonly change: GridNavigationChange<T> | null;\n readonly handled: true;\n};\n\n/**\n * Columns per row: a fixed count, or a getter resolved on every navigation. Responsive grids\n * read a media query or computed style; measured grids read the rendered row length.\n */\nexport type GridColumns = number | (() => number);\n\nexport type GridNavigationOptions<T> = {\n /** Columns per row, resolved on every navigation. */\n columns: GridColumns;\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n /**\n * Reads the active index from the outside (usually the index of `document.activeElement`\n * among the items), so focus moved by pointer or Tab stays in sync. When omitted, the\n * navigation tracks the index internally through `set()`.\n */\n getActiveIndex?: () => number;\n getItems: () => readonly T[];\n keys?: Partial<Record<GridNavigationAction, readonly string[]>>;\n /** Wrap moves around the grid's edges by flat index. Default clamps at the edges. */\n loop?: boolean;\n};\n\nexport type GridNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): GridKeyResult<T> | null;\n navigate(action: GridNavigationAction): GridNavigationChange<T> | null;\n reset(): void;\n set(index: number): void;\n};\n\nconst DEFAULT_KEYS: Record<GridNavigationAction, readonly string[]> = {\n first: ['Home'],\n last: ['End'],\n next: ['ArrowRight'],\n nextRow: ['ArrowDown'],\n prev: ['ArrowLeft'],\n prevRow: ['ArrowUp'],\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n ...DEFAULT_KEYS,\n next: ['ArrowLeft'],\n prev: ['ArrowRight'],\n};\n\nconst GRID_ACTIONS = ['first', 'last', 'next', 'nextRow', 'prev', 'prevRow'] as const;\n\nconst resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n/**\n * Keyboard navigation over a two-dimensional grid of items: horizontal arrows step one\n * item, vertical arrows step one row (the column count), Home/End jump to the ends.\n *\n * Unlike `createListNavigation`, items are not skipped when disabled — skipping in two\n * dimensions would break row alignment. Focus (or otherwise activate) the item reported\n * by the returned change; `handleKeydown` calls `preventDefault()` for every recognized\n * key so unhandled arrows never scroll the page.\n */\nexport const createGridNavigation = <T>(options: GridNavigationOptions<T>): GridNavigation<T> => {\n const owners = new Map<string, GridNavigationAction>();\n\n for (const action of GRID_ACTIONS) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n\n const resolveColumns = (): number => {\n const columns = resolve(options.columns, 1);\n if (!Number.isInteger(columns) || columns < 1)\n throw new FocusConfigError(`columns must resolve to a positive integer, got: ${columns}`);\n return columns;\n };\n\n const activeIndex = (items: readonly T[]): number => {\n const derived = options.getActiveIndex?.();\n if (derived !== undefined) index = derived >= 0 && derived < items.length ? derived : -1;\n return index;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> => {\n index = nextIndex;\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> | null => {\n if (!items.length) {\n index = -1;\n return null;\n }\n\n if (action === 'first') return commitIndex(items, 0, action, event);\n if (action === 'last') return commitIndex(items, items.length - 1, action, event);\n\n const columns = resolveColumns();\n const offset = action === 'next' ? 1 : action === 'prev' ? -1 : action === 'nextRow' ? columns : -columns;\n const current = activeIndex(items);\n const base = current < 0 ? (offset > 0 ? 0 : items.length - 1) : current + offset;\n\n if (options.loop) {\n return commitIndex(items, ((base % items.length) + items.length) % items.length, action, event);\n }\n\n const clamped = Math.min(items.length - 1, Math.max(0, base));\n if (clamped === current) return null;\n\n return commitIndex(items, clamped, action, event);\n };\n\n const resolveKeyAction = (event: KeyboardEvent): GridNavigationAction | undefined => {\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n\n for (const action of GRID_ACTIONS) {\n if ((options.keys?.[action] ?? keyTable[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): GridKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const action = resolveKeyAction(event);\n if (!action) return null;\n\n const change = navigateWithItems(options.getItems(), action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n const navigate = (action: GridNavigationAction): GridNavigationChange<T> | null =>\n navigateWithItems(options.getItems(), action);\n\n const set = (nextIndex: number): void => {\n const items = options.getItems();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n return;\n }\n\n index = Math.min(nextIndex, items.length - 1);\n };\n\n const reset = (): void => {\n index = -1;\n };\n\n const getIndex = (): number => activeIndex(options.getItems());\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = activeIndex(items);\n return current >= 0 ? items[current] : undefined;\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\n\nexport type ListNavigationAction = 'first' | 'last' | 'next' | 'prev';\nexport type ListKeyAction = ListNavigationAction | 'typeahead';\n\nexport type ListNavigationChange<T> = {\n readonly action: ListKeyAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type ListKeyResult<T> = {\n readonly change: ListNavigationChange<T> | null;\n readonly handled: true;\n};\n\nexport type ListNavigationTypeaheadOptions<T> = {\n delayMs?: number;\n getLabel: (item: T, index: number) => string;\n preventDefault?: boolean;\n};\n\nexport type MaybeGetter<T> = T | (() => T);\n\nexport type ListNavigationOptions<T> = {\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n getItems: () => readonly T[];\n isItemDisabled?: (item: T, index: number) => boolean;\n keys?: Partial<Record<ListNavigationAction, readonly string[]>>;\n loop?: boolean;\n orientation?: MaybeGetter<'both' | 'horizontal' | 'vertical'>;\n typeahead?: ListNavigationTypeaheadOptions<T>;\n};\n\nexport type ListNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): ListKeyResult<T> | null;\n navigate(action: ListNavigationAction): ListNavigationChange<T> | null;\n reset(): void;\n set(index: number): number;\n};\n\nconst DEFAULT_KEYS: Record<'both' | 'horizontal' | 'vertical', Record<ListNavigationAction, readonly string[]>> = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowRight'], prev: ['ArrowUp', 'ArrowLeft'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowRight'], prev: ['ArrowLeft'] },\n vertical: { first: ['Home'], last: ['End'], next: ['ArrowDown'], prev: ['ArrowUp'] },\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowLeft'], prev: ['ArrowUp', 'ArrowRight'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowLeft'], prev: ['ArrowRight'] },\n vertical: DEFAULT_KEYS.vertical,\n};\n\nconst DEFAULT_TYPEAHEAD_DELAY_MS = 500;\n\nconst findForward = <T>(items: readonly T[], start: number, predicate: (item: T, index: number) => boolean): number => {\n for (let index = start; index < items.length; index++) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst findBackward = <T>(\n items: readonly T[],\n start: number,\n predicate: (item: T, index: number) => boolean,\n): number => {\n for (let index = start; index >= 0; index--) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst resolveTypeaheadDelay = (delay: number | undefined): number => {\n if (delay === undefined) return DEFAULT_TYPEAHEAD_DELAY_MS;\n if (!Number.isFinite(delay) || delay <= 0)\n throw new FocusConfigError('Typeahead delay must be a positive finite number');\n return delay;\n};\n\nexport const createListNavigation = <T>(options: ListNavigationOptions<T>): ListNavigation<T> => {\n const typeaheadDelay = resolveTypeaheadDelay(options.typeahead?.delayMs);\n const owners = new Map<string, ListNavigationAction>();\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n let typeaheadBuffer = '';\n let lastTypeaheadAt = 0;\n\n const isItemDisabled = (item: T, itemIndex: number): boolean => options.isItemDisabled?.(item, itemIndex) ?? false;\n\n const isUsableIndex = (items: readonly T[], itemIndex: number): boolean =>\n itemIndex >= 0 && itemIndex < items.length && !isItemDisabled(items[itemIndex], itemIndex);\n\n const normalizeIndex = (items: readonly T[]): number => {\n if (!isUsableIndex(items, index)) index = -1;\n\n return index;\n };\n\n const resetTypeahead = (): void => {\n typeaheadBuffer = '';\n lastTypeaheadAt = 0;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: ListKeyAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n if (!isUsableIndex(items, nextIndex)) {\n index = -1;\n\n return null;\n }\n\n index = nextIndex;\n\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const findEnabledIndex = (items: readonly T[], start: number, direction: 'forward' | 'backward'): number => {\n if (direction === 'forward')\n return findForward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n\n return findBackward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n };\n\n const move = (\n items: readonly T[],\n direction: 'forward' | 'backward',\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n const current = normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n const start =\n current < 0\n ? direction === 'forward'\n ? 0\n : items.length - 1\n : direction === 'forward'\n ? current + 1\n : current - 1;\n\n const nextIndex = findEnabledIndex(items, start, direction);\n const action: ListNavigationAction = direction === 'forward' ? 'next' : 'prev';\n\n if (nextIndex >= 0) return commitIndex(items, nextIndex, action, event);\n\n if (options.loop) {\n const wrapStart = direction === 'forward' ? 0 : items.length - 1;\n const wrapped = findEnabledIndex(items, wrapStart, direction);\n\n if (wrapped >= 0) return commitIndex(items, wrapped, action, event);\n }\n\n return null;\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: ListNavigationAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n resetTypeahead();\n normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n if (action === 'first') {\n return commitIndex(items, findEnabledIndex(items, 0, 'forward'), action, event);\n }\n\n if (action === 'last') {\n return commitIndex(items, findEnabledIndex(items, items.length - 1, 'backward'), action, event);\n }\n\n return move(items, action === 'next' ? 'forward' : 'backward', event);\n };\n\n const findTypeaheadMatch = (items: readonly T[], search: string, includeCurrent: boolean): number => {\n const current = normalizeIndex(items);\n const start = current < 0 ? 0 : includeCurrent ? current : current + 1;\n\n for (let offset = 0; offset < items.length; offset++) {\n const itemIndex = (start + offset) % items.length;\n const item = items[itemIndex];\n\n if (isItemDisabled(item, itemIndex)) continue;\n\n if (options.typeahead?.getLabel(item, itemIndex).toLocaleLowerCase().startsWith(search)) {\n return itemIndex;\n }\n }\n\n return -1;\n };\n\n const handleTypeahead = (event: KeyboardEvent, items: readonly T[]): ListNavigationChange<T> | null => {\n if (!options.typeahead || event.key.length !== 1 || event.ctrlKey || event.altKey || event.metaKey) return null;\n\n const now = performance.now();\n const key = event.key.toLocaleLowerCase();\n\n if (now - lastTypeaheadAt >= typeaheadDelay) typeaheadBuffer = '';\n\n lastTypeaheadAt = now;\n\n const candidate = typeaheadBuffer + key;\n const repeatedCharacter = candidate.length > 1 && [...candidate].every((character) => character === key);\n typeaheadBuffer = repeatedCharacter ? key : candidate;\n\n let nextIndex = findTypeaheadMatch(items, typeaheadBuffer, typeaheadBuffer.length > 1);\n\n if (nextIndex < 0 && typeaheadBuffer.length > 1) {\n typeaheadBuffer = key;\n nextIndex = findTypeaheadMatch(items, typeaheadBuffer, false);\n }\n\n if (nextIndex < 0) return null;\n\n return commitIndex(items, nextIndex, 'typeahead', event);\n };\n\n const set = (nextIndex: number): number => {\n const items = options.getItems();\n normalizeIndex(items);\n resetTypeahead();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n\n return index;\n }\n\n const clamped = Math.min(nextIndex, items.length - 1);\n\n if (!isUsableIndex(items, clamped)) {\n index = -1;\n\n return index;\n }\n\n index = clamped;\n\n return index;\n };\n\n const navigate = (action: ListNavigationAction): ListNavigationChange<T> | null => {\n return navigateWithItems(options.getItems(), action);\n };\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = normalizeIndex(items);\n\n return current >= 0 ? items[current] : undefined;\n };\n\n const reset = (): void => {\n index = -1;\n resetTypeahead();\n };\n\n const getIndex = (): number => {\n return normalizeIndex(options.getItems());\n };\n\n const resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n const resolveKeyAction = (event: KeyboardEvent): ListNavigationAction | undefined => {\n const keys = options.keys;\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n const defaults = keyTable[resolve(options.orientation, 'vertical')];\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n if ((keys?.[action] ?? defaults[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): ListKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const items = options.getItems();\n normalizeIndex(items);\n\n const action = resolveKeyAction(event);\n\n if (action) {\n const change = navigateWithItems(items, action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n }\n\n const change = handleTypeahead(event, items);\n if (!change) return null;\n if (options.typeahead?.preventDefault) event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","export type FocusableElement = HTMLElement | SVGElement;\nexport type FocusTarget = FocusableElement | null | undefined | (() => FocusableElement | null | undefined);\n\nexport type RestoreFocusOptions = {\n fallback?: FocusTarget;\n preventScroll?: boolean;\n};\n\nexport type CaptureFocusOptions = RestoreFocusOptions & {\n signal?: AbortSignal;\n};\n\nexport type FocusRestorer = () => boolean;\n\nconst resolveTarget = (target: FocusTarget): FocusableElement | null | undefined => {\n return typeof target === 'function' ? target() : target;\n};\n\nconst getDeepActiveElement = (rootDocument: Document): Element | null => {\n let active: Element | null = rootDocument.activeElement;\n\n while (active instanceof HTMLElement && active.shadowRoot?.activeElement) {\n active = active.shadowRoot.activeElement;\n }\n\n return active;\n};\n\nconst isConnected = (target: FocusableElement): boolean => {\n // `isConnected` pierces shadow boundaries (shadow-including root is the document);\n // `ownerDocument.contains` does NOT — it would reject elements inside shadow roots.\n return target.isConnected;\n};\n\nconst isDisabled = (target: FocusableElement): boolean => {\n return target instanceof HTMLElement && 'disabled' in target && Boolean((target as HTMLInputElement).disabled);\n};\n\nconst isInert = (target: FocusableElement): boolean => {\n if (target.closest('[inert]')) return true;\n\n return target instanceof HTMLElement && target.inert;\n};\n\nconst canRestoreTo = (target: FocusableElement): boolean => {\n return isConnected(target) && !isDisabled(target) && !isInert(target);\n};\n\nconst focusAndVerify = (target: FocusableElement, preventScroll: boolean | undefined): boolean => {\n try {\n target.focus({ preventScroll });\n return getDeepActiveElement(target.ownerDocument) === target;\n } catch {\n return false;\n }\n};\n\nexport const restoreFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n let next: FocusableElement | null | undefined;\n try {\n next = resolveTarget(target);\n } catch {\n next = null;\n }\n\n if (next && canRestoreTo(next) && focusAndVerify(next, options.preventScroll)) {\n return true;\n }\n\n if (!options.fallback) return false;\n\n let fallback: FocusableElement | null | undefined;\n try {\n fallback = resolveTarget(options.fallback);\n } catch {\n return false;\n }\n\n if (!fallback || !canRestoreTo(fallback)) return false;\n\n return focusAndVerify(fallback, options.preventScroll);\n};\n\n/**\n * Hands focus to `target` when focus has been lost to the document body — the state left\n * behind when the focused element unmounts mid-swap (browsing a dialog, paging a list),\n * where keydown would never reach a handler and every shortcut dies. Returns `true` when\n * focus was rescued; `false` when focus is already on a real element or the target cannot\n * take focus (the `fallback` option of {@link RestoreFocusOptions} still applies).\n */\nexport const rescueFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n const active = getDeepActiveElement(document);\n\n if (active && active !== document.body) return false;\n\n return restoreFocus(target, options);\n};\n\nexport const captureFocus = (options: CaptureFocusOptions = {}): FocusRestorer => {\n let captured = getDeepActiveElement(document) as FocusableElement | null;\n let available = !options.signal?.aborted;\n\n if (!available) captured = null;\n\n const cancel = (): void => {\n available = false;\n captured = null;\n };\n\n if (!options.signal?.aborted) {\n options.signal?.addEventListener('abort', cancel, { once: true });\n }\n\n return () => {\n if (!available) return false;\n\n available = false;\n options.signal?.removeEventListener('abort', cancel);\n\n const target = captured;\n captured = null;\n\n if (target) {\n return restoreFocus(target, {\n fallback: options.fallback,\n preventScroll: options.preventScroll,\n });\n }\n\n if (options.fallback) {\n return restoreFocus(options.fallback, {\n preventScroll: options.preventScroll,\n });\n }\n\n return false;\n };\n};\n"],"mappings":"qGACA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Q,SAClB,cAAe,EAAQ,aACzB,CAAC,EAGC,EAAQ,SACH,aAAa,EAAQ,SAAU,CACpC,cAAe,EAAQ,aACzB,CAAC,EAGI,EACT,CACF"}
@@ -1,2 +1,2 @@
1
- var Focus=(function(e){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var t={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowRight`],prev:[`ArrowUp`,`ArrowLeft`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowRight`],prev:[`ArrowLeft`]},vertical:{first:[`Home`],last:[`End`],next:[`ArrowDown`],prev:[`ArrowUp`]}},n={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowLeft`],prev:[`ArrowUp`,`ArrowRight`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowLeft`],prev:[`ArrowRight`]},vertical:t.vertical},r=500,i=(e,t,n)=>{for(let r=t;r<e.length;r++)if(n(e[r],r))return r;return-1},a=(e,t,n)=>{for(let r=t;r>=0;r--)if(n(e[r],r))return r;return-1},o=e=>{if(e===void 0)return r;if(!Number.isFinite(e)||e<=0)throw RangeError(`Typeahead delay must be a positive finite number`);return e},s=e=>{let r=o(e.typeahead?.delayMs),s=new Map;for(let t of[`next`,`prev`,`first`,`last`])for(let n of e.keys?.[t]??[]){let e=s.get(n);if(e&&e!==t)throw RangeError(`Key "${n}" is assigned to both ${e} and ${t}`);s.set(n,t)}let c=-1,l=``,u=0,d=(t,n)=>e.isItemDisabled?.(t,n)??!1,f=(e,t)=>t>=0&&t<e.length&&!d(e[t],t),p=e=>(f(e,c)||(c=-1),c),m=()=>{l=``,u=0},h=(e,t,n,r)=>f(e,t)?(c=t,Object.freeze({action:n,event:r,index:t,item:e[t]})):(c=-1,null),g=(e,t,n)=>n===`forward`?i(e,t,(e,t)=>!d(e,t)):a(e,t,(e,t)=>!d(e,t)),_=(t,n,r)=>{let i=p(t);if(!t.length)return c=-1,null;let a=i<0?n===`forward`?0:t.length-1:n===`forward`?i+1:i-1,o=g(t,a,n),s=n===`forward`?`next`:`prev`;if(o>=0)return h(t,o,s,r);if(e.loop){let e=n===`forward`?0:t.length-1,i=g(t,e,n);if(i>=0)return h(t,i,s,r)}return null},v=(e,t,n)=>(m(),p(e),e.length?t===`first`?h(e,g(e,0,`forward`),t,n):t===`last`?h(e,g(e,e.length-1,`backward`),t,n):_(e,t===`next`?`forward`:`backward`,n):(c=-1,null)),y=(t,n,r)=>{let i=p(t),a=i<0?0:r?i:i+1;for(let r=0;r<t.length;r++){let i=(a+r)%t.length,o=t[i];if(!d(o,i)&&e.typeahead?.getLabel(o,i).toLocaleLowerCase().startsWith(n))return i}return-1},b=(t,n)=>{if(!e.typeahead||t.key.length!==1||t.ctrlKey||t.altKey||t.metaKey)return null;let i=performance.now(),a=t.key.toLocaleLowerCase();i-u>=r&&(l=``),u=i;let o=l+a;l=o.length>1&&[...o].every(e=>e===a)?a:o;let s=y(n,l,l.length>1);return s<0&&l.length>1&&(l=a,s=y(n,l,!1)),s<0?null:h(n,s,`typeahead`,t)},x=t=>{let n=e.getItems();if(p(n),m(),!Number.isInteger(t)||t<0||!n.length)return c=-1,c;let r=Math.min(t,n.length-1);return f(n,r)?(c=r,c):(c=-1,c)},S=t=>v(e.getItems(),t),C=()=>{let t=e.getItems(),n=p(t);return n>=0?t[n]:void 0},w=()=>{c=-1,m()},T=()=>p(e.getItems()),E=(e,t)=>typeof e==`function`?e():e??t,D=r=>{let i=e.keys,a=(E(e.direction,`ltr`)===`rtl`?n:t)[E(e.orientation,`vertical`)];for(let e of[`next`,`prev`,`first`,`last`])if((i?.[e]??a[e]).includes(r))return e};return{getActiveItem:C,getIndex:T,handleKeydown:t=>{if(t.defaultPrevented||t.isComposing||E(e.disabled,!1))return null;let n=e.getItems();p(n);let r=D(t.key);if(r){let e=v(n,r,t);return t.preventDefault(),Object.freeze({change:e,handled:!0})}let i=b(t,n);return i?(e.typeahead?.preventDefault&&t.preventDefault(),Object.freeze({change:i,handled:!0})):null},navigate:S,reset:w,set:x}},c=e=>typeof e==`function`?e():e,l=e=>{let t=e.activeElement;for(;t instanceof HTMLElement&&t.shadowRoot?.activeElement;)t=t.shadowRoot.activeElement;return t},u=e=>e.isConnected,d=e=>e instanceof HTMLElement&&`disabled`in e&&!!e.disabled,f=e=>e.closest(`[inert]`)?!0:e instanceof HTMLElement&&e.inert,p=e=>u(e)&&!d(e)&&!f(e),m=(e,t)=>{try{return e.focus({preventScroll:t}),l(e.ownerDocument)===e}catch{return!1}},h=(e,t={})=>{let n;try{n=c(e)}catch{n=null}if(n&&p(n)&&m(n,t.preventScroll))return!0;if(!t.fallback)return!1;let r;try{r=c(t.fallback)}catch{return!1}return!r||!p(r)?!1:m(r,t.preventScroll)};return e.captureFocus=(e={})=>{let t=l(document),n=!e.signal?.aborted;n||(t=null);let r=()=>{n=!1,t=null};return e.signal?.aborted||e.signal?.addEventListener(`abort`,r,{once:!0}),()=>{if(!n)return!1;n=!1,e.signal?.removeEventListener(`abort`,r);let i=t;return t=null,i?h(i,{fallback:e.fallback,preventScroll:e.preventScroll}):e.fallback?h(e.fallback,{preventScroll:e.preventScroll}):!1}},e.createListNavigation=s,e.restoreFocus=h,e})({});
1
+ var Focus=(function(e,t){Object.defineProperty(e,Symbol.toStringTag,{value:`Module`});var FocusError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},FocusConfigError=class extends FocusError{},n={first:[`Home`],last:[`End`],next:[`ArrowRight`],nextRow:[`ArrowDown`],prev:[`ArrowLeft`],prevRow:[`ArrowUp`]},r={...n,next:[`ArrowLeft`],prev:[`ArrowRight`]},i=[`first`,`last`,`next`,`nextRow`,`prev`,`prevRow`],resolve=(e,t)=>typeof e==`function`?e():e??t,createGridNavigation=e=>{let a=new Map;for(let t of i)for(let n of e.keys?.[t]??[]){let e=a.get(n);if(e&&e!==t)throw new FocusConfigError(`Key "${n}" is assigned to both ${e} and ${t}`);a.set(n,t)}let o=-1,resolveColumns=()=>{let t=resolve(e.columns,1);if(!Number.isInteger(t)||t<1)throw new FocusConfigError(`columns must resolve to a positive integer, got: ${t}`);return t},activeIndex=t=>{let n=e.getActiveIndex?.();return n!==void 0&&(o=n>=0&&n<t.length?n:-1),o},commitIndex=(e,t,n,r)=>(o=t,Object.freeze({action:n,event:r,index:t,item:e[t]})),navigateWithItems=(t,n,r)=>{if(!t.length)return o=-1,null;if(n===`first`)return commitIndex(t,0,n,r);if(n===`last`)return commitIndex(t,t.length-1,n,r);let i=resolveColumns(),a=n===`next`?1:n===`prev`?-1:n===`nextRow`?i:-i,s=activeIndex(t),c=s<0?a>0?0:t.length-1:s+a;if(e.loop)return commitIndex(t,(c%t.length+t.length)%t.length,n,r);let l=Math.min(t.length-1,Math.max(0,c));return l===s?null:commitIndex(t,l,n,r)},resolveKeyAction=a=>{let o=resolve(e.direction,`ltr`)===`rtl`?r:n;for(let n of i)if((e.keys?.[n]??o[n]).some(e=>(0,t.matchKey)(a,e)))return n},handleKeydown=t=>{if(t.defaultPrevented||t.isComposing||resolve(e.disabled,!1))return null;let n=resolveKeyAction(t);if(!n)return null;let r=navigateWithItems(e.getItems(),n,t);return t.preventDefault(),Object.freeze({change:r,handled:!0})},navigate=t=>navigateWithItems(e.getItems(),t),set=t=>{let n=e.getItems();if(!Number.isInteger(t)||t<0||!n.length){o=-1;return}o=Math.min(t,n.length-1)},reset=()=>{o=-1},getIndex=()=>activeIndex(e.getItems()),getActiveItem=()=>{let t=e.getItems(),n=activeIndex(t);return n>=0?t[n]:void 0};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}},a={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowRight`],prev:[`ArrowUp`,`ArrowLeft`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowRight`],prev:[`ArrowLeft`]},vertical:{first:[`Home`],last:[`End`],next:[`ArrowDown`],prev:[`ArrowUp`]}},o={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowLeft`],prev:[`ArrowUp`,`ArrowRight`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowLeft`],prev:[`ArrowRight`]},vertical:a.vertical},s=500,findForward=(e,t,n)=>{for(let r=t;r<e.length;r++)if(n(e[r],r))return r;return-1},findBackward=(e,t,n)=>{for(let r=t;r>=0;r--)if(n(e[r],r))return r;return-1},resolveTypeaheadDelay=e=>{if(e===void 0)return s;if(!Number.isFinite(e)||e<=0)throw new FocusConfigError(`Typeahead delay must be a positive finite number`);return e},createListNavigation=e=>{let n=resolveTypeaheadDelay(e.typeahead?.delayMs),r=new Map;for(let t of[`next`,`prev`,`first`,`last`])for(let n of e.keys?.[t]??[]){let e=r.get(n);if(e&&e!==t)throw new FocusConfigError(`Key "${n}" is assigned to both ${e} and ${t}`);r.set(n,t)}let i=-1,s=``,c=0,isItemDisabled=(t,n)=>e.isItemDisabled?.(t,n)??!1,isUsableIndex=(e,t)=>t>=0&&t<e.length&&!isItemDisabled(e[t],t),normalizeIndex=e=>(isUsableIndex(e,i)||(i=-1),i),resetTypeahead=()=>{s=``,c=0},commitIndex=(e,t,n,r)=>isUsableIndex(e,t)?(i=t,Object.freeze({action:n,event:r,index:t,item:e[t]})):(i=-1,null),findEnabledIndex=(e,t,n)=>n===`forward`?findForward(e,t,(e,t)=>!isItemDisabled(e,t)):findBackward(e,t,(e,t)=>!isItemDisabled(e,t)),move=(t,n,r)=>{let a=normalizeIndex(t);if(!t.length)return i=-1,null;let o=a<0?n===`forward`?0:t.length-1:n===`forward`?a+1:a-1,s=findEnabledIndex(t,o,n),c=n===`forward`?`next`:`prev`;if(s>=0)return commitIndex(t,s,c,r);if(e.loop){let e=n===`forward`?0:t.length-1,i=findEnabledIndex(t,e,n);if(i>=0)return commitIndex(t,i,c,r)}return null},navigateWithItems=(e,t,n)=>(resetTypeahead(),normalizeIndex(e),e.length?t===`first`?commitIndex(e,findEnabledIndex(e,0,`forward`),t,n):t===`last`?commitIndex(e,findEnabledIndex(e,e.length-1,`backward`),t,n):move(e,t===`next`?`forward`:`backward`,n):(i=-1,null)),findTypeaheadMatch=(t,n,r)=>{let i=normalizeIndex(t),a=i<0?0:r?i:i+1;for(let r=0;r<t.length;r++){let i=(a+r)%t.length,o=t[i];if(!isItemDisabled(o,i)&&e.typeahead?.getLabel(o,i).toLocaleLowerCase().startsWith(n))return i}return-1},handleTypeahead=(t,r)=>{if(!e.typeahead||t.key.length!==1||t.ctrlKey||t.altKey||t.metaKey)return null;let i=performance.now(),a=t.key.toLocaleLowerCase();i-c>=n&&(s=``),c=i;let o=s+a;s=o.length>1&&[...o].every(e=>e===a)?a:o;let l=findTypeaheadMatch(r,s,s.length>1);return l<0&&s.length>1&&(s=a,l=findTypeaheadMatch(r,s,!1)),l<0?null:commitIndex(r,l,`typeahead`,t)},set=t=>{let n=e.getItems();if(normalizeIndex(n),resetTypeahead(),!Number.isInteger(t)||t<0||!n.length)return i=-1,i;let r=Math.min(t,n.length-1);return isUsableIndex(n,r)?(i=r,i):(i=-1,i)},navigate=t=>navigateWithItems(e.getItems(),t),getActiveItem=()=>{let t=e.getItems(),n=normalizeIndex(t);return n>=0?t[n]:void 0},reset=()=>{i=-1,resetTypeahead()},getIndex=()=>normalizeIndex(e.getItems()),resolve=(e,t)=>typeof e==`function`?e():e??t,resolveKeyAction=n=>{let r=e.keys,i=(resolve(e.direction,`ltr`)===`rtl`?o:a)[resolve(e.orientation,`vertical`)];for(let e of[`next`,`prev`,`first`,`last`])if((r?.[e]??i[e]).some(e=>(0,t.matchKey)(n,e)))return e},handleKeydown=t=>{if(t.defaultPrevented||t.isComposing||resolve(e.disabled,!1))return null;let n=e.getItems();normalizeIndex(n);let r=resolveKeyAction(t);if(r){let e=navigateWithItems(n,r,t);return t.preventDefault(),Object.freeze({change:e,handled:!0})}let i=handleTypeahead(t,n);return i?(e.typeahead?.preventDefault&&t.preventDefault(),Object.freeze({change:i,handled:!0})):null};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}},resolveTarget=e=>typeof e==`function`?e():e,getDeepActiveElement=e=>{let t=e.activeElement;for(;t instanceof HTMLElement&&t.shadowRoot?.activeElement;)t=t.shadowRoot.activeElement;return t},isConnected=e=>e.isConnected,isDisabled=e=>e instanceof HTMLElement&&`disabled`in e&&!!e.disabled,isInert=e=>e.closest(`[inert]`)?!0:e instanceof HTMLElement&&e.inert,canRestoreTo=e=>isConnected(e)&&!isDisabled(e)&&!isInert(e),focusAndVerify=(e,t)=>{try{return e.focus({preventScroll:t}),getDeepActiveElement(e.ownerDocument)===e}catch{return!1}},restoreFocus=(e,t={})=>{let n;try{n=resolveTarget(e)}catch{n=null}if(n&&canRestoreTo(n)&&focusAndVerify(n,t.preventScroll))return!0;if(!t.fallback)return!1;let r;try{r=resolveTarget(t.fallback)}catch{return!1}return!r||!canRestoreTo(r)?!1:focusAndVerify(r,t.preventScroll)},rescueFocus=(e,t={})=>{let n=getDeepActiveElement(document);return n&&n!==document.body?!1:restoreFocus(e,t)},captureFocus=(e={})=>{let t=getDeepActiveElement(document),n=!e.signal?.aborted;n||(t=null);let cancel=()=>{n=!1,t=null};return e.signal?.aborted||e.signal?.addEventListener(`abort`,cancel,{once:!0}),()=>{if(!n)return!1;n=!1,e.signal?.removeEventListener(`abort`,cancel);let r=t;return t=null,r?restoreFocus(r,{fallback:e.fallback,preventScroll:e.preventScroll}):e.fallback?restoreFocus(e.fallback,{preventScroll:e.preventScroll}):!1}};return e.FocusConfigError=FocusConfigError,e.FocusError=FocusError,e.captureFocus=captureFocus,e.createGridNavigation=createGridNavigation,e.createListNavigation=createListNavigation,e.rescueFocus=rescueFocus,e.restoreFocus=restoreFocus,e})({},Keymap);
2
2
  //# sourceMappingURL=focus.iife.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"focus.iife.js","names":[],"sources":["../src/list-navigation.ts","../src/restore-focus.ts"],"sourcesContent":["export type ListNavigationAction = 'first' | 'last' | 'next' | 'prev';\nexport type ListKeyAction = ListNavigationAction | 'typeahead';\n\nexport type ListNavigationChange<T> = {\n readonly action: ListKeyAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type ListKeyResult<T> = {\n readonly change: ListNavigationChange<T> | null;\n readonly handled: true;\n};\n\nexport type ListNavigationTypeaheadOptions<T> = {\n delayMs?: number;\n getLabel: (item: T, index: number) => string;\n preventDefault?: boolean;\n};\n\nexport type MaybeGetter<T> = T | (() => T);\n\nexport type ListNavigationOptions<T> = {\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n getItems: () => readonly T[];\n isItemDisabled?: (item: T, index: number) => boolean;\n keys?: Partial<Record<ListNavigationAction, readonly string[]>>;\n loop?: boolean;\n orientation?: MaybeGetter<'both' | 'horizontal' | 'vertical'>;\n typeahead?: ListNavigationTypeaheadOptions<T>;\n};\n\nexport type ListNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): ListKeyResult<T> | null;\n navigate(action: ListNavigationAction): ListNavigationChange<T> | null;\n reset(): void;\n set(index: number): number;\n};\n\nconst DEFAULT_KEYS: Record<'both' | 'horizontal' | 'vertical', Record<ListNavigationAction, readonly string[]>> = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowRight'], prev: ['ArrowUp', 'ArrowLeft'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowRight'], prev: ['ArrowLeft'] },\n vertical: { first: ['Home'], last: ['End'], next: ['ArrowDown'], prev: ['ArrowUp'] },\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowLeft'], prev: ['ArrowUp', 'ArrowRight'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowLeft'], prev: ['ArrowRight'] },\n vertical: DEFAULT_KEYS.vertical,\n};\n\nconst DEFAULT_TYPEAHEAD_DELAY_MS = 500;\n\nconst findForward = <T>(items: readonly T[], start: number, predicate: (item: T, index: number) => boolean): number => {\n for (let index = start; index < items.length; index++) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst findBackward = <T>(\n items: readonly T[],\n start: number,\n predicate: (item: T, index: number) => boolean,\n): number => {\n for (let index = start; index >= 0; index--) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst resolveTypeaheadDelay = (delay: number | undefined): number => {\n if (delay === undefined) return DEFAULT_TYPEAHEAD_DELAY_MS;\n if (!Number.isFinite(delay) || delay <= 0) throw new RangeError('Typeahead delay must be a positive finite number');\n return delay;\n};\n\nexport const createListNavigation = <T>(options: ListNavigationOptions<T>): ListNavigation<T> => {\n const typeaheadDelay = resolveTypeaheadDelay(options.typeahead?.delayMs);\n const owners = new Map<string, ListNavigationAction>();\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action) throw new RangeError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n let typeaheadBuffer = '';\n let lastTypeaheadAt = 0;\n\n const isItemDisabled = (item: T, itemIndex: number): boolean => options.isItemDisabled?.(item, itemIndex) ?? false;\n\n const isUsableIndex = (items: readonly T[], itemIndex: number): boolean =>\n itemIndex >= 0 && itemIndex < items.length && !isItemDisabled(items[itemIndex], itemIndex);\n\n const normalizeIndex = (items: readonly T[]): number => {\n if (!isUsableIndex(items, index)) index = -1;\n\n return index;\n };\n\n const resetTypeahead = (): void => {\n typeaheadBuffer = '';\n lastTypeaheadAt = 0;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: ListKeyAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n if (!isUsableIndex(items, nextIndex)) {\n index = -1;\n\n return null;\n }\n\n index = nextIndex;\n\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const findEnabledIndex = (items: readonly T[], start: number, direction: 'forward' | 'backward'): number => {\n if (direction === 'forward')\n return findForward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n\n return findBackward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n };\n\n const move = (\n items: readonly T[],\n direction: 'forward' | 'backward',\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n const current = normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n const start =\n current < 0\n ? direction === 'forward'\n ? 0\n : items.length - 1\n : direction === 'forward'\n ? current + 1\n : current - 1;\n\n const nextIndex = findEnabledIndex(items, start, direction);\n const action: ListNavigationAction = direction === 'forward' ? 'next' : 'prev';\n\n if (nextIndex >= 0) return commitIndex(items, nextIndex, action, event);\n\n if (options.loop) {\n const wrapStart = direction === 'forward' ? 0 : items.length - 1;\n const wrapped = findEnabledIndex(items, wrapStart, direction);\n\n if (wrapped >= 0) return commitIndex(items, wrapped, action, event);\n }\n\n return null;\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: ListNavigationAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n resetTypeahead();\n normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n if (action === 'first') {\n return commitIndex(items, findEnabledIndex(items, 0, 'forward'), action, event);\n }\n\n if (action === 'last') {\n return commitIndex(items, findEnabledIndex(items, items.length - 1, 'backward'), action, event);\n }\n\n return move(items, action === 'next' ? 'forward' : 'backward', event);\n };\n\n const findTypeaheadMatch = (items: readonly T[], search: string, includeCurrent: boolean): number => {\n const current = normalizeIndex(items);\n const start = current < 0 ? 0 : includeCurrent ? current : current + 1;\n\n for (let offset = 0; offset < items.length; offset++) {\n const itemIndex = (start + offset) % items.length;\n const item = items[itemIndex];\n\n if (isItemDisabled(item, itemIndex)) continue;\n\n if (options.typeahead?.getLabel(item, itemIndex).toLocaleLowerCase().startsWith(search)) {\n return itemIndex;\n }\n }\n\n return -1;\n };\n\n const handleTypeahead = (event: KeyboardEvent, items: readonly T[]): ListNavigationChange<T> | null => {\n if (!options.typeahead || event.key.length !== 1 || event.ctrlKey || event.altKey || event.metaKey) return null;\n\n const now = performance.now();\n const key = event.key.toLocaleLowerCase();\n\n if (now - lastTypeaheadAt >= typeaheadDelay) typeaheadBuffer = '';\n\n lastTypeaheadAt = now;\n\n const candidate = typeaheadBuffer + key;\n const repeatedCharacter = candidate.length > 1 && [...candidate].every((character) => character === key);\n typeaheadBuffer = repeatedCharacter ? key : candidate;\n\n let nextIndex = findTypeaheadMatch(items, typeaheadBuffer, typeaheadBuffer.length > 1);\n\n if (nextIndex < 0 && typeaheadBuffer.length > 1) {\n typeaheadBuffer = key;\n nextIndex = findTypeaheadMatch(items, typeaheadBuffer, false);\n }\n\n if (nextIndex < 0) return null;\n\n return commitIndex(items, nextIndex, 'typeahead', event);\n };\n\n const set = (nextIndex: number): number => {\n const items = options.getItems();\n normalizeIndex(items);\n resetTypeahead();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n\n return index;\n }\n\n const clamped = Math.min(nextIndex, items.length - 1);\n\n if (!isUsableIndex(items, clamped)) {\n index = -1;\n\n return index;\n }\n\n index = clamped;\n\n return index;\n };\n\n const navigate = (action: ListNavigationAction): ListNavigationChange<T> | null => {\n return navigateWithItems(options.getItems(), action);\n };\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = normalizeIndex(items);\n\n return current >= 0 ? items[current] : undefined;\n };\n\n const reset = (): void => {\n index = -1;\n resetTypeahead();\n };\n\n const getIndex = (): number => {\n return normalizeIndex(options.getItems());\n };\n\n const resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n const resolveKeyAction = (eventKey: string): ListNavigationAction | undefined => {\n const keys = options.keys;\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n const defaults = keyTable[resolve(options.orientation, 'vertical')];\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n if ((keys?.[action] ?? defaults[action]).includes(eventKey)) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): ListKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const items = options.getItems();\n normalizeIndex(items);\n\n const action = resolveKeyAction(event.key);\n\n if (action) {\n const change = navigateWithItems(items, action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n }\n\n const change = handleTypeahead(event, items);\n if (!change) return null;\n if (options.typeahead?.preventDefault) event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","export type FocusableElement = HTMLElement | SVGElement;\nexport type FocusTarget = FocusableElement | null | undefined | (() => FocusableElement | null | undefined);\n\nexport type RestoreFocusOptions = {\n fallback?: FocusTarget;\n preventScroll?: boolean;\n};\n\nexport type CaptureFocusOptions = RestoreFocusOptions & {\n signal?: AbortSignal;\n};\n\nexport type FocusRestorer = () => boolean;\n\nconst resolveTarget = (target: FocusTarget): FocusableElement | null | undefined => {\n return typeof target === 'function' ? target() : target;\n};\n\nconst getDeepActiveElement = (rootDocument: Document): Element | null => {\n let active: Element | null = rootDocument.activeElement;\n\n while (active instanceof HTMLElement && active.shadowRoot?.activeElement) {\n active = active.shadowRoot.activeElement;\n }\n\n return active;\n};\n\nconst isConnected = (target: FocusableElement): boolean => {\n // `isConnected` pierces shadow boundaries (shadow-including root is the document);\n // `ownerDocument.contains` does NOT — it would reject elements inside shadow roots.\n return target.isConnected;\n};\n\nconst isDisabled = (target: FocusableElement): boolean => {\n return target instanceof HTMLElement && 'disabled' in target && Boolean((target as HTMLInputElement).disabled);\n};\n\nconst isInert = (target: FocusableElement): boolean => {\n if (target.closest('[inert]')) return true;\n\n return target instanceof HTMLElement && target.inert;\n};\n\nconst canRestoreTo = (target: FocusableElement): boolean => {\n return isConnected(target) && !isDisabled(target) && !isInert(target);\n};\n\nconst focusAndVerify = (target: FocusableElement, preventScroll: boolean | undefined): boolean => {\n try {\n target.focus({ preventScroll });\n return getDeepActiveElement(target.ownerDocument) === target;\n } catch {\n return false;\n }\n};\n\nexport const restoreFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n let next: FocusableElement | null | undefined;\n try {\n next = resolveTarget(target);\n } catch {\n next = null;\n }\n\n if (next && canRestoreTo(next) && focusAndVerify(next, options.preventScroll)) {\n return true;\n }\n\n if (!options.fallback) return false;\n\n let fallback: FocusableElement | null | undefined;\n try {\n fallback = resolveTarget(options.fallback);\n } catch {\n return false;\n }\n\n if (!fallback || !canRestoreTo(fallback)) return false;\n\n return focusAndVerify(fallback, options.preventScroll);\n};\n\nexport const captureFocus = (options: CaptureFocusOptions = {}): FocusRestorer => {\n let captured = getDeepActiveElement(document) as FocusableElement | null;\n let available = !options.signal?.aborted;\n\n if (!available) captured = null;\n\n const cancel = (): void => {\n available = false;\n captured = null;\n };\n\n if (!options.signal?.aborted) {\n options.signal?.addEventListener('abort', cancel, { once: true });\n }\n\n return () => {\n if (!available) return false;\n\n available = false;\n options.signal?.removeEventListener('abort', cancel);\n\n const target = captured;\n captured = null;\n\n if (target) {\n return restoreFocus(target, {\n fallback: options.fallback,\n preventScroll: options.preventScroll,\n });\n }\n\n if (options.fallback) {\n return restoreFocus(options.fallback, {\n preventScroll: options.preventScroll,\n });\n }\n\n return false;\n };\n};\n"],"mappings":"oFA2CA,IAAM,EAA4G,CAChH,KAAM,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAa,YAAY,EAAG,KAAM,CAAC,UAAW,WAAW,CAAE,EAC1G,WAAY,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAY,EAAG,KAAM,CAAC,WAAW,CAAE,EACxF,SAAU,CAAE,MAAO,CAAC,MAAM,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BAAiB,QAAS,EAAQ,CAAE,KAAM,EAAK,CAAC,MAGrD,CACX,GAAI,CAAC,EAAW,MAAO,GAEvB,EAAY,GACZ,EAAQ,QAAQ,oBAAoB,QAAS,CAAM,EAEnD,IAAM,EAAS,EAgBf,MAfA,GAAW,KAEP,EACK,EAAa,EAAQ,CAC1B,SAAU,EAAQ,SAClB,cAAe,EAAQ,aACzB,CAAC,EAGC,EAAQ,SACH,EAAa,EAAQ,SAAU,CACpC,cAAe,EAAQ,aACzB,CAAC,EAGI,EACT,CACF"}
1
+ {"version":3,"file":"focus.iife.js","names":[],"sources":["../src/errors.ts","../src/grid-navigation.ts","../src/list-navigation.ts","../src/restore-focus.ts"],"sourcesContent":["/** Base class for all focus errors. Use `instanceof FocusError` to catch any focus-originated error. */\nexport class FocusError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when navigation options are invalid, such as a non-positive typeahead delay or a key assigned to two actions. */\nexport class FocusConfigError extends FocusError {}\n","import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\nimport type { MaybeGetter } from './list-navigation.js';\n\nexport type GridNavigationAction = 'first' | 'last' | 'next' | 'nextRow' | 'prev' | 'prevRow';\n\nexport type GridNavigationChange<T> = {\n readonly action: GridNavigationAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type GridKeyResult<T> = {\n readonly change: GridNavigationChange<T> | null;\n readonly handled: true;\n};\n\n/**\n * Columns per row: a fixed count, or a getter resolved on every navigation. Responsive grids\n * read a media query or computed style; measured grids read the rendered row length.\n */\nexport type GridColumns = number | (() => number);\n\nexport type GridNavigationOptions<T> = {\n /** Columns per row, resolved on every navigation. */\n columns: GridColumns;\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n /**\n * Reads the active index from the outside (usually the index of `document.activeElement`\n * among the items), so focus moved by pointer or Tab stays in sync. When omitted, the\n * navigation tracks the index internally through `set()`.\n */\n getActiveIndex?: () => number;\n getItems: () => readonly T[];\n keys?: Partial<Record<GridNavigationAction, readonly string[]>>;\n /** Wrap moves around the grid's edges by flat index. Default clamps at the edges. */\n loop?: boolean;\n};\n\nexport type GridNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): GridKeyResult<T> | null;\n navigate(action: GridNavigationAction): GridNavigationChange<T> | null;\n reset(): void;\n set(index: number): void;\n};\n\nconst DEFAULT_KEYS: Record<GridNavigationAction, readonly string[]> = {\n first: ['Home'],\n last: ['End'],\n next: ['ArrowRight'],\n nextRow: ['ArrowDown'],\n prev: ['ArrowLeft'],\n prevRow: ['ArrowUp'],\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n ...DEFAULT_KEYS,\n next: ['ArrowLeft'],\n prev: ['ArrowRight'],\n};\n\nconst GRID_ACTIONS = ['first', 'last', 'next', 'nextRow', 'prev', 'prevRow'] as const;\n\nconst resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n/**\n * Keyboard navigation over a two-dimensional grid of items: horizontal arrows step one\n * item, vertical arrows step one row (the column count), Home/End jump to the ends.\n *\n * Unlike `createListNavigation`, items are not skipped when disabled — skipping in two\n * dimensions would break row alignment. Focus (or otherwise activate) the item reported\n * by the returned change; `handleKeydown` calls `preventDefault()` for every recognized\n * key so unhandled arrows never scroll the page.\n */\nexport const createGridNavigation = <T>(options: GridNavigationOptions<T>): GridNavigation<T> => {\n const owners = new Map<string, GridNavigationAction>();\n\n for (const action of GRID_ACTIONS) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n\n const resolveColumns = (): number => {\n const columns = resolve(options.columns, 1);\n if (!Number.isInteger(columns) || columns < 1)\n throw new FocusConfigError(`columns must resolve to a positive integer, got: ${columns}`);\n return columns;\n };\n\n const activeIndex = (items: readonly T[]): number => {\n const derived = options.getActiveIndex?.();\n if (derived !== undefined) index = derived >= 0 && derived < items.length ? derived : -1;\n return index;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> => {\n index = nextIndex;\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> | null => {\n if (!items.length) {\n index = -1;\n return null;\n }\n\n if (action === 'first') return commitIndex(items, 0, action, event);\n if (action === 'last') return commitIndex(items, items.length - 1, action, event);\n\n const columns = resolveColumns();\n const offset = action === 'next' ? 1 : action === 'prev' ? -1 : action === 'nextRow' ? columns : -columns;\n const current = activeIndex(items);\n const base = current < 0 ? (offset > 0 ? 0 : items.length - 1) : current + offset;\n\n if (options.loop) {\n return commitIndex(items, ((base % items.length) + items.length) % items.length, action, event);\n }\n\n const clamped = Math.min(items.length - 1, Math.max(0, base));\n if (clamped === current) return null;\n\n return commitIndex(items, clamped, action, event);\n };\n\n const resolveKeyAction = (event: KeyboardEvent): GridNavigationAction | undefined => {\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n\n for (const action of GRID_ACTIONS) {\n if ((options.keys?.[action] ?? keyTable[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): GridKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const action = resolveKeyAction(event);\n if (!action) return null;\n\n const change = navigateWithItems(options.getItems(), action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n const navigate = (action: GridNavigationAction): GridNavigationChange<T> | null =>\n navigateWithItems(options.getItems(), action);\n\n const set = (nextIndex: number): void => {\n const items = options.getItems();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n return;\n }\n\n index = Math.min(nextIndex, items.length - 1);\n };\n\n const reset = (): void => {\n index = -1;\n };\n\n const getIndex = (): number => activeIndex(options.getItems());\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = activeIndex(items);\n return current >= 0 ? items[current] : undefined;\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\n\nexport type ListNavigationAction = 'first' | 'last' | 'next' | 'prev';\nexport type ListKeyAction = ListNavigationAction | 'typeahead';\n\nexport type ListNavigationChange<T> = {\n readonly action: ListKeyAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type ListKeyResult<T> = {\n readonly change: ListNavigationChange<T> | null;\n readonly handled: true;\n};\n\nexport type ListNavigationTypeaheadOptions<T> = {\n delayMs?: number;\n getLabel: (item: T, index: number) => string;\n preventDefault?: boolean;\n};\n\nexport type MaybeGetter<T> = T | (() => T);\n\nexport type ListNavigationOptions<T> = {\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n getItems: () => readonly T[];\n isItemDisabled?: (item: T, index: number) => boolean;\n keys?: Partial<Record<ListNavigationAction, readonly string[]>>;\n loop?: boolean;\n orientation?: MaybeGetter<'both' | 'horizontal' | 'vertical'>;\n typeahead?: ListNavigationTypeaheadOptions<T>;\n};\n\nexport type ListNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): ListKeyResult<T> | null;\n navigate(action: ListNavigationAction): ListNavigationChange<T> | null;\n reset(): void;\n set(index: number): number;\n};\n\nconst DEFAULT_KEYS: Record<'both' | 'horizontal' | 'vertical', Record<ListNavigationAction, readonly string[]>> = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowRight'], prev: ['ArrowUp', 'ArrowLeft'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowRight'], prev: ['ArrowLeft'] },\n vertical: { first: ['Home'], last: ['End'], next: ['ArrowDown'], prev: ['ArrowUp'] },\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowLeft'], prev: ['ArrowUp', 'ArrowRight'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowLeft'], prev: ['ArrowRight'] },\n vertical: DEFAULT_KEYS.vertical,\n};\n\nconst DEFAULT_TYPEAHEAD_DELAY_MS = 500;\n\nconst findForward = <T>(items: readonly T[], start: number, predicate: (item: T, index: number) => boolean): number => {\n for (let index = start; index < items.length; index++) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst findBackward = <T>(\n items: readonly T[],\n start: number,\n predicate: (item: T, index: number) => boolean,\n): number => {\n for (let index = start; index >= 0; index--) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst resolveTypeaheadDelay = (delay: number | undefined): number => {\n if (delay === undefined) return DEFAULT_TYPEAHEAD_DELAY_MS;\n if (!Number.isFinite(delay) || delay <= 0)\n throw new FocusConfigError('Typeahead delay must be a positive finite number');\n return delay;\n};\n\nexport const createListNavigation = <T>(options: ListNavigationOptions<T>): ListNavigation<T> => {\n const typeaheadDelay = resolveTypeaheadDelay(options.typeahead?.delayMs);\n const owners = new Map<string, ListNavigationAction>();\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n let typeaheadBuffer = '';\n let lastTypeaheadAt = 0;\n\n const isItemDisabled = (item: T, itemIndex: number): boolean => options.isItemDisabled?.(item, itemIndex) ?? false;\n\n const isUsableIndex = (items: readonly T[], itemIndex: number): boolean =>\n itemIndex >= 0 && itemIndex < items.length && !isItemDisabled(items[itemIndex], itemIndex);\n\n const normalizeIndex = (items: readonly T[]): number => {\n if (!isUsableIndex(items, index)) index = -1;\n\n return index;\n };\n\n const resetTypeahead = (): void => {\n typeaheadBuffer = '';\n lastTypeaheadAt = 0;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: ListKeyAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n if (!isUsableIndex(items, nextIndex)) {\n index = -1;\n\n return null;\n }\n\n index = nextIndex;\n\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const findEnabledIndex = (items: readonly T[], start: number, direction: 'forward' | 'backward'): number => {\n if (direction === 'forward')\n return findForward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n\n return findBackward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n };\n\n const move = (\n items: readonly T[],\n direction: 'forward' | 'backward',\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n const current = normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n const start =\n current < 0\n ? direction === 'forward'\n ? 0\n : items.length - 1\n : direction === 'forward'\n ? current + 1\n : current - 1;\n\n const nextIndex = findEnabledIndex(items, start, direction);\n const action: ListNavigationAction = direction === 'forward' ? 'next' : 'prev';\n\n if (nextIndex >= 0) return commitIndex(items, nextIndex, action, event);\n\n if (options.loop) {\n const wrapStart = direction === 'forward' ? 0 : items.length - 1;\n const wrapped = findEnabledIndex(items, wrapStart, direction);\n\n if (wrapped >= 0) return commitIndex(items, wrapped, action, event);\n }\n\n return null;\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: ListNavigationAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n resetTypeahead();\n normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n if (action === 'first') {\n return commitIndex(items, findEnabledIndex(items, 0, 'forward'), action, event);\n }\n\n if (action === 'last') {\n return commitIndex(items, findEnabledIndex(items, items.length - 1, 'backward'), action, event);\n }\n\n return move(items, action === 'next' ? 'forward' : 'backward', event);\n };\n\n const findTypeaheadMatch = (items: readonly T[], search: string, includeCurrent: boolean): number => {\n const current = normalizeIndex(items);\n const start = current < 0 ? 0 : includeCurrent ? current : current + 1;\n\n for (let offset = 0; offset < items.length; offset++) {\n const itemIndex = (start + offset) % items.length;\n const item = items[itemIndex];\n\n if (isItemDisabled(item, itemIndex)) continue;\n\n if (options.typeahead?.getLabel(item, itemIndex).toLocaleLowerCase().startsWith(search)) {\n return itemIndex;\n }\n }\n\n return -1;\n };\n\n const handleTypeahead = (event: KeyboardEvent, items: readonly T[]): ListNavigationChange<T> | null => {\n if (!options.typeahead || event.key.length !== 1 || event.ctrlKey || event.altKey || event.metaKey) return null;\n\n const now = performance.now();\n const key = event.key.toLocaleLowerCase();\n\n if (now - lastTypeaheadAt >= typeaheadDelay) typeaheadBuffer = '';\n\n lastTypeaheadAt = now;\n\n const candidate = typeaheadBuffer + key;\n const repeatedCharacter = candidate.length > 1 && [...candidate].every((character) => character === key);\n typeaheadBuffer = repeatedCharacter ? key : candidate;\n\n let nextIndex = findTypeaheadMatch(items, typeaheadBuffer, typeaheadBuffer.length > 1);\n\n if (nextIndex < 0 && typeaheadBuffer.length > 1) {\n typeaheadBuffer = key;\n nextIndex = findTypeaheadMatch(items, typeaheadBuffer, false);\n }\n\n if (nextIndex < 0) return null;\n\n return commitIndex(items, nextIndex, 'typeahead', event);\n };\n\n const set = (nextIndex: number): number => {\n const items = options.getItems();\n normalizeIndex(items);\n resetTypeahead();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n\n return index;\n }\n\n const clamped = Math.min(nextIndex, items.length - 1);\n\n if (!isUsableIndex(items, clamped)) {\n index = -1;\n\n return index;\n }\n\n index = clamped;\n\n return index;\n };\n\n const navigate = (action: ListNavigationAction): ListNavigationChange<T> | null => {\n return navigateWithItems(options.getItems(), action);\n };\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = normalizeIndex(items);\n\n return current >= 0 ? items[current] : undefined;\n };\n\n const reset = (): void => {\n index = -1;\n resetTypeahead();\n };\n\n const getIndex = (): number => {\n return normalizeIndex(options.getItems());\n };\n\n const resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n const resolveKeyAction = (event: KeyboardEvent): ListNavigationAction | undefined => {\n const keys = options.keys;\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n const defaults = keyTable[resolve(options.orientation, 'vertical')];\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n if ((keys?.[action] ?? defaults[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): ListKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const items = options.getItems();\n normalizeIndex(items);\n\n const action = resolveKeyAction(event);\n\n if (action) {\n const change = navigateWithItems(items, action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n }\n\n const change = handleTypeahead(event, items);\n if (!change) return null;\n if (options.typeahead?.preventDefault) event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","export type FocusableElement = HTMLElement | SVGElement;\nexport type FocusTarget = FocusableElement | null | undefined | (() => FocusableElement | null | undefined);\n\nexport type RestoreFocusOptions = {\n fallback?: FocusTarget;\n preventScroll?: boolean;\n};\n\nexport type CaptureFocusOptions = RestoreFocusOptions & {\n signal?: AbortSignal;\n};\n\nexport type FocusRestorer = () => boolean;\n\nconst resolveTarget = (target: FocusTarget): FocusableElement | null | undefined => {\n return typeof target === 'function' ? target() : target;\n};\n\nconst getDeepActiveElement = (rootDocument: Document): Element | null => {\n let active: Element | null = rootDocument.activeElement;\n\n while (active instanceof HTMLElement && active.shadowRoot?.activeElement) {\n active = active.shadowRoot.activeElement;\n }\n\n return active;\n};\n\nconst isConnected = (target: FocusableElement): boolean => {\n // `isConnected` pierces shadow boundaries (shadow-including root is the document);\n // `ownerDocument.contains` does NOT — it would reject elements inside shadow roots.\n return target.isConnected;\n};\n\nconst isDisabled = (target: FocusableElement): boolean => {\n return target instanceof HTMLElement && 'disabled' in target && Boolean((target as HTMLInputElement).disabled);\n};\n\nconst isInert = (target: FocusableElement): boolean => {\n if (target.closest('[inert]')) return true;\n\n return target instanceof HTMLElement && target.inert;\n};\n\nconst canRestoreTo = (target: FocusableElement): boolean => {\n return isConnected(target) && !isDisabled(target) && !isInert(target);\n};\n\nconst focusAndVerify = (target: FocusableElement, preventScroll: boolean | undefined): boolean => {\n try {\n target.focus({ preventScroll });\n return getDeepActiveElement(target.ownerDocument) === target;\n } catch {\n return false;\n }\n};\n\nexport const restoreFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n let next: FocusableElement | null | undefined;\n try {\n next = resolveTarget(target);\n } catch {\n next = null;\n }\n\n if (next && canRestoreTo(next) && focusAndVerify(next, options.preventScroll)) {\n return true;\n }\n\n if (!options.fallback) return false;\n\n let fallback: FocusableElement | null | undefined;\n try {\n fallback = resolveTarget(options.fallback);\n } catch {\n return false;\n }\n\n if (!fallback || !canRestoreTo(fallback)) return false;\n\n return focusAndVerify(fallback, options.preventScroll);\n};\n\n/**\n * Hands focus to `target` when focus has been lost to the document body — the state left\n * behind when the focused element unmounts mid-swap (browsing a dialog, paging a list),\n * where keydown would never reach a handler and every shortcut dies. Returns `true` when\n * focus was rescued; `false` when focus is already on a real element or the target cannot\n * take focus (the `fallback` option of {@link RestoreFocusOptions} still applies).\n */\nexport const rescueFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n const active = getDeepActiveElement(document);\n\n if (active && active !== document.body) return false;\n\n return restoreFocus(target, options);\n};\n\nexport const captureFocus = (options: CaptureFocusOptions = {}): FocusRestorer => {\n let captured = getDeepActiveElement(document) as FocusableElement | null;\n let available = !options.signal?.aborted;\n\n if (!available) captured = null;\n\n const cancel = (): void => {\n available = false;\n captured = null;\n };\n\n if (!options.signal?.aborted) {\n options.signal?.addEventListener('abort', cancel, { once: true });\n }\n\n return () => {\n if (!available) return false;\n\n available = false;\n options.signal?.removeEventListener('abort', cancel);\n\n const target = captured;\n captured = null;\n\n if (target) {\n return restoreFocus(target, {\n fallback: options.fallback,\n preventScroll: options.preventScroll,\n });\n }\n\n if (options.fallback) {\n return restoreFocus(options.fallback, {\n preventScroll: options.preventScroll,\n });\n }\n\n return false;\n };\n};\n"],"mappings":"sFACA,IAAa,WAAb,cAAgC,KAAM,CACpC,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,iBAAb,cAAsC,UAAW,CAAC,ECyC5C,EAAgE,CACpE,MAAO,CAAC,MAAM,EACd,KAAM,CAAC,KAAK,EACZ,KAAM,CAAC,YAAY,EACnB,QAAS,CAAC,WAAW,EACrB,KAAM,CAAC,WAAW,EAClB,QAAS,CAAC,SAAS,CACrB,EAEM,EAAwC,CAC5C,GAAG,EACH,KAAM,CAAC,WAAW,EAClB,KAAM,CAAC,YAAY,CACrB,EAEM,EAAe,CAAC,QAAS,OAAQ,OAAQ,UAAW,OAAQ,SAAS,EAErE,SAAc,EAAmC,IACrD,OAAO,GAAU,WAAc,EAAkB,EAAK,GAAS,EAWpD,qBAA2B,GAAyD,CAC/F,IAAM,EAAS,IAAI,IAEnB,IAAK,IAAM,KAAU,EACnB,IAAK,IAAM,KA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U,EAAQ,aAAa,CACvD,EASa,aAAe,EAAqB,EAA+B,CAAC,IAAe,CAC9F,IAAM,EAAS,qBAAqB,QAAQ,EAI5C,OAFI,GAAU,IAAW,SAAS,KAAa,GAExC,aAAa,EAAQ,CAAO,CACrC,EAEa,cAAgB,EAA+B,CAAC,IAAqB,CAChF,IAAI,EAAW,qBAAqB,QAAQ,EACxC,EAAY,CAAC,EAAQ,QAAQ,QAE5B,IAAW,EAAW,MAE3B,IAAM,WAAqB,CACzB,EAAY,GACZ,EAAW,IACb,EAMA,OAJK,EAAQ,QAAQ,SACnB,EAAQ,QAAQ,iBAAiB,QAAS,OAAQ,CAAE,KAAM,EAAK,CAAC,MAGrD,CACX,GAAI,CAAC,EAAW,MAAO,GAEvB,EAAY,GACZ,EAAQ,QAAQ,oBAAoB,QAAS,MAAM,EAEnD,IAAM,EAAS,EAgBf,MAfA,GAAW,KAEP,EACK,aAAa,EAAQ,CAC1B,SAAU,EAAQ,SAClB,cAAe,EAAQ,aACzB,CAAC,EAGC,EAAQ,SACH,aAAa,EAAQ,SAAU,CACpC,cAAe,EAAQ,aACzB,CAAC,EAGI,EACT,CACF"}
package/dist/focus.js CHANGED
@@ -1,2 +1,2 @@
1
- var e={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowRight`],prev:[`ArrowUp`,`ArrowLeft`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowRight`],prev:[`ArrowLeft`]},vertical:{first:[`Home`],last:[`End`],next:[`ArrowDown`],prev:[`ArrowUp`]}},t={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowLeft`],prev:[`ArrowUp`,`ArrowRight`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowLeft`],prev:[`ArrowRight`]},vertical:e.vertical},n=500,r=(e,t,n)=>{for(let r=t;r<e.length;r++)if(n(e[r],r))return r;return-1},i=(e,t,n)=>{for(let r=t;r>=0;r--)if(n(e[r],r))return r;return-1},a=e=>{if(e===void 0)return n;if(!Number.isFinite(e)||e<=0)throw RangeError(`Typeahead delay must be a positive finite number`);return e},o=n=>{let o=a(n.typeahead?.delayMs),s=new Map;for(let e of[`next`,`prev`,`first`,`last`])for(let t of n.keys?.[e]??[]){let n=s.get(t);if(n&&n!==e)throw RangeError(`Key "${t}" is assigned to both ${n} and ${e}`);s.set(t,e)}let c=-1,l=``,u=0,d=(e,t)=>n.isItemDisabled?.(e,t)??!1,f=(e,t)=>t>=0&&t<e.length&&!d(e[t],t),p=e=>(f(e,c)||(c=-1),c),m=()=>{l=``,u=0},h=(e,t,n,r)=>f(e,t)?(c=t,Object.freeze({action:n,event:r,index:t,item:e[t]})):(c=-1,null),g=(e,t,n)=>n===`forward`?r(e,t,(e,t)=>!d(e,t)):i(e,t,(e,t)=>!d(e,t)),_=(e,t,r)=>{let i=p(e);if(!e.length)return c=-1,null;let a=i<0?t===`forward`?0:e.length-1:t===`forward`?i+1:i-1,o=g(e,a,t),s=t===`forward`?`next`:`prev`;if(o>=0)return h(e,o,s,r);if(n.loop){let n=t===`forward`?0:e.length-1,i=g(e,n,t);if(i>=0)return h(e,i,s,r)}return null},v=(e,t,n)=>(m(),p(e),e.length?t===`first`?h(e,g(e,0,`forward`),t,n):t===`last`?h(e,g(e,e.length-1,`backward`),t,n):_(e,t===`next`?`forward`:`backward`,n):(c=-1,null)),y=(e,t,r)=>{let i=p(e),a=i<0?0:r?i:i+1;for(let r=0;r<e.length;r++){let i=(a+r)%e.length,o=e[i];if(!d(o,i)&&n.typeahead?.getLabel(o,i).toLocaleLowerCase().startsWith(t))return i}return-1},b=(e,t)=>{if(!n.typeahead||e.key.length!==1||e.ctrlKey||e.altKey||e.metaKey)return null;let r=performance.now(),i=e.key.toLocaleLowerCase();r-u>=o&&(l=``),u=r;let a=l+i;l=a.length>1&&[...a].every(e=>e===i)?i:a;let s=y(t,l,l.length>1);return s<0&&l.length>1&&(l=i,s=y(t,l,!1)),s<0?null:h(t,s,`typeahead`,e)},x=e=>{let t=n.getItems();if(p(t),m(),!Number.isInteger(e)||e<0||!t.length)return c=-1,c;let r=Math.min(e,t.length-1);return f(t,r)?(c=r,c):(c=-1,c)},S=e=>v(n.getItems(),e),C=()=>{let e=n.getItems(),t=p(e);return t>=0?e[t]:void 0},w=()=>{c=-1,m()},T=()=>p(n.getItems()),E=(e,t)=>typeof e==`function`?e():e??t,D=r=>{let i=n.keys,a=(E(n.direction,`ltr`)===`rtl`?t:e)[E(n.orientation,`vertical`)];for(let e of[`next`,`prev`,`first`,`last`])if((i?.[e]??a[e]).includes(r))return e};return{getActiveItem:C,getIndex:T,handleKeydown:e=>{if(e.defaultPrevented||e.isComposing||E(n.disabled,!1))return null;let t=n.getItems();p(t);let r=D(e.key);if(r){let n=v(t,r,e);return e.preventDefault(),Object.freeze({change:n,handled:!0})}let i=b(e,t);return i?(n.typeahead?.preventDefault&&e.preventDefault(),Object.freeze({change:i,handled:!0})):null},navigate:S,reset:w,set:x}},s=e=>typeof e==`function`?e():e,c=e=>{let t=e.activeElement;for(;t instanceof HTMLElement&&t.shadowRoot?.activeElement;)t=t.shadowRoot.activeElement;return t},l=e=>e.isConnected,u=e=>e instanceof HTMLElement&&`disabled`in e&&!!e.disabled,d=e=>e.closest(`[inert]`)?!0:e instanceof HTMLElement&&e.inert,f=e=>l(e)&&!u(e)&&!d(e),p=(e,t)=>{try{return e.focus({preventScroll:t}),c(e.ownerDocument)===e}catch{return!1}},m=(e,t={})=>{let n;try{n=s(e)}catch{n=null}if(n&&f(n)&&p(n,t.preventScroll))return!0;if(!t.fallback)return!1;let r;try{r=s(t.fallback)}catch{return!1}return!r||!f(r)?!1:p(r,t.preventScroll)},h=(e={})=>{let t=c(document),n=!e.signal?.aborted;n||(t=null);let r=()=>{n=!1,t=null};return e.signal?.aborted||e.signal?.addEventListener(`abort`,r,{once:!0}),()=>{if(!n)return!1;n=!1,e.signal?.removeEventListener(`abort`,r);let i=t;return t=null,i?m(i,{fallback:e.fallback,preventScroll:e.preventScroll}):e.fallback?m(e.fallback,{preventScroll:e.preventScroll}):!1}};export{h as captureFocus,o as createListNavigation,m as restoreFocus};
1
+ import{matchKey as e}from"@vielzeug/keymap";var FocusError=class extends Error{constructor(e,t){super(e,t),this.name=new.target.name,Object.setPrototypeOf(this,new.target.prototype)}},FocusConfigError=class extends FocusError{},t={first:[`Home`],last:[`End`],next:[`ArrowRight`],nextRow:[`ArrowDown`],prev:[`ArrowLeft`],prevRow:[`ArrowUp`]},n={...t,next:[`ArrowLeft`],prev:[`ArrowRight`]},r=[`first`,`last`,`next`,`nextRow`,`prev`,`prevRow`],resolve=(e,t)=>typeof e==`function`?e():e??t,createGridNavigation=i=>{let a=new Map;for(let e of r)for(let t of i.keys?.[e]??[]){let n=a.get(t);if(n&&n!==e)throw new FocusConfigError(`Key "${t}" is assigned to both ${n} and ${e}`);a.set(t,e)}let o=-1,resolveColumns=()=>{let e=resolve(i.columns,1);if(!Number.isInteger(e)||e<1)throw new FocusConfigError(`columns must resolve to a positive integer, got: ${e}`);return e},activeIndex=e=>{let t=i.getActiveIndex?.();return t!==void 0&&(o=t>=0&&t<e.length?t:-1),o},commitIndex=(e,t,n,r)=>(o=t,Object.freeze({action:n,event:r,index:t,item:e[t]})),navigateWithItems=(e,t,n)=>{if(!e.length)return o=-1,null;if(t===`first`)return commitIndex(e,0,t,n);if(t===`last`)return commitIndex(e,e.length-1,t,n);let r=resolveColumns(),a=t===`next`?1:t===`prev`?-1:t===`nextRow`?r:-r,s=activeIndex(e),c=s<0?a>0?0:e.length-1:s+a;if(i.loop)return commitIndex(e,(c%e.length+e.length)%e.length,t,n);let l=Math.min(e.length-1,Math.max(0,c));return l===s?null:commitIndex(e,l,t,n)},resolveKeyAction=a=>{let o=resolve(i.direction,`ltr`)===`rtl`?n:t;for(let t of r)if((i.keys?.[t]??o[t]).some(t=>e(a,t)))return t},handleKeydown=e=>{if(e.defaultPrevented||e.isComposing||resolve(i.disabled,!1))return null;let t=resolveKeyAction(e);if(!t)return null;let n=navigateWithItems(i.getItems(),t,e);return e.preventDefault(),Object.freeze({change:n,handled:!0})},navigate=e=>navigateWithItems(i.getItems(),e),set=e=>{let t=i.getItems();if(!Number.isInteger(e)||e<0||!t.length){o=-1;return}o=Math.min(e,t.length-1)},reset=()=>{o=-1},getIndex=()=>activeIndex(i.getItems()),getActiveItem=()=>{let e=i.getItems(),t=activeIndex(e);return t>=0?e[t]:void 0};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}},i={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowRight`],prev:[`ArrowUp`,`ArrowLeft`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowRight`],prev:[`ArrowLeft`]},vertical:{first:[`Home`],last:[`End`],next:[`ArrowDown`],prev:[`ArrowUp`]}},a={both:{first:[`Home`],last:[`End`],next:[`ArrowDown`,`ArrowLeft`],prev:[`ArrowUp`,`ArrowRight`]},horizontal:{first:[`Home`],last:[`End`],next:[`ArrowLeft`],prev:[`ArrowRight`]},vertical:i.vertical},o=500,findForward=(e,t,n)=>{for(let r=t;r<e.length;r++)if(n(e[r],r))return r;return-1},findBackward=(e,t,n)=>{for(let r=t;r>=0;r--)if(n(e[r],r))return r;return-1},resolveTypeaheadDelay=e=>{if(e===void 0)return o;if(!Number.isFinite(e)||e<=0)throw new FocusConfigError(`Typeahead delay must be a positive finite number`);return e},createListNavigation=t=>{let n=resolveTypeaheadDelay(t.typeahead?.delayMs),r=new Map;for(let e of[`next`,`prev`,`first`,`last`])for(let n of t.keys?.[e]??[]){let t=r.get(n);if(t&&t!==e)throw new FocusConfigError(`Key "${n}" is assigned to both ${t} and ${e}`);r.set(n,e)}let o=-1,s=``,c=0,isItemDisabled=(e,n)=>t.isItemDisabled?.(e,n)??!1,isUsableIndex=(e,t)=>t>=0&&t<e.length&&!isItemDisabled(e[t],t),normalizeIndex=e=>(isUsableIndex(e,o)||(o=-1),o),resetTypeahead=()=>{s=``,c=0},commitIndex=(e,t,n,r)=>isUsableIndex(e,t)?(o=t,Object.freeze({action:n,event:r,index:t,item:e[t]})):(o=-1,null),findEnabledIndex=(e,t,n)=>n===`forward`?findForward(e,t,(e,t)=>!isItemDisabled(e,t)):findBackward(e,t,(e,t)=>!isItemDisabled(e,t)),move=(e,n,r)=>{let i=normalizeIndex(e);if(!e.length)return o=-1,null;let a=i<0?n===`forward`?0:e.length-1:n===`forward`?i+1:i-1,s=findEnabledIndex(e,a,n),c=n===`forward`?`next`:`prev`;if(s>=0)return commitIndex(e,s,c,r);if(t.loop){let t=n===`forward`?0:e.length-1,i=findEnabledIndex(e,t,n);if(i>=0)return commitIndex(e,i,c,r)}return null},navigateWithItems=(e,t,n)=>(resetTypeahead(),normalizeIndex(e),e.length?t===`first`?commitIndex(e,findEnabledIndex(e,0,`forward`),t,n):t===`last`?commitIndex(e,findEnabledIndex(e,e.length-1,`backward`),t,n):move(e,t===`next`?`forward`:`backward`,n):(o=-1,null)),findTypeaheadMatch=(e,n,r)=>{let i=normalizeIndex(e),a=i<0?0:r?i:i+1;for(let r=0;r<e.length;r++){let i=(a+r)%e.length,o=e[i];if(!isItemDisabled(o,i)&&t.typeahead?.getLabel(o,i).toLocaleLowerCase().startsWith(n))return i}return-1},handleTypeahead=(e,r)=>{if(!t.typeahead||e.key.length!==1||e.ctrlKey||e.altKey||e.metaKey)return null;let i=performance.now(),a=e.key.toLocaleLowerCase();i-c>=n&&(s=``),c=i;let o=s+a;s=o.length>1&&[...o].every(e=>e===a)?a:o;let l=findTypeaheadMatch(r,s,s.length>1);return l<0&&s.length>1&&(s=a,l=findTypeaheadMatch(r,s,!1)),l<0?null:commitIndex(r,l,`typeahead`,e)},set=e=>{let n=t.getItems();if(normalizeIndex(n),resetTypeahead(),!Number.isInteger(e)||e<0||!n.length)return o=-1,o;let r=Math.min(e,n.length-1);return isUsableIndex(n,r)?(o=r,o):(o=-1,o)},navigate=e=>navigateWithItems(t.getItems(),e),getActiveItem=()=>{let e=t.getItems(),n=normalizeIndex(e);return n>=0?e[n]:void 0},reset=()=>{o=-1,resetTypeahead()},getIndex=()=>normalizeIndex(t.getItems()),resolve=(e,t)=>typeof e==`function`?e():e??t,resolveKeyAction=n=>{let r=t.keys,o=(resolve(t.direction,`ltr`)===`rtl`?a:i)[resolve(t.orientation,`vertical`)];for(let t of[`next`,`prev`,`first`,`last`])if((r?.[t]??o[t]).some(t=>e(n,t)))return t},handleKeydown=e=>{if(e.defaultPrevented||e.isComposing||resolve(t.disabled,!1))return null;let n=t.getItems();normalizeIndex(n);let r=resolveKeyAction(e);if(r){let t=navigateWithItems(n,r,e);return e.preventDefault(),Object.freeze({change:t,handled:!0})}let i=handleTypeahead(e,n);return i?(t.typeahead?.preventDefault&&e.preventDefault(),Object.freeze({change:i,handled:!0})):null};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}},resolveTarget=e=>typeof e==`function`?e():e,getDeepActiveElement=e=>{let t=e.activeElement;for(;t instanceof HTMLElement&&t.shadowRoot?.activeElement;)t=t.shadowRoot.activeElement;return t},isConnected=e=>e.isConnected,isDisabled=e=>e instanceof HTMLElement&&`disabled`in e&&!!e.disabled,isInert=e=>e.closest(`[inert]`)?!0:e instanceof HTMLElement&&e.inert,canRestoreTo=e=>isConnected(e)&&!isDisabled(e)&&!isInert(e),focusAndVerify=(e,t)=>{try{return e.focus({preventScroll:t}),getDeepActiveElement(e.ownerDocument)===e}catch{return!1}},restoreFocus=(e,t={})=>{let n;try{n=resolveTarget(e)}catch{n=null}if(n&&canRestoreTo(n)&&focusAndVerify(n,t.preventScroll))return!0;if(!t.fallback)return!1;let r;try{r=resolveTarget(t.fallback)}catch{return!1}return!r||!canRestoreTo(r)?!1:focusAndVerify(r,t.preventScroll)},rescueFocus=(e,t={})=>{let n=getDeepActiveElement(document);return n&&n!==document.body?!1:restoreFocus(e,t)},captureFocus=(e={})=>{let t=getDeepActiveElement(document),n=!e.signal?.aborted;n||(t=null);let cancel=()=>{n=!1,t=null};return e.signal?.aborted||e.signal?.addEventListener(`abort`,cancel,{once:!0}),()=>{if(!n)return!1;n=!1,e.signal?.removeEventListener(`abort`,cancel);let r=t;return t=null,r?restoreFocus(r,{fallback:e.fallback,preventScroll:e.preventScroll}):e.fallback?restoreFocus(e.fallback,{preventScroll:e.preventScroll}):!1}};export{FocusConfigError,FocusError,captureFocus,createGridNavigation,createListNavigation,rescueFocus,restoreFocus};
2
2
  //# sourceMappingURL=focus.js.map
package/dist/focus.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"focus.js","names":[],"sources":["../src/list-navigation.ts","../src/restore-focus.ts"],"sourcesContent":["export type ListNavigationAction = 'first' | 'last' | 'next' | 'prev';\nexport type ListKeyAction = ListNavigationAction | 'typeahead';\n\nexport type ListNavigationChange<T> = {\n readonly action: ListKeyAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type ListKeyResult<T> = {\n readonly change: ListNavigationChange<T> | null;\n readonly handled: true;\n};\n\nexport type ListNavigationTypeaheadOptions<T> = {\n delayMs?: number;\n getLabel: (item: T, index: number) => string;\n preventDefault?: boolean;\n};\n\nexport type MaybeGetter<T> = T | (() => T);\n\nexport type ListNavigationOptions<T> = {\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n getItems: () => readonly T[];\n isItemDisabled?: (item: T, index: number) => boolean;\n keys?: Partial<Record<ListNavigationAction, readonly string[]>>;\n loop?: boolean;\n orientation?: MaybeGetter<'both' | 'horizontal' | 'vertical'>;\n typeahead?: ListNavigationTypeaheadOptions<T>;\n};\n\nexport type ListNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): ListKeyResult<T> | null;\n navigate(action: ListNavigationAction): ListNavigationChange<T> | null;\n reset(): void;\n set(index: number): number;\n};\n\nconst DEFAULT_KEYS: Record<'both' | 'horizontal' | 'vertical', Record<ListNavigationAction, readonly string[]>> = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowRight'], prev: ['ArrowUp', 'ArrowLeft'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowRight'], prev: ['ArrowLeft'] },\n vertical: { first: ['Home'], last: ['End'], next: ['ArrowDown'], prev: ['ArrowUp'] },\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowLeft'], prev: ['ArrowUp', 'ArrowRight'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowLeft'], prev: ['ArrowRight'] },\n vertical: DEFAULT_KEYS.vertical,\n};\n\nconst DEFAULT_TYPEAHEAD_DELAY_MS = 500;\n\nconst findForward = <T>(items: readonly T[], start: number, predicate: (item: T, index: number) => boolean): number => {\n for (let index = start; index < items.length; index++) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst findBackward = <T>(\n items: readonly T[],\n start: number,\n predicate: (item: T, index: number) => boolean,\n): number => {\n for (let index = start; index >= 0; index--) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst resolveTypeaheadDelay = (delay: number | undefined): number => {\n if (delay === undefined) return DEFAULT_TYPEAHEAD_DELAY_MS;\n if (!Number.isFinite(delay) || delay <= 0) throw new RangeError('Typeahead delay must be a positive finite number');\n return delay;\n};\n\nexport const createListNavigation = <T>(options: ListNavigationOptions<T>): ListNavigation<T> => {\n const typeaheadDelay = resolveTypeaheadDelay(options.typeahead?.delayMs);\n const owners = new Map<string, ListNavigationAction>();\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action) throw new RangeError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n let typeaheadBuffer = '';\n let lastTypeaheadAt = 0;\n\n const isItemDisabled = (item: T, itemIndex: number): boolean => options.isItemDisabled?.(item, itemIndex) ?? false;\n\n const isUsableIndex = (items: readonly T[], itemIndex: number): boolean =>\n itemIndex >= 0 && itemIndex < items.length && !isItemDisabled(items[itemIndex], itemIndex);\n\n const normalizeIndex = (items: readonly T[]): number => {\n if (!isUsableIndex(items, index)) index = -1;\n\n return index;\n };\n\n const resetTypeahead = (): void => {\n typeaheadBuffer = '';\n lastTypeaheadAt = 0;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: ListKeyAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n if (!isUsableIndex(items, nextIndex)) {\n index = -1;\n\n return null;\n }\n\n index = nextIndex;\n\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const findEnabledIndex = (items: readonly T[], start: number, direction: 'forward' | 'backward'): number => {\n if (direction === 'forward')\n return findForward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n\n return findBackward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n };\n\n const move = (\n items: readonly T[],\n direction: 'forward' | 'backward',\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n const current = normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n const start =\n current < 0\n ? direction === 'forward'\n ? 0\n : items.length - 1\n : direction === 'forward'\n ? current + 1\n : current - 1;\n\n const nextIndex = findEnabledIndex(items, start, direction);\n const action: ListNavigationAction = direction === 'forward' ? 'next' : 'prev';\n\n if (nextIndex >= 0) return commitIndex(items, nextIndex, action, event);\n\n if (options.loop) {\n const wrapStart = direction === 'forward' ? 0 : items.length - 1;\n const wrapped = findEnabledIndex(items, wrapStart, direction);\n\n if (wrapped >= 0) return commitIndex(items, wrapped, action, event);\n }\n\n return null;\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: ListNavigationAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n resetTypeahead();\n normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n if (action === 'first') {\n return commitIndex(items, findEnabledIndex(items, 0, 'forward'), action, event);\n }\n\n if (action === 'last') {\n return commitIndex(items, findEnabledIndex(items, items.length - 1, 'backward'), action, event);\n }\n\n return move(items, action === 'next' ? 'forward' : 'backward', event);\n };\n\n const findTypeaheadMatch = (items: readonly T[], search: string, includeCurrent: boolean): number => {\n const current = normalizeIndex(items);\n const start = current < 0 ? 0 : includeCurrent ? current : current + 1;\n\n for (let offset = 0; offset < items.length; offset++) {\n const itemIndex = (start + offset) % items.length;\n const item = items[itemIndex];\n\n if (isItemDisabled(item, itemIndex)) continue;\n\n if (options.typeahead?.getLabel(item, itemIndex).toLocaleLowerCase().startsWith(search)) {\n return itemIndex;\n }\n }\n\n return -1;\n };\n\n const handleTypeahead = (event: KeyboardEvent, items: readonly T[]): ListNavigationChange<T> | null => {\n if (!options.typeahead || event.key.length !== 1 || event.ctrlKey || event.altKey || event.metaKey) return null;\n\n const now = performance.now();\n const key = event.key.toLocaleLowerCase();\n\n if (now - lastTypeaheadAt >= typeaheadDelay) typeaheadBuffer = '';\n\n lastTypeaheadAt = now;\n\n const candidate = typeaheadBuffer + key;\n const repeatedCharacter = candidate.length > 1 && [...candidate].every((character) => character === key);\n typeaheadBuffer = repeatedCharacter ? key : candidate;\n\n let nextIndex = findTypeaheadMatch(items, typeaheadBuffer, typeaheadBuffer.length > 1);\n\n if (nextIndex < 0 && typeaheadBuffer.length > 1) {\n typeaheadBuffer = key;\n nextIndex = findTypeaheadMatch(items, typeaheadBuffer, false);\n }\n\n if (nextIndex < 0) return null;\n\n return commitIndex(items, nextIndex, 'typeahead', event);\n };\n\n const set = (nextIndex: number): number => {\n const items = options.getItems();\n normalizeIndex(items);\n resetTypeahead();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n\n return index;\n }\n\n const clamped = Math.min(nextIndex, items.length - 1);\n\n if (!isUsableIndex(items, clamped)) {\n index = -1;\n\n return index;\n }\n\n index = clamped;\n\n return index;\n };\n\n const navigate = (action: ListNavigationAction): ListNavigationChange<T> | null => {\n return navigateWithItems(options.getItems(), action);\n };\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = normalizeIndex(items);\n\n return current >= 0 ? items[current] : undefined;\n };\n\n const reset = (): void => {\n index = -1;\n resetTypeahead();\n };\n\n const getIndex = (): number => {\n return normalizeIndex(options.getItems());\n };\n\n const resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n const resolveKeyAction = (eventKey: string): ListNavigationAction | undefined => {\n const keys = options.keys;\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n const defaults = keyTable[resolve(options.orientation, 'vertical')];\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n if ((keys?.[action] ?? defaults[action]).includes(eventKey)) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): ListKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const items = options.getItems();\n normalizeIndex(items);\n\n const action = resolveKeyAction(event.key);\n\n if (action) {\n const change = navigateWithItems(items, action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n }\n\n const change = handleTypeahead(event, items);\n if (!change) return null;\n if (options.typeahead?.preventDefault) event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","export type FocusableElement = HTMLElement | SVGElement;\nexport type FocusTarget = FocusableElement | null | undefined | (() => FocusableElement | null | undefined);\n\nexport type RestoreFocusOptions = {\n fallback?: FocusTarget;\n preventScroll?: boolean;\n};\n\nexport type CaptureFocusOptions = RestoreFocusOptions & {\n signal?: AbortSignal;\n};\n\nexport type FocusRestorer = () => boolean;\n\nconst resolveTarget = (target: FocusTarget): FocusableElement | null | undefined => {\n return typeof target === 'function' ? target() : target;\n};\n\nconst getDeepActiveElement = (rootDocument: Document): Element | null => {\n let active: Element | null = rootDocument.activeElement;\n\n while (active instanceof HTMLElement && active.shadowRoot?.activeElement) {\n active = active.shadowRoot.activeElement;\n }\n\n return active;\n};\n\nconst isConnected = (target: FocusableElement): boolean => {\n // `isConnected` pierces shadow boundaries (shadow-including root is the document);\n // `ownerDocument.contains` does NOT — it would reject elements inside shadow roots.\n return target.isConnected;\n};\n\nconst isDisabled = (target: FocusableElement): boolean => {\n return target instanceof HTMLElement && 'disabled' in target && Boolean((target as HTMLInputElement).disabled);\n};\n\nconst isInert = (target: FocusableElement): boolean => {\n if (target.closest('[inert]')) return true;\n\n return target instanceof HTMLElement && target.inert;\n};\n\nconst canRestoreTo = (target: FocusableElement): boolean => {\n return isConnected(target) && !isDisabled(target) && !isInert(target);\n};\n\nconst focusAndVerify = (target: FocusableElement, preventScroll: boolean | undefined): boolean => {\n try {\n target.focus({ preventScroll });\n return getDeepActiveElement(target.ownerDocument) === target;\n } catch {\n return false;\n }\n};\n\nexport const restoreFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n let next: FocusableElement | null | undefined;\n try {\n next = resolveTarget(target);\n } catch {\n next = null;\n }\n\n if (next && canRestoreTo(next) && focusAndVerify(next, options.preventScroll)) {\n return true;\n }\n\n if (!options.fallback) return false;\n\n let fallback: FocusableElement | null | undefined;\n try {\n fallback = resolveTarget(options.fallback);\n } catch {\n return false;\n }\n\n if (!fallback || !canRestoreTo(fallback)) return false;\n\n return focusAndVerify(fallback, options.preventScroll);\n};\n\nexport const captureFocus = (options: CaptureFocusOptions = {}): FocusRestorer => {\n let captured = getDeepActiveElement(document) as FocusableElement | null;\n let available = !options.signal?.aborted;\n\n if (!available) captured = null;\n\n const cancel = (): void => {\n available = false;\n captured = null;\n };\n\n if (!options.signal?.aborted) {\n options.signal?.addEventListener('abort', cancel, { once: true });\n }\n\n return () => {\n if (!available) return false;\n\n available = false;\n options.signal?.removeEventListener('abort', cancel);\n\n const target = captured;\n captured = null;\n\n if (target) {\n return restoreFocus(target, {\n fallback: options.fallback,\n preventScroll: options.preventScroll,\n });\n }\n\n if (options.fallback) {\n return restoreFocus(options.fallback, {\n preventScroll: options.preventScroll,\n });\n }\n\n return false;\n };\n};\n"],"mappings":"AA2CA,IAAM,EAA4G,CAChH,KAAM,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAa,YAAY,EAAG,KAAM,CAAC,UAAW,WAAW,CAAE,EAC1G,WAAY,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAY,EAAG,KAAM,CAAC,WAAW,CAAE,EACxF,SAAU,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,WAAW,EAAG,KAAM,CAAC,SAAS,CAAE,CACrF,EAEM,EAAwC,CAC5C,KAAM,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,YAAa,WAAW,EAAG,KAAM,CAAC,UAAW,YAAY,CAAE,EAC1G,WAAY,CAAE,MAAO,CAAC,MAAM,EAAG,KAAM,CAAC,KAAK,EAAG,KAAM,CAAC,WAAW,EAAG,KAAM,CAAC,YAAY,CAAE,EACxF,SAAU,EAAa,QACzB,EAEM,EAA6B,IAE7B,GAAkB,EAAqB,EAAe,IAA2D,CACrH,IAAK,IAAI,EAAQ,EAAO,EAAQ,EAAM,OAAQ,IAC5C,GAAI,EAAU,EAAM,GAAQ,CAAK,EAAG,OAAO,EAG7C,MAAO,EACT,EAEM,GACJ,EACA,EACA,IACW,CACX,IAAK,IAAI,EAAQ,EAAO,GAAS,EAAG,IAClC,GAAI,EAAU,EAAM,GAAQ,CAAK,EAAG,OAAO,EAG7C,MAAO,EACT,EAEM,EAAyB,GAAsC,CACnE,GAAI,IAAU,IAAA,GAAW,OAAO,EAChC,GAAI,CAAC,OAAO,SAAS,CAAK,GAAK,GAAS,EAAG,MAAU,WAAW,k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Aa,EAC1E,MAAO,GAGT,GAAI,CAAC,EAAQ,SAAU,MAAO,GAE9B,IAAI,EACJ,GAAI,CACF,EAAW,EAAc,EAAQ,QAAQ,CAC3C,MAAQ,CACN,MAAO,EACT,CAIA,MAFI,CAAC,GAAY,CAAC,EAAa,CAAQ,EAAU,GAE1C,EAAe,EAAU,EAAQ,aAAa,CACvD,EAEa,GAAgB,EAA+B,CAAC,IAAqB,CAChF,IAAI,EAAW,EAAqB,QAAQ,EACxC,EAAY,CAAC,EAAQ,QAAQ,QAE5B,IAAW,EAAW,MAE3B,IAAM,MAAqB,CACzB,EAAY,GACZ,EAAW,IACb,EAMA,OAJK,EAAQ,QAAQ,SACnB,EAAQ,QAAQ,iBAAiB,QAAS,EAAQ,CAAE,KAAM,EAAK,CAAC,MAGrD,CACX,GAAI,CAAC,EAAW,MAAO,GAEvB,EAAY,GACZ,EAAQ,QAAQ,oBAAoB,QAAS,CAAM,EAEnD,IAAM,EAAS,EAgBf,MAfA,GAAW,KAEP,EACK,EAAa,EAAQ,CAC1B,SAAU,EAAQ,SAClB,cAAe,EAAQ,aACzB,CAAC,EAGC,EAAQ,SACH,EAAa,EAAQ,SAAU,CACpC,cAAe,EAAQ,aACzB,CAAC,EAGI,EACT,CACF"}
1
+ {"version":3,"file":"focus.js","names":[],"sources":["../src/errors.ts","../src/grid-navigation.ts","../src/list-navigation.ts","../src/restore-focus.ts"],"sourcesContent":["/** Base class for all focus errors. Use `instanceof FocusError` to catch any focus-originated error. */\nexport class FocusError extends Error {\n constructor(message: string, opts?: ErrorOptions) {\n super(message, opts);\n this.name = new.target.name;\n Object.setPrototypeOf(this, new.target.prototype);\n }\n}\n\n/** Thrown when navigation options are invalid, such as a non-positive typeahead delay or a key assigned to two actions. */\nexport class FocusConfigError extends FocusError {}\n","import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\nimport type { MaybeGetter } from './list-navigation.js';\n\nexport type GridNavigationAction = 'first' | 'last' | 'next' | 'nextRow' | 'prev' | 'prevRow';\n\nexport type GridNavigationChange<T> = {\n readonly action: GridNavigationAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type GridKeyResult<T> = {\n readonly change: GridNavigationChange<T> | null;\n readonly handled: true;\n};\n\n/**\n * Columns per row: a fixed count, or a getter resolved on every navigation. Responsive grids\n * read a media query or computed style; measured grids read the rendered row length.\n */\nexport type GridColumns = number | (() => number);\n\nexport type GridNavigationOptions<T> = {\n /** Columns per row, resolved on every navigation. */\n columns: GridColumns;\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n /**\n * Reads the active index from the outside (usually the index of `document.activeElement`\n * among the items), so focus moved by pointer or Tab stays in sync. When omitted, the\n * navigation tracks the index internally through `set()`.\n */\n getActiveIndex?: () => number;\n getItems: () => readonly T[];\n keys?: Partial<Record<GridNavigationAction, readonly string[]>>;\n /** Wrap moves around the grid's edges by flat index. Default clamps at the edges. */\n loop?: boolean;\n};\n\nexport type GridNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): GridKeyResult<T> | null;\n navigate(action: GridNavigationAction): GridNavigationChange<T> | null;\n reset(): void;\n set(index: number): void;\n};\n\nconst DEFAULT_KEYS: Record<GridNavigationAction, readonly string[]> = {\n first: ['Home'],\n last: ['End'],\n next: ['ArrowRight'],\n nextRow: ['ArrowDown'],\n prev: ['ArrowLeft'],\n prevRow: ['ArrowUp'],\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n ...DEFAULT_KEYS,\n next: ['ArrowLeft'],\n prev: ['ArrowRight'],\n};\n\nconst GRID_ACTIONS = ['first', 'last', 'next', 'nextRow', 'prev', 'prevRow'] as const;\n\nconst resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n/**\n * Keyboard navigation over a two-dimensional grid of items: horizontal arrows step one\n * item, vertical arrows step one row (the column count), Home/End jump to the ends.\n *\n * Unlike `createListNavigation`, items are not skipped when disabled — skipping in two\n * dimensions would break row alignment. Focus (or otherwise activate) the item reported\n * by the returned change; `handleKeydown` calls `preventDefault()` for every recognized\n * key so unhandled arrows never scroll the page.\n */\nexport const createGridNavigation = <T>(options: GridNavigationOptions<T>): GridNavigation<T> => {\n const owners = new Map<string, GridNavigationAction>();\n\n for (const action of GRID_ACTIONS) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n\n const resolveColumns = (): number => {\n const columns = resolve(options.columns, 1);\n if (!Number.isInteger(columns) || columns < 1)\n throw new FocusConfigError(`columns must resolve to a positive integer, got: ${columns}`);\n return columns;\n };\n\n const activeIndex = (items: readonly T[]): number => {\n const derived = options.getActiveIndex?.();\n if (derived !== undefined) index = derived >= 0 && derived < items.length ? derived : -1;\n return index;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> => {\n index = nextIndex;\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> | null => {\n if (!items.length) {\n index = -1;\n return null;\n }\n\n if (action === 'first') return commitIndex(items, 0, action, event);\n if (action === 'last') return commitIndex(items, items.length - 1, action, event);\n\n const columns = resolveColumns();\n const offset = action === 'next' ? 1 : action === 'prev' ? -1 : action === 'nextRow' ? columns : -columns;\n const current = activeIndex(items);\n const base = current < 0 ? (offset > 0 ? 0 : items.length - 1) : current + offset;\n\n if (options.loop) {\n return commitIndex(items, ((base % items.length) + items.length) % items.length, action, event);\n }\n\n const clamped = Math.min(items.length - 1, Math.max(0, base));\n if (clamped === current) return null;\n\n return commitIndex(items, clamped, action, event);\n };\n\n const resolveKeyAction = (event: KeyboardEvent): GridNavigationAction | undefined => {\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n\n for (const action of GRID_ACTIONS) {\n if ((options.keys?.[action] ?? keyTable[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): GridKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const action = resolveKeyAction(event);\n if (!action) return null;\n\n const change = navigateWithItems(options.getItems(), action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n const navigate = (action: GridNavigationAction): GridNavigationChange<T> | null =>\n navigateWithItems(options.getItems(), action);\n\n const set = (nextIndex: number): void => {\n const items = options.getItems();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n return;\n }\n\n index = Math.min(nextIndex, items.length - 1);\n };\n\n const reset = (): void => {\n index = -1;\n };\n\n const getIndex = (): number => activeIndex(options.getItems());\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = activeIndex(items);\n return current >= 0 ? items[current] : undefined;\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\n\nexport type ListNavigationAction = 'first' | 'last' | 'next' | 'prev';\nexport type ListKeyAction = ListNavigationAction | 'typeahead';\n\nexport type ListNavigationChange<T> = {\n readonly action: ListKeyAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type ListKeyResult<T> = {\n readonly change: ListNavigationChange<T> | null;\n readonly handled: true;\n};\n\nexport type ListNavigationTypeaheadOptions<T> = {\n delayMs?: number;\n getLabel: (item: T, index: number) => string;\n preventDefault?: boolean;\n};\n\nexport type MaybeGetter<T> = T | (() => T);\n\nexport type ListNavigationOptions<T> = {\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n getItems: () => readonly T[];\n isItemDisabled?: (item: T, index: number) => boolean;\n keys?: Partial<Record<ListNavigationAction, readonly string[]>>;\n loop?: boolean;\n orientation?: MaybeGetter<'both' | 'horizontal' | 'vertical'>;\n typeahead?: ListNavigationTypeaheadOptions<T>;\n};\n\nexport type ListNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): ListKeyResult<T> | null;\n navigate(action: ListNavigationAction): ListNavigationChange<T> | null;\n reset(): void;\n set(index: number): number;\n};\n\nconst DEFAULT_KEYS: Record<'both' | 'horizontal' | 'vertical', Record<ListNavigationAction, readonly string[]>> = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowRight'], prev: ['ArrowUp', 'ArrowLeft'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowRight'], prev: ['ArrowLeft'] },\n vertical: { first: ['Home'], last: ['End'], next: ['ArrowDown'], prev: ['ArrowUp'] },\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n both: { first: ['Home'], last: ['End'], next: ['ArrowDown', 'ArrowLeft'], prev: ['ArrowUp', 'ArrowRight'] },\n horizontal: { first: ['Home'], last: ['End'], next: ['ArrowLeft'], prev: ['ArrowRight'] },\n vertical: DEFAULT_KEYS.vertical,\n};\n\nconst DEFAULT_TYPEAHEAD_DELAY_MS = 500;\n\nconst findForward = <T>(items: readonly T[], start: number, predicate: (item: T, index: number) => boolean): number => {\n for (let index = start; index < items.length; index++) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst findBackward = <T>(\n items: readonly T[],\n start: number,\n predicate: (item: T, index: number) => boolean,\n): number => {\n for (let index = start; index >= 0; index--) {\n if (predicate(items[index], index)) return index;\n }\n\n return -1;\n};\n\nconst resolveTypeaheadDelay = (delay: number | undefined): number => {\n if (delay === undefined) return DEFAULT_TYPEAHEAD_DELAY_MS;\n if (!Number.isFinite(delay) || delay <= 0)\n throw new FocusConfigError('Typeahead delay must be a positive finite number');\n return delay;\n};\n\nexport const createListNavigation = <T>(options: ListNavigationOptions<T>): ListNavigation<T> => {\n const typeaheadDelay = resolveTypeaheadDelay(options.typeahead?.delayMs);\n const owners = new Map<string, ListNavigationAction>();\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n let typeaheadBuffer = '';\n let lastTypeaheadAt = 0;\n\n const isItemDisabled = (item: T, itemIndex: number): boolean => options.isItemDisabled?.(item, itemIndex) ?? false;\n\n const isUsableIndex = (items: readonly T[], itemIndex: number): boolean =>\n itemIndex >= 0 && itemIndex < items.length && !isItemDisabled(items[itemIndex], itemIndex);\n\n const normalizeIndex = (items: readonly T[]): number => {\n if (!isUsableIndex(items, index)) index = -1;\n\n return index;\n };\n\n const resetTypeahead = (): void => {\n typeaheadBuffer = '';\n lastTypeaheadAt = 0;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: ListKeyAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n if (!isUsableIndex(items, nextIndex)) {\n index = -1;\n\n return null;\n }\n\n index = nextIndex;\n\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const findEnabledIndex = (items: readonly T[], start: number, direction: 'forward' | 'backward'): number => {\n if (direction === 'forward')\n return findForward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n\n return findBackward(items, start, (item, itemIndex) => !isItemDisabled(item, itemIndex));\n };\n\n const move = (\n items: readonly T[],\n direction: 'forward' | 'backward',\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n const current = normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n const start =\n current < 0\n ? direction === 'forward'\n ? 0\n : items.length - 1\n : direction === 'forward'\n ? current + 1\n : current - 1;\n\n const nextIndex = findEnabledIndex(items, start, direction);\n const action: ListNavigationAction = direction === 'forward' ? 'next' : 'prev';\n\n if (nextIndex >= 0) return commitIndex(items, nextIndex, action, event);\n\n if (options.loop) {\n const wrapStart = direction === 'forward' ? 0 : items.length - 1;\n const wrapped = findEnabledIndex(items, wrapStart, direction);\n\n if (wrapped >= 0) return commitIndex(items, wrapped, action, event);\n }\n\n return null;\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: ListNavigationAction,\n event?: KeyboardEvent,\n ): ListNavigationChange<T> | null => {\n resetTypeahead();\n normalizeIndex(items);\n\n if (!items.length) {\n index = -1;\n\n return null;\n }\n\n if (action === 'first') {\n return commitIndex(items, findEnabledIndex(items, 0, 'forward'), action, event);\n }\n\n if (action === 'last') {\n return commitIndex(items, findEnabledIndex(items, items.length - 1, 'backward'), action, event);\n }\n\n return move(items, action === 'next' ? 'forward' : 'backward', event);\n };\n\n const findTypeaheadMatch = (items: readonly T[], search: string, includeCurrent: boolean): number => {\n const current = normalizeIndex(items);\n const start = current < 0 ? 0 : includeCurrent ? current : current + 1;\n\n for (let offset = 0; offset < items.length; offset++) {\n const itemIndex = (start + offset) % items.length;\n const item = items[itemIndex];\n\n if (isItemDisabled(item, itemIndex)) continue;\n\n if (options.typeahead?.getLabel(item, itemIndex).toLocaleLowerCase().startsWith(search)) {\n return itemIndex;\n }\n }\n\n return -1;\n };\n\n const handleTypeahead = (event: KeyboardEvent, items: readonly T[]): ListNavigationChange<T> | null => {\n if (!options.typeahead || event.key.length !== 1 || event.ctrlKey || event.altKey || event.metaKey) return null;\n\n const now = performance.now();\n const key = event.key.toLocaleLowerCase();\n\n if (now - lastTypeaheadAt >= typeaheadDelay) typeaheadBuffer = '';\n\n lastTypeaheadAt = now;\n\n const candidate = typeaheadBuffer + key;\n const repeatedCharacter = candidate.length > 1 && [...candidate].every((character) => character === key);\n typeaheadBuffer = repeatedCharacter ? key : candidate;\n\n let nextIndex = findTypeaheadMatch(items, typeaheadBuffer, typeaheadBuffer.length > 1);\n\n if (nextIndex < 0 && typeaheadBuffer.length > 1) {\n typeaheadBuffer = key;\n nextIndex = findTypeaheadMatch(items, typeaheadBuffer, false);\n }\n\n if (nextIndex < 0) return null;\n\n return commitIndex(items, nextIndex, 'typeahead', event);\n };\n\n const set = (nextIndex: number): number => {\n const items = options.getItems();\n normalizeIndex(items);\n resetTypeahead();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n\n return index;\n }\n\n const clamped = Math.min(nextIndex, items.length - 1);\n\n if (!isUsableIndex(items, clamped)) {\n index = -1;\n\n return index;\n }\n\n index = clamped;\n\n return index;\n };\n\n const navigate = (action: ListNavigationAction): ListNavigationChange<T> | null => {\n return navigateWithItems(options.getItems(), action);\n };\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = normalizeIndex(items);\n\n return current >= 0 ? items[current] : undefined;\n };\n\n const reset = (): void => {\n index = -1;\n resetTypeahead();\n };\n\n const getIndex = (): number => {\n return normalizeIndex(options.getItems());\n };\n\n const resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n const resolveKeyAction = (event: KeyboardEvent): ListNavigationAction | undefined => {\n const keys = options.keys;\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n const defaults = keyTable[resolve(options.orientation, 'vertical')];\n\n for (const action of ['next', 'prev', 'first', 'last'] as const) {\n if ((keys?.[action] ?? defaults[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): ListKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const items = options.getItems();\n normalizeIndex(items);\n\n const action = resolveKeyAction(event);\n\n if (action) {\n const change = navigateWithItems(items, action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n }\n\n const change = handleTypeahead(event, items);\n if (!change) return null;\n if (options.typeahead?.preventDefault) event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n","export type FocusableElement = HTMLElement | SVGElement;\nexport type FocusTarget = FocusableElement | null | undefined | (() => FocusableElement | null | undefined);\n\nexport type RestoreFocusOptions = {\n fallback?: FocusTarget;\n preventScroll?: boolean;\n};\n\nexport type CaptureFocusOptions = RestoreFocusOptions & {\n signal?: AbortSignal;\n};\n\nexport type FocusRestorer = () => boolean;\n\nconst resolveTarget = (target: FocusTarget): FocusableElement | null | undefined => {\n return typeof target === 'function' ? target() : target;\n};\n\nconst getDeepActiveElement = (rootDocument: Document): Element | null => {\n let active: Element | null = rootDocument.activeElement;\n\n while (active instanceof HTMLElement && active.shadowRoot?.activeElement) {\n active = active.shadowRoot.activeElement;\n }\n\n return active;\n};\n\nconst isConnected = (target: FocusableElement): boolean => {\n // `isConnected` pierces shadow boundaries (shadow-including root is the document);\n // `ownerDocument.contains` does NOT — it would reject elements inside shadow roots.\n return target.isConnected;\n};\n\nconst isDisabled = (target: FocusableElement): boolean => {\n return target instanceof HTMLElement && 'disabled' in target && Boolean((target as HTMLInputElement).disabled);\n};\n\nconst isInert = (target: FocusableElement): boolean => {\n if (target.closest('[inert]')) return true;\n\n return target instanceof HTMLElement && target.inert;\n};\n\nconst canRestoreTo = (target: FocusableElement): boolean => {\n return isConnected(target) && !isDisabled(target) && !isInert(target);\n};\n\nconst focusAndVerify = (target: FocusableElement, preventScroll: boolean | undefined): boolean => {\n try {\n target.focus({ preventScroll });\n return getDeepActiveElement(target.ownerDocument) === target;\n } catch {\n return false;\n }\n};\n\nexport const restoreFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n let next: FocusableElement | null | undefined;\n try {\n next = resolveTarget(target);\n } catch {\n next = null;\n }\n\n if (next && canRestoreTo(next) && focusAndVerify(next, options.preventScroll)) {\n return true;\n }\n\n if (!options.fallback) return false;\n\n let fallback: FocusableElement | null | undefined;\n try {\n fallback = resolveTarget(options.fallback);\n } catch {\n return false;\n }\n\n if (!fallback || !canRestoreTo(fallback)) return false;\n\n return focusAndVerify(fallback, options.preventScroll);\n};\n\n/**\n * Hands focus to `target` when focus has been lost to the document body — the state left\n * behind when the focused element unmounts mid-swap (browsing a dialog, paging a list),\n * where keydown would never reach a handler and every shortcut dies. Returns `true` when\n * focus was rescued; `false` when focus is already on a real element or the target cannot\n * take focus (the `fallback` option of {@link RestoreFocusOptions} still applies).\n */\nexport const rescueFocus = (target: FocusTarget, options: RestoreFocusOptions = {}): boolean => {\n const active = getDeepActiveElement(document);\n\n if (active && active !== document.body) return false;\n\n return restoreFocus(target, options);\n};\n\nexport const captureFocus = (options: CaptureFocusOptions = {}): FocusRestorer => {\n let captured = getDeepActiveElement(document) as FocusableElement | null;\n let available = !options.signal?.aborted;\n\n if (!available) captured = null;\n\n const cancel = (): void => {\n available = false;\n captured = null;\n };\n\n if (!options.signal?.aborted) {\n options.signal?.addEventListener('abort', cancel, { once: true });\n }\n\n return () => {\n if (!available) return false;\n\n available = false;\n options.signal?.removeEventListener('abort', cancel);\n\n const target = captured;\n captured = null;\n\n if (target) {\n return restoreFocus(target, {\n fallback: options.fallback,\n preventScroll: options.preventScroll,\n });\n }\n\n if (options.fallback) {\n return restoreFocus(options.fallback, {\n preventScroll: options.preventScroll,\n });\n }\n\n return false;\n };\n};\n"],"mappings":"4CACA,IAAa,WAAb,cAAgC,KAAM,CACpC,YAAY,EAAiB,EAAqB,CAChD,MAAM,EAAS,CAAI,EACnB,KAAK,KAAO,WAAW,KACvB,OAAO,eAAe,KAAM,WAAW,SAAS,CAClD,CACF,EAGa,iBAAb,cAAsC,UAAW,CAAC,ECyC5C,EAAgE,CAC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prev:[`ArrowLeft`],prevRow:[`ArrowUp`]},r={...n,next:[`ArrowLeft`],prev:[`ArrowRight`]},i=[`first`,`last`,`next`,`nextRow`,`prev`,`prevRow`],resolve=(e,t)=>typeof e==`function`?e():e??t,createGridNavigation=a=>{let o=new Map;for(let t of i)for(let n of a.keys?.[t]??[]){let r=o.get(n);if(r&&r!==t)throw new e.FocusConfigError(`Key "${n}" is assigned to both ${r} and ${t}`);o.set(n,t)}let s=-1,resolveColumns=()=>{let t=resolve(a.columns,1);if(!Number.isInteger(t)||t<1)throw new e.FocusConfigError(`columns must resolve to a positive integer, got: ${t}`);return t},activeIndex=e=>{let t=a.getActiveIndex?.();return t!==void 0&&(s=t>=0&&t<e.length?t:-1),s},commitIndex=(e,t,n,r)=>(s=t,Object.freeze({action:n,event:r,index:t,item:e[t]})),navigateWithItems=(e,t,n)=>{if(!e.length)return s=-1,null;if(t===`first`)return commitIndex(e,0,t,n);if(t===`last`)return commitIndex(e,e.length-1,t,n);let r=resolveColumns(),i=t===`next`?1:t===`prev`?-1:t===`nextRow`?r:-r,o=activeIndex(e),c=o<0?i>0?0:e.length-1:o+i;if(a.loop)return commitIndex(e,(c%e.length+e.length)%e.length,t,n);let l=Math.min(e.length-1,Math.max(0,c));return l===o?null:commitIndex(e,l,t,n)},resolveKeyAction=e=>{let o=resolve(a.direction,`ltr`)===`rtl`?r:n;for(let n of i)if((a.keys?.[n]??o[n]).some(n=>(0,t.matchKey)(e,n)))return n},handleKeydown=e=>{if(e.defaultPrevented||e.isComposing||resolve(a.disabled,!1))return null;let t=resolveKeyAction(e);if(!t)return null;let n=navigateWithItems(a.getItems(),t,e);return e.preventDefault(),Object.freeze({change:n,handled:!0})},navigate=e=>navigateWithItems(a.getItems(),e),set=e=>{let t=a.getItems();if(!Number.isInteger(e)||e<0||!t.length){s=-1;return}s=Math.min(e,t.length-1)},reset=()=>{s=-1},getIndex=()=>activeIndex(a.getItems()),getActiveItem=()=>{let e=a.getItems(),t=activeIndex(e);return t>=0?e[t]:void 0};return{getActiveItem,getIndex,handleKeydown,navigate,reset,set}};exports.createGridNavigation=createGridNavigation;
2
+ //# sourceMappingURL=grid-navigation.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"grid-navigation.cjs","names":[],"sources":["../src/grid-navigation.ts"],"sourcesContent":["import { matchKey } from '@vielzeug/keymap';\n\nimport { FocusConfigError } from './errors.js';\nimport type { MaybeGetter } from './list-navigation.js';\n\nexport type GridNavigationAction = 'first' | 'last' | 'next' | 'nextRow' | 'prev' | 'prevRow';\n\nexport type GridNavigationChange<T> = {\n readonly action: GridNavigationAction;\n readonly event?: KeyboardEvent;\n readonly index: number;\n readonly item: T;\n};\n\nexport type GridKeyResult<T> = {\n readonly change: GridNavigationChange<T> | null;\n readonly handled: true;\n};\n\n/**\n * Columns per row: a fixed count, or a getter resolved on every navigation. Responsive grids\n * read a media query or computed style; measured grids read the rendered row length.\n */\nexport type GridColumns = number | (() => number);\n\nexport type GridNavigationOptions<T> = {\n /** Columns per row, resolved on every navigation. */\n columns: GridColumns;\n direction?: MaybeGetter<'ltr' | 'rtl'>;\n disabled?: MaybeGetter<boolean>;\n /**\n * Reads the active index from the outside (usually the index of `document.activeElement`\n * among the items), so focus moved by pointer or Tab stays in sync. When omitted, the\n * navigation tracks the index internally through `set()`.\n */\n getActiveIndex?: () => number;\n getItems: () => readonly T[];\n keys?: Partial<Record<GridNavigationAction, readonly string[]>>;\n /** Wrap moves around the grid's edges by flat index. Default clamps at the edges. */\n loop?: boolean;\n};\n\nexport type GridNavigation<T> = {\n getActiveItem(): T | undefined;\n getIndex(): number;\n handleKeydown(event: KeyboardEvent): GridKeyResult<T> | null;\n navigate(action: GridNavigationAction): GridNavigationChange<T> | null;\n reset(): void;\n set(index: number): void;\n};\n\nconst DEFAULT_KEYS: Record<GridNavigationAction, readonly string[]> = {\n first: ['Home'],\n last: ['End'],\n next: ['ArrowRight'],\n nextRow: ['ArrowDown'],\n prev: ['ArrowLeft'],\n prevRow: ['ArrowUp'],\n};\n\nconst DEFAULT_KEYS_RTL: typeof DEFAULT_KEYS = {\n ...DEFAULT_KEYS,\n next: ['ArrowLeft'],\n prev: ['ArrowRight'],\n};\n\nconst GRID_ACTIONS = ['first', 'last', 'next', 'nextRow', 'prev', 'prevRow'] as const;\n\nconst resolve = <V>(value: MaybeGetter<V> | undefined, fallback: V): V =>\n typeof value === 'function' ? (value as () => V)() : (value ?? fallback);\n\n/**\n * Keyboard navigation over a two-dimensional grid of items: horizontal arrows step one\n * item, vertical arrows step one row (the column count), Home/End jump to the ends.\n *\n * Unlike `createListNavigation`, items are not skipped when disabled — skipping in two\n * dimensions would break row alignment. Focus (or otherwise activate) the item reported\n * by the returned change; `handleKeydown` calls `preventDefault()` for every recognized\n * key so unhandled arrows never scroll the page.\n */\nexport const createGridNavigation = <T>(options: GridNavigationOptions<T>): GridNavigation<T> => {\n const owners = new Map<string, GridNavigationAction>();\n\n for (const action of GRID_ACTIONS) {\n for (const key of options.keys?.[action] ?? []) {\n const owner = owners.get(key);\n if (owner && owner !== action)\n throw new FocusConfigError(`Key \"${key}\" is assigned to both ${owner} and ${action}`);\n owners.set(key, action);\n }\n }\n\n let index = -1;\n\n const resolveColumns = (): number => {\n const columns = resolve(options.columns, 1);\n if (!Number.isInteger(columns) || columns < 1)\n throw new FocusConfigError(`columns must resolve to a positive integer, got: ${columns}`);\n return columns;\n };\n\n const activeIndex = (items: readonly T[]): number => {\n const derived = options.getActiveIndex?.();\n if (derived !== undefined) index = derived >= 0 && derived < items.length ? derived : -1;\n return index;\n };\n\n const commitIndex = (\n items: readonly T[],\n nextIndex: number,\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> => {\n index = nextIndex;\n return Object.freeze({ action, event, index: nextIndex, item: items[nextIndex] });\n };\n\n const navigateWithItems = (\n items: readonly T[],\n action: GridNavigationAction,\n event?: KeyboardEvent,\n ): GridNavigationChange<T> | null => {\n if (!items.length) {\n index = -1;\n return null;\n }\n\n if (action === 'first') return commitIndex(items, 0, action, event);\n if (action === 'last') return commitIndex(items, items.length - 1, action, event);\n\n const columns = resolveColumns();\n const offset = action === 'next' ? 1 : action === 'prev' ? -1 : action === 'nextRow' ? columns : -columns;\n const current = activeIndex(items);\n const base = current < 0 ? (offset > 0 ? 0 : items.length - 1) : current + offset;\n\n if (options.loop) {\n return commitIndex(items, ((base % items.length) + items.length) % items.length, action, event);\n }\n\n const clamped = Math.min(items.length - 1, Math.max(0, base));\n if (clamped === current) return null;\n\n return commitIndex(items, clamped, action, event);\n };\n\n const resolveKeyAction = (event: KeyboardEvent): GridNavigationAction | undefined => {\n const keyTable = resolve(options.direction, 'ltr') === 'rtl' ? DEFAULT_KEYS_RTL : DEFAULT_KEYS;\n\n for (const action of GRID_ACTIONS) {\n if ((options.keys?.[action] ?? keyTable[action]).some((pattern) => matchKey(event, pattern))) return action;\n }\n\n return undefined;\n };\n\n const handleKeydown = (event: KeyboardEvent): GridKeyResult<T> | null => {\n if (event.defaultPrevented || event.isComposing || resolve(options.disabled, false)) return null;\n\n const action = resolveKeyAction(event);\n if (!action) return null;\n\n const change = navigateWithItems(options.getItems(), action, event);\n event.preventDefault();\n return Object.freeze({ change, handled: true });\n };\n\n const navigate = (action: GridNavigationAction): GridNavigationChange<T> | null =>\n navigateWithItems(options.getItems(), action);\n\n const set = (nextIndex: number): void => {\n const items = options.getItems();\n\n if (!Number.isInteger(nextIndex) || nextIndex < 0 || !items.length) {\n index = -1;\n return;\n }\n\n index = Math.min(nextIndex, items.length - 1);\n };\n\n const reset = (): void => {\n index = -1;\n };\n\n const getIndex = (): number => activeIndex(options.getItems());\n\n const getActiveItem = (): T | undefined => {\n const items = options.getItems();\n const current = activeIndex(items);\n return current >= 0 ? items[current] : undefined;\n };\n\n return {\n getActiveItem,\n getIndex,\n handleKeydown,\n navigate,\n reset,\n set,\n };\n};\n"],"mappings":"kEAmDA,IAAM,EAAgE,CACpE,MAAO,CAAC,MAAM,EACd,KAAM,CAAC,K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