Skip to content

Commit 0b56e60

Browse files
committed
AB#17181 Convert defineElement to decorator and use in all elements and tests
1 parent b786b41 commit 0b56e60

File tree

72 files changed

+212
-56
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+212
-56
lines changed

packages/uui-action-bar/lib/uui-action-bar.element.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
12
import { UUIButtonGroupElement } from '@umbraco-ui/uui-button-group/lib';
23
import { css } from 'lit';
34

45
/**
56
* @element uui-action-bar
67
* @description - Extends button group to hold buttons with icons that represent actions.
78
*/
9+
@defineElement('uui-action-bar')
810
export class UUIActionBarElement extends UUIButtonGroupElement {
911
static styles = [
1012
...UUIButtonGroupElement.styles,

packages/uui-action-bar/lib/uui-action-bar.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { html, fixture, expect } from '@open-wc/testing';
1+
import { expect, fixture, html } from '@open-wc/testing';
2+
23
import { UUIActionBarElement } from './uui-action-bar.element';
3-
import '.';
44

55
describe('UUIActionBarElement', () => {
66
let element: UUIActionBarElement;
@@ -9,6 +9,10 @@ describe('UUIActionBarElement', () => {
99
element = await fixture(html` <uui-action-bar></uui-action-bar> `);
1010
});
1111

12+
it('is defined', () => {
13+
expect(element).to.be.instanceOf(UUIActionBarElement);
14+
});
15+
1216
it('passes the a11y audit', async () => {
1317
await expect(element).shadowDom.to.be.accessible();
1418
});

packages/uui-avatar-group/lib/uui-avatar-group.element.ts

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

@@ -7,6 +8,7 @@ import { property, queryAssignedElements, state } from 'lit/decorators.js';
78
* @element uui-avatar-group
89
* @slot for uui-avatar elements
910
*/
11+
@defineElement('uui-avatar-group')
1012
export class UUIAvatarGroupElement extends LitElement {
1113
static styles = [
1214
css`

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

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

45
/**
56
* Avatar for displaying users
67
* @element uui-avatar
78
* @slot For anything other than initials (no more than 2-3 characters)
89
*/
10+
@defineElement('uui-avatar')
911
export class UUIAvatarElement extends LitElement {
1012
static styles = [
1113
css`

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

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

45
import { InterfaceLookType } from '@umbraco-ui/uui-base/lib/types';
@@ -8,6 +9,7 @@ import { InterfaceLookType } from '@umbraco-ui/uui-base/lib/types';
89
* @element uui-badge
910
* @slot - for badge contents
1011
*/
12+
@defineElement('uui-badge')
1113
export class UUIBadgeElement extends LitElement {
1214
static styles = [
1315
css`

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55
* @param {string} constructor - Constructor for the new custom element.
66
* @param {string} [options] - Object that controls how the element is defined.
77
*/
8-
export const defineElement = (
8+
export function defineElement(
99
name: string,
10-
constructor: CustomElementConstructor,
1110
options?: ElementDefinitionOptions | undefined
12-
) => {
13-
const isValidElementName = name.indexOf('-') > 0;
11+
) {
12+
return (constructor: CustomElementConstructor) => {
13+
const isValidElementName = name.indexOf('-') > 0;
1414

15-
if (isValidElementName === false) {
16-
console.error(
17-
`${name} is not a valid custom element name. A custom element name should consist of at least two words separated by a hyphen.`
18-
);
19-
return;
20-
}
15+
if (isValidElementName === false) {
16+
console.error(
17+
`${name} is not a valid custom element name. A custom element name should consist of at least two words separated by a hyphen.`
18+
);
19+
return;
20+
}
2121

22-
if (customElements.get(name)) {
23-
console.error(`${name} is already defined`);
24-
} else {
25-
customElements.define(name, constructor, options);
26-
}
27-
};
22+
const existingElement = window.customElements.get(name);
23+
24+
if (existingElement) {
25+
console.warn(`${name} is already defined`, existingElement); // TODO: Remove this in production
26+
} else {
27+
window.customElements.define(name, constructor, options);
28+
}
29+
};
30+
}

packages/uui-boolean-input/lib/uui-boolean-input.element.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import { LitElement, html, css, TemplateResult } from 'lit';
2-
import { property, query } from 'lit/decorators.js';
31
import { FormControlMixin, LabelMixin } from '@umbraco-ui/uui-base/lib/mixins';
2+
import { css, html, LitElement, TemplateResult } from 'lit';
3+
import { property, query } from 'lit/decorators.js';
4+
45
import { UUIBooleanInputEvent } from './UUIBooleanInputEvent';
56

67
type LabelPosition = 'left' | 'right' | 'top' | 'bottom';
@@ -208,9 +209,3 @@ export abstract class UUIBooleanInputElement extends FormControlMixin(
208209
`;
209210
}
210211
}
211-
212-
declare global {
213-
interface HTMLElementTagNameMap {
214-
'uui-boolean-input': UUIBooleanInputElement;
215-
}
216-
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { LitElement, html, css } from 'lit';
2+
import { defineElement } from '@umbraco-ui/uui-base/lib/registration';
23

34
/**
45
* A box for grouping elements
@@ -8,6 +9,7 @@ import { LitElement, html, css } from 'lit';
89
* @slot area with no padding
910
*
1011
*/
12+
@defineElement('uui-box')
1113
export class UUIBoxElement extends LitElement {
1214
static styles = [
1315
css`

packages/uui-box/lib/uui-box.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { html, fixture, expect } from '@open-wc/testing';
2-
import './index';
1+
import { expect, fixture, html } from '@open-wc/testing';
2+
33
import { UUIBoxElement } from './uui-box.element';
44

55
describe('UUIBox', () => {
@@ -11,6 +11,10 @@ describe('UUIBox', () => {
1111
</uui-box>`);
1212
});
1313

14+
it('is defined', () => {
15+
expect(element).to.be.instanceOf(UUIBoxElement);
16+
});
17+
1418
it('passes the a11y audit', async () => {
1519
await expect(element).shadowDom.to.be.accessible();
1620
});

packages/uui-breadcrumbs/lib/uui-breadcrumb-item.element.ts

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

@@ -7,6 +8,7 @@ import { property } from 'lit/decorators.js';
78
* @slot - to show display an element inside the breadcrumb
89
* @csspart separator - change the content of the after element of this part to change the separator
910
*/
11+
@defineElement('uui-breadcrumb-item')
1012
export class UUIBreadcrumbItemElement extends LitElement {
1113
static styles = [
1214
css`

0 commit comments

Comments
 (0)