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

feat(web-components): add support for popovertarget and popovertargetaction to button #32397

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

chrisdholt
Copy link
Member

Previous Behavior

No support for declarative popovers

New Behavior

While custom elements currently cannot take advantage of the popovertarget and popovertargetaction API's, this PR adds capabilities to replicate the behavior to provide basic support for declarative popovers.

Related Issue(s)

  • Fixes #

* HTML Attribute: `popovertargetaction`
*/
@attr
public popovertargetaction?: 'hide' | 'show' | 'toggle';
Copy link
Member Author

Choose a reason for hiding this comment

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

Create a type for this that can be used.

@@ -18,6 +18,13 @@ import { ButtonType } from './button.options.js';
* @public
*/
export class BaseButton extends FASTElement {
private popoverOpen?: boolean = false;
Copy link
Member Author

Choose a reason for hiding this comment

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

Comments...

'toggle',
e =>
(this.popoverOpen =
(e as Event & { newState: 'open' | 'closed'; oldState: 'open' | 'closed' }).newState === 'open'),
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't love this but in the case of toggling the popover, it seems like we need to force it closed if it is open.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I got the same issue with Dropdown, if the invoker element is clicked, first the popover element would close since a click event happened outside of it, then the click event fires on the invoker, which is too late to prevent any default behavior. +1 to this solution.

@fabricteam
Copy link
Collaborator

fabricteam commented Aug 27, 2024

📊 Bundle size report

✅ No changes found

case 'toggle':
default:
// @ts-expect-error - Baseline 2024
this.popoverTargetElement?.togglePopover(!this.popoverOpen);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason we can't use this.popoverTargetElement?.matches(":popover-open")?

I don't know if we want to be in the business of mirroring state here when the browser gives us a pseudo-state for free. We could also wouldn't need the eventListener in setPopoverTargetElement.

https://developer.mozilla.org/en-US/docs/Web/API/Popover_API/Using#:~:text=%7B%0A%20%20%20%20if%20(-,popover.matches(%22%3Apopover%2Dopen%22),-)%20%7B%0A%20%20%20%20%20%20popover

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, I like this way better.

Copy link
Contributor

Choose a reason for hiding this comment

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

@davatron5000 I think this is to cover the case that, when the popover is open, if you click the button again, it won't close the popover. Because when you click the button, the popover closes (click event outside of the popover element), and .match(':popover-open') is false, if it only checks :popover-open, the button click will open the popover again. This is handled nicely by browser if the invoker is a <button> or <input>, but we have to do it for other elements.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, unfortunately this wasn't working for me when I tried to implement it.

I'm open to alternative approaches here.

Copy link
Contributor

Choose a reason for hiding this comment

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

I went into a bit of rabbit before the end of day yesterday (because I was trying to fix this issue for Dropdown as well), and created this codepen: https://codepen.io/marchbox/pen/ExBOVvE. The event order for an artificial popover invoker (non-button/input) is a bit odd, but it's consistent across Edge, Firefox, and Safari. I haven't figure out what would be the best to mitigate this issue though. Using popover=manual would close the popover when the invoker is clicked again, but we have to manage to close them manually.

Copy link
Contributor

Choose a reason for hiding this comment

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

@chrisdholt I think your solution here works, only thing is you need to set this.popoverOpen after togglePopover(), so:

if (this.popoverTargetElement && !!this.popoverTargetElement.hasAttribute('popover')) {
  const nextPopoverOpen = !this.popoverOpen;
  switch (this.popovertargetaction) {
    case 'hide':
      this.popoverTargetElement?.hidePopover();
      this.popoverOpen = false;
      break;
    case 'show':
      this.popoverTargetElement?.showPopover();
      this.popoverOpen = true;
      break;
    default:
      const nextPopoverOpen = !this.popoverOpen;
      this.popoverTargetElement?.togglePopover(nextPopoverOpen);
      this.popoverOpen = nextPopoverOpen;
  }
}

The key is NOT to update this.popoverOpen inside a beforetoggle event, which your code currently doesn't.

@chrisdholt chrisdholt force-pushed the users/chhol/add-popover-support-to-button branch from 829f8ed to 9bcb26c Compare August 28, 2024 17:55
@@ -0,0 +1,7 @@
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

🕵🏾‍♀️ visual regressions to review in the fluentui-web-components-v3 Visual Regression Report

Avatar 1 screenshots
Image Name Diff(in Pixels) Image Type
Avatar.Image.chromium.png 248 Changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants