|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { tString, useLanguage } from '@/intl/client'; |
| 4 | +import { tcls } from '@/lib/tailwind'; |
| 5 | +import * as React from 'react'; |
| 6 | +import { Button } from './Button'; |
| 7 | + |
| 8 | +/** |
| 9 | + * A container that encapsulates a scrollable area with usability features. |
| 10 | + * - Faded edges when there is more content than the container can display. |
| 11 | + * - Buttons to advance the scroll position. |
| 12 | + * - Auto-scroll to the active item when it's initially active. |
| 13 | + */ |
| 14 | +export type ScrollContainerProps = { |
| 15 | + children: React.ReactNode; |
| 16 | + className?: string; |
| 17 | + |
| 18 | + /** The direction of the scroll container. */ |
| 19 | + orientation: 'horizontal' | 'vertical'; |
| 20 | + |
| 21 | + /** The ID of the active item to scroll to. */ |
| 22 | + activeId?: string; |
| 23 | +} & React.HTMLAttributes<HTMLDivElement>; |
| 24 | + |
| 25 | +export function ScrollContainer(props: ScrollContainerProps) { |
| 26 | + const { children, className, orientation, activeId, ...rest } = props; |
| 27 | + |
| 28 | + const containerRef = React.useRef<HTMLDivElement>(null); |
| 29 | + |
| 30 | + const [scrollPosition, setScrollPosition] = React.useState(0); |
| 31 | + const [scrollSize, setScrollSize] = React.useState(0); |
| 32 | + |
| 33 | + const language = useLanguage(); |
| 34 | + |
| 35 | + React.useEffect(() => { |
| 36 | + const container = containerRef.current; |
| 37 | + if (!container) { |
| 38 | + return; |
| 39 | + } |
| 40 | + |
| 41 | + // Update scroll position on scroll using requestAnimationFrame |
| 42 | + const scrollListener: EventListener = () => { |
| 43 | + requestAnimationFrame(() => { |
| 44 | + setScrollPosition( |
| 45 | + orientation === 'horizontal' ? container.scrollLeft : container.scrollTop |
| 46 | + ); |
| 47 | + }); |
| 48 | + }; |
| 49 | + container.addEventListener('scroll', scrollListener); |
| 50 | + |
| 51 | + // Update max scroll position using resize observer |
| 52 | + const resizeObserver = new ResizeObserver((entries) => { |
| 53 | + const containerEntry = entries.find((i) => i.target === containerRef.current); |
| 54 | + if (containerEntry) { |
| 55 | + setScrollSize( |
| 56 | + orientation === 'horizontal' |
| 57 | + ? containerEntry.target.scrollWidth - containerEntry.target.clientWidth - 1 |
| 58 | + : containerEntry.target.scrollHeight - |
| 59 | + containerEntry.target.clientHeight - |
| 60 | + 1 |
| 61 | + ); |
| 62 | + } |
| 63 | + }); |
| 64 | + resizeObserver.observe(container); |
| 65 | + |
| 66 | + return () => { |
| 67 | + container.removeEventListener('scroll', scrollListener); |
| 68 | + resizeObserver.disconnect(); |
| 69 | + }; |
| 70 | + }, [orientation]); |
| 71 | + |
| 72 | + // Scroll to the active item |
| 73 | + React.useEffect(() => { |
| 74 | + const container = containerRef.current; |
| 75 | + if (!container || !activeId) { |
| 76 | + return; |
| 77 | + } |
| 78 | + const activeItem = container.querySelector(`#${CSS.escape(activeId)}`); |
| 79 | + if (activeItem) { |
| 80 | + activeItem.scrollIntoView({ |
| 81 | + inline: 'center', |
| 82 | + block: 'center', |
| 83 | + }); |
| 84 | + } |
| 85 | + }, [activeId]); |
| 86 | + |
| 87 | + const scrollFurther = () => { |
| 88 | + const container = containerRef.current; |
| 89 | + if (!container) { |
| 90 | + return; |
| 91 | + } |
| 92 | + container.scrollTo({ |
| 93 | + top: orientation === 'vertical' ? scrollPosition + container.clientHeight : undefined, |
| 94 | + left: orientation === 'horizontal' ? scrollPosition + container.clientWidth : undefined, |
| 95 | + behavior: 'smooth', |
| 96 | + }); |
| 97 | + }; |
| 98 | + |
| 99 | + const scrollBack = () => { |
| 100 | + const container = containerRef.current; |
| 101 | + if (!container) { |
| 102 | + return; |
| 103 | + } |
| 104 | + container.scrollTo({ |
| 105 | + top: orientation === 'vertical' ? scrollPosition - container.clientHeight : undefined, |
| 106 | + left: orientation === 'horizontal' ? scrollPosition - container.clientWidth : undefined, |
| 107 | + behavior: 'smooth', |
| 108 | + }); |
| 109 | + }; |
| 110 | + |
| 111 | + return ( |
| 112 | + <div |
| 113 | + className={tcls('group/scroll-container relative flex overflow-hidden', className)} |
| 114 | + {...rest} |
| 115 | + > |
| 116 | + {/* Scrollable content */} |
| 117 | + <div |
| 118 | + className={tcls( |
| 119 | + 'flex shrink grow', |
| 120 | + orientation === 'horizontal' ? 'no-scrollbar' : 'hide-scrollbar', |
| 121 | + orientation === 'horizontal' ? 'overflow-x-scroll' : 'overflow-y-auto', |
| 122 | + scrollPosition > 0 |
| 123 | + ? orientation === 'horizontal' |
| 124 | + ? 'mask-l-from-[calc(100%-2rem)]' |
| 125 | + : 'mask-t-from-[calc(100%-2rem)]' |
| 126 | + : '', |
| 127 | + scrollPosition < scrollSize |
| 128 | + ? orientation === 'horizontal' |
| 129 | + ? 'mask-r-from-[calc(100%-2rem)]' |
| 130 | + : 'mask-b-from-[calc(100%-2rem)]' |
| 131 | + : '' |
| 132 | + )} |
| 133 | + ref={containerRef} |
| 134 | + > |
| 135 | + {children} |
| 136 | + </div> |
| 137 | + |
| 138 | + {/* Scroll buttons back & forward */} |
| 139 | + <Button |
| 140 | + icon={orientation === 'horizontal' ? 'chevron-left' : 'chevron-up'} |
| 141 | + iconOnly |
| 142 | + size="xsmall" |
| 143 | + variant="secondary" |
| 144 | + tabIndex={-1} |
| 145 | + className={tcls( |
| 146 | + orientation === 'horizontal' |
| 147 | + ? '-translate-y-1/2! top-1/2 left-0 ml-2' |
| 148 | + : '-translate-x-1/2! top-0 left-1/2 mt-2', |
| 149 | + 'absolute not-pointer-none:block hidden scale-0 opacity-0 transition-[scale,opacity]', |
| 150 | + scrollPosition > 0 |
| 151 | + ? 'not-pointer-none:group-hover/scroll-container:scale-100 not-pointer-none:group-hover/scroll-container:opacity-11' |
| 152 | + : 'pointer-events-none' |
| 153 | + )} |
| 154 | + onClick={scrollBack} |
| 155 | + label={tString(language, 'scroll_back')} |
| 156 | + /> |
| 157 | + <Button |
| 158 | + icon={orientation === 'horizontal' ? 'chevron-right' : 'chevron-down'} |
| 159 | + iconOnly |
| 160 | + size="xsmall" |
| 161 | + variant="secondary" |
| 162 | + tabIndex={-1} |
| 163 | + className={tcls( |
| 164 | + orientation === 'horizontal' |
| 165 | + ? '-translate-y-1/2! top-1/2 right-0 mr-2' |
| 166 | + : '-translate-x-1/2! bottom-0 left-1/2 mb-2', |
| 167 | + 'absolute not-pointer-none:block hidden scale-0 transition-[scale,opacity]', |
| 168 | + scrollPosition < scrollSize |
| 169 | + ? 'not-pointer-none:group-hover/scroll-container:scale-100 not-pointer-none:group-hover/scroll-container:opacity-11' |
| 170 | + : 'pointer-events-none' |
| 171 | + )} |
| 172 | + onClick={scrollFurther} |
| 173 | + label={tString(language, 'scroll_further')} |
| 174 | + /> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments