Skip to content

Commit 1150d5f

Browse files
Barsnesmimarz
authored andcommitted
chore(Accordion): fix defaultOpen flicker on first render (#2214)
resolves #2178
1 parent f404d9e commit 1150d5f

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

.changeset/chilled-icons-chew.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@digdir/designsystemet-react": patch
3+
---
4+
5+
Accordion: Fix `defaultOpen` flicker on first render

packages/react/src/utilities/AnimateHeight/AnimateHeight.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('AnimateHeight', () => {
3535
it('Appends given style to root element', () => {
3636
const style = { color: 'rgb(255, 0, 0)' };
3737
const { container } = render({ style });
38-
expect(container.firstChild).toHaveStyle({ height: 0 });
38+
expect(container.firstChild).toHaveStyle({ height: undefined });
3939
expect(container.firstChild).toHaveStyle(style);
4040
});
4141

packages/react/src/utilities/AnimateHeight/AnimateHeight.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ export const AnimateHeight = ({
2222
style,
2323
...rest
2424
}: AnimateHeightProps) => {
25-
const [height, setHeight] = useState<number>(0);
25+
/* We don't know the initial height we want to start with.
26+
It depends on if it should start open or not, therefore we set height to `undefined`,
27+
so we don't get any layoutshift on first render */
28+
const [height, setHeight] = useState<number | undefined>(undefined);
2629
const prevOpen = usePrevious(open);
2730
const openOrClosed: InternalState = open ? 'open' : 'closed';
2831
const [state, setState] = useState<InternalState>(openOrClosed);

0 commit comments

Comments
 (0)