Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Accordion): fix defaultOpen flicker on first render #2214

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/chilled-icons-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@digdir/designsystemet-react": patch
---

Accordion: Fix `defaultOpen` flicker on first render
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('AnimateHeight', () => {
it('Appends given style to root element', () => {
const style = { color: 'rgb(255, 0, 0)' };
const { container } = render({ style });
expect(container.firstChild).toHaveStyle({ height: 0 });
expect(container.firstChild).toHaveStyle({ height: undefined });
expect(container.firstChild).toHaveStyle(style);
});

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