Skip to content

Commit 860a15f

Browse files
committed
use the utils function in uui button and popover container
1 parent 433dbf8 commit 860a15f

File tree

2 files changed

+7
-53
lines changed

2 files changed

+7
-53
lines changed

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

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import {
22
UUIHorizontalShakeAnimationValue,
33
UUIHorizontalShakeKeyframes,
44
} from '@umbraco-ui/uui-base/lib/animations';
5-
import { demandCustomElement } from '@umbraco-ui/uui-base/lib/utils';
5+
import {
6+
demandCustomElement,
7+
findAncestorByAttributeValue,
8+
} from '@umbraco-ui/uui-base/lib/utils';
69
import { FormControlMixin, LabelMixin } from '@umbraco-ui/uui-base/lib/mixins';
710
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
811
import {
@@ -527,7 +530,7 @@ export class UUIButtonElement extends FormControlMixin(
527530
#updatePopover = () => {
528531
if (!this.popoverContainerElement) return;
529532

530-
const popoverContainerElement = this.#findAncestorWithAttribute(
533+
const popoverContainerElement = findAncestorByAttributeValue(
531534
this,
532535
'id',
533536
this.popoverContainerElement
@@ -541,31 +544,6 @@ export class UUIButtonElement extends FormControlMixin(
541544
popoverContainerElement.showPopover();
542545
};
543546

544-
#findAncestorWithAttribute(
545-
element: HTMLElement,
546-
attributeName: string,
547-
attributeValue: string
548-
) {
549-
while (element !== null && element.parentElement !== null) {
550-
element = element.parentElement;
551-
552-
const elementHasAttribute =
553-
element.hasAttribute(attributeName) &&
554-
element.getAttribute(attributeName) === attributeValue;
555-
const elementContainsAttribute =
556-
element.querySelector(`[${attributeName}="${attributeValue}"]`) !==
557-
null;
558-
if (elementHasAttribute) {
559-
return element;
560-
} else if (elementContainsAttribute) {
561-
return element.querySelector(
562-
`[${attributeName}="${attributeValue}"]`
563-
) as HTMLElement;
564-
}
565-
}
566-
return null;
567-
}
568-
569547
render() {
570548
return this.href
571549
? html`

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

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
2+
import { findAncestorByAttributeValue } from '@umbraco-ui/uui-base/lib/utils';
23
import { css, html, LitElement } from 'lit';
34
import { property, state } from 'lit/decorators.js';
45

@@ -91,7 +92,7 @@ export class UUIPopoverContainerElement extends LitElement {
9192
#onBeforeToggle = async (event: any) => {
9293
this._open = event.newState === 'open';
9394

94-
this.#targetElement = this.#findAncestorWithAttribute(
95+
this.#targetElement = findAncestorByAttributeValue(
9596
this,
9697
'popovertarget',
9798
this.id
@@ -306,31 +307,6 @@ export class UUIPopoverContainerElement extends LitElement {
306307
`${oppositeDirection}-${position}` as PopoverContainerPlacement;
307308
}
308309

309-
#findAncestorWithAttribute(
310-
element: HTMLElement,
311-
attributeName: string,
312-
attributeValue: string
313-
) {
314-
while (element !== null && element.parentElement !== null) {
315-
element = element.parentElement;
316-
317-
const elementHasAttribute =
318-
element.hasAttribute(attributeName) &&
319-
element.getAttribute(attributeName) === attributeValue;
320-
const elementContainsAttribute =
321-
element.querySelector(`[${attributeName}="${attributeValue}"]`) !==
322-
null;
323-
if (elementHasAttribute) {
324-
return element;
325-
} else if (elementContainsAttribute) {
326-
return element.querySelector(
327-
`[${attributeName}="${attributeValue}"]`
328-
) as HTMLElement;
329-
}
330-
}
331-
return null;
332-
}
333-
334310
render() {
335311
return html`<slot></slot>`;
336312
}

0 commit comments

Comments
 (0)