@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.
37 lines (36 loc) • 961 B
JavaScript
'use client';
import * as React from 'react';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useButton } from '../../use-button/index.js';
export function useCollapsibleTrigger(parameters) {
const {
panelId,
disabled,
open,
rootRef: externalRef,
setOpen
} = parameters;
const {
getButtonProps,
buttonRef
} = useButton({
disabled,
focusableWhenDisabled: true,
type: 'button'
});
const handleRef = useForkRef(externalRef, buttonRef);
const getRootProps = React.useCallback((externalProps = {}) => mergeReactProps(externalProps, mergeReactProps({
type: 'button',
'aria-controls': panelId,
'aria-expanded': open,
disabled,
onClick() {
setOpen(!open);
},
ref: handleRef
}, getButtonProps())), [panelId, disabled, getButtonProps, handleRef, open, setOpen]);
return {
getRootProps
};
}