Skip to content

Commit d6b1ea2

Browse files
committed
fix: Blink when values changed
1 parent 94a2148 commit d6b1ea2

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

assets/index.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@overflow-prefix-cls: rc-overflow;
22

33
.@{overflow-prefix-cls} {
4-
display: inline-flex;
4+
display: flex;
55
flex-wrap: wrap;
66
max-width: 100%;
77

examples/basic.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function renderRest(items: ItemType[]) {
4747

4848
const Demo = () => {
4949
const [responsive, setResponsive] = React.useState(true);
50-
const [data, setData] = React.useState(createData(5));
50+
const [data, setData] = React.useState(createData(1));
5151

5252
return (
5353
<div style={{ padding: 32 }}>

examples/blink.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ const data = createData(5);
4141
const Demo = () => {
4242
return (
4343
<div style={{ padding: 32 }}>
44+
<p>
45+
Test for a edge case that rest can not decide the final display count
46+
</p>
4447
<div
4548
style={{
4649
boxShadow: '0 0 1px green',

src/Overflow.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ function Overflow<ItemType = any>(
4848
const [itemWidths, setItemWidths] = createUseState(
4949
new Map<React.Key, number>(),
5050
);
51+
52+
const [prevRestWidth, setPrevRestWidth] = createUseState(0);
5153
const [restWidth, setRestWidth] = createUseState(0);
54+
// Always use the max width to avoid blink
55+
const mergedRestWidth = Math.max(prevRestWidth, restWidth);
5256

5357
const [displayCount, setDisplayCount] = React.useState(0);
5458
const [restReady, setRestReady] = React.useState(false);
@@ -123,11 +127,12 @@ function Overflow<ItemType = any>(
123127

124128
function registerOverflowSize(_: React.Key, width: number) {
125129
setRestWidth(width);
130+
setPrevRestWidth(restWidth);
126131
}
127132

128133
// ================================ Effect ================================
129134
React.useLayoutEffect(() => {
130-
if (containerWidth && restWidth && mergedData) {
135+
if (containerWidth && mergedRestWidth && mergedData) {
131136
let totalWidth = 0;
132137

133138
const len = mergedData.length;
@@ -144,7 +149,7 @@ function Overflow<ItemType = any>(
144149
// Find best match
145150
totalWidth += currentItemWidth;
146151

147-
if (totalWidth + restWidth > containerWidth) {
152+
if (totalWidth + mergedRestWidth > containerWidth) {
148153
updateDisplayCount(i - 1);
149154
break;
150155
} else if (i === len - 1) {
@@ -153,7 +158,7 @@ function Overflow<ItemType = any>(
153158
}
154159
}
155160
}
156-
}, [containerWidth, itemWidths, restWidth, getKey, mergedData]);
161+
}, [containerWidth, itemWidths, mergedRestWidth, getKey, mergedData]);
157162

158163
// ================================ Render ================================
159164
let overflowNode = (

0 commit comments

Comments
 (0)