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.

212 lines (210 loc) 6.13 kB
'use client'; import * as React from 'react'; import PropTypes from 'prop-types'; import { getStyleHookProps } from '../../utils/getStyleHookProps.js'; import { mergeReactProps } from '../../utils/mergeReactProps.js'; import { resolveClassName } from '../../utils/resolveClassName.js'; import { useForkRef } from '../../utils/useForkRef.js'; import { useSliderRootContext } from '../root/SliderRootContext.js'; import { useSliderThumb } from './useSliderThumb.js'; import { isReactVersionAtLeast } from '../../utils/reactVersion.js'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; function defaultRender(props, inputProps) { const { children, ...thumbProps } = props; return /*#__PURE__*/_jsxs("div", { ...thumbProps, children: [children, /*#__PURE__*/_jsx("input", { ...inputProps })] }); } /** * The draggable part of the the slider at the tip of the indicator. * Renders a `<div>` element. * * Documentation: [Base UI Slider](https://base-ui.com/react/components/slider) */ const SliderThumb = /*#__PURE__*/React.forwardRef(function SliderThumb(props, forwardedRef) { const { render: renderProp, 'aria-label': ariaLabel, 'aria-valuetext': ariaValuetext, className, disabled: disabledProp = false, getAriaLabel, getAriaValueText, id, inputId, ...otherProps } = props; const render = renderProp ?? defaultRender; const { active: activeIndex, 'aria-labelledby': ariaLabelledby, changeValue, direction, disabled: contextDisabled, format, largeStep, max, min, minStepsBetweenValues, name, orientation, state, percentageValues, registerInputId, step, tabIndex, values } = useSliderRootContext(); let renderPropRef = null; if (typeof render !== 'function') { renderPropRef = isReactVersionAtLeast(19) ? render.props.ref : render.ref; } const mergedRef = useForkRef(renderPropRef, forwardedRef); const { getRootProps, getThumbInputProps, disabled, index } = useSliderThumb({ active: activeIndex, 'aria-label': ariaLabel, 'aria-labelledby': ariaLabelledby, 'aria-valuetext': ariaValuetext, changeValue, direction, disabled: disabledProp || contextDisabled, format, getAriaLabel, getAriaValueText, id, inputId, largeStep, max, min, minStepsBetweenValues, name, orientation, percentageValues, registerInputId, rootRef: mergedRef, step, tabIndex, values }); const styleHooks = React.useMemo(() => getStyleHookProps({ disabled, dragging: index !== -1 && activeIndex === index }), [activeIndex, disabled, index]); const thumbProps = getRootProps({ ...styleHooks, ...otherProps, className: resolveClassName(className, state) }); const inputProps = getThumbInputProps({ disabled }); if (typeof render === 'function') { return render(thumbProps, inputProps, state); } const { children: renderPropsChildren, ...otherRenderProps } = render.props; const children = thumbProps.children ?? renderPropsChildren; return /*#__PURE__*/React.cloneElement(render, { ...mergeReactProps(otherRenderProps, { ...thumbProps, children: /*#__PURE__*/_jsxs(React.Fragment, { children: [typeof children === 'function' ? children() : children, /*#__PURE__*/_jsx("input", { ...inputProps })] }) }), // @ts-ignore ref: thumbProps.ref }); }); export { SliderThumb }; process.env.NODE_ENV !== "production" ? SliderThumb.propTypes /* remove-proptypes */ = { // ┌────────────────────────────── Warning ──────────────────────────────┐ // │ These PropTypes are generated from the TypeScript type definitions. │ // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │ // └─────────────────────────────────────────────────────────────────────┘ /** * The label for the input element. */ 'aria-label': PropTypes.string, /** * A string value that provides a user-friendly name for the current value of the slider. */ 'aria-valuetext': PropTypes.string, /** * @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]), /** * @ignore */ disabled: PropTypes.bool, /** * Accepts a function which returns a string value that provides a user-friendly name for the input associated with the thumb * @param {number} index The index of the input * @returns {string} */ getAriaLabel: PropTypes.func, /** * Accepts a function which returns a string value that provides a user-friendly name for the current value of the slider. * This is important for screen reader users. * @param {string} formattedValue The thumb's formatted value. * @param {number} value The thumb's numerical value. * @param {number} index The thumb's index. * @returns {string} */ getAriaValueText: PropTypes.func, /** * @ignore */ id: PropTypes.string, /** * @ignore */ inputId: PropTypes.string, /** * @ignore */ onBlur: PropTypes.func, /** * @ignore */ onFocus: PropTypes.func, /** * @ignore */ onKeyDown: PropTypes.func, /** * @ignore */ onPointerLeave: PropTypes.func, /** * @ignore */ onPointerOver: PropTypes.func, /** * 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 /* @typescript-to-proptypes-ignore */.oneOfType([PropTypes.func, PropTypes.node]) } : void 0;