({
children,
maxWidth,
maxHeight,
overflowDirection = 'top',
additionalHiddenLinesCount = 0,
})
| 98 | * </MaxSizedBox> |
| 99 | */ |
| 100 | export const MaxSizedBox: React.FC<MaxSizedBoxProps> = ({ |
| 101 | children, |
| 102 | maxWidth, |
| 103 | maxHeight, |
| 104 | overflowDirection = 'top', |
| 105 | additionalHiddenLinesCount = 0, |
| 106 | }) => { |
| 107 | const id = useId(); |
| 108 | const { addOverflowingId, removeOverflowingId } = useOverflowActions() || {}; |
| 109 | |
| 110 | const laidOutStyledText: StyledText[][] = []; |
| 111 | const targetMaxHeight = Math.max( |
| 112 | Math.round(maxHeight ?? Number.MAX_SAFE_INTEGER), |
| 113 | MINIMUM_MAX_HEIGHT, |
| 114 | ); |
| 115 | |
| 116 | if (maxWidth === undefined) { |
| 117 | throw new Error('maxWidth must be defined when maxHeight is set.'); |
| 118 | } |
| 119 | function visitRows(element: React.ReactNode) { |
| 120 | if (!React.isValidElement<{ children?: React.ReactNode }>(element)) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | if (element.type === Fragment) { |
| 125 | React.Children.forEach(element.props.children, visitRows); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | if (element.type === Box) { |
| 130 | layoutInkElementAsStyledText(element, maxWidth!, laidOutStyledText); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | debugReportError('MaxSizedBox children must be <Box> elements', element); |
| 135 | } |
| 136 | |
| 137 | React.Children.forEach(children, visitRows); |
| 138 | |
| 139 | const contentWillOverflow = |
| 140 | (targetMaxHeight !== undefined && |
| 141 | laidOutStyledText.length > targetMaxHeight) || |
| 142 | additionalHiddenLinesCount > 0; |
| 143 | const visibleContentHeight = |
| 144 | contentWillOverflow && targetMaxHeight !== undefined |
| 145 | ? targetMaxHeight - 1 |
| 146 | : targetMaxHeight; |
| 147 | |
| 148 | const hiddenLinesCount = |
| 149 | visibleContentHeight !== undefined |
| 150 | ? Math.max(0, laidOutStyledText.length - visibleContentHeight) |
| 151 | : 0; |
| 152 | const totalHiddenLines = hiddenLinesCount + additionalHiddenLinesCount; |
| 153 | |
| 154 | useEffect(() => { |
| 155 | if (totalHiddenLines > 0) { |
| 156 | addOverflowingId?.(id); |
| 157 | } else { |
nothing calls this directly
no test coverage detected