@ariakit/react
Version:
Toolkit for building accessible web apps with React
132 lines (110 loc) • 4.72 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true});
var _MTBSKKM4cjs = require('./MTBSKKM4.cjs');
var _LZ6L3ECGcjs = require('./LZ6L3ECG.cjs');
// ../ariakit-react-components/dist/command/command.js
var _react = require('react');
var TagName = "button";
function isNativeClick(event) {
if (!event.isTrusted) return false;
const element = event.currentTarget;
if (event.key === "Enter") return _LZ6L3ECGcjs.isButton.call(void 0, element) || element.tagName === "SUMMARY" || element.tagName === "A";
if (event.key === " ") return _LZ6L3ECGcjs.isButton.call(void 0, element) || element.tagName === "SUMMARY" || element.tagName === "INPUT" || element.tagName === "SELECT";
return false;
}
var symbol = /* @__PURE__ */ Symbol("command");
var useCommand = _LZ6L3ECGcjs.createHook.call(void 0, function useCommand2({ clickOnEnter = true, clickOnSpace = true, ...props }) {
const ref = _react.useRef.call(void 0, null);
const [isNativeButton, setIsNativeButton] = _react.useState.call(void 0, false);
_react.useEffect.call(void 0, () => {
if (!ref.current) return;
setIsNativeButton(_LZ6L3ECGcjs.isButton.call(void 0, ref.current));
}, []);
const [active, setActive] = _react.useState.call(void 0, false);
const activeRef = _react.useRef.call(void 0, false);
const disabled = _LZ6L3ECGcjs.disabledFromProps.call(void 0, props);
const [isDuplicate, metadataProps] = _LZ6L3ECGcjs.useMetadataProps.call(void 0, props, symbol, true);
_LZ6L3ECGcjs.useSafeLayoutEffect.call(void 0, () => {
if (!disabled) return;
activeRef.current = false;
setActive(false);
}, [disabled]);
const onKeyDownProp = props.onKeyDown;
const onKeyDown = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onKeyDownProp == null ? void 0 : onKeyDownProp(event);
const element = event.currentTarget;
if (event.defaultPrevented) return;
if (isDuplicate) return;
if (disabled) return;
if (!_LZ6L3ECGcjs.isSelfTarget.call(void 0, event)) return;
if (_LZ6L3ECGcjs.isTextField.call(void 0, element)) return;
if (element.isContentEditable) return;
const isEnter = clickOnEnter && event.key === "Enter";
const isSpace = clickOnSpace && event.key === " ";
const shouldPreventEnter = event.key === "Enter" && !clickOnEnter;
const shouldPreventSpace = event.key === " " && !clickOnSpace;
if (shouldPreventEnter || shouldPreventSpace) {
event.preventDefault();
return;
}
if (isEnter || isSpace) {
const nativeClick = isNativeClick(event);
if (isEnter) {
if (!nativeClick) {
event.preventDefault();
const { view, ...eventInit } = event;
const click = () => _LZ6L3ECGcjs.fireClickEvent.call(void 0, element, eventInit);
if (_LZ6L3ECGcjs.isFirefox.call(void 0, )) _LZ6L3ECGcjs.queueBeforeEvent.call(void 0, element, "keyup", click);
else queueMicrotask(click);
}
} else if (isSpace) {
activeRef.current = true;
if (!nativeClick) {
event.preventDefault();
setActive(true);
}
}
}
});
const onKeyUpProp = props.onKeyUp;
const onKeyUp = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onKeyUpProp == null ? void 0 : onKeyUpProp(event);
if (isDuplicate) return;
const isSpace = clickOnSpace && event.key === " ";
if (!activeRef.current || !isSpace) return;
const nativeClick = isNativeClick(event);
activeRef.current = false;
if (!nativeClick) setActive(false);
if (event.defaultPrevented) return;
if (!_LZ6L3ECGcjs.isSelfTarget.call(void 0, event)) return;
if (disabled) return;
if (event.metaKey) return;
if (nativeClick) return;
event.preventDefault();
const element = event.currentTarget;
const { view, ...eventInit } = event;
queueMicrotask(() => _LZ6L3ECGcjs.fireClickEvent.call(void 0, element, eventInit));
});
const onBlurProp = props.onBlur;
const onBlur = _LZ6L3ECGcjs.useEvent.call(void 0, (event) => {
onBlurProp == null ? void 0 : onBlurProp(event);
if (!activeRef.current) return;
activeRef.current = false;
setActive(false);
});
props = {
"data-active": active || void 0,
type: isNativeButton ? "button" : void 0,
...metadataProps,
...props,
ref: _LZ6L3ECGcjs.useMergeRefs.call(void 0, ref, props.ref),
onKeyDown,
onKeyUp,
onBlur
};
props = _MTBSKKM4cjs.useFocusable.call(void 0, props);
return props;
});
var Command = _LZ6L3ECGcjs.forwardRef.call(void 0, function Command2(props) {
return _LZ6L3ECGcjs.createElement.call(void 0, TagName, useCommand(props));
});
exports.useCommand = useCommand; exports.Command = Command;