-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
base: master
Are you sure you want to change the base?
feat(web-components): add support for popovertarget
and popovertargetaction
to button
#32397
Conversation
* HTML Attribute: `popovertargetaction` | ||
*/ | ||
@attr | ||
public popovertargetaction?: 'hide' | 'show' | 'toggle'; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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'), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
📊 Bundle size report✅ No changes found |
case 'toggle': | ||
default: | ||
// @ts-expect-error - Baseline 2024 | ||
this.popoverTargetElement?.togglePopover(!this.popoverOpen); |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
829f8ed
to
9bcb26c
Compare
@@ -0,0 +1,7 @@ | |||
{ |
There was a problem hiding this comment.
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 |
Previous Behavior
No support for declarative popovers
New Behavior
While custom elements currently cannot take advantage of the
popovertarget
andpopovertargetaction
API's, this PR adds capabilities to replicate the behavior to provide basic support for declarative popovers.Related Issue(s)