Skip to content

Commit 515a3b0

Browse files
committed
Fix border collapsing when borderStyle changes
1 parent e664cea commit 515a3b0

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

src/reconciler.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,30 @@ type UpdatePayload = {
9595
style: Styles | undefined;
9696
};
9797

98+
const finalizeChangedStyle = (
99+
changedStyle: Styles | undefined,
100+
currentStyle: Styles | undefined,
101+
): Styles | undefined => {
102+
if (!changedStyle) {
103+
return;
104+
}
105+
106+
if (!currentStyle) {
107+
return changedStyle;
108+
}
109+
110+
const style = {...changedStyle};
111+
112+
if ('borderStyle' in style) {
113+
style.borderTop = currentStyle.borderTop;
114+
style.borderRight = currentStyle.borderRight;
115+
style.borderBottom = currentStyle.borderBottom;
116+
style.borderLeft = currentStyle.borderLeft;
117+
}
118+
119+
return style;
120+
};
121+
98122
export default createReconciler<
99123
ElementNames,
100124
Props,
@@ -254,15 +278,20 @@ export default createReconciler<
254278

255279
const props = diff(oldProps, newProps);
256280

257-
const style = diff(
281+
const changedStyle = diff(
258282
oldProps['style'] as Styles,
259283
newProps['style'] as Styles,
260284
);
261285

262-
if (!props && !style) {
286+
if (!props && !changedStyle) {
263287
return null;
264288
}
265289

290+
const style = finalizeChangedStyle(
291+
changedStyle as Styles,
292+
newProps['style'] as Styles,
293+
);
294+
266295
return {props, style};
267296
},
268297
commitUpdate(node, {props, style}) {

test/borders.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import boxen, {type Options} from 'boxen';
44
import indentString from 'indent-string';
55
import delay from 'delay';
66
import widestLine from 'widest-line';
7-
import cliBoxes from 'cli-boxes';
7+
import cliBoxes, {type Boxes} from 'cli-boxes';
88
import chalk from 'chalk';
99
import {render, Box, Text} from '../src/index.js';
1010
import {renderToString} from './helpers/render-to-string.js';
@@ -613,6 +613,35 @@ test('hide all borders', t => {
613613
t.is(output, ['Above', 'Content', 'Below'].join('\n'));
614614
});
615615

616+
test('keep borders hidden on `borderStyle` change', async t => {
617+
const stdout = createStdout();
618+
619+
function Test() {
620+
const [state, setState] = useState<keyof Boxes>('single');
621+
622+
useEffect(() => {
623+
setState('double');
624+
}, []);
625+
626+
return (
627+
<Box
628+
borderStyle={state}
629+
borderTop={false}
630+
borderBottom={false}
631+
borderLeft={false}
632+
borderRight={false}
633+
>
634+
<Text>Content</Text>
635+
</Box>
636+
);
637+
}
638+
639+
render(<Test />, {stdout, debug: true});
640+
t.is((stdout.write as any).firstCall.args[0], 'Content');
641+
await delay(100);
642+
t.is((stdout.write as any).lastCall.args[0], 'Content');
643+
});
644+
616645
test('change color of top border', t => {
617646
const output = renderToString(
618647
<Box flexDirection="column" alignItems="flex-start">

0 commit comments

Comments
 (0)