@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
30 lines (29 loc) • 841 B
JavaScript
import * as React from 'react';
import { useEnhancedClickHandler } from './useEnhancedClickHandler.js';
/**
* Determines the interaction type (keyboard, mouse, touch, etc.) that opened the component.
*
* @param open The open state of the component.
*/
export function useOpenInteractionType(open) {
const [openMethod, setOpenMethod] = React.useState(null);
if (!open && openMethod !== null) {
setOpenMethod(null);
}
const handleTriggerClick = React.useCallback((_, interactionType) => {
if (!open) {
setOpenMethod(interactionType);
}
}, [open, setOpenMethod]);
const {
onClick,
onPointerDown
} = useEnhancedClickHandler(handleTriggerClick);
return React.useMemo(() => ({
openMethod,
triggerProps: {
onClick,
onPointerDown
}
}), [openMethod, onClick, onPointerDown]);
}