Skip to content

Conversation

@unekinn
Copy link
Contributor

@unekinn unekinn commented Nov 11, 2025

Summary

Popover: Fix unnecesary call of onOpen and missing call of onClose

  • Don't call onOpen when clicking Popover.Trigger when Popover is already open.
  • Call onClose when a controlled Popover is closed by clicking on Popover.Trigger.

Checks

@changeset-bot
Copy link

changeset-bot bot commented Nov 11, 2025

🦋 Changeset detected

Latest commit: 4ab3095

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@digdir/designsystemet-react Patch
@digdir/designsystemet Patch
@digdir/designsystemet-css Patch
@digdir/designsystemet-theme Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Nov 11, 2025

Preview deployments for this pull request:

storybook - 21. Nov 2025 - 14:18

@unekinn unekinn force-pushed the fix/popover-onOpen-when-open branch from 7fd7b2d to cd112c2 Compare November 11, 2025 10:16
@unekinn unekinn force-pushed the fix/popover-onOpen-when-open branch from cd112c2 to dfdee89 Compare November 11, 2025 10:34
event.preventDefault(); // Prevent native Popover API
setInternalOpen((open) => !open);
onOpen?.();
if (!controlledOpen) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but to make it more readable, could we swap the ifs, so we can do if (controlledOpen)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you thinking something like this? Not sure if it's actually more readable, but at least we only have the close logic once in this version.

        if (controlledOpen) {
          if (isTrigger) {
            event.preventDefault(); // Prevent native Popover API
          }
          if (isTrigger || isOutside) {
            setInternalOpen(false);
            onClose?.();
          }
        } else if (isTrigger) {
          event.preventDefault(); // Prevent native Popover API
          setInternalOpen(true);
          onOpen?.();
        }

Copy link
Contributor Author

@unekinn unekinn Nov 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe like this

        if (isTrigger) {
          event.preventDefault(); // Prevent native Popover API
        }
        if (controlledOpen && (isTrigger || isOutside)) {
          setInternalOpen(false);
          onClose?.();
        } else if (isTrigger) {
          setInternalOpen(true);
          onOpen?.();
        }
      };

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Liked your last suggestion, went ahead and added it to the PR

@Barsnes
Copy link
Member

Barsnes commented Nov 21, 2025

I'll make sure to fix this before next release 😄

@Barsnes Barsnes merged commit ba0fc4a into main Nov 24, 2025
16 checks passed
@Barsnes Barsnes deleted the fix/popover-onOpen-when-open branch November 24, 2025 07:06
@github-actions github-actions bot mentioned this pull request Nov 21, 2025
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.

3 participants