@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.
100 lines (97 loc) • 3.23 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { triggerOpenStateMapping } from '../../utils/collapsibleOpenStateMapping.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useEnhancedEffect } from '../../utils/useEnhancedEffect.js';
import { useCollapsibleRootContext } from '../../collapsible/root/CollapsibleRootContext.js';
import { useCollapsibleTrigger } from '../../collapsible/trigger/useCollapsibleTrigger.js';
import { useAccordionItemContext } from '../item/AccordionItemContext.js';
/**
* A button that opens and closes the corresponding panel.
* Renders a `<button>` element.
*
* Documentation: [Base UI Accordion](https://base-ui.com/react/components/accordion)
*/
const AccordionTrigger = /*#__PURE__*/React.forwardRef(function AccordionTrigger(props, forwardedRef) {
const {
disabled: disabledProp,
className,
id,
render,
...otherProps
} = props;
const {
panelId,
disabled: contextDisabled,
open,
setOpen
} = useCollapsibleRootContext();
const {
getRootProps
} = useCollapsibleTrigger({
panelId,
disabled: disabledProp || contextDisabled,
open,
rootRef: forwardedRef,
setOpen
});
const {
state,
setTriggerId,
triggerId
} = useAccordionItemContext();
useEnhancedEffect(() => {
setTriggerId(id);
return () => {
setTriggerId(undefined);
};
}, [id, setTriggerId]);
const {
renderElement
} = useComponentRenderer({
propGetter: getRootProps,
render: render ?? 'button',
state,
className,
extraProps: {
...otherProps,
// the `id` prop doesn't go here directly, it updates a context
// and becomes `triggerId`
id: triggerId
},
customStyleHookMapping: triggerOpenStateMapping
});
return renderElement();
});
export { AccordionTrigger };
process.env.NODE_ENV !== "production" ? AccordionTrigger.propTypes /* remove-proptypes */ = {
// ┌────────────────────────────── Warning ──────────────────────────────┐
// │ These PropTypes are generated from the TypeScript type definitions. │
// │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
// └─────────────────────────────────────────────────────────────────────┘
/**
* @ignore
*/
children: PropTypes.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* @ignore
*/
disabled: PropTypes.bool,
/**
* @ignore
*/
id: PropTypes.string,
/**
* Allows you to replace the component’s HTML element
* with a different tag, or compose it with another component.
*
* Accepts a `ReactElement` or a function that returns the element to render.
*/
render: PropTypes.oneOfType([PropTypes.element, PropTypes.func])
} : void 0;