# All files / src/Slider Slider.tsx

93.33% Statements 56/60
87.5% Branches 21/24
75% Functions 6/8
92.3% Lines 48/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    1663x
    3x

    3x
    26x
    26x
    26x

    26x
    8x

    18x

    3x
    20x
    20x
    20x

    20x

    3x
    27x
    27x
    27x
    27x

    27x
    14x
    13x
    7x
    6x
    6x

    3x
    12x
    12x
    12x

    12x
    12x

    3x

    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x
    9x

    9x

    9x
    9x

    9x

    9x

    17x

     import React from 'react'
    import {
      StyledContainer,
      StyledIncrementContainer,
      StyledIncrementOverlay,
      StyledLabel,
      StyledSlider,
    } from './Slider.styles'
    import { SliderProps } from './Slider.types'

    export const getIncrements = ({
      min,
      max,
      step,
    }: {
      min: number
      max: number
      step?: number
    }): number => {
      if (!step) {
        return 0
      }
      return (max - min) / step
    }

    export const getPercentage = ({
      max,
      min,
      value,
    }: {
      max: number
      min: number
      value: number
    }): number => {
      return ((value - min) / (max - min)) * 100
    }

    export const getLabel = ({
      max,
      min,
      step,
      index,
    }: {
      max: number
      min: number
      step: number
      index: number
    }): number => {
      if (min === 0) {
        return index * step
      } else if (min >= 0) {
        return index * step + min
      } else if (min <= 0) {
        return (index - getIncrements({ max, min, step })) * step + max
      } else E{
        return 0
      }
    }

    export const getDefaultValue = ({
      min,
      max,
      step,
    }: {
      min: number
      max: number
      step?: number
    }): number => {
      // See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/range#value
      const defaultVal = max < min ? min : min + (max - min) / 2
      return step ? Math.round(defaultVal / step) * step : defaultVal
    }

    /**

     A slider is a bar with selectable/grabbable handle. It allows users to click
     and drag horizontally to a desired value. It is used when users need to select
     a value from a predefined range of values. Sliders should always have a
     corresponding value or increment displayed within proximity of the component.

     @since 10.19.0

     @see [Storybook](https://procore.github.io/core/core-react/latest/?path=/story/demos-slider--demo)

     @see [Design Guidelines](https://design.procore.com/slider)

     */
    export const Slider = React.forwardRef<HTMLInputElement, SliderProps>(
      function Slider(
        {
          className,
          disabled,
          onChange,
          max = 100,
          min = 0,
          showLabel,
          step,
          style,
          value: value_,
          ...props
        },
        ref
      ) {
        const [isDragging, setIsDragging] = React.useState<boolean>(false)

        const value = value_ ?? getDefaultValue({ max, min, step })
        const increments = getIncrements({ max, min, step })

        // check if the "step" value does not cleanly
        //  divide into max - min
        Iif (increments % 1 !== 0) {
          console.warn(
            '@procore/core-react: SLIDER COMPONENT WARNING: Your step value does not cleanly divide into max - min. This will cause your Slider to display incorrectly.'
          )
        }

        return (
          <StyledContainer
            className={className}
            style={style}
            showLabel={showLabel}
          >
            <StyledSlider
              {...props}
              disabled={disabled}
              onChange={onChange}
              max={max}
              min={min}
              $percent={getPercentage({ max, min, value })}
              ref={ref}
              step={step}
              type="range"
              value={value}
              onMouseDown={() => setIsDragging(true)}
              onMouseUp={() => setIsDragging(false)}
              isDragging={isDragging}
            />
            {step && (
              <StyledIncrementContainer>
                {Array(Math.floor(increments) + 1)
                  .fill(null)
                  .map((_, index) => {
                    return (
                      <StyledIncrementOverlay
                        key={`core-slider-increment-overlay-${index}-${min}-${max}-${step}`}
                      >
                        {showLabel && (
                          <StyledLabel intent="small">
                            {getLabel({
                              min,
                              max,
                              step,
                              index,
                            })}
                          </StyledLabel>
                        )}
                      </StyledIncrementOverlay>
                    )
                  })}
              </StyledIncrementContainer>
            )}
          </StyledContainer>
        )
      }
    )