Skip to content

Commit 44bf4c6

Browse files
committed
use popovertarget mixin
1 parent c073cda commit 44bf4c6

File tree

3 files changed

+19
-42
lines changed

3 files changed

+19
-42
lines changed

packages/uui-base/lib/mixins/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './LabelMixin';
33
export * from './SelectableMixin';
44
export * from './SelectOnlyMixin';
55
export * from './FormControlMixin';
6+
export * from './PopoverTargetMixin';

packages/uui-button/lib/uui-button.element.ts

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {
22
UUIHorizontalShakeAnimationValue,
33
UUIHorizontalShakeKeyframes,
44
} from '@umbraco-ui/uui-base/lib/animations';
5+
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
56
import {
6-
demandCustomElement,
7-
findAncestorByAttributeValue,
8-
} from '@umbraco-ui/uui-base/lib/utils';
9-
import { FormControlMixin, LabelMixin } from '@umbraco-ui/uui-base/lib/mixins';
7+
FormControlMixin,
8+
LabelMixin,
9+
PopoverTargetMixin,
10+
} from '@umbraco-ui/uui-base/lib/mixins';
1011
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
1112
import {
1213
iconCheck,
@@ -46,7 +47,7 @@ export type UUIButtonType = 'submit' | 'button' | 'reset';
4647
*/
4748
@defineElement('uui-button')
4849
export class UUIButtonElement extends FormControlMixin(
49-
LabelMixin('', LitElement)
50+
LabelMixin('', PopoverTargetMixin(LitElement))
5051
) {
5152
static styles = [
5253
UUIHorizontalShakeKeyframes,
@@ -418,15 +419,6 @@ export class UUIButtonElement extends FormControlMixin(
418419
@property({ type: String })
419420
public href?: string;
420421

421-
/**
422-
* Set a popovertarget.
423-
* @type {string}
424-
* @attr
425-
* @default undefined
426-
*/
427-
@property({ type: String, attribute: 'popovertarget' })
428-
public popoverContainerElement?: string;
429-
430422
/**
431423
* Set an anchor tag target, only used when using href.
432424
* @type {string}
@@ -439,12 +431,9 @@ export class UUIButtonElement extends FormControlMixin(
439431
@query('#button')
440432
protected _button!: HTMLInputElement;
441433

442-
#popoverIsOpen = false;
443-
444434
constructor() {
445435
super();
446436
this.addEventListener('click', this._onHostClick);
447-
this.addEventListener('uui-popover-before-toggle', this.#popoverListener);
448437
}
449438

450439
protected getFormElement(): HTMLElement {
@@ -475,7 +464,7 @@ export class UUIButtonElement extends FormControlMixin(
475464
}
476465
}
477466

478-
this.#updatePopover();
467+
this._togglePopover();
479468
}
480469

481470
private _resetStateTimeout?: number;
@@ -520,30 +509,6 @@ export class UUIButtonElement extends FormControlMixin(
520509
return html`<div id="state">${element}</div>`;
521510
}
522511

523-
#popoverListener = (event: any) => {
524-
// Wait for the click event to finish before updating the popover state
525-
requestAnimationFrame(() => {
526-
this.#popoverIsOpen = event.detail.newState === 'open';
527-
});
528-
};
529-
530-
#updatePopover = () => {
531-
if (!this.popoverContainerElement) return;
532-
533-
const popoverContainerElement = findAncestorByAttributeValue(
534-
this,
535-
'id',
536-
this.popoverContainerElement
537-
);
538-
if (!popoverContainerElement) return;
539-
540-
this.#popoverIsOpen
541-
? // @ts-ignore - This is part of the new popover API, but typescript doesn't recognize it yet.
542-
popoverContainerElement.hidePopover()
543-
: // @ts-ignore - This is part of the new popover API, but typescript doesn't recognize it yet.
544-
popoverContainerElement.showPopover();
545-
};
546-
547512
render() {
548513
return this.href
549514
? html`

packages/uui-popover-container/lib/uui-popover-container.element.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ export class UUIPopoverContainerElement extends LitElement {
7272
connectedCallback(): void {
7373
super.connectedCallback();
7474

75+
this.addEventListener('focusout', this.#onFocusOut);
76+
7577
// CHECK BROWSER SUPPORT
7678
if (!HTMLElement.prototype.hasOwnProperty('popover')) {
7779
alert(
@@ -89,6 +91,15 @@ export class UUIPopoverContainerElement extends LitElement {
8991
this.removeEventListener('beforetoggle', this.#onBeforeToggle);
9092
}
9193

94+
#onFocusOut = (event: FocusEvent) => {
95+
// If focus is outside of the container, then the popover will close.
96+
if (!event.relatedTarget || !this.contains(event.relatedTarget as Node)) {
97+
// @ts-ignore - This is part of the new popover API, but typescript doesn't recognize it yet.
98+
this.hidePopover();
99+
this._open = false;
100+
}
101+
};
102+
92103
#onBeforeToggle = async (event: any) => {
93104
this._open = event.newState === 'open';
94105

0 commit comments

Comments
 (0)