|
| 1 | +import type { JSX, ValidComponent } from "solid-js"; |
| 2 | +import { splitProps } from "solid-js"; |
| 3 | + |
| 4 | +import type { PolymorphicProps } from "@kobalte/core/polymorphic"; |
| 5 | +import * as SliderPrimitive from "@kobalte/core/slider"; |
| 6 | + |
| 7 | +import { Label } from "~/components/ui/label"; |
| 8 | +import { cn } from "~/lib/utils"; |
| 9 | + |
| 10 | +type SliderRootProps<T extends ValidComponent = "div"> = |
| 11 | + SliderPrimitive.SliderRootProps<T> & { |
| 12 | + class?: string | undefined; |
| 13 | + }; |
| 14 | + |
| 15 | +const Slider = <T extends ValidComponent = "div">( |
| 16 | + props: PolymorphicProps<T, SliderRootProps<T>>, |
| 17 | +) => { |
| 18 | + const [local, others] = splitProps(props as SliderRootProps, ["class"]); |
| 19 | + return ( |
| 20 | + <SliderPrimitive.Root |
| 21 | + class={cn( |
| 22 | + "relative flex w-full touch-none select-none flex-col items-center", |
| 23 | + local.class, |
| 24 | + )} |
| 25 | + {...others} |
| 26 | + /> |
| 27 | + ); |
| 28 | +}; |
| 29 | + |
| 30 | +type SliderTrackProps<T extends ValidComponent = "div"> = |
| 31 | + SliderPrimitive.SliderTrackProps<T> & { |
| 32 | + class?: string | undefined; |
| 33 | + }; |
| 34 | + |
| 35 | +const SliderTrack = <T extends ValidComponent = "div">( |
| 36 | + props: PolymorphicProps<T, SliderTrackProps<T>>, |
| 37 | +) => { |
| 38 | + const [local, others] = splitProps(props as SliderTrackProps, ["class"]); |
| 39 | + return ( |
| 40 | + <SliderPrimitive.Track |
| 41 | + class={cn( |
| 42 | + "relative h-2 w-full grow rounded-full bg-secondary", |
| 43 | + local.class, |
| 44 | + )} |
| 45 | + {...others} |
| 46 | + /> |
| 47 | + ); |
| 48 | +}; |
| 49 | + |
| 50 | +type SliderFillProps<T extends ValidComponent = "div"> = |
| 51 | + SliderPrimitive.SliderFillProps<T> & { |
| 52 | + class?: string | undefined; |
| 53 | + }; |
| 54 | + |
| 55 | +const SliderFill = <T extends ValidComponent = "div">( |
| 56 | + props: PolymorphicProps<T, SliderFillProps<T>>, |
| 57 | +) => { |
| 58 | + const [local, others] = splitProps(props as SliderFillProps, ["class"]); |
| 59 | + return ( |
| 60 | + <SliderPrimitive.Fill |
| 61 | + class={cn("absolute h-full rounded-full bg-primary", local.class)} |
| 62 | + {...others} |
| 63 | + /> |
| 64 | + ); |
| 65 | +}; |
| 66 | + |
| 67 | +type SliderThumbProps<T extends ValidComponent = "span"> = |
| 68 | + SliderPrimitive.SliderThumbProps<T> & { |
| 69 | + class?: string | undefined; |
| 70 | + children?: JSX.Element; |
| 71 | + }; |
| 72 | + |
| 73 | +const SliderThumb = <T extends ValidComponent = "span">( |
| 74 | + props: PolymorphicProps<T, SliderThumbProps<T>>, |
| 75 | +) => { |
| 76 | + const [local, others] = splitProps(props as SliderThumbProps, [ |
| 77 | + "class", |
| 78 | + "children", |
| 79 | + ]); |
| 80 | + return ( |
| 81 | + <SliderPrimitive.Thumb |
| 82 | + class={cn( |
| 83 | + "top-[-6px] block size-5 rounded-full border-2 border-primary bg-background ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50", |
| 84 | + local.class, |
| 85 | + )} |
| 86 | + {...others} |
| 87 | + > |
| 88 | + <SliderPrimitive.Input /> |
| 89 | + </SliderPrimitive.Thumb> |
| 90 | + ); |
| 91 | +}; |
| 92 | + |
| 93 | +const SliderLabel = <T extends ValidComponent = "label">( |
| 94 | + props: PolymorphicProps<T, SliderPrimitive.SliderLabelProps<T>>, |
| 95 | +) => { |
| 96 | + return <SliderPrimitive.Label as={Label} {...props} />; |
| 97 | +}; |
| 98 | + |
| 99 | +const SliderValueLabel = <T extends ValidComponent = "label">( |
| 100 | + props: PolymorphicProps<T, SliderPrimitive.SliderValueLabelProps<T>>, |
| 101 | +) => { |
| 102 | + return <SliderPrimitive.ValueLabel as={Label} {...props} />; |
| 103 | +}; |
| 104 | + |
| 105 | +export { |
| 106 | + Slider, |
| 107 | + SliderTrack, |
| 108 | + SliderFill, |
| 109 | + SliderThumb, |
| 110 | + SliderLabel, |
| 111 | + SliderValueLabel, |
| 112 | +}; |
0 commit comments