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.

91 lines (89 loc) 3.16 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { useComponentRenderer } from '../../utils/useComponentRenderer.js'; import { useRadioRootContext } from '../root/RadioRootContext.js'; import { customStyleHookMapping } from '../utils/customStyleHookMapping.js'; import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { useTransitionStatus } from '../../utils/useTransitionStatus.js'; /** * Indicates whether the radio button is selected. * Renders a `<span>` element. * * Documentation: [Base UI Radio](https://base-ui.com/react/components/radio) */ const RadioIndicator = /*#__PURE__*/React.forwardRef(function RadioIndicator(props, forwardedRef) { const { render, className, keepMounted = true, ...otherProps } = props; const rootState = useRadioRootContext(); const rendered = rootState.checked; const { mounted, transitionStatus, setMounted } = useTransitionStatus(rendered); const state = React.useMemo(() => ({ ...rootState, transitionStatus }), [rootState, transitionStatus]); const indicatorRef = React.useRef(null); const mergedRef = useForkRef(forwardedRef, indicatorRef); const { renderElement } = useComponentRenderer({ render: render ?? 'span', ref: mergedRef, className, state, extraProps: { hidden: !mounted, ...otherProps }, customStyleHookMapping }); useAfterExitAnimation({ open: rendered, animatedElementRef: indicatorRef, onFinished() { setMounted(false); } }); const shouldRender = keepMounted || rendered; if (!shouldRender) { return null; } return renderElement(); }); process.env.NODE_ENV !== "production" ? RadioIndicator.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 radio button is inactive. * @default true */ 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 { RadioIndicator };