Skip to content

Commit 06e8ae2

Browse files
authored
Merge pull request #81 from kleros/feat/slider-theme-override
feat: allow-theme-override-for-slider
2 parents 6533622 + 3c1ce60 commit 06e8ae2

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/lib/form/slider.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from "react";
2-
import { cn } from "../../utils";
2+
import { cn, isUndefined } from "../../utils";
33
import {
44
Slider as AriaSlider,
55
Label,
@@ -17,6 +17,13 @@ interface SliderProps
1717
formatter?: (value: number) => string;
1818
/** Callback function that provides the selected value as an argument. */
1919
callback: (value: number) => void;
20+
/** Override default theme of Slider */
21+
theme?: {
22+
sliderColor: string;
23+
trackColor: string;
24+
thumbColor: string;
25+
labelColor: string;
26+
};
2027
}
2128

2229
/** Slider allows a user to select a value within a range. */
@@ -25,8 +32,10 @@ function Slider({
2532
rightLabel,
2633
formatter,
2734
callback,
35+
theme,
2836
...props
2937
}: Readonly<SliderProps>) {
38+
const { sliderColor, thumbColor, labelColor, trackColor } = theme ?? {};
3039
return (
3140
<AriaSlider
3241
className={cn("mt-4 box-border w-125")}
@@ -39,37 +48,57 @@ function Slider({
3948
<Label className="hidden">{state.getThumbValue(0)}</Label>
4049
{/* track */}
4150
<div
51+
id="slider-track"
4252
className={cn(
43-
"bg-klerosUIComponentsStroke absolute top-1/2 -translate-y-1/2",
53+
"absolute top-1/2 -translate-y-1/2",
4454
"h-2 w-full cursor-pointer rounded-[30px]",
4555
isDisabled && "cursor-default",
4656
)}
57+
style={{
58+
backgroundColor:
59+
trackColor ?? "var(--klerosUIComponentsStroke)",
60+
}}
4761
/>
4862
{/* fill */}
4963
<div
64+
id="slider-fill"
5065
className={cn(
51-
"bg-klerosUIComponentsPrimaryBlue absolute top-1/2 h-2 -translate-y-1/2",
66+
"absolute top-1/2 h-2 -translate-y-1/2",
5267
"cursor-pointer rounded-[30px]",
5368
isDisabled &&
5469
"bg-klerosUIComponentsSecondaryText cursor-default",
5570
)}
56-
style={{ width: state.getThumbPercent(0) * 100 + "%" }}
71+
style={{
72+
width: state.getThumbPercent(0) * 100 + "%",
73+
backgroundColor:
74+
sliderColor ?? "var(--klerosUIComponentsPrimaryBlue)",
75+
}}
5776
/>
5877
<SliderThumb
78+
id="slider-thumb"
5979
className={cn(
6080
"dragging:bg-klerosUIComponentsLightBlue bg-klerosUIComponentsWhiteBackground relative top-1/2 size-6",
61-
"border-klerosUIComponentsPrimaryBlue rounded-full border-3 border-solid",
62-
"dragging:shadow-input cursor-pointer outline-hidden transition",
81+
"rounded-full border-3 border-solid",
82+
"cursor-pointer outline-hidden transition",
6383
isDisabled &&
6484
"border-klerosUIComponentsSecondaryText cursor-default",
85+
isUndefined(thumbColor) && "dragging:shadow-input",
6586
)}
87+
style={{
88+
borderColor:
89+
thumbColor ?? "var(--klerosUIComponentsPrimaryBlue)",
90+
}}
6691
>
6792
<Label
93+
id="slider-label"
6894
className={cn(
6995
"absolute -top-7 left-1/2 w-max -translate-x-1/2",
70-
"text-klerosUIComponentsPrimaryBlue text-sm",
96+
"text-sm",
7197
isDisabled && "hidden",
7298
)}
99+
style={{
100+
color: labelColor ?? "var(--klerosUIComponentsPrimaryBlue)",
101+
}}
73102
>
74103
{formatter ? (
75104
formatter(state.getThumbValue(0))
@@ -84,6 +113,7 @@ function Slider({
84113
<div className="mt-2 flex w-full items-center justify-between">
85114
{leftLabel && (
86115
<Label
116+
id="slider-left-label"
87117
className="text-klerosUIComponentsPrimaryText text-sm break-words"
88118
aria-label="min-label"
89119
>
@@ -92,6 +122,7 @@ function Slider({
92122
)}
93123
{rightLabel && (
94124
<Label
125+
id="slider-right-label"
95126
className="text-klerosUIComponentsPrimaryText text-sm break-words"
96127
aria-label="max-label"
97128
>

0 commit comments

Comments
 (0)