Skip to content

Commit

Permalink
chore(Accordion): fix defaultOpen flicker on first render
Browse files Browse the repository at this point in the history
  • Loading branch information
Barsnes committed Aug 8, 2024
1 parent 89f959d commit 48b688f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/react/src/components/Accordion/Accordion.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default {

export const Preview: StoryFn<typeof Accordion.Root> = (args) => (
<Accordion.Root {...args}>
<Accordion.Item>
<Accordion.Item defaultOpen>
<Accordion.Heading level={3}>
Hvem kan registrere seg i Frivillighetsregisteret?
</Accordion.Heading>
Expand All @@ -26,7 +26,7 @@ export const Preview: StoryFn<typeof Accordion.Root> = (args) => (
til fysiske personer. Virksomheten må ha et styre.
</Accordion.Content>
</Accordion.Item>
<Accordion.Item>
<Accordion.Item defaultOpen>
<Accordion.Heading level={3}>
Hvordan går jeg fram for å registrere i Frivillighetsregisteret?
</Accordion.Heading>
Expand Down
5 changes: 4 additions & 1 deletion packages/react/src/utilities/AnimateHeight/AnimateHeight.tsx
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

0 comments on commit 48b688f

Please sign in to comment.