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