MCPcopy Create free account
hub / github.com/adobe/react-spectrum / ScrollableToc

Function ScrollableToc

packages/dev/s2-docs/src/ScrollableToc.tsx:6–61  ·  view source on GitHub ↗
({children})

Source from the content-addressed store, hash-verified

4import {style} from '@react-spectrum/s2/style' with {type: 'macro'};
5
6export function ScrollableToc({children}) {
7 let [topMaskSize, setTopMaskSize] = useState(0);
8 let [bottomMaskSize, setBottomMaskSize] = useState(0);
9 let scrollRef = useRef<HTMLDivElement>(null);
10
11 let updateMasks = (element: HTMLDivElement) => {
12 let scrollTop = element.scrollTop;
13 let scrollHeight = element.scrollHeight;
14 let clientHeight = element.clientHeight;
15 let distanceFromBottom = scrollHeight - scrollTop - clientHeight;
16
17 setTopMaskSize(Math.min(scrollTop, 32));
18 setBottomMaskSize(Math.min(distanceFromBottom, 32));
19 };
20
21 useEffect(() => {
22 if (scrollRef.current) {
23 updateMasks(scrollRef.current);
24 }
25 }, [children]);
26
27 let maskImage: string | undefined;
28 if (topMaskSize > 0 || bottomMaskSize > 0) {
29 let parts: string[] = [];
30 if (topMaskSize > 0) {
31 parts.push('transparent 0px');
32 parts.push(`black ${topMaskSize}px`);
33 } else {
34 parts.push('black 0px');
35 }
36 if (bottomMaskSize > 0) {
37 parts.push(`black calc(100% - ${bottomMaskSize}px)`);
38 parts.push('transparent 100%');
39 } else {
40 parts.push('black 100%');
41 }
42 maskImage = `linear-gradient(to bottom, ${parts.join(', ')})`;
43 }
44
45 return (
46 <div
47 ref={scrollRef}
48 onScroll={e => updateMasks(e.currentTarget)}
49 style={{
50 maskImage
51 }}
52 className={style({
53 overflowY: 'auto',
54 flexShrink: 1,
55 minHeight: 0,
56 maxHeight: '100%'
57 })}>
58 {children}
59 </div>
60 );
61}

Callers

nothing calls this directly

Calls 2

updateMasksFunction · 0.85
pushMethod · 0.80

Tested by

no test coverage detected