@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.
219 lines (217 loc) • 7.49 kB
JavaScript
"use strict";
'use client';
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.SliderThumb = void 0;
var React = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _getStyleHookProps = require("../../utils/getStyleHookProps");
var _mergeReactProps = require("../../utils/mergeReactProps");
var _resolveClassName = require("../../utils/resolveClassName");
var _useForkRef = require("../../utils/useForkRef");
var _SliderRootContext = require("../root/SliderRootContext");
var _useSliderThumb = require("./useSliderThumb");
var _reactVersion = require("../../utils/reactVersion");
var _jsxRuntime = require("react/jsx-runtime");
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
function defaultRender(props, inputProps) {
const {
children,
...thumbProps
} = props;
return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
...thumbProps,
children: [children, /*#__PURE__*/(0, _jsxRuntime.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 = exports.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
} = (0, _SliderRootContext.useSliderRootContext)();
let renderPropRef = null;
if (typeof render !== 'function') {
renderPropRef = (0, _reactVersion.isReactVersionAtLeast)(19) ? render.props.ref : render.ref;
}
const mergedRef = (0, _useForkRef.useForkRef)(renderPropRef, forwardedRef);
const {
getRootProps,
getThumbInputProps,
disabled,
index
} = (0, _useSliderThumb.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(() => (0, _getStyleHookProps.getStyleHookProps)({
disabled,
dragging: index !== -1 && activeIndex === index
}), [activeIndex, disabled, index]);
const thumbProps = getRootProps({
...styleHooks,
...otherProps,
className: (0, _resolveClassName.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, {
...(0, _mergeReactProps.mergeReactProps)(otherRenderProps, {
...thumbProps,
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(React.Fragment, {
children: [typeof children === 'function' ? children() : children, /*#__PURE__*/(0, _jsxRuntime.jsx)("input", {
...inputProps
})]
})
}),
// @ts-ignore
ref: thumbProps.ref
});
});
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.default.string,
/**
* A string value that provides a user-friendly name for the current value of the slider.
*/
'aria-valuetext': _propTypes.default.string,
/**
* @ignore
*/
children: _propTypes.default.node,
/**
* CSS class applied to the element, or a function that
* returns a class based on the component’s state.
*/
className: _propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.string]),
/**
* @ignore
*/
disabled: _propTypes.default.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.default.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.default.func,
/**
* @ignore
*/
id: _propTypes.default.string,
/**
* @ignore
*/
inputId: _propTypes.default.string,
/**
* @ignore
*/
onBlur: _propTypes.default.func,
/**
* @ignore
*/
onFocus: _propTypes.default.func,
/**
* @ignore
*/
onKeyDown: _propTypes.default.func,
/**
* @ignore
*/
onPointerLeave: _propTypes.default.func,
/**
* @ignore
*/
onPointerOver: _propTypes.default.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.default /* @typescript-to-proptypes-ignore */.oneOfType([_propTypes.default.func, _propTypes.default.node])
} : void 0;