|
| 1 | +import { useFocusRing } from "@react-aria/focus"; |
| 2 | +import { useSliderThumb } from "@react-aria/slider"; |
| 3 | +import { mergeProps } from "@react-aria/utils"; |
| 4 | +import { VisuallyHidden } from "@react-aria/visually-hidden"; |
| 5 | +import { SliderState } from "@react-stately/slider"; |
| 6 | +import { OutputHTMLAttributes, RefObject, useRef } from "react"; |
| 7 | +import { Box, Column, Columns, Stack } from "../internal"; |
| 8 | +import { Label } from "../Typography/Label/Label"; |
| 9 | +import { unsafeLocalizedString } from "../util/LocalizedString"; |
| 10 | +import { SliderConfig } from "./Config"; |
| 11 | +import { slider, thumbRecipe, trackActive, trackContainer, trackInactive } from "./Slider.css"; |
| 12 | + |
| 13 | +type Props = { |
| 14 | + type: "single" | "double"; |
| 15 | + disabled?: boolean; |
| 16 | + trackRef: RefObject<HTMLDivElement>; |
| 17 | + state: SliderState; |
| 18 | + groupProps: React.HTMLAttributes<HTMLElement>; |
| 19 | + trackProps: React.HTMLAttributes<HTMLElement>; |
| 20 | + outputProps: OutputHTMLAttributes<HTMLOutputElement>; |
| 21 | + numberFormatter: Intl.NumberFormat; |
| 22 | +}; |
| 23 | + |
| 24 | +export function createSlider(config: SliderConfig) { |
| 25 | + return function Slider(props: Props) { |
| 26 | + return ( |
| 27 | + <Box className={slider} {...props.groupProps} color={undefined}> |
| 28 | + <Columns space={24} alignY="center"> |
| 29 | + <Column width="content"> |
| 30 | + <Label size="large" color={props.disabled ? "disabled" : "secondary"}> |
| 31 | + {unsafeLocalizedString(props.numberFormatter.format(props.state.getThumbMinValue(0)))} |
| 32 | + </Label> |
| 33 | + </Column> |
| 34 | + <Box |
| 35 | + className={trackContainer} |
| 36 | + {...props.trackProps} |
| 37 | + ref={props.trackRef} |
| 38 | + color={undefined} |
| 39 | + > |
| 40 | + <Box |
| 41 | + className={trackInactive} |
| 42 | + disabled={props.disabled} |
| 43 | + borderRadius={config.trailRadius} |
| 44 | + /> |
| 45 | + <Box |
| 46 | + className={trackActive} |
| 47 | + color={config.trailColor} |
| 48 | + background="currentColor" |
| 49 | + disabled={props.disabled} |
| 50 | + borderRadius={config.trailRadius} |
| 51 | + style={{ |
| 52 | + left: props.type === "single" ? 0 : `${props.state.getThumbPercent(0) * 100}%`, |
| 53 | + width: |
| 54 | + props.type === "single" |
| 55 | + ? `${props.state.getThumbPercent(0) * 100}%` |
| 56 | + : `${(props.state.getThumbPercent(1) - props.state.getThumbPercent(0)) * 100}%`, |
| 57 | + }} |
| 58 | + /> |
| 59 | + <Thumb |
| 60 | + index={0} |
| 61 | + state={props.state} |
| 62 | + trackRef={props.trackRef} |
| 63 | + outputProps={props.outputProps} |
| 64 | + disabled={props.disabled} |
| 65 | + /> |
| 66 | + {props.type === "double" && ( |
| 67 | + <Thumb |
| 68 | + index={1} |
| 69 | + state={props.state} |
| 70 | + trackRef={props.trackRef} |
| 71 | + outputProps={props.outputProps} |
| 72 | + disabled={props.disabled} |
| 73 | + /> |
| 74 | + )} |
| 75 | + </Box> |
| 76 | + <Column width="content"> |
| 77 | + <Label size="large" color={props.disabled ? "disabled" : "secondary"}> |
| 78 | + {unsafeLocalizedString( |
| 79 | + props.numberFormatter.format( |
| 80 | + props.state.getThumbMaxValue(props.type === "single" ? 0 : 1) |
| 81 | + ) |
| 82 | + )} |
| 83 | + </Label> |
| 84 | + </Column> |
| 85 | + </Columns> |
| 86 | + </Box> |
| 87 | + ); |
| 88 | + }; |
| 89 | + |
| 90 | + type ThumbProps = { |
| 91 | + trackRef: RefObject<HTMLDivElement>; |
| 92 | + state: SliderState; |
| 93 | + index: number; |
| 94 | + outputProps: OutputHTMLAttributes<HTMLOutputElement>; |
| 95 | + disabled?: boolean; |
| 96 | + }; |
| 97 | + |
| 98 | + function Thumb(props: ThumbProps) { |
| 99 | + const { state, trackRef, index } = props; |
| 100 | + const inputRef = useRef<HTMLInputElement>(null); |
| 101 | + const { thumbProps, inputProps } = useSliderThumb( |
| 102 | + { index, trackRef, inputRef, isDisabled: props.disabled }, |
| 103 | + state |
| 104 | + ); |
| 105 | + const { focusProps, isFocusVisible } = useFocusRing(); |
| 106 | + |
| 107 | + return ( |
| 108 | + <Box |
| 109 | + position="absolute" |
| 110 | + style={{ |
| 111 | + transform: "translateX(-50%)", |
| 112 | + left: `${state.getThumbPercent(index) * 100}%`, |
| 113 | + }} |
| 114 | + > |
| 115 | + <Stack space={8} align="center"> |
| 116 | + <Box |
| 117 | + className={thumbRecipe({ |
| 118 | + isFocused: isFocusVisible, |
| 119 | + })} |
| 120 | + {...thumbProps} |
| 121 | + color={undefined} |
| 122 | + disabled={props.disabled} |
| 123 | + borderRadius={config.thumbRadius} |
| 124 | + > |
| 125 | + <VisuallyHidden> |
| 126 | + <input ref={inputRef} {...mergeProps(inputProps, focusProps)} /> |
| 127 | + </VisuallyHidden> |
| 128 | + </Box> |
| 129 | + <Box as="output" {...props.outputProps} color={undefined}> |
| 130 | + <Label size="large" color={props.disabled ? "disabled" : undefined}> |
| 131 | + {unsafeLocalizedString(state.getThumbValueLabel(props.index))} |
| 132 | + </Label> |
| 133 | + </Box> |
| 134 | + </Stack> |
| 135 | + </Box> |
| 136 | + ); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +export type { Props as SliderProps }; |
0 commit comments