{"version":3,"file":"Slider.mjs","names":[],"sources":["../../../src/slider/Slider.tsx"],"sourcesContent":["/*!\n *\n *  Copyright (c) \"Neo4j\"\n *  Neo4j Sweden AB [http://neo4j.com]\n *\n *  This file is part of Neo4j.\n *\n *  Neo4j is free software: you can redistribute it and/or modify\n *  it under the terms of the GNU General Public License as published by\n *  the Free Software Foundation, either version 3 of the License, or\n *  (at your option) any later version.\n *\n *  This program is distributed in the hope that it will be useful,\n *  but WITHOUT ANY WARRANTY; without even the implied warranty of\n *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n *  GNU General Public License for more details.\n *\n *  You should have received a copy of the GNU General Public License\n *  along with this program.  If not, see <http://www.gnu.org/licenses/>.\n */\nimport classNames from 'classnames';\nimport React, { useCallback, useEffect } from 'react';\nimport {\n  mergeProps,\n  useFocusRing,\n  useSlider,\n  useSliderThumb,\n  VisuallyHidden,\n} from 'react-aria';\nimport { type SliderState, useSliderState } from 'react-stately';\n\nimport {\n  type HtmlAttributes,\n  type PolymorphicCommonProps,\n} from '../_common/types';\nimport { Tooltip } from '../tooltip';\n\ntype SliderPropsBase = {\n  /** Input props of the underlying input element */\n  inputProps?: HtmlAttributes<'input'>;\n  /** Determines the step size that will be added or removed from the current value when moving the slider button */\n  step?: number;\n  /** Whether the slider is disabled */\n  isDisabled?: boolean;\n  /** The minimum value possible to choose */\n  minValue?: number;\n  /** The maximum value possible to choose */\n  maxValue?: number;\n  /** Whether to display a tooltip with the current value when the slider is being dragged */\n  shouldShowValues?: boolean;\n  /** Whether to display visual indicators for each step in the slider */\n  shouldShowSteps?: boolean;\n};\n\ntype SingleSlider = SliderPropsBase & {\n  /** Type of the slider */\n  type?: 'single';\n  /** Current value of the single slider. **Only required when the type is 'single'**. */\n  value: number;\n  /** Callback function triggered when the slider value(s) changes. The function parameter varies depending on the slider type, for single slider, it is a number and for range slider, it is an array of two numbers. */\n  onChange: (value: number) => void;\n};\n\ntype RangeSlider = SliderPropsBase & {\n  /** Type of the slider */\n  type: 'range';\n  /** Current values of the range slider as an array of two numbers `[min, max]`. **Only required when the type is 'range'**. */\n  values: [number, number];\n  /** Callback function triggered when the slider value(s) changes. The function parameter varies depending on the slider type, for single slider, it is a number and for range slider, it is an array of two numbers. */\n  onChange: (value: [number, number]) => void;\n};\n\n/**\n * @remarks\n * The `type` prop is used to determine the type of the slider. It can be `single` or `range`. The `value` prop is required when the `type` is `single` and `values` is required when the `type` is `range`. It also affects the `onChange` prop type.\n */\ntype SliderProps = SingleSlider | RangeSlider;\n\nexport const Slider = <T extends React.ElementType = 'div'>({\n  as,\n  inputProps = {},\n  className,\n  style,\n  minValue = 0,\n  maxValue = 100,\n  isDisabled = false,\n  shouldShowSteps = false,\n  shouldShowValues = false,\n  step = 1,\n  type = 'single',\n  onChange,\n  htmlAttributes,\n  ref,\n  ...restProps\n}: PolymorphicCommonProps<T, SliderProps>) => {\n  const trackRef = React.useRef<HTMLDivElement | null>(null);\n\n  const castedValue =\n    type === 'range'\n      ? (restProps as RangeSlider).values\n      : (restProps as SingleSlider).value;\n\n  const castedOnChange =\n    type === 'range'\n      ? (onChange as RangeSlider['onChange'])\n      : (onChange as SingleSlider['onChange']);\n\n  const state = useSliderState<typeof castedValue>({\n    defaultValue: castedValue,\n    isDisabled,\n    maxValue,\n    minValue,\n    numberFormatter: new Intl.NumberFormat('en-US'),\n    // oxlint-disable-next-line no-explicit-any\n    onChange: castedOnChange as any,\n    step,\n  });\n\n  const { setThumbValue } = state;\n\n  const { groupProps, trackProps } = useSlider(\n    { 'aria-label': 'slider' },\n    state,\n    trackRef,\n  );\n\n  const updatesSliderValue = useCallback(\n    (newValue: number | [number, number]) => {\n      if (type === 'single' && typeof newValue === 'number') {\n        setThumbValue(0, newValue);\n      } else if (type === 'range' && Array.isArray(newValue)) {\n        setThumbValue(0, newValue[0]);\n        setThumbValue(1, newValue[1]);\n      }\n    },\n    [type, setThumbValue],\n  );\n\n  useEffect(() => {\n    updatesSliderValue(castedValue);\n    // Needed to make the slider work with controlled values\n    // eslint-disable-next-line react-hooks/exhaustive-deps\n  }, [castedValue]);\n\n  const Component: React.ElementType = as ?? 'div';\n\n  return (\n    <Component\n      className={classNames('ndl-slider', className)}\n      style={style}\n      role=\"presentation\"\n      ref={ref}\n      {...groupProps}\n      {...restProps}\n      {...htmlAttributes}\n    >\n      {/* The track element holds the visible track line, the filled track line and the thumb. */}\n      <div\n        {...trackProps}\n        ref={trackRef as React.RefObject<HTMLDivElement>}\n        className={classNames('ndl-track', {\n          'ndl-is-disabled': state.isDisabled,\n        })}\n      >\n        {/* Circular marks, acting as indicators for the steps */}\n        {shouldShowSteps && (\n          <TrackMarks state={state} min={minValue} max={maxValue} />\n        )}\n        <FilledTrack state={state} />\n        <Thumb\n          index={0}\n          state={state}\n          trackRef={trackRef as React.RefObject<HTMLDivElement>}\n          shouldShowValues={shouldShowValues}\n          inputForwardedProps={inputProps}\n        />\n        {type === 'range' && (\n          <Thumb\n            index={1}\n            state={state}\n            trackRef={trackRef as React.RefObject<HTMLDivElement>}\n            shouldShowValues={shouldShowValues}\n            inputForwardedProps={inputProps}\n          />\n        )}\n      </div>\n    </Component>\n  );\n};\n\nSlider.displayName = 'Slider';\n\nfunction FilledTrack({ state }: { state: SliderState }) {\n  let width: number;\n  let left: number;\n  if (state.values.length === 1) {\n    width = state.getValuePercent(state.values[0]) * 100;\n    left = 0;\n  } else {\n    width =\n      state.getValuePercent(state.values[1]) * 100 -\n      state.getValuePercent(state.values[0]) * 100;\n    left = state.getValuePercent(state.values[0]) * 100;\n  }\n\n  return (\n    <div\n      className=\"ndl-filled-track\"\n      style={{\n        left: `${left}%`,\n        width: `${width}%`,\n      }}\n    />\n  );\n}\n\nfunction TrackMarks({\n  state,\n  max,\n  min,\n}: {\n  state: SliderState;\n  max: number;\n  min: number;\n}) {\n  const markNo = Math.round((max - min) / state.step);\n\n  return (\n    <div role=\"presentation\" className=\"ndl-track-marks\">\n      {Array.from({ length: markNo - 1 }).map((_unused, index) => {\n        const percent = (index / markNo) * 100;\n\n        let isFilled;\n        if (state.values.length === 1) {\n          isFilled = state.getValuePercent(state.values[0]) * 100 > percent;\n        } else {\n          isFilled =\n            state.getValuePercent(state.values[0]) * 100 < percent &&\n            state.getValuePercent(state.values[1]) * 100 > percent;\n        }\n\n        return (\n          <div\n            key={`track-mark-${index}`}\n            className={classNames('ndl-track-mark', {\n              'ndl-on-active-track': isFilled,\n            })}\n            style={{\n              left: `${((index + 1) / markNo) * 100}%`,\n            }}\n          />\n        );\n      })}\n    </div>\n  );\n}\n\nfunction Thumb(props: {\n  index: number;\n  state: SliderState;\n  shouldShowValues?: boolean;\n  trackRef: React.RefObject<HTMLDivElement>;\n  inputForwardedProps: HtmlAttributes<'input'>;\n}) {\n  const { state, trackRef, index, inputForwardedProps } = props;\n  const inputRef = React.useRef(null);\n  const { thumbProps, inputProps, isDragging } = useSliderThumb(\n    {\n      index,\n      inputRef,\n      name: 'slider-thumb',\n      trackRef,\n    },\n    state,\n  );\n\n  const { focusProps, isFocusVisible } = useFocusRing();\n\n  /**\n   * Solve issue with:\n   * Warning: Received NaN for the `value` attribute. If this is expected, cast the value to a string.\n   * Receiving NaN for other attributes also, like `max`. Waiting for a fix in react-aria.\n   * @see https://github.com/adobe/react-spectrum/issues/5859\n   * @see https://github.com/adobe/react-spectrum/pull/5861\n   */\n  const mergedProps = mergeProps(\n    inputProps,\n    focusProps,\n    // Omitting aria-valuetext double announcements, unless explicitly provided\n    { 'aria-valuetext': '' },\n    inputForwardedProps,\n  );\n  const value =\n    mergedProps.value !== undefined || mergedProps.value === 0\n      ? mergedProps.value\n      : '';\n\n  return (\n    <Tooltip\n      isDisabled={props.shouldShowValues !== true}\n      isOpen={isDragging || undefined}\n      placement=\"top\"\n      type=\"simple\"\n    >\n      <Tooltip.Trigger hasButtonWrapper>\n        <div\n          {...thumbProps}\n          className={classNames('ndl-thumb', {\n            'ndl-focus': isFocusVisible,\n            'ndl-is-dragging': isDragging,\n          })}\n        >\n          <VisuallyHidden>\n            <input ref={inputRef} {...mergedProps} />\n          </VisuallyHidden>\n        </div>\n      </Tooltip.Trigger>\n      <Tooltip.Content>{value}</Tooltip.Content>\n    </Tooltip>\n  );\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CA+EE;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;;AAdF,MAAa,UAA+C,SAgBd;CAhBc,IAAA,EAC1D,IACA,aAAa,CAAC,GACd,WACA,OACA,WAAW,GACX,WAAW,KACX,aAAa,OACb,kBAAkB,OAClB,mBAAmB,OACnB,OAAO,GACP,OAAO,UACP,UACA,gBACA,QAAA,MACG,YAAA,yBAAA,MAAA,SAAA;CAEH,MAAM,WAAW,MAAM,OAA8B,IAAI;CAEzD,MAAM,cACJ,SAAS,UACJ,UAA0B,SAC1B,UAA2B;CAElC,MAAM,iBACJ,SAAS,UACJ,WACA;CAEP,MAAM,QAAQ,eAAmC;EAC/C,cAAc;EACd;EACA;EACA;EACA,iBAAiB,IAAI,KAAK,aAAa,OAAO;EAE9C,UAAU;EACV;CACF,CAAC;CAED,MAAM,EAAE,kBAAkB;CAE1B,MAAM,EAAE,YAAY,eAAe,UACjC,EAAE,cAAc,SAAS,GACzB,OACA,QACF;CAEA,MAAM,qBAAqB,aACxB,aAAwC;EACvC,IAAI,SAAS,YAAY,OAAO,aAAa,UAC3C,cAAc,GAAG,QAAQ;OACpB,IAAI,SAAS,WAAW,MAAM,QAAQ,QAAQ,GAAG;GACtD,cAAc,GAAG,SAAS,EAAE;GAC5B,cAAc,GAAG,SAAS,EAAE;EAC9B;CACF,GACA,CAAC,MAAM,aAAa,CACtB;CAEA,gBAAgB;EACd,mBAAmB,WAAW;CAGhC,GAAG,CAAC,WAAW,CAAC;CAIhB,OACE,oBAHmC,OAAA,QAAA,OAAA,KAAA,IAAA,KAAM,OAAA,eAAA,eAAA,eAAA,eAAA;EAIvC,WAAW,WAAW,cAAc,SAAS;EACt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