Skip to content

Commit 03d2cd5

Browse files
committed
Support maxWidth and maxHeight
1 parent 2a7ec65 commit 03d2cd5

File tree

4 files changed

+400
-0
lines changed

4 files changed

+400
-0
lines changed

src/styles.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,16 @@ export type Styles = {
175175
*/
176176
readonly minHeight?: number | string;
177177

178+
/**
179+
Maximum width of the element.
180+
*/
181+
readonly maxWidth?: number | string;
182+
183+
/**
184+
Maximum height of the element.
185+
*/
186+
readonly maxHeight?: number | string;
187+
178188
/**
179189
Set this property to `none` to hide the element.
180190
*/
@@ -540,6 +550,22 @@ const applyDimensionStyles = (node: YogaNode, style: Styles): void => {
540550
node.setMinHeight(style.minHeight ?? 0);
541551
}
542552
}
553+
554+
if ('maxWidth' in style) {
555+
if (typeof style.maxWidth === 'string') {
556+
node.setMaxWidthPercent(Number.parseInt(style.maxWidth, 10));
557+
} else {
558+
node.setMaxWidth(style.maxWidth);
559+
}
560+
}
561+
562+
if ('maxHeight' in style) {
563+
if (typeof style.maxHeight === 'string') {
564+
node.setMaxHeightPercent(Number.parseInt(style.maxHeight, 10));
565+
} else {
566+
node.setMaxHeight(style.maxHeight);
567+
}
568+
}
543569
};
544570

545571
const applyDisplayStyles = (node: YogaNode, style: Styles): void => {

test/scroll.tsx

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,3 +529,62 @@ test('interactive scroll', async t => {
529529
const afterScrollRight = (stdout.write as any).lastCall.args[0] as string;
530530
t.snapshot(afterScrollRight, 'after scrolling right');
531531
});
532+
533+
const maxDimensionsScrollTests = [
534+
{
535+
name: 'max-width',
536+
props: {
537+
maxWidth: 10
538+
}
539+
},
540+
{
541+
name: 'max-height',
542+
props: {
543+
maxHeight: 5
544+
}
545+
},
546+
{
547+
name: 'max-width-percent',
548+
props: {
549+
maxWidth: '50%'
550+
}
551+
},
552+
{
553+
name: 'max-height-percent',
554+
props: {
555+
maxHeight: '50%'
556+
}
557+
},
558+
{
559+
name: 'max-width-and-max-height',
560+
props: {
561+
maxWidth: 10,
562+
maxHeight: 5
563+
}
564+
},
565+
{
566+
name: 'max-width-and-max-height-percents',
567+
props: {
568+
maxWidth: '20%',
569+
maxHeight: '20%'
570+
}
571+
}
572+
];
573+
574+
for (const {name, props} of maxDimensionsScrollTests) {
575+
test(name, t => {
576+
const output = renderToString(
577+
<Box
578+
overflow="scroll"
579+
borderStyle="round"
580+
{...props}
581+
>
582+
<Box width={100} height={100} flexShrink={0}>
583+
<Text>Scroll me</Text>
584+
</Box>
585+
</Box>
586+
);
587+
588+
t.snapshot(output);
589+
});
590+
}

0 commit comments

Comments
 (0)