Connect, collaborate, and grow with a community of TypeScript developers
Bugfix: Hide Carousel arrows if the container cannot scroll anymore (#514)
Andrés Martínez991b8c8b0ed7
diff --git a/apps/web/src/components/ui/carousel.tsx b/apps/web/src/components/ui/carousel.tsxindex 7a0ed07f..fd01cf7b 100644--- a/apps/web/src/components/ui/carousel.tsx+++ b/apps/web/src/components/ui/carousel.tsx@@ -1,6 +1,8 @@'use client';-import { ChevronRight } from '@repo/ui/icons';-import { useEffect, useRef, useState, type ReactNode } from 'react';+import { ChevronLeft, ChevronRight } from '@repo/ui/icons';+import clsx from 'clsx';+import { useEffect, useRef, useState, type ReactNode, useCallback } from 'react';+import useResizeObserver from '~/utils/useResizeObserver';interface Props {children: ReactNode;@@ -8,12 +10,26 @@ interface Props {export function Carousel({ children }: Props) {const containerRef = useRef<HTMLDivElement>(null);+ const containerElement = containerRef.current;+const [showLeftButton, setShowLeftButton] = useState(false);- const [showRightButton, setShowRightButton] = useState(true);+ const [showRightButton, setShowRightButton] = useState(false);- useEffect(() => {- const containerElement = containerRef.current;+ const handleScroll = useCallback(() => {+ if (containerElement) {+ const scrollLeft = containerElement.scrollLeft;+ const scrollWidth = containerElement.scrollWidth;+ const clientWidth = containerElement.clientWidth;++ setShowLeftButton(scrollLeft > 0);+ setShowRightButton(scrollLeft + clientWidth < scrollWidth);+ }+ }, [containerElement]);+ // observe and handle resizing+ useResizeObserver(containerRef, handleScroll);++ useEffect(() => {const handleSlideRight = () => {if (containerElement) {containerElement.scrollLeft += 330;@@ -26,17 +42,6 @@ export function Carousel({ children }: Props) {}};- const handleScroll = () => {- if (containerElement) {- const scrollLeft = containerElement.scrollLeft;- const scrollWidth = containerElement.scrollWidth;- const clientWidth = containerElement.clientWidth;-- setShowLeftButton(scrollLeft > 0);- setShowRightButton(scrollLeft + clientWidth < scrollWidth);- }- };-const buttonRight = containerElement?.querySelector('#slideRight');const buttonLeft = containerElement?.querySelector('#slideLeft');@@ -63,7 +68,7 @@ export function Carousel({ children }: Props) {buttonLeft.removeEventListener('click', handleSlideLeft);}};- }, []);+ }, [handleScroll, containerElement]);return (<div@@ -73,16 +78,26 @@ export function Carousel({ children }: Props) {>{children}<button- className="absolute right-5 top-1/2 hidden -translate-y-1/2 rounded-[5rem] border border-neutral-400 bg-neutral-200/50 px-2 py-4 backdrop-blur-sm duration-300 focus:outline-none focus-visible:ring-2 active:scale-75 dark:border-neutral-600 dark:bg-neutral-700/50 sm:block"- id="slideRight"+ className={clsx(+ 'absolute left-5 top-1/2 hidden -translate-y-1/2 rounded-[5rem] border border-neutral-400 bg-neutral-200/50 px-2 py-4 backdrop-blur-sm duration-300 focus:outline-none focus-visible:ring-2 active:scale-75 dark:border-neutral-600 dark:bg-neutral-700/50',+ showLeftButton && 'sm:block',+ )}+ id="slideLeft"+ aria-hidden={!showLeftButton}+ aria-label="Slide carousel of challenges to the left">- <ChevronRight className="h-4 w-4 stroke-[3]" />+ <ChevronLeft className="h-4 w-4 stroke-[3]" /></button><button- className="absolute left-5 top-1/2 hidden -translate-y-1/2 rounded-[5rem] border border-neutral-400 bg-neutral-200/50 px-2 py-4 backdrop-blur-sm duration-300 focus:outline-none focus-visible:ring-2 active:scale-75 dark:border-neutral-600 dark:bg-neutral-700/50 sm:block"- id="slideLeft"+ className={clsx(+ 'absolute right-5 top-1/2 hidden -translate-y-1/2 rounded-[5rem] border border-neutral-400 bg-neutral-200/50 px-2 py-4 backdrop-blur-sm duration-300 focus:outline-none focus-visible:ring-2 active:scale-75 dark:border-neutral-600 dark:bg-neutral-700/50',+ showRightButton && 'sm:block',+ )}+ id="slideRight"+ aria-hidden={!showRightButton}+ aria-label="Slide carousel of challenges to the right">- <ChevronRight className="h-4 w-4 rotate-180 transform stroke-[3]" />+ <ChevronRight className="h-4 w-4 stroke-[3]" /></button></div>);diff --git a/apps/web/src/utils/useResizeObserver.ts b/apps/web/src/utils/useResizeObserver.tsnew file mode 100644index 00000000..daef954d--- /dev/null+++ b/apps/web/src/utils/useResizeObserver.ts@@ -0,0 +1,43 @@+import { useCallback, useEffect, useState } from 'react';++interface Size {+ width: number;+ height: number;+}++const useResizeObserver = (+ ref: React.RefObject<HTMLElement>,+ callback: (size: Size) => unknown,+) => {+ const [attached, setAttached] = useState(false);++ const handleResize = useCallback(+ (entries: ResizeObserverEntry[]) => {+ for (const entry of entries) {+ if (entry.target === ref.current) {+ callback({+ width: entry.contentRect.width,+ height: entry.contentRect.height,+ });+ }+ }+ },+ [ref, callback],+ );++ useEffect(() => {+ if (ref.current) {+ const resizeObserver = new ResizeObserver(handleResize);+ resizeObserver.observe(ref.current);+ setAttached(true);++ return () => {+ resizeObserver.disconnect();+ };+ }+ }, [ref, handleResize]);++ return attached;+};++export default useResizeObserver;