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;KAAM;AAC/C;AAEA,SAAS,qCAAe,KAAK,EAAE,YAAY,EAAE,QAAQ;IACnD,OAAO,CAAC;QACN,IAAI,OAAO,UAAU,YAAY,OAAO,iBAAiB,UACvD,WAAW,QAAQ,CAAC,EAAE;aAEtB,WAAW;IAEf;AACF","sources":["packages/react-stately/src/slider/useSliderState.ts"],"sourcesContent":["/*\n * Copyright 2020 Adobe. All rights reserved.\n * This file is licensed to you under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License. You may obtain a copy\n * of the License at http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software distributed under\n * the License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\n * OF ANY KIND, either express or implied. See the License for the specific language\n * governing permissions and limitations under the License.\n */\n\nimport {clamp, snapValueToStep} from '../utils/number';\n\nimport {LabelableProps, Orientation, RangeInputBase, ValueBase} from '@react-types/shared';\nimport {useCallback, useMemo, useRef, useState} from 'react';\nimport {useControlledState} from '../utils/useControlledState';\n\nexport interface SliderProps<T = number | number[]>\n extends RangeInputBase<number>, ValueBase<T>, LabelableProps {\n /**\n * The orientation of the Slider.\n *\n * @default 'horizontal'\n */\n orientation?: Orientation;\n /** Whether the whole Slider is disabled. */\n isDisabled?: boolean;\n /** Fired when the slider stops moving, due to being let go. */\n onChangeEnd?: (value: T) => void;\n // These are duplicated from ValueBase to define defaults for the docs.\n /**\n * The slider's minimum value.\n *\n * @default 0\n */\n minValue?: number;\n /**\n * The slider's maximum value.\n *\n * @default 100\n */\n maxValue?: number;\n /**\n * The slider's step value.\n *\n * @default 1\n */\n step?: number;\n}\n\nexport interface SliderState {\n /**\n * Values managed by the slider by thumb index.\n */\n readonly values: number[];\n /**\n * The default values for each thumb.\n */\n readonly defaultValues: number[];\n /**\n * Get the value for the specified thumb.\n *\n * @param index\n */\n getThumbValue(index: number): number;\n\n /**\n * Sets the value for the specified thumb.\n * The actual value set will be clamped and rounded according to min/max/step.\n *\n * @param index\n * @param value\n */\n setThumbValue(index: number, value: number): void;\n\n /**\n * Sets value for the specified thumb by percent offset (between 0 and 1).\n *\n * @param index\n * @param percent\n */\n setThumbPercent(index: number, percent: number): void;\n\n /**\n * Whether the specific thumb is being dragged.\n *\n * @param index\n */\n isThumbDragging(index: number): boolean;\n /**\n * Set is dragging on the specified thumb.\n *\n * @param index\n * @param dragging\n */\n setThumbDragging(index: number, dragging: boolean): void;\n\n /**\n * Currently-focused thumb index.\n */\n readonly focusedThumb: number | undefined;\n /**\n * Set focused true on specified thumb. This will remove focus from\n * any thumb that had it before.\n *\n * @param index\n */\n setFocusedThumb(index: number | undefined): void;\n\n /**\n * Returns the specified thumb's value as a percentage from 0 to 1.\n *\n * @param index\n */\n getThumbPercent(index: number): number;\n\n /**\n * Returns the value as a percent between the min and max of the slider.\n *\n * @param index\n */\n getValuePercent(value: number): number;\n\n /**\n * Returns the string label for the specified thumb's value, per props.formatOptions.\n *\n * @param index\n */\n getThumbValueLabel(index: number): string;\n\n /**\n * Returns the string label for the value, per props.formatOptions.\n *\n * @param value\n */\n getFormattedValue(value?: number | number[]): string;\n\n /**\n * Returns the min allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMinValue(index: number): number;\n\n /**\n * Returns the max allowed value for the specified thumb.\n *\n * @param index\n */\n getThumbMaxValue(index: number): number;\n\n /**\n * Converts a percent along track (between 0 and 1) to the corresponding value.\n *\n * @param percent\n */\n getPercentValue(percent: number): number;\n\n /**\n * Returns if the specified thumb is editable.\n *\n * @param index\n */\n isThumbEditable(index: number): boolean;\n\n /**\n * Set the specified thumb's editable state.\n *\n * @param index\n * @param editable\n */\n setThumbEditable(index: number, editable: boolean): void;\n\n /**\n * Increments the value of the thumb by the step or page amount.\n */\n incrementThumb(index: number, stepSize?: number): void;\n /**\n * Decrements the value of the thumb by the step or page amount.\n */\n decrementThumb(index: number, stepSize?: number): void;\n\n /**\n * The step amount for the slider.\n */\n readonly step: number;\n\n /**\n * The page size for the slider, used to do a bigger step.\n */\n readonly pageSize: number;\n\n /** The orientation of the slider. */\n readonly orientation: Orientation;\n\n /** Whether the slider is disabled. */\n readonly isDisabled: boolean;\n}\n\nconst DEFAULT_MIN_VALUE = 0;\nconst DEFAULT_MAX_VALUE = 100;\nconst DEFAULT_STEP_VALUE = 1;\n\nexport interface SliderStateOptions<T> extends SliderProps<T> {\n numberFormatter: Intl.NumberFormat;\n}\n\n/**\n * Provides state management for a slider component. Stores values for all thumbs,\n * formats values for localization, and provides methods to update the position\n * of any thumbs.\n *\n * @param props\n */\nexport function useSliderState<T extends number | number[]>(\n props: SliderStateOptions<T>\n): SliderState {\n const {\n isDisabled = false,\n minValue = DEFAULT_MIN_VALUE,\n maxValue = DEFAULT_MAX_VALUE,\n numberFormatter: formatter,\n step = DEFAULT_STEP_VALUE,\n orientation = 'horizontal'\n } = props;\n\n // Page step should be at least equal to step and always a multiple of the step.\n let pageSize = useMemo(() => {\n let calcPageSize = (maxValue - minValue) / 10;\n calcPageSize = snapValueToStep(calcPageSize, 0, calcPageSize + step, step);\n return Math.max(calcPageSize, step);\n }, [step, maxValue, minValue]);\n\n let restrictValues = useCallback(\n (values: number[] | undefined) =>\n values?.map((val, idx) => {\n let min = idx === 0 ? minValue : values[idx - 1];\n let max = idx === values.length - 1 ? maxValue : values[idx + 1];\n return snapValueToStep(val, min, max, step);\n }),\n [minValue, maxValue, step]\n );\n\n let value = useMemo(\n () => restrictValues(convertValue(props.value)),\n [props.value, restrictValues]\n );\n let defaultValue = useMemo(\n () => restrictValues(convertValue(props.defaultValue) ?? [minValue])!,\n [props.defaultValue, minValue, restrictValues]\n );\n let onChange = createOnChange(props.value, props.defaultValue, props.onChange);\n let onChangeEnd = createOnChange(props.value, props.defaultValue, props.onChangeEnd);\n\n const [values, setValuesState] = useControlledState<number[]>(value, defaultValue, onChange);\n let [initialValues] = useState(values);\n const [isDraggings, setDraggingsState] = useState<boolean[]>(\n new Array(values.length).fill(false)\n );\n const isEditablesRef = useRef<boolean[]>(new Array(values.length).fill(true));\n const [focusedIndex, setFocusedIndex] = useState<number | undefined>(undefined);\n\n const valuesRef = useRef<number[]>(values);\n const isDraggingsRef = useRef<boolean[]>(isDraggings);\n\n let setValues = (values: number[]) => {\n valuesRef.current = values;\n setValuesState(values);\n };\n\n let setDraggings = (draggings: boolean[]) => {\n isDraggingsRef.current = draggings;\n setDraggingsState(draggings);\n };\n\n function getValuePercent(value: number) {\n return (value - minValue) / (maxValue - minValue);\n }\n\n function getThumbMinValue(index: number) {\n return index === 0 ? minValue : values[index - 1];\n }\n function getThumbMaxValue(index: number) {\n return index === values.length - 1 ? maxValue : values[index + 1];\n }\n\n function isThumbEditable(index: number) {\n return isEditablesRef.current[index];\n }\n\n function setThumbEditable(index: number, editable: boolean) {\n isEditablesRef.current[index] = editable;\n }\n\n function updateValue(index: number, value: number) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n const thisMin = getThumbMinValue(index);\n const thisMax = getThumbMaxValue(index);\n\n // Round value to multiple of step, clamp value between min and max\n value = snapValueToStep(value, thisMin, thisMax, step);\n let newValues = replaceIndex(valuesRef.current, index, value);\n setValues(newValues);\n }\n\n function updateDragging(index: number, dragging: boolean) {\n if (isDisabled || !isThumbEditable(index)) {\n return;\n }\n if (dragging) {\n valuesRef.current = values;\n }\n\n const wasDragging = isDraggingsRef.current[index];\n isDraggingsRef.current = replaceIndex(isDraggingsRef.current, index, dragging);\n setDraggings(isDraggingsRef.current);\n\n // Call onChangeEnd if no handles are dragging.\n if (onChangeEnd && wasDragging && !isDraggingsRef.current.some(Boolean)) {\n onChangeEnd(valuesRef.current);\n }\n }\n\n let listFormatRef = useRef<Intl.ListFormat | null>(null);\n\n function getFormattedValue(valueParam: number | number[] = values) {\n if (typeof valueParam === 'number') {\n valueParam = [valueParam];\n }\n\n switch (valueParam.length) {\n case 0:\n return '';\n case 1:\n return formatter.format(valueParam[0]);\n case 2:\n return formatter.formatRange(valueParam[0], valueParam[1]);\n default: {\n let formatted = valueParam.map(value => formatter.format(value));\n let listFormat = listFormatRef.current;\n if (\n !listFormat ||\n listFormat.resolvedOptions().locale !== formatter.resolvedOptions().locale\n ) {\n listFormat = listFormatRef.current = new Intl.ListFormat(\n formatter.resolvedOptions().locale,\n {type: 'unit'}\n );\n }\n return listFormat.format(formatted);\n }\n }\n }\n\n function setThumbPercent(index: number, percent: number) {\n updateValue(index, getPercentValue(percent));\n }\n\n function getRoundedValue(value: number) {\n return Math.round((value - minValue) / step) * step + minValue;\n }\n\n function getPercentValue(percent: number) {\n const val = percent * (maxValue - minValue) + minValue;\n return clamp(getRoundedValue(val), minValue, maxValue);\n }\n\n function incrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] + s, minValue, maxValue, step));\n }\n\n function decrementThumb(index: number, stepSize: number = 1) {\n let s = Math.max(stepSize, step);\n updateValue(index, snapValueToStep(values[index] - s, minValue, maxValue, step));\n }\n\n return {\n values: values,\n defaultValues: props.defaultValue !== undefined ? defaultValue : initialValues,\n getThumbValue: (index: number) => values[index],\n setThumbValue: updateValue,\n setThumbPercent,\n isThumbDragging: (index: number) => isDraggings[index],\n setThumbDragging: updateDragging,\n focusedThumb: focusedIndex,\n setFocusedThumb: setFocusedIndex,\n getThumbPercent: (index: number) => getValuePercent(values[index]),\n getValuePercent,\n getThumbValueLabel: (index: number) => getFormattedValue(values[index]),\n getFormattedValue,\n getThumbMinValue,\n getThumbMaxValue,\n getPercentValue,\n isThumbEditable,\n setThumbEditable,\n incrementThumb,\n decrementThumb,\n step,\n pageSize,\n orientation,\n isDisabled\n };\n}\n\nfunction replaceIndex<T>(array: T[], index: number, value: T) {\n if (array[index] === value) {\n return array;\n }\n\n return [...array.slice(0, index), value, ...array.slice(index + 1)];\n}\n\nfunction convertValue(value?: number | number[]) {\n if (value == null) {\n return undefined;\n }\n\n return Array.isArray(value) ? value : [value];\n}\n\nfunction createOnChange(value, defaultValue, onChange) {\n return (newValue: number[]) => {\n if (typeof value === 'number' || typeof defaultValue === 'number') {\n onChange?.(newValue[0]);\n } else {\n onChange?.(newValue);\n }\n };\n}\n"],"names":[],"version":3,"file":"useSliderState.cjs.map"}