47 lines
1.7 KiB
JavaScript
47 lines
1.7 KiB
JavaScript
"use strict";
|
|
'use client';
|
|
|
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = useFocusableWhenDisabled;
|
|
var React = _interopRequireWildcard(require("react"));
|
|
function useFocusableWhenDisabled(parameters) {
|
|
const {
|
|
focusableWhenDisabled,
|
|
disabled,
|
|
composite = false,
|
|
tabIndex: tabIndexProp = 0,
|
|
isNativeButton
|
|
} = parameters;
|
|
const isFocusableComposite = composite && focusableWhenDisabled !== false;
|
|
const isNonFocusableComposite = composite && focusableWhenDisabled === false;
|
|
|
|
// we can't explicitly assign `undefined` to any of these props because it
|
|
// would otherwise prevent subsequently merged props from setting them
|
|
const props = React.useMemo(() => {
|
|
const additionalProps = {
|
|
// allow Tabbing away from focusableWhenDisabled elements
|
|
onKeyDown(event) {
|
|
if (disabled && focusableWhenDisabled && event.key !== 'Tab') {
|
|
event.preventDefault();
|
|
}
|
|
}
|
|
};
|
|
if (!composite) {
|
|
additionalProps.tabIndex = tabIndexProp;
|
|
if (!isNativeButton && disabled) {
|
|
additionalProps.tabIndex = focusableWhenDisabled ? tabIndexProp : -1;
|
|
}
|
|
}
|
|
if (isNativeButton && (focusableWhenDisabled || isFocusableComposite) || !isNativeButton && disabled) {
|
|
additionalProps['aria-disabled'] = disabled;
|
|
}
|
|
if (isNativeButton && (!focusableWhenDisabled || isNonFocusableComposite)) {
|
|
additionalProps.disabled = disabled;
|
|
}
|
|
return additionalProps;
|
|
}, [composite, disabled, focusableWhenDisabled, isFocusableComposite, isNonFocusableComposite, isNativeButton, tabIndexProp]);
|
|
return props;
|
|
} |