|
| 1 | +import process from 'node:process'; |
| 2 | +import React, {useState, useRef, useEffect, useLayoutEffect} from 'react'; |
| 3 | +import {Box, Text, useInput, measureElement} from '../../src/index.js'; |
| 4 | + |
| 5 | +type ScrollMode = 'vertical' | 'horizontal' | 'both' | 'hidden'; |
| 6 | + |
| 7 | +const items = Array.from({length: 100}).map((_, i) => ({ |
| 8 | + id: i, |
| 9 | + text: `Line ${i} ${'-'.repeat(i)}`, |
| 10 | +})); |
| 11 | + |
| 12 | +export function useTerminalSize(): {columns: number; rows: number} { |
| 13 | + const [size, setSize] = useState({ |
| 14 | + columns: process.stdout.columns || 80, |
| 15 | + rows: process.stdout.rows || 20, |
| 16 | + }); |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const updateSize = () => { |
| 20 | + setSize({ |
| 21 | + columns: process.stdout.columns || 80, |
| 22 | + rows: process.stdout.rows || 20, |
| 23 | + }); |
| 24 | + }; |
| 25 | + |
| 26 | + process.stdout.on('resize', updateSize); |
| 27 | + |
| 28 | + return () => { |
| 29 | + process.stdout.off('resize', updateSize); |
| 30 | + }; |
| 31 | + }, []); |
| 32 | + |
| 33 | + return size; |
| 34 | +} |
| 35 | + |
| 36 | +const scrollModes: ScrollMode[] = ['vertical', 'horizontal', 'both', 'hidden']; |
| 37 | + |
| 38 | +function ScrollableContent() { |
| 39 | + const [scrollMode, setScrollMode] = useState<ScrollMode>('vertical'); |
| 40 | + const [scrollTop, setScrollTop] = useState(0); |
| 41 | + const [scrollLeft, setScrollLeft] = useState(0); |
| 42 | + const reference = useRef<any>(null); |
| 43 | + const {columns, rows} = useTerminalSize(); |
| 44 | + const [size, setSize] = useState({ |
| 45 | + innerHeight: 0, |
| 46 | + scrollHeight: 0, |
| 47 | + innerWidth: 0, |
| 48 | + scrollWidth: 0, |
| 49 | + }); |
| 50 | + |
| 51 | + const sizeReference = useRef(size); |
| 52 | + useEffect(() => { |
| 53 | + sizeReference.current = size; |
| 54 | + }, [size]); |
| 55 | + |
| 56 | + const scrollIntervalReference = useRef<NodeJS.Timeout | undefined>(null); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + return () => { |
| 60 | + if (scrollIntervalReference.current) { |
| 61 | + clearInterval(scrollIntervalReference.current); |
| 62 | + } |
| 63 | + }; |
| 64 | + }, []); |
| 65 | + |
| 66 | + useLayoutEffect(() => { |
| 67 | + if (reference.current) { |
| 68 | + const {innerHeight, scrollHeight, innerWidth, scrollWidth} = |
| 69 | + measureElement(reference.current); |
| 70 | + |
| 71 | + if ( |
| 72 | + size.innerHeight !== innerHeight || |
| 73 | + size.scrollHeight !== scrollHeight || |
| 74 | + size.innerWidth !== innerWidth || |
| 75 | + size.scrollWidth !== scrollWidth |
| 76 | + ) { |
| 77 | + setSize({innerHeight, scrollHeight, innerWidth, scrollWidth}); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + useInput((input, key) => { |
| 83 | + if (input === 'm') { |
| 84 | + setScrollMode(previousMode => { |
| 85 | + const currentIndex = scrollModes.indexOf(previousMode); |
| 86 | + const nextIndex = (currentIndex + 1) % scrollModes.length; |
| 87 | + return scrollModes[nextIndex]!; |
| 88 | + }); |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + if (!key.upArrow && !key.downArrow && !key.leftArrow && !key.rightArrow) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (scrollIntervalReference.current) { |
| 97 | + clearInterval(scrollIntervalReference.current); |
| 98 | + } |
| 99 | + |
| 100 | + const scroll = ( |
| 101 | + setter: React.Dispatch<React.SetStateAction<number>>, |
| 102 | + getNewValue: (current: number) => number, |
| 103 | + ) => { |
| 104 | + let frame = 0; |
| 105 | + const frames = 10; |
| 106 | + scrollIntervalReference.current = setInterval(() => { |
| 107 | + if (frame < frames) { |
| 108 | + setter(s => getNewValue(s)); |
| 109 | + frame++; |
| 110 | + } else if (scrollIntervalReference.current) { |
| 111 | + clearInterval(scrollIntervalReference.current); |
| 112 | + scrollIntervalReference.current = null; |
| 113 | + } |
| 114 | + }, 16); |
| 115 | + }; |
| 116 | + |
| 117 | + if (key.upArrow) { |
| 118 | + scroll(setScrollTop, s => Math.max(0, s - 1)); |
| 119 | + } |
| 120 | + |
| 121 | + if (key.downArrow) { |
| 122 | + scroll(setScrollTop, s => |
| 123 | + Math.min( |
| 124 | + s + 1, |
| 125 | + Math.max( |
| 126 | + 0, |
| 127 | + sizeReference.current.scrollHeight - |
| 128 | + sizeReference.current.innerHeight, |
| 129 | + ), |
| 130 | + ), |
| 131 | + ); |
| 132 | + } |
| 133 | + |
| 134 | + if (key.leftArrow) { |
| 135 | + scroll(setScrollLeft, s => Math.max(0, s - 1)); |
| 136 | + } |
| 137 | + |
| 138 | + if (key.rightArrow) { |
| 139 | + scroll(setScrollLeft, s => |
| 140 | + Math.min( |
| 141 | + s + 1, |
| 142 | + Math.max( |
| 143 | + 0, |
| 144 | + sizeReference.current.scrollWidth - sizeReference.current.innerWidth, |
| 145 | + ), |
| 146 | + ), |
| 147 | + ); |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + const overflowX = |
| 152 | + scrollMode === 'horizontal' || scrollMode === 'both' ? 'scroll' : 'hidden'; |
| 153 | + const overflowY = |
| 154 | + scrollMode === 'vertical' || scrollMode === 'both' ? 'scroll' : 'hidden'; |
| 155 | + |
| 156 | + return ( |
| 157 | + <Box flexDirection="column" height={rows - 2} width={columns}> |
| 158 | + <Box flexDirection="column" flexShrink={0} overflow="hidden"> |
| 159 | + <Text>This is a demo showing a scrollable box.</Text> |
| 160 | + <Text>Press up/down arrow to scroll vertically.</Text> |
| 161 | + <Text>Press left/right arrow to scroll horizontally.</Text> |
| 162 | + <Text> |
| 163 | + Press 'm' to cycle through scroll modes (current: {scrollMode}) |
| 164 | + </Text> |
| 165 | + <Text>ScrollTop: {scrollTop}</Text> |
| 166 | + <Text>ScrollLeft: {scrollLeft}</Text> |
| 167 | + <Text> |
| 168 | + Size: {size.innerWidth}x{size.innerHeight} |
| 169 | + </Text> |
| 170 | + <Text> |
| 171 | + Inner scrollable size: {size.scrollWidth}x{size.scrollHeight} |
| 172 | + </Text> |
| 173 | + </Box> |
| 174 | + <Box |
| 175 | + ref={reference} |
| 176 | + borderStyle="round" |
| 177 | + flexShrink={1} |
| 178 | + width="80%" |
| 179 | + flexDirection="column" |
| 180 | + overflowX={overflowX} |
| 181 | + overflowY={overflowY} |
| 182 | + paddingLeft={1} |
| 183 | + paddingRight={1} |
| 184 | + scrollTop={scrollTop} |
| 185 | + scrollLeft={scrollLeft} |
| 186 | + > |
| 187 | + <Box |
| 188 | + flexDirection="column" |
| 189 | + flexShrink={0} |
| 190 | + width={ |
| 191 | + scrollMode === 'horizontal' || scrollMode === 'both' ? 120 : 'auto' |
| 192 | + } |
| 193 | + > |
| 194 | + {items.map(item => ( |
| 195 | + <Text key={item.id}>{item.text}</Text> |
| 196 | + ))} |
| 197 | + <Text key="last-line" color="yellow"> |
| 198 | + This is the last line. |
| 199 | + </Text> |
| 200 | + </Box> |
| 201 | + </Box> |
| 202 | + <Box flexShrink={0} overflow="hidden"> |
| 203 | + <Text>Example footer</Text> |
| 204 | + </Box> |
| 205 | + </Box> |
| 206 | + ); |
| 207 | +} |
| 208 | + |
| 209 | +export default ScrollableContent; |
0 commit comments