@engie-group/fluid-design-system-react
Version:
Fluid Design System React
34 lines (31 loc) • 1.19 kB
JavaScript
import React__default, { useEffect } from 'react';
/**
* Apply the inert attribute to an HTMLElement depending on the value of the inert parameter.
* @param ref The reference to the HTMLElement to which the inert attribute will be applied.
* @param inert Whether the inert attribute should be applied or not.
*/
const useInert = (ref, inert = false) => {
useEffect(() => {
if (!ref.current) {
return;
}
const element = ref.current;
element.inert = inert;
return () => {
element.inert = false;
};
}, [inert, ref]);
};
const useStateControl = (initialValue, controlledValue, changeCallback) => {
const [uncontrolledValue, setUncontrolledValue] = React__default.useState(initialValue);
const setValue = React__default.useCallback((value, emit = true) => {
if (emit) {
changeCallback?.(value);
}
if (controlledValue === undefined) {
setUncontrolledValue(value);
}
}, [controlledValue, changeCallback]);
return [controlledValue === undefined ? uncontrolledValue : controlledValue, setValue];
};
export { useInert, useStateControl };