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

Fix crash when using DisclosureButton inside of a DisclosurePanel when the Disclosure is open by default #3465

Merged
merged 3 commits into from
Sep 9, 2024

Conversation

RobinMalfait
Copy link
Member

This PR fixes an issue where React hooks were called unconditionally>

The PopoverButton and DisclosureButton act as a CloseButton when used inside of a panel. We conditionally handled the ref when it's inside a panel. To ensure that the callback is stable, the conditionally used function was wrapped in a useEvent(…) hook.

This seemed to be ok (even though we break the rules of hooks) because a button can only be in a panel or not be in a panel, but it can't switch during the lifetime of the button. Aka, the rules of hooks are not broken because all code paths lead to the same hooks being called.

<Disclosure defaultOpen>
  <DisclosureButton>Open</DisclosureButton>
  <DisclosurePanel>
    <DisclosureButton>Close</DisclosureButton>
  </DisclosurePanel>
<Disclosure>

But... it can be called conditionally, because the way we know whether we are in a panel relies on a state value which comes from context and is populated by a useEffect(…) hook.

The reason we didn't catch this in the Disclosure component, is because all the state is stable and known by the time the DisclosurePanel opens. But if you use the defaultOpen prop, the DisclosurePanel is already open and then the state is not ready yet (because we have to wait for the useEffect(…) hook).

Long story short, moved the isWithinPanel check inside the useEvent(…) hook that holds the stable function which means that we don't call this hook unconditionally anymore.

Copy link

vercel bot commented Sep 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
headlessui-react ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 9, 2024 7:12pm
headlessui-vue ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 9, 2024 7:12pm

The `PopoverButton` and `DisclosureButton` act as a `CloseButton` when
used inside of a panel. We conditionally handled the `ref` when it's
inside a panel. We wanted to make sure that the callback is stable and
used `useEvent`.

This seemed to be ok (even though we break the rules of hooks) because a
button can only be in a panel or not be in a panel, but it can't switch
during the lifetime of the button.

But it can, because the way we know whether we are in a Panel relies
on a state value which comes from context.

The reason we didn't catch this in the `Disclosure`, is because all the
state is stable and known by the time the `Panel` opens. But if you use
the `defaultOpen` prop, it is already open and then the state is not
ready yet.

Long story short, moved the `isWithinPanel` check inside the
`useEvent(…)` hook that holds the stable function which means that we
don't call this hook unconditionally anymore.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant