Skip to content

The Open Standard for Generative UI

Refactor slider (#202)

subham575b44dfee47
diff --git a/js/packages/react-ui/src/components/Slider/Slider.tsx b/js/packages/react-ui/src/components/Slider/Slider.tsx
index 938bcbd10..f51046e96 100644
--- a/js/packages/react-ui/src/components/Slider/Slider.tsx
+++ b/js/packages/react-ui/src/components/Slider/Slider.tsx
@@ -2,6 +2,22 @@ import * as SliderPrimitive from "@radix-ui/react-slider";
import clsx from "clsx";
import { forwardRef, ReactNode, useMemo, useState } from "react";
+const formatNumber = (num: number | undefined): string => {
+ if (num === undefined) {
+ return "";
+ }
+ // format number to k, m, b
+ if (num >= 1000) {
+ return new Intl.NumberFormat("en-US", {
+ notation: "compact",
+ compactDisplay: "short",
+ })
+ .format(num)
+ .toLowerCase();
+ }
+ return String(num);
+};
+
export interface SliderProps
extends Omit<
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root>,
@@ -39,6 +55,8 @@ export const Slider = forwardRef<React.ComponentRef<typeof SliderPrimitive.Root>
},
ref,
) => {
+ const effectiveStep = variant === "continuous" ? 1 : step;
+
// used to show the correct value on thumb
const [internalValue, setInternalValue] = useState(
defaultValue && defaultValue.length > 0 ? defaultValue : [min],
@@ -50,19 +68,19 @@ export const Slider = forwardRef<React.ComponentRef<typeof SliderPrimitive.Root>
const isRange = valueToShow && valueToShow.length > 1;
const thumbs = useMemo(() => {
- const thumbClass = "slider-thumb-handle";
- const valueIndicatorClass = "slider-thumb-value";
+ const thumbClass = "crayon-slider-thumb-handle";
+ const valueIndicatorClass = "crayon-slider-thumb-value";
if (isRange) {
return (
<>
{valueToShow?.map((v, i) => (
- <SliderPrimitive.Thumb key={i} className="slider-thumb">
+ <SliderPrimitive.Thumb key={i} className="crayon-slider-thumb">
<div className={thumbClass}>
- <div className="slider-thumb-handle-inner">
- <div className="slider-thumb-handle-inner-dot" />
+ <div className="crayon-slider-thumb-handle-inner">
+ <div className="crayon-slider-thumb-handle-inner-dot" />
</div>
- {!disabled && <div className={valueIndicatorClass}>{v}</div>}
+ {!disabled && <div className={valueIndicatorClass}>{formatNumber(v)}</div>}
</div>
</SliderPrimitive.Thumb>
))}
@@ -71,12 +89,14 @@ export const Slider = forwardRef<React.ComponentRef<typeof SliderPrimitive.Root>
}
return (
- <SliderPrimitive.Thumb className="slider-thumb">
+ <SliderPrimitive.Thumb className="crayon-slider-thumb">
<div className={thumbClass}>
- <div className="slider-thumb-handle-inner">
- <div className="slider-thumb-handle-inner-dot" />
+ <div className="crayon-slider-thumb-handle-inner">
+ <div className="crayon-slider-thumb-handle-inner-dot" />
</div>
- {!disabled && <div className={valueIndicatorClass}>{valueToShow?.[0]}</div>}
+ {!disabled && (
+ <div className={valueIndicatorClass}>{formatNumber(valueToShow?.[0])}</div>
+ )}
</div>
</SliderPrimitive.Thumb>
);
@@ -88,47 +108,59 @@ export const Slider = forwardRef<React.ComponentRef<typeof SliderPrimitive.Root>
return Array.from({ length: numSteps + 1 }, (_, index) => {
const value = min + step * index;
const position = ((value - min) / (max - min)) * 100;
- return <div key={value} className="slider-dots-dot" style={{ left: `${position}%` }} />;
+ return (
+ <div key={value} className="crayon-slider-dots-dot" style={{ left: `${position}%` }} />
+ );
});
}
return null;
};
return (
- <div className="slider-wrapper">
- {leftContent && <div className="slider-left-content">{leftContent}</div>}
- <div className="slider-container">
- <SliderPrimitive.Root
- ref={ref}
- className={clsx("slider-root", { "slider--disabled": disabled }, className)}
- {...props}
- min={min}
- max={max}
- step={step}
- value={valueToShow}
- onValueChange={(val) => {
- if (!isControlled) {
- setInternalValue(val);
- }
- onValueChange?.(val);
- }}
- minStepsBetweenThumbs={1}
- disabled={disabled}
- key={variant}
- style={style}
- >
- <SliderPrimitive.Track className="slider-track">
- <SliderPrimitive.Range
- className={clsx("slider-range", {
- "slider-range--at-min": !isRange && valueToShow?.[0] === min,
- })}
- />
- {variant === "discrete" && renderDots()}
- </SliderPrimitive.Track>
- {thumbs}
- </SliderPrimitive.Root>
+ <div className="crayon-slider-wrapper">
+ {leftContent && <div className="crayon-slider-left-content">{leftContent}</div>}
+ <div className="crayon-slider-container-wrapper">
+ <div className="crayon-slider-container">
+ <SliderPrimitive.Root
+ ref={ref}
+ className={clsx(
+ "crayon-slider-root",
+ { "crayon-slider--disabled": disabled },
+ className,
+ )}
+ {...props}
+ min={min}
+ max={max}
+ step={effectiveStep}
+ value={valueToShow}
+ onValueChange={(val) => {
+ if (!isControlled) {
+ setInternalValue(val);
+ }
+ onValueChange?.(val);
+ }}
+ minStepsBetweenThumbs={1}
+ disabled={disabled}
+ key={variant}
+ style={style}
+ >
+ <SliderPrimitive.Track className="crayon-slider-track">
+ <SliderPrimitive.Range
+ className={clsx("crayon-slider-range", {
+ "crayon-slider-range--at-min": !isRange && valueToShow?.[0] === min,
+ })}
+ />
+ {variant === "discrete" && renderDots()}
+ </SliderPrimitive.Track>
+ {thumbs}
+ </SliderPrimitive.Root>
+ </div>
+ <div className="crayon-slider-labels">
+ <span>{formatNumber(min)}</span>
+ <span>{formatNumber(max)}</span>
+ </div>
</div>
- {rightContent && <div className="slider-right-content">{rightContent}</div>}
+ {rightContent && <div className="crayon-slider-right-content">{rightContent}</div>}
</div>
);
},
diff --git a/js/packages/react-ui/src/components/Slider/slider.scss b/js/packages/react-ui/src/components/Slider/slider.scss
index 010ab327a..4b1d6c2e6 100644
--- a/js/packages/react-ui/src/components/Slider/slider.scss
+++ b/js/packages/react-ui/src/components/Slider/slider.scss
@@ -1,6 +1,6 @@
@use "../../cssUtils" as cssUtils;
-.slider {
+.crayon-slider {
&-wrapper {
display: flex;
align-items: center;
@@ -23,9 +23,9 @@
}
&-container {
- padding: 0 8px;
+ padding: 0 10px;
background-color: cssUtils.$bg-sunk;
- height: 16px;
+ height: 10px;
width: 100%;
display: flex;
align-items: center;
@@ -47,7 +47,7 @@
&-track {
position: relative;
- height: 16px;
+ height: 10px;
flex-grow: 1;
border-radius: cssUtils.$rounded-full;
}
@@ -57,7 +57,7 @@
height: 100%;
border-radius: cssUtils.$rounded-s;
background-color: cssUtils.$interactive-accent;
- margin-left: -8px;
+ margin-left: -10px;
margin-right: -8px;
&--at-min {
@@ -69,9 +69,9 @@
outline: none;
&-handle {
display: block;
- width: 12px;
- height: 12px;
- border-radius: cssUtils.$rounded-xs;
+ width: 20px;
+ height: 20px;
+ border-radius: cssUtils.$rounded-s;
outline: 1px solid cssUtils.$stroke-default;
background-color: cssUtils.$bg-container;
overflow: hidden;
@@ -91,9 +91,9 @@
}
&-dot {
- width: 4px;
- height: 4px;
- border-radius: cssUtils.$rounded-full;
+ width: 6px;
+ height: 6px;
+ border-radius: cssUtils.$rounded-3xs;
background-color: cssUtils.$interactive-accent;
}
}
@@ -116,8 +116,8 @@
border: 1px solid cssUtils.$stroke-interactive-el;
box-shadow: cssUtils.$shadow-s;
- .slider-thumb-handle:hover &,
- .slider-thumb-handle:focus & {
+ .crayon-slider-thumb-handle:hover &,
+ .crayon-slider-thumb-handle:focus & {
opacity: 1;
}
}
@@ -127,7 +127,7 @@
&-dot {
position: absolute;
top: 50%;
- transform: translateY(-50%);
+ transform: translate(-50%, -50%);
width: 4px;
height: 4px;
border-radius: cssUtils.$rounded-full;
@@ -135,6 +135,19 @@
}
}
+ &-container-wrapper {
+ width: 100%;
+ }
+
+ &-labels {
+ display: flex;
+ justify-content: space-between;
+ margin-top: cssUtils.$spacing-m;
+ color: cssUtils.$secondary-text;
+ @include cssUtils.typography(label, small);
+ padding: 0 cssUtils.$spacing-2xs;
+ }
+
&--disabled {
opacity: 0.5;
pointer-events: none;