UNPKG

@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.

253 lines (249 loc) 7.78 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useFloatingTree } from '@floating-ui/react'; import { useMenuCheckboxItem } from './useMenuCheckboxItem.js'; import { MenuCheckboxItemContext } from './MenuCheckboxItemContext.js'; import { useMenuRootContext } from '../root/MenuRootContext.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useBaseUiId } from '../../utils/useBaseUiId.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { itemMapping } from '../utils/styleHookMapping.js'; import { useCompositeListItem } from '../../composite/list/useCompositeListItem.js'; import { jsx as _jsx } from "react/jsx-runtime"; const InnerMenuCheckboxItem = /*#__PURE__*/React.forwardRef(function InnerMenuItem(props, forwardedRef) { const { checked: checkedProp, defaultChecked, onCheckedChange, className, closeOnClick, disabled = false, highlighted, id, menuEvents, propGetter, render, allowMouseUpTriggerRef, typingRef, ...other } = props; const { getRootProps, checked } = useMenuCheckboxItem({ closeOnClick, disabled, highlighted, id, menuEvents, ref: forwardedRef, allowMouseUpTriggerRef, checked: checkedProp, defaultChecked, onCheckedChange, typingRef }); const state = React.useMemo(() => ({ disabled, highlighted, checked }), [disabled, highlighted, checked]); const { renderElement } = useComponentRenderer({ render: render || 'div', className, state, propGetter: externalProps => propGetter(getRootProps(externalProps)), customStyleHookMapping: itemMapping, extraProps: other }); return /*#__PURE__*/_jsx(MenuCheckboxItemContext.Provider, { value: state, children: renderElement() }); }); process.env.NODE_ENV !== "production" ? InnerMenuCheckboxItem.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 */ allowMouseUpTriggerRef: PropTypes.shape({ current: PropTypes.bool.isRequired }).isRequired, /** * Whether the checkbox item is currently ticked. * * To render an uncontrolled checkbox item, use the `defaultChecked` prop instead. */ checked: PropTypes.bool, /** * @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 close the menu when the item is clicked. */ closeOnClick: PropTypes.bool.isRequired, /** * Whether the checkbox item is initially ticked. * * To render a controlled checkbox item, use the `checked` prop instead. * @default false */ defaultChecked: PropTypes.bool, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * @ignore */ highlighted: PropTypes.bool.isRequired, /** * @ignore */ id: PropTypes.string, /** * Overrides the text label to use when the item is matched during keyboard text navigation. */ label: PropTypes.string, /** * @ignore */ menuEvents: PropTypes.shape({ emit: PropTypes.func.isRequired, off: PropTypes.func.isRequired, on: PropTypes.func.isRequired }).isRequired, /** * Event handler called when the checkbox item is ticked or unticked. */ onCheckedChange: PropTypes.func, /** * The click handler for the menu item. */ onClick: PropTypes.func, /** * @ignore */ propGetter: PropTypes.func.isRequired, /** * 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]), /** * @ignore */ typingRef: PropTypes.shape({ current: PropTypes.bool.isRequired }).isRequired } : void 0; const MemoizedInnerMenuCheckboxItem = /*#__PURE__*/React.memo(InnerMenuCheckboxItem); /** * A menu item that toggles a setting on or off. * Renders a `<div>` element. * * Documentation: [Base UI Menu](https://base-ui.com/react/components/menu) */ const MenuCheckboxItem = /*#__PURE__*/React.forwardRef(function MenuCheckboxItem(props, forwardedRef) { const { id: idProp, label, closeOnClick = false, ...other } = props; const itemRef = React.useRef(null); const listItem = useCompositeListItem({ label }); const mergedRef = useForkRef(forwardedRef, listItem.ref, itemRef); const { getItemProps, activeIndex, allowMouseUpTriggerRef, typingRef } = useMenuRootContext(); const id = useBaseUiId(idProp); const highlighted = listItem.index === activeIndex; const { events: menuEvents } = useFloatingTree(); // This wrapper component is used as a performance optimization. // MenuCheckboxItem reads the context and re-renders the actual MenuCheckboxItem // only when it needs to. return /*#__PURE__*/_jsx(MemoizedInnerMenuCheckboxItem, { ...other, id: id, ref: mergedRef, highlighted: highlighted, menuEvents: menuEvents, propGetter: getItemProps, allowMouseUpTriggerRef: allowMouseUpTriggerRef, typingRef: typingRef, closeOnClick: closeOnClick }); }); process.env.NODE_ENV !== "production" ? MenuCheckboxItem.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * Whether the checkbox item is currently ticked. * * To render an uncontrolled checkbox item, use the `defaultChecked` prop instead. */ checked: PropTypes.bool, /** * @ignore */ children: PropTypes.node, /** * Whether to close the menu when the item is clicked. * @default false */ closeOnClick: PropTypes.bool, /** * Whether the checkbox item is initially ticked. * * To render a controlled checkbox item, use the `checked` prop instead. * @default false */ defaultChecked: PropTypes.bool, /** * Whether the component should ignore user interaction. * @default false */ disabled: PropTypes.bool, /** * @ignore */ id: PropTypes.string, /** * Overrides the text label to use when the item is matched during keyboard text navigation. */ label: PropTypes.string, /** * Event handler called when the checkbox item is ticked or unticked. */ onCheckedChange: PropTypes.func, /** * The click handler for the menu item. */ onClick: PropTypes.func } : void 0; export { MenuCheckboxItem };