@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
59 lines (57 loc) • 1.74 kB
JavaScript
import * as React from "react";
//#region src/utils/dom.ts
function runKeyAction(ev, actions, { preventDefault = true } = {}) {
if (ev.key === " ") ev.key = ev.code;
const action = actions[ev.key];
if (!action) return;
if (preventDefault) ev.preventDefault();
action(ev);
}
function isComposing(ev) {
if ("keyCode" in ev) return ev.nativeEvent.isComposing || ev.keyCode === 229;
else if (ev.nativeEvent instanceof InputEvent) return ev.nativeEvent.isComposing;
else return false;
}
function useAttributeObserver(ref, attributeFilter, enabled, func) {
React.useEffect(() => {
if (!ref.current || !enabled) return;
const observer = new (ref.current.ownerDocument.defaultView ?? window).MutationObserver((changes) => {
for (const { type, attributeName } of changes) {
if (type !== "attributes") continue;
if (!attributeName) continue;
if (attributeFilter.includes(attributeName)) func();
}
});
observer.observe(ref.current, {
attributeFilter,
attributes: true
});
return () => observer.disconnect();
});
}
function getEventRelatedTarget(ev) {
return ev.relatedTarget ?? ev.currentTarget.ownerDocument.activeElement;
}
const visuallyHiddenStyle = {
border: "0px",
clipPath: "rect(0px 0px 0px 0px)",
height: "1px",
margin: "-1px",
overflow: "hidden",
padding: "0px",
position: "absolute",
whiteSpace: "nowrap",
width: "1px"
};
const visuallyHiddenAttributes = {
style: visuallyHiddenStyle,
"aria-hidden": true,
tabIndex: -1
};
function* useIds() {
const id = React.useId();
for (let i = 0;; i++) yield `${id}-${i}`;
}
//#endregion
export { getEventRelatedTarget, isComposing, runKeyAction, useAttributeObserver, useIds, visuallyHiddenAttributes };
//# sourceMappingURL=dom.js.map