@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.
96 lines (94 loc) • 3.3 kB
JavaScript
'use client';
import * as React from 'react';
import PropTypes from 'prop-types';
import { useComponentRenderer } from '../../utils/useComponentRenderer.js';
import { useSelectItemContext } from '../item/SelectItemContext.js';
import { mergeReactProps } from '../../utils/mergeReactProps.js';
import { useForkRef } from '../../utils/useForkRef.js';
import { useTransitionStatus } from '../../utils/useTransitionStatus.js';
import { useAfterExitAnimation } from '../../utils/useAfterExitAnimation.js';
/**
* Indicates whether the select item is selected.
* Renders a `<div>` element.
*
* Documentation: [Base UI Select](https://base-ui.com/react/components/select)
*/
const SelectItemIndicator = /*#__PURE__*/React.forwardRef(function SelectItemIndicator(props, forwardedRef) {
const {
render,
className,
keepMounted = false,
...otherProps
} = props;
const {
selected
} = useSelectItemContext();
const indicatorRef = React.useRef(null);
const mergedRef = useForkRef(forwardedRef, indicatorRef);
const {
mounted,
transitionStatus,
setMounted
} = useTransitionStatus(selected);
const getItemProps = React.useCallback((externalProps = {}) => mergeReactProps(externalProps, {
'aria-hidden': true,
children: '✔️'
}), []);
const state = React.useMemo(() => ({
selected,
transitionStatus
}), [selected, transitionStatus]);
const {
renderElement
} = useComponentRenderer({
propGetter: getItemProps,
render: render ?? 'span',
ref: mergedRef,
className,
state,
extraProps: {
hidden: !mounted,
...otherProps
}
});
useAfterExitAnimation({
open: selected,
animatedElementRef: indicatorRef,
onFinished() {
setMounted(false);
}
});
const shouldRender = keepMounted || selected;
if (!shouldRender) {
return null;
}
return renderElement();
});
process.env.NODE_ENV !== "production" ? SelectItemIndicator.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 item is not selected.
* @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 { SelectItemIndicator };