@zag-js/focus-visible 0.0.0-dev-20220604174031 → 0.1.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  declare type FocusVisibleCallback = (isFocusVisible: boolean) => void;
2
- export declare function trackFocusVisible(fn: FocusVisibleCallback): () => boolean;
2
+ export declare function trackFocusVisible(fn: FocusVisibleCallback): () => void;
3
3
  export {};
4
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,aAAK,oBAAoB,GAAG,CAAC,cAAc,EAAE,OAAO,KAAK,IAAI,CAAA;AAkG7D,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,oBAAoB,iBAQzD"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,aAAK,oBAAoB,GAAG,CAAC,cAAc,EAAE,OAAO,KAAK,IAAI,CAAA;AAkG7D,wBAAgB,iBAAiB,CAAC,EAAE,EAAE,oBAAoB,cAUzD"}
package/dist/index.js CHANGED
@@ -92,6 +92,8 @@ function trackFocusVisible(fn) {
92
92
  fn(isFocusVisible());
93
93
  const handler = () => fn(isFocusVisible());
94
94
  handlers.add(handler);
95
- return () => handlers.delete(handler);
95
+ return () => {
96
+ handlers.delete(handler);
97
+ };
96
98
  }
97
99
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["type Modality = \"keyboard\" | \"pointer\"\ntype HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent\ntype Handler = (modality: Modality, e: HandlerEvent) => void\ntype FocusVisibleCallback = (isFocusVisible: boolean) => void\n\nlet hasSetup = false\nlet modality: Modality | null = null\nlet hasEventBeforeFocus = false\n\nconst handlers = new Set<Handler>()\n\nconst isMac = typeof window !== \"undefined\" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false\n\nfunction isValidKey(event: KeyboardEvent) {\n return !(event.metaKey || (!isMac && event.altKey) || event.ctrlKey)\n}\n\nfunction trigger(modality: Modality, event: HandlerEvent) {\n handlers.forEach((handler) => handler(modality, event))\n}\n\nfunction onKeyboardEvent(event: KeyboardEvent) {\n hasEventBeforeFocus = true\n if (isValidKey(event)) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n}\n\nfunction onPointerEvent(event: PointerEvent | MouseEvent) {\n modality = \"pointer\"\n if (event.type === \"mousedown\" || event.type === \"pointerdown\") {\n hasEventBeforeFocus = true\n trigger(\"pointer\", event)\n }\n}\n\nfunction onWindowFocus(event: FocusEvent) {\n // Firefox fires two extra focus events when the user first clicks into an iframe:\n // first on the window, then on the document. We ignore these events so they don't\n // cause keyboard focus rings to appear.\n if (event.target === window || event.target === document) {\n return\n }\n\n // If a focus event occurs without a preceding keyboard or pointer event, switch to keyboard modality.\n // This occurs, for example, when navigating a form with the next/previous buttons on iOS.\n if (!hasEventBeforeFocus) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n\n hasEventBeforeFocus = false\n}\n\nfunction onWindowBlur() {\n // When the window is blurred, reset state. This is necessary when tabbing out of the window,\n // for example, since a subsequent focus event won't be fired.\n hasEventBeforeFocus = false\n}\n\nfunction isFocusVisible() {\n return modality !== \"pointer\"\n}\n\nfunction setupGlobalFocusEvents() {\n if (typeof window === \"undefined\" || hasSetup) {\n return\n }\n\n // Programmatic focus() calls shouldn't affect the current input modality.\n // However, we need to detect other cases when a focus event occurs without\n // a preceding user event (e.g. screen reader focus). Overriding the focus\n // method on HTMLElement.prototype is a bit hacky, but works.\n const { focus } = HTMLElement.prototype\n HTMLElement.prototype.focus = function focusElement(...args) {\n hasEventBeforeFocus = true\n focus.apply(this, args)\n }\n\n document.addEventListener(\"keydown\", onKeyboardEvent, true)\n document.addEventListener(\"keyup\", onKeyboardEvent, true)\n\n // Register focus events on the window so they are sure to happen\n // before React's event listeners (registered on the document).\n window.addEventListener(\"focus\", onWindowFocus, true)\n window.addEventListener(\"blur\", onWindowBlur, false)\n\n if (typeof PointerEvent !== \"undefined\") {\n document.addEventListener(\"pointerdown\", onPointerEvent, true)\n document.addEventListener(\"pointermove\", onPointerEvent, true)\n document.addEventListener(\"pointerup\", onPointerEvent, true)\n } else {\n document.addEventListener(\"mousedown\", onPointerEvent, true)\n document.addEventListener(\"mousemove\", onPointerEvent, true)\n document.addEventListener(\"mouseup\", onPointerEvent, true)\n }\n\n hasSetup = true\n}\n\nexport function trackFocusVisible(fn: FocusVisibleCallback) {\n setupGlobalFocusEvents()\n\n fn(isFocusVisible())\n const handler = () => fn(isFocusVisible())\n\n handlers.add(handler)\n return () => handlers.delete(handler)\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,IAAI,WAAW;AACf,IAAI,WAA4B;AAChC,IAAI,sBAAsB;AAE1B,IAAM,WAAW,oBAAI,IAAa;AAElC,IAAM,QAAQ,OAAO,WAAW,eAAe,OAAO,aAAa,OAAO,OAAO,KAAK,OAAO,UAAU,QAAQ,IAAI;AAEnH,oBAAoB,OAAsB;AACxC,SAAO,CAAE,OAAM,WAAY,CAAC,SAAS,MAAM,UAAW,MAAM;AAC9D;AAEA,iBAAiB,WAAoB,OAAqB;AACxD,WAAS,QAAQ,CAAC,YAAY,QAAQ,WAAU,KAAK,CAAC;AACxD;AAEA,yBAAyB,OAAsB;AAC7C,wBAAsB;AACtB,MAAI,WAAW,KAAK,GAAG;AACrB,eAAW;AACX,YAAQ,YAAY,KAAK;AAAA,EAC3B;AACF;AAEA,wBAAwB,OAAkC;AACxD,aAAW;AACX,MAAI,MAAM,SAAS,eAAe,MAAM,SAAS,eAAe;AAC9D,0BAAsB;AACtB,YAAQ,WAAW,KAA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usEvent\ntype Handler = (modality: Modality, e: HandlerEvent) => void\ntype FocusVisibleCallback = (isFocusVisible: boolean) => void\n\nlet hasSetup = false\nlet modality: Modality | null = null\nlet hasEventBeforeFocus = false\n\nconst handlers = new Set<Handler>()\n\nconst isMac = typeof window !== \"undefined\" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false\n\nfunction isValidKey(event: KeyboardEvent) {\n return !(event.metaKey || (!isMac && event.altKey) || event.ctrlKey)\n}\n\nfunction trigger(modality: Modality, event: HandlerEvent) {\n handlers.forEach((handler) => handler(modality, event))\n}\n\nfunction onKeyboardEvent(event: KeyboardEvent) {\n hasEventBeforeFocus = true\n if (isValidKey(event)) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n}\n\nfunction onPointerEvent(event: PointerEvent | MouseEvent) {\n modality = \"pointer\"\n if (event.type === \"mousedown\" || event.type === \"pointerdown\") {\n hasEventBeforeFocus = true\n trigger(\"pointer\", event)\n }\n}\n\nfunction onWindowFocus(event: FocusEvent) {\n // Firefox fires two extra focus events when the user first clicks into an iframe:\n // first on the window, then on the document. We ignore these events so they don't\n // cause keyboard focus rings to appear.\n if (event.target === window || event.target === document) {\n return\n }\n\n // If a focus event occurs without a preceding keyboard or pointer event, switch to keyboard modality.\n // This occurs, for example, when navigating a form with the next/previous buttons on iOS.\n if (!hasEventBeforeFocus) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n\n hasEventBeforeFocus = false\n}\n\nfunction onWindowBlur() {\n // When the window is blurred, reset state. This is necessary when tabbing out of the window,\n // for example, since a subsequent focus event won't be fired.\n hasEventBeforeFocus = false\n}\n\nfunction isFocusVisible() {\n return modality !== \"pointer\"\n}\n\nfunction setupGlobalFocusEvents() {\n if (typeof window === \"undefined\" || hasSetup) {\n return\n }\n\n // Programmatic focus() calls shouldn't affect the current input modality.\n // However, we need to detect other cases when a focus event occurs without\n // a preceding user event (e.g. screen reader focus). Overriding the focus\n // method on HTMLElement.prototype is a bit hacky, but works.\n const { focus } = HTMLElement.prototype\n HTMLElement.prototype.focus = function focusElement(...args) {\n hasEventBeforeFocus = true\n focus.apply(this, args)\n }\n\n document.addEventListener(\"keydown\", onKeyboardEvent, true)\n document.addEventListener(\"keyup\", onKeyboardEvent, true)\n\n // Register focus events on the window so they are sure to happen\n // before React's event listeners (registered on the document).\n window.addEventListener(\"focus\", onWindowFocus, true)\n window.addEventListener(\"blur\", onWindowBlur, false)\n\n if (typeof PointerEvent !== \"undefined\") {\n document.addEventListener(\"pointerdown\", onPointerEvent, true)\n document.addEventListener(\"pointermove\", onPointerEvent, true)\n document.addEventListener(\"pointerup\", onPointerEvent, true)\n } else {\n document.addEventListener(\"mousedown\", onPointerEvent, true)\n document.addEventListener(\"mousemove\", onPointerEvent, true)\n document.addEventListener(\"mouseup\", onPointerEvent, true)\n }\n\n hasSetup = true\n}\n\nexport function trackFocusVisible(fn: FocusVisibleCallback) {\n setupGlobalFocusEvents()\n\n fn(isFocusVisible())\n const handler = () => fn(isFocusVisible())\n\n handlers.add(handler)\n return () => {\n handlers.delete(handler)\n }\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAKA,IAAI,WAAW;AACf,IAAI,WAA4B;AAChC,IAAI,sBAAsB;AAE1B,IAAM,WAAW,oBAAI,IAAa;AAElC,IAAM,QAAQ,OAAO,WAAW,eAAe,OAAO,aAAa,OAAO,OAAO,KAAK,OAAO,UAAU,QAAQ,IAAI;AAEnH,oBAAoB,OAAsB;AACxC,SAAO,CAAE,OAAM,WAAY,CAAC,SAAS,MAAM,UAAW,MAAM;AAC9D;AAEA,iBAAiB,WAAoB,OAAqB;AACxD,WAAS,QAAQ,CAAC,YAAY,QAAQ,WAAU,KAAK,CAAC;AACxD;AAEA,yBAAyB,OAAsB;AAC7C,wBAAsB;AACtB,MAAI,WAAW,KAAK,GAAG;AACrB,eAAW;AACX,YAAQ,YAAY,KAAK;AAAA,EAC3B;AACF;AAE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M;AACX,aAAS,OAAO,OAAO;AAAA,EACzB;AACF;",
6
6
  "names": []
7
7
  }
package/dist/index.mjs CHANGED
@@ -69,7 +69,9 @@ function trackFocusVisible(fn) {
69
69
  fn(isFocusVisible());
70
70
  const handler = () => fn(isFocusVisible());
71
71
  handlers.add(handler);
72
- return () => handlers.delete(handler);
72
+ return () => {
73
+ handlers.delete(handler);
74
+ };
73
75
  }
74
76
  export {
75
77
  trackFocusVisible
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/index.ts"],
4
- "sourcesContent": ["type Modality = \"keyboard\" | \"pointer\"\ntype HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent\ntype Handler = (modality: Modality, e: HandlerEvent) => void\ntype FocusVisibleCallback = (isFocusVisible: boolean) => void\n\nlet hasSetup = false\nlet modality: Modality | null = null\nlet hasEventBeforeFocus = false\n\nconst handlers = new Set<Handler>()\n\nconst isMac = typeof window !== \"undefined\" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false\n\nfunction isValidKey(event: KeyboardEvent) {\n return !(event.metaKey || (!isMac && event.altKey) || event.ctrlKey)\n}\n\nfunction trigger(modality: Modality, event: HandlerEvent) {\n handlers.forEach((handler) => handler(modality, event))\n}\n\nfunction onKeyboardEvent(event: KeyboardEvent) {\n hasEventBeforeFocus = true\n if (isValidKey(event)) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n}\n\nfunction onPointerEvent(event: PointerEvent | MouseEvent) {\n modality = \"pointer\"\n if (event.type === \"mousedown\" || event.type === \"pointerdown\") {\n hasEventBeforeFocus = true\n trigger(\"pointer\", event)\n }\n}\n\nfunction onWindowFocus(event: FocusEvent) {\n // Firefox fires two extra focus events when the user first clicks into an iframe:\n // first on the window, then on the document. We ignore these events so they don't\n // cause keyboard focus rings to appear.\n if (event.target === window || event.target === document) {\n return\n }\n\n // If a focus event occurs without a preceding keyboard or pointer event, switch to keyboard modality.\n // This occurs, for example, when navigating a form with the next/previous buttons on iOS.\n if (!hasEventBeforeFocus) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n\n hasEventBeforeFocus = false\n}\n\nfunction onWindowBlur() {\n // When the window is blurred, reset state. This is necessary when tabbing out of the window,\n // for example, since a subsequent focus event won't be fired.\n hasEventBeforeFocus = false\n}\n\nfunction isFocusVisible() {\n return modality !== \"pointer\"\n}\n\nfunction setupGlobalFocusEvents() {\n if (typeof window === \"undefined\" || hasSetup) {\n return\n }\n\n // Programmatic focus() calls shouldn't affect the current input modality.\n // However, we need to detect other cases when a focus event occurs without\n // a preceding user event (e.g. screen reader focus). Overriding the focus\n // method on HTMLElement.prototype is a bit hacky, but works.\n const { focus } = HTMLElement.prototype\n HTMLElement.prototype.focus = function focusElement(...args) {\n hasEventBeforeFocus = true\n focus.apply(this, args)\n }\n\n document.addEventListener(\"keydown\", onKeyboardEvent, true)\n document.addEventListener(\"keyup\", onKeyboardEvent, true)\n\n // Register focus events on the window so they are sure to happen\n // before React's event listeners (registered on the document).\n window.addEventListener(\"focus\", onWindowFocus, true)\n window.addEventListener(\"blur\", onWindowBlur, false)\n\n if (typeof PointerEvent !== \"undefined\") {\n document.addEventListener(\"pointerdown\", onPointerEvent, true)\n document.addEventListener(\"pointermove\", onPointerEvent, true)\n document.addEventListener(\"pointerup\", onPointerEvent, true)\n } else {\n document.addEventListener(\"mousedown\", onPointerEvent, true)\n document.addEventListener(\"mousemove\", onPointerEvent, true)\n document.addEventListener(\"mouseup\", onPointerEvent, true)\n }\n\n hasSetup = true\n}\n\nexport function trackFocusVisible(fn: FocusVisibleCallback) {\n setupGlobalFocusEvents()\n\n fn(isFocusVisible())\n const handler = () => fn(isFocusVisible())\n\n handlers.add(handler)\n return () => handlers.delete(handler)\n}\n"],
5
- "mappings": ";AAKA,IAAI,WAAW;AACf,IAAI,WAA4B;AAChC,IAAI,sBAAsB;AAE1B,IAAM,WAAW,oBAAI,IAAa;AAElC,IAAM,QAAQ,OAAO,WA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AgB,IAAI;AAC7D,aAAS,iBAAiB,aAAa,gBAAgB,IAAI;AAAA,EAC7D,OAAO;AACL,aAAS,iBAAiB,aAAa,gBAAgB,IAAI;AAC3D,aAAS,iBAAiB,aAAa,gBAAgB,IAAI;AAC3D,aAAS,iBAAiB,WAAW,gBAAgB,IAAI;AAAA,EAC3D;AAEA,aAAW;AACb;AAEO,2BAA2B,IAA0B;AAC1D,yBAAuB;AAEvB,KAAG,eAAe,CAAC;AACnB,QAAM,UAAU,MAAM,GAAG,eAAe,CAAC;AAEzC,WAAS,IAAI,OAAO;AACpB,SAAO,MAAM,SAAS,OAAO,OAAO;AACtC;",
4
+ "sourcesContent": ["type Modality = \"keyboard\" | \"pointer\"\ntype HandlerEvent = PointerEvent | MouseEvent | KeyboardEvent | FocusEvent\ntype Handler = (modality: Modality, e: HandlerEvent) => void\ntype FocusVisibleCallback = (isFocusVisible: boolean) => void\n\nlet hasSetup = false\nlet modality: Modality | null = null\nlet hasEventBeforeFocus = false\n\nconst handlers = new Set<Handler>()\n\nconst isMac = typeof window !== \"undefined\" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false\n\nfunction isValidKey(event: KeyboardEvent) {\n return !(event.metaKey || (!isMac && event.altKey) || event.ctrlKey)\n}\n\nfunction trigger(modality: Modality, event: HandlerEvent) {\n handlers.forEach((handler) => handler(modality, event))\n}\n\nfunction onKeyboardEvent(event: KeyboardEvent) {\n hasEventBeforeFocus = true\n if (isValidKey(event)) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n}\n\nfunction onPointerEvent(event: PointerEvent | MouseEvent) {\n modality = \"pointer\"\n if (event.type === \"mousedown\" || event.type === \"pointerdown\") {\n hasEventBeforeFocus = true\n trigger(\"pointer\", event)\n }\n}\n\nfunction onWindowFocus(event: FocusEvent) {\n // Firefox fires two extra focus events when the user first clicks into an iframe:\n // first on the window, then on the document. We ignore these events so they don't\n // cause keyboard focus rings to appear.\n if (event.target === window || event.target === document) {\n return\n }\n\n // If a focus event occurs without a preceding keyboard or pointer event, switch to keyboard modality.\n // This occurs, for example, when navigating a form with the next/previous buttons on iOS.\n if (!hasEventBeforeFocus) {\n modality = \"keyboard\"\n trigger(\"keyboard\", event)\n }\n\n hasEventBeforeFocus = false\n}\n\nfunction onWindowBlur() {\n // When the window is blurred, reset state. This is necessary when tabbing out of the window,\n // for example, since a subsequent focus event won't be fired.\n hasEventBeforeFocus = false\n}\n\nfunction isFocusVisible() {\n return modality !== \"pointer\"\n}\n\nfunction setupGlobalFocusEvents() {\n if (typeof window === \"undefined\" || hasSetup) {\n return\n }\n\n // Programmatic focus() calls shouldn't affect the current input modality.\n // However, we need to detect other cases when a focus event occurs without\n // a preceding user event (e.g. screen reader focus). Overriding the focus\n // method on HTMLElement.prototype is a bit hacky, but works.\n const { focus } = HTMLElement.prototype\n HTMLElement.prototype.focus = function focusElement(...args) {\n hasEventBeforeFocus = true\n focus.apply(this, args)\n }\n\n document.addEventListener(\"keydown\", onKeyboardEvent, true)\n document.addEventListener(\"keyup\", onKeyboardEvent, true)\n\n // Register focus events on the window so they are sure to happen\n // before React's event listeners (registered on the document).\n window.addEventListener(\"focus\", onWindowFocus, true)\n window.addEventListener(\"blur\", onWindowBlur, false)\n\n if (typeof PointerEvent !== \"undefined\") {\n document.addEventListener(\"pointerdown\", onPointerEvent, true)\n document.addEventListener(\"pointermove\", onPointerEvent, true)\n document.addEventListener(\"pointerup\", onPointerEvent, true)\n } else {\n document.addEventListener(\"mousedown\", onPointerEvent, true)\n document.addEventListener(\"mousemove\", onPointerEvent, true)\n document.addEventListener(\"mouseup\", onPointerEvent, true)\n }\n\n hasSetup = true\n}\n\nexport function trackFocusVisible(fn: FocusVisibleCallback) {\n setupGlobalFocusEvents()\n\n fn(isFocusVisible())\n const handler = () => fn(isFocusVisible())\n\n handlers.add(handler)\n return () => {\n handlers.delete(handler)\n }\n}\n"],
5
+ "mappings": ";AAKA,IAAI,WAAW;AACf,IAAI,WAA4B;AAChC,IAAI,sBAA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AS,iBAAiB,eAAe,gBAAgB,IAAI;AAC7D,aAAS,iBAAiB,eAAe,gBAAgB,IAAI;AAC7D,aAAS,iBAAiB,aAAa,gBAAgB,IAAI;AAAA,EAC7D,OAAO;AACL,aAAS,iBAAiB,aAAa,gBAAgB,IAAI;AAC3D,aAAS,iBAAiB,aAAa,gBAAgB,IAAI;AAC3D,aAAS,iBAAiB,WAAW,gBAAgB,IAAI;AAAA,EAC3D;AAEA,aAAW;AACb;AAEO,2BAA2B,IAA0B;AAC1D,yBAAuB;AAEvB,KAAG,eAAe,CAAC;AACnB,QAAM,UAAU,MAAM,GAAG,eAAe,CAAC;AAEzC,WAAS,IAAI,OAAO;AACpB,SAAO,MAAM;AACX,aAAS,OAAO,OAAO;AAAA,EACzB;AACF;",
6
6
  "names": []
7
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zag-js/focus-visible",
3
- "version": "0.0.0-dev-20220604174031",
3
+ "version": "0.1.0",
4
4
  "description": "Focus visible polyfill utility based on WICG",
5
5
  "keywords": [
6
6
  "js",