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.

97 lines (95 loc) 3.5 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useCheckboxRootContext } from '../root/CheckboxRootContext.js'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useCustomStyleHookMapping } from '../utils/useCustomStyleHookMapping.js'; import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation.js'; import { useTransitionStatus } from '../../utils/useTransitionStatus.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { transitionStatusMapping } from '../../utils/styleHookMapping.js'; /** * Indicates whether the checkbox is ticked. * Renders a `<span>` element. * * Documentation: [Base UI Checkbox](https://base-ui.com/react/components/checkbox) */ const CheckboxIndicator = /*#__PURE__*/React.forwardRef(function CheckboxIndicator(props, forwardedRef) { const { render, className, keepMounted = false, ...otherProps } = props; const rootState = useCheckboxRootContext(); const rendered = rootState.checked || rootState.indeterminate; const { mounted, transitionStatus, setMounted } = useTransitionStatus(rendered); const indicatorRef = React.useRef(null); const mergedRef = useForkRef(forwardedRef, indicatorRef); const state = React.useMemo(() => ({ ...rootState, transitionStatus }), [rootState, transitionStatus]); useAfterExitAnimation({ open: rendered, animatedElementRef: indicatorRef, onFinished() { setMounted(false); } }); const baseStyleHookMapping = useCustomStyleHookMapping(rootState); const customStyleHookMapping = React.useMemo(() => ({ ...baseStyleHookMapping, ...transitionStatusMapping }), [baseStyleHookMapping]); const { renderElement } = useComponentRenderer({ render: render ?? 'span', ref: mergedRef, state, className, customStyleHookMapping, extraProps: { hidden: !mounted, ...otherProps } }); const shouldRender = keepMounted || rendered; if (!shouldRender) { return null; } return renderElement(); }); process.env.NODE_ENV !== "production" ? CheckboxIndicator.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 element in the DOM when the checkbox 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 { CheckboxIndicator };