UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

156 lines (153 loc) 5.31 kB
import React, { useState, useRef, useEffect } from 'react'; import cx from 'clsx'; import { useReducedMotion, useUncontrolled, useId } from '@mantine/hooks'; import useStyles, { WRAPPER_PADDING } from './SegmentedControl.styles.js'; import { useMantineTheme } from '../../theme/use-mantine-theme/use-mantine-theme.js'; import { mergeStyles } from '../../theme/utils/merge-styles/merge-styles.js'; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __objRest = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop]; } return target; }; function SegmentedControl(_a) { var _b = _a, { className, style, themeOverride, data, name, value, onChange, color, fullWidth, radius = "sm", size = "sm", transitionDuration = 200, transitionTimingFunction, classNames, styles, defaultValue } = _b, others = __objRest(_b, [ "className", "style", "themeOverride", "data", "name", "value", "onChange", "color", "fullWidth", "radius", "size", "transitionDuration", "transitionTimingFunction", "classNames", "styles", "defaultValue" ]); const reduceMotion = useReducedMotion(); const theme = useMantineTheme(themeOverride); const [shouldAnimate, setShouldAnimate] = useState(false); const [_value, handleValueChange] = useUncontrolled({ value, defaultValue, finalValue: Array.isArray(data) ? data[0].value : null, onChange, rule: (val) => !!val }); const classes = useStyles({ theme, size, fullWidth, color, radius, reduceMotion: reduceMotion || !shouldAnimate, transitionDuration, transitionTimingFunction }, classNames, "segmented-control"); const _styles = mergeStyles(classes, styles); const [activePosition, setActivePosition] = useState({ width: 0, translate: 0 }); const uuid = useId(name); const refs = useRef({}); const wrapperRef = useRef(null); useEffect(() => { const observer = new ResizeObserver(() => { if (_value in refs.current && wrapperRef.current) { const element = refs.current[_value]; const rect = element.getBoundingClientRect(); setActivePosition({ width: rect.width, translate: rect.x - wrapperRef.current.getBoundingClientRect().x - WRAPPER_PADDING }); if (!shouldAnimate) { setTimeout(() => { setShouldAnimate(true); }, 4); } } }); observer.observe(wrapperRef.current); return () => observer.disconnect(); }, [_value]); const controls = data.map((item) => /* @__PURE__ */ React.createElement("div", { className: cx(classes.control, { [classes.controlActive]: _value === item.value }), style: __spreadValues(__spreadValues({}, _styles.control), _value === item.value ? _styles.controlActive : null), key: item.value }, /* @__PURE__ */ React.createElement("input", { className: classes.input, style: _styles.input, type: "radio", name: uuid, value: item.value, id: `${uuid}-${item.value}`, checked: _value === item.value, onChange: () => handleValueChange(item.value) }), /* @__PURE__ */ React.createElement("label", { className: cx(classes.label, { [classes.labelActive]: _value === item.value }), style: __spreadValues(__spreadValues({}, _styles.label), _value === item.value ? _styles.labelActive : null), htmlFor: `${uuid}-${item.value}`, ref: (node) => { refs.current[item.value] = node; } }, item.label))); return /* @__PURE__ */ React.createElement("div", __spreadValues({ className: cx(classes.root, className), ref: wrapperRef, style: __spreadValues(__spreadValues({}, style), _styles.root) }, others), !!_value && /* @__PURE__ */ React.createElement("span", { className: classes.active, style: __spreadProps(__spreadValues({}, _styles.active), { width: activePosition.width, transform: `translateX(${activePosition.translate}px)` }) }), controls); } SegmentedControl.displayName = "@mantine/core/SegmentedControl"; export { SegmentedControl }; //# sourceMappingURL=SegmentedControl.js.map