@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.
95 lines (93 loc) • 3.52 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useMenuCheckboxItemContext } from '../checkbox-item/MenuCheckboxItemContext.js';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { itemMapping } from '../utils/styleHookMapping.js';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
import { useTransitionStatus } from '../../utils/useTransitionStatus.js';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation.js';
import { useForkRef } from '../../utils/useForkRef.js';
/**
* Indicates whether the checkbox item is ticked.
* Renders a `<div>` element.
*
* Documentation: [Base UI Menu](https://base-ui.com/react/components/menu)
*/
const MenuCheckboxItemIndicator = /*#__PURE__*/React.forwardRef(function MenuCheckboxItemIndicator(props, forwardedRef) {
const {
render,
className,
keepMounted = false,
...other
} = props;
const item = useMenuCheckboxItemContext();
const indicatorRef = React.useRef(null);
const mergedRef = useForkRef(forwardedRef, indicatorRef);
const {
mounted,
transitionStatus,
setMounted
} = useTransitionStatus(item.checked);
const getItemProps = React.useCallback((externalProps = {}) => mergeReactProps(externalProps, {
'aria-hidden': true,
hidden: !mounted
}), [mounted]);
useAfterExitAnimation({
open: item.checked,
animatedElementRef: indicatorRef,
onFinished() {
setMounted(false);
}
});
const state = React.useMemo(() => ({
checked: item.checked,
disabled: item.disabled,
highlighted: item.highlighted,
transitionStatus
}), [item.checked, item.disabled, item.highlighted, transitionStatus]);
const {
renderElement
} = useComponentRenderer({
propGetter: getItemProps,
render: render || 'span',
className,
state,
customStyleHookMapping: itemMapping,
extraProps: other,
ref: mergedRef
});
const shouldRender = keepMounted || item.checked;
if (!shouldRender) {
return null;
}
return renderElement();
});
process.env.NODE_ENV !== "production" ? MenuCheckboxItemIndicator.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]),
/**
* Whether to keep the HTML element in the DOM when the checkbox item is not checked.
* @default false
*/
keepMounted: PropTypes.bool,
/**
* 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;
export { MenuCheckboxItemIndicator };