Skip to content

The Open Standard for Generative UI

Update version to 0.8.36 and refactor Slider component to simplify step handling (#207)

subham31569619f168
diff --git a/js/packages/react-ui/package.json b/js/packages/react-ui/package.json
index 62b2fd9e0..59eeda799 100644
--- a/js/packages/react-ui/package.json
+++ b/js/packages/react-ui/package.json
@@ -2,7 +2,7 @@
"type": "module",
"name": "@crayonai/react-ui",
"license": "MIT",
- "version": "0.8.35",
+ "version": "0.8.36",
"description": "Component library for Generative UI SDK",
"main": "dist/index.js",
"types": "dist/index.d.ts",
diff --git a/js/packages/react-ui/src/components/Carousel/Carousel.tsx b/js/packages/react-ui/src/components/Carousel/Carousel.tsx
index e0418e417..4ead2933e 100644
--- a/js/packages/react-ui/src/components/Carousel/Carousel.tsx
+++ b/js/packages/react-ui/src/components/Carousel/Carousel.tsx
@@ -64,46 +64,49 @@ export const Carousel = forwardRef<CarouselRef, CarouselProps>(
const [isPrevVisible, setIsPrevVisible] = useState(false);
const [isNextVisible, setIsNextVisible] = useState(false);
- const scroll = useCallback((direction: "left" | "right") => {
- if (!scrollDivRef.current) {
- return;
- }
-
- const container = scrollDivRef.current;
- const items = noSnap
- ? Array.from(container.children[0]?.children ?? [])
- : Array.from(container.children);
-
- if (items.length === 0) {
- return;
- }
-
- const containerRect = container.getBoundingClientRect();
- const visibleIndex = items.findIndex((child) => {
- const rect = child.getBoundingClientRect();
- return rect.left >= containerRect.left;
- });
-
- let currentIndex = visibleIndex;
- if (visibleIndex === -1) {
- // Scrolled to the far right, so the last item is the current one
- currentIndex = items.length - 1;
- }
-
- const targetIndex =
- direction === "left"
- ? Math.max(0, currentIndex - itemsToScroll)
- : Math.min(items.length - 1, currentIndex + itemsToScroll);
-
- const targetElement = items[targetIndex] as HTMLElement;
-
- if (targetElement) {
- container.scrollTo({
- left: targetElement.offsetLeft,
- behavior: "smooth",
+ const scroll = useCallback(
+ (direction: "left" | "right") => {
+ if (!scrollDivRef.current) {
+ return;
+ }
+
+ const container = scrollDivRef.current;
+ const items = noSnap
+ ? Array.from(container.children[0]?.children ?? [])
+ : Array.from(container.children);
+
+ if (items.length === 0) {
+ return;
+ }
+
+ const containerRect = container.getBoundingClientRect();
+ const visibleIndex = items.findIndex((child) => {
+ const rect = child.getBoundingClientRect();
+ return rect.left >= containerRect.left;
});
- }
- }, []);
+
+ let currentIndex = visibleIndex;
+ if (visibleIndex === -1) {
+ // Scrolled to the far right, so the last item is the current one
+ currentIndex = items.length - 1;
+ }
+
+ const targetIndex =
+ direction === "left"
+ ? Math.max(0, currentIndex - itemsToScroll)
+ : Math.min(items.length - 1, currentIndex + itemsToScroll);
+
+ const targetElement = items[targetIndex] as HTMLElement;
+
+ if (targetElement) {
+ container.scrollTo({
+ left: targetElement.offsetLeft,
+ behavior: "smooth",
+ });
+ }
+ },
+ [noSnap, itemsToScroll],
+ );
useEffect(() => {
if (!scrollDivRef.current) return;
@@ -131,7 +134,7 @@ export const Carousel = forwardRef<CarouselRef, CarouselProps>(
resizeObserver.disconnect();
mutationObserver.disconnect();
};
- }, [scrollDivRef]);
+ }, []);
useEffect(() => {
onScrollLeftEnabled?.(isPrevVisible);
@@ -146,7 +149,7 @@ export const Carousel = forwardRef<CarouselRef, CarouselProps>(
scroll,
scrollDivRef,
};
- }, []);
+ }, [scroll]);
const contextValue = useMemo(
() => ({
diff --git a/js/packages/react-ui/src/components/Slider/Slider.tsx b/js/packages/react-ui/src/components/Slider/Slider.tsx
index f51046e96..4a67f074f 100644
--- a/js/packages/react-ui/src/components/Slider/Slider.tsx
+++ b/js/packages/react-ui/src/components/Slider/Slider.tsx
@@ -55,8 +55,6 @@ 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],
@@ -105,11 +103,23 @@ export const Slider = forwardRef<React.ComponentRef<typeof SliderPrimitive.Root>
const renderDots = () => {
if (variant === "discrete" && step) {
const numSteps = Math.floor((max - min) / step);
+ const currentValue = valueToShow?.[0] ?? min;
+
return Array.from({ length: numSteps + 1 }, (_, index) => {
const value = min + step * index;
const position = ((value - min) / (max - min)) * 100;
+ const isActive = isRange
+ ? value >= (valueToShow?.[0] ?? min) && value <= (valueToShow?.[1] ?? max)
+ : value <= currentValue;
+
return (
- <div key={value} className="crayon-slider-dots-dot" style={{ left: `${position}%` }} />
+ <div
+ key={value}
+ className={clsx("crayon-slider-dots-dot", {
+ "crayon-slider-dots-dot--active": isActive,
+ })}
+ style={{ left: `${position}%` }}
+ />
);
});
}
@@ -131,7 +141,7 @@ export const Slider = forwardRef<React.ComponentRef<typeof SliderPrimitive.Root>
{...props}
min={min}
max={max}
- step={effectiveStep}
+ step={step}
value={valueToShow}
onValueChange={(val) => {
if (!isControlled) {
diff --git a/js/packages/react-ui/src/components/Slider/slider.scss b/js/packages/react-ui/src/components/Slider/slider.scss
index 4b1d6c2e6..e20a9a7d1 100644
--- a/js/packages/react-ui/src/components/Slider/slider.scss
+++ b/js/packages/react-ui/src/components/Slider/slider.scss
@@ -131,7 +131,11 @@
width: 4px;
height: 4px;
border-radius: cssUtils.$rounded-full;
- background-color: cssUtils.$bg-container;
+ background-color: cssUtils.$disabled-text;
+
+ &--active {
+ background-color: cssUtils.$bg-container;
+ }
}
}