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
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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

"type": "prerelease",
"comment": "add support for popovertarget and popovertargetaction in button web component",
"packageName": "@fluentui/web-components",
"email": "[email protected]",
"dependentChangeType": "patch"
}
7 changes: 7 additions & 0 deletions packages/web-components/src/button/button.stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,10 @@ export const ResetAndSubmitButtonsInForm: Story<FluentButton> = renderComponent(
<div id="something">Div Label</div>
<output id="output"></output>
`);

export const Popover: Story<FluentButton> = renderComponent(html<StoryArgs<FluentButton>>`
<fluent-button popovertarget="foo" popovertargetaction="show">Show Popover</fluent-button>
<fluent-button popovertarget="foo" popovertargetaction="hide">Hide Popover</fluent-button>
<fluent-button popovertarget="foo" popovertargetaction="toggle">Toggle Popover</fluent-button>
<div id="foo" popover open>This is a popover</div>
`);
76 changes: 75 additions & 1 deletion packages/web-components/src/button/button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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...


/**
* Holds an element reference for the popovertarget
*/
private popoverTargetElement: HTMLElement | null | undefined;

/**
* Indicates the button should be focused when the page is loaded.
* @see The {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#autofocus | `autofocus`} attribute
Expand All @@ -29,6 +36,28 @@ export class BaseButton extends FASTElement {
@attr({ mode: 'boolean' })
public autofocus!: boolean;

/**
* The ID for the popover which is controlled by the element
* @see The {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#popovertarget | `popovertarget`} attribute
*
* @public
* @remarks
* HTML Attribute: `popovertarget`
*/
@attr
public popovertarget?: string;

/**
* Specifies the action to be performed on a popover element being controlled by the element
* @see The {@link https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#popovertargetaction | `popovertargetaction`} attribute
*
* @public
* @remarks
* 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.


/**
* Default slotted content.
*
Expand Down Expand Up @@ -68,6 +97,17 @@ export class BaseButton extends FASTElement {
@attr({ attribute: 'tabindex', mode: 'fromView', converter: nullableNumberConverter })
public override tabIndex: number = 0;

/**
* Handles changes to the popovertarget attribute
*
* @param previous - the previous popovertarget value
* @param next - the current popovertarget value
* @internal
*/
public popoverTargetChanged(previous: string | undefined, next: string | undefined): void {
this.setPopoverTargetElement(next);
}

/**
* Sets the element's internal disabled state when the element is focusable while disabled.
*
Expand Down Expand Up @@ -254,20 +294,54 @@ export class BaseButton extends FASTElement {
return;
}

if (this.popoverTargetElement) {
switch (this.popovertargetaction) {
case 'hide':
this.popoverTargetElement?.hidePopover();
break;
case 'show':
this.popoverTargetElement?.showPopover();
break;
case 'toggle':
default:
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.

break;
}
}

this.press();
return true;
}

connectedCallback(): void {
public connectedCallback(): void {
super.connectedCallback();
this.elementInternals.ariaDisabled = `${!!this.disabledFocusable}`;

if (this.popovertarget) {
this.setPopoverTargetElement(this.popovertarget);
}
}

constructor() {
super();
this.elementInternals.role = 'button';
}

/**
* Sets up handling for the popover target element
* @param element - The element to reference
*/
private setPopoverTargetElement(element: string | undefined) {
this.popoverTargetElement = element ? document.getElementById(element) : undefined;

this.popoverTargetElement?.addEventListener(
'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.

);
}

/**
* This fallback creates a new slot, then creates a submit button to mirror the custom element's
* properties. The submit button is then appended to the slot and the form is submitted.
Expand Down
Loading