@zag-js/focus-visible
Advanced tools
Comparing version 0.0.0-dev-20220616113129 to 0.0.0-dev-20220617070002
@@ -0,3 +1,5 @@ | ||
declare type Modality = "keyboard" | "pointer" | "virtual"; | ||
declare type FocusVisibleCallback = (isFocusVisible: boolean) => void; | ||
export declare function trackFocusVisible(fn: FocusVisibleCallback): () => void; | ||
export declare function trackInteractionModality(fn: (v: Modality | null) => void): () => void; | ||
export {}; |
@@ -23,3 +23,4 @@ "use strict"; | ||
__export(src_exports, { | ||
trackFocusVisible: () => trackFocusVisible | ||
trackFocusVisible: () => trackFocusVisible, | ||
trackInteractionModality: () => trackInteractionModality | ||
}); | ||
@@ -30,10 +31,11 @@ module.exports = __toCommonJS(src_exports); | ||
var hasEventBeforeFocus = false; | ||
var hasBlurredWindowRecently = false; | ||
var handlers = /* @__PURE__ */ new Set(); | ||
var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false; | ||
function isValidKey(event) { | ||
return !(event.metaKey || !isMac && event.altKey || event.ctrlKey); | ||
} | ||
function trigger(modality2, event) { | ||
handlers.forEach((handler) => handler(modality2, event)); | ||
} | ||
var isMac = typeof window !== "undefined" && window.navigator != null ? /^Mac/.test(window.navigator.platform) : false; | ||
function isValidKey(e) { | ||
return !(e.metaKey || !isMac && e.altKey || e.ctrlKey || e.key === "Control" || e.key === "Shift" || e.key === "Meta"); | ||
} | ||
function onKeyboardEvent(event) { | ||
@@ -56,2 +58,13 @@ hasEventBeforeFocus = true; | ||
} | ||
function isVirtualClick(event) { | ||
if (event.mozInputSource === 0 && event.isTrusted) | ||
return true; | ||
return event.detail === 0 && !event.pointerType; | ||
} | ||
function onClickEvent(e) { | ||
if (isVirtualClick(e)) { | ||
hasEventBeforeFocus = true; | ||
modality = "virtual"; | ||
} | ||
} | ||
function onWindowFocus(event) { | ||
@@ -61,13 +74,15 @@ if (event.target === window || event.target === document) { | ||
} | ||
if (!hasEventBeforeFocus) { | ||
modality = "keyboard"; | ||
trigger("keyboard", event); | ||
if (!hasEventBeforeFocus && !hasBlurredWindowRecently) { | ||
modality = "virtual"; | ||
trigger("virtual", event); | ||
} | ||
hasEventBeforeFocus = false; | ||
hasBlurredWindowRecently = false; | ||
} | ||
function onWindowBlur() { | ||
hasEventBeforeFocus = false; | ||
hasBlurredWindowRecently = true; | ||
} | ||
function isFocusVisible() { | ||
return modality === "keyboard"; | ||
return modality != null && modality !== "pointer"; | ||
} | ||
@@ -85,2 +100,3 @@ function setupGlobalFocusEvents() { | ||
document.addEventListener("keyup", onKeyboardEvent, true); | ||
document.addEventListener("click", onClickEvent, true); | ||
window.addEventListener("focus", onWindowFocus, true); | ||
@@ -108,1 +124,10 @@ window.addEventListener("blur", onWindowBlur, false); | ||
} | ||
function trackInteractionModality(fn) { | ||
setupGlobalFocusEvents(); | ||
fn(modality); | ||
const handler = () => fn(modality); | ||
handlers.add(handler); | ||
return () => { | ||
handlers.delete(handler); | ||
}; | ||
} |
{ | ||
"name": "@zag-js/focus-visible", | ||
"version": "0.0.0-dev-20220616113129", | ||
"version": "0.0.0-dev-20220617070002", | ||
"description": "Focus visible polyfill utility based on WICG", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9309
238