Skip to content

Commit

Permalink
feat(api): add register static method
Browse files Browse the repository at this point in the history
AuroButton.register is to easily register the element without extra importing
`import '@aurodesignsystem/auro-button'` will still register this element to `<auro-button>`
`import { AuroButton } from '../src/auro-button.js'` wont register this element until `AuroButton.register` gets called
  • Loading branch information
sun-mota committed Oct 17, 2024
1 parent 06866e9 commit 5ff9241
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 25 deletions.
9 changes: 1 addition & 8 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,7 @@
});
</script>
<script type="module" data-demo-script="true">
import { AuroButton } from '../src/auro-button.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-button', AuroButton);

import { initExamples } from './index.min.js';

registerComponent('custom-button');
import { initExamples } from './index.min.js';
initExamples();
</script>
<script src="https://cdn.jsdelivr.net/npm/@aurodesignsystem/auro-accordion@latest/dist/auro-accordion__bundled.js" type="module"></script>
Expand Down
3 changes: 3 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { AuroButton } from '../src/auro-button.js';

AuroButton.register('custom-button');

export function initExamples(initCount) {
initCount = initCount || 0;
Expand Down
5 changes: 2 additions & 3 deletions demo/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,12 @@ Don't combine `disabled` and `loading` attributes on any single instance of `aur

There are two important parts of every Auro component. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">class</a> and the custom clement. The class is exported and then used as part of defining the Web Component. When importing this component as described in the <a href="#install">install</a> section, the class is imported and the `auro-button` custom element is defined automatically.

To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `registerComponent(name)` method and pass in a unique name.
To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `AuroButton.register(name)` method and pass in a unique name.

```js
import { AuroButton } from './src/auro-button.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-button', AuroButton);
AuroButton.register('custom-button');
```

This will create a new custom element that you can use in your HTML that will function identically to the `<auro-button>` element.
Expand Down
251 changes: 251 additions & 0 deletions demo/index.min.js

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions docs/partials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ Don't combine `disabled` and `loading` attributes on any single instance of `aur

There are two important parts of every Auro component. The <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes">class</a> and the custom clement. The class is exported and then used as part of defining the Web Component. When importing this component as described in the <a href="#install">install</a> section, the class is imported and the `auro-button` custom element is defined automatically.

To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `registerComponent(name)` method and pass in a unique name.
To protect from versioning conflicts with other instances of the component being loaded, it is recommended to use our `AuroButton.register(name)` method and pass in a unique name.

```js
import { AuroButton } from './src/auro-button.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-button', AuroButton);
AuroButton.register('custom-button');
```

This will create a new custom element that you can use in your HTML that will function identically to the `<auro-button>` element.
Expand Down
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AuroButton } from './src/auro-button.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-button', AuroButton);
AuroButton.register();
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 16 additions & 5 deletions src/auro-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@

import { LitElement } from "lit";
import { html } from 'lit/static-html.js';

import { classMap } from 'lit/directives/class-map.js';
import { ifDefined } from 'lit/directives/if-defined.js';

import { AuroDependencyVersioning } from '@aurodesignsystem/auro-library/scripts/runtime/dependencyTagVersioning.mjs';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

import styleCss from "./style-css.js";
import colorCss from "./color-css.js";
import tokensCss from "./tokens-css.js";
Expand Down Expand Up @@ -150,6 +154,18 @@ export class AuroButton extends LitElement {
};
}

/**
* This will register this element with the browser.
* @param {string} [name="auro-button"] - The name of element that you want to register to.
*
* @example
* AuroButton.register("custom-button") // this will register this element to <custom-button/>
*
*/
static register(name = "auro-button") {
RuntimeUtils.default.prototype.registerComponent(name, AuroButton);
}

/**
* Internal method to apply focus to the HTML5 button.
* @private
Expand Down Expand Up @@ -231,8 +247,3 @@ export class AuroButton extends LitElement {
`;
}
}

// default internal definition
if (!customElements.get("auro-button")) {
customElements.define("auro-button", AuroButton);
}
3 changes: 1 addition & 2 deletions test/auro-button.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
/* eslint-disable no-undef */
import { fixture, html, expect, elementUpdated } from '@open-wc/testing';
import { AuroButton } from '../src/auro-button.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';
import '../index.js';

describe('auro-button', () => {
Expand All @@ -33,7 +32,7 @@ describe('auro-button', () => {
});

it('successfully registers custom component', async () => {
RuntimeUtils.default.prototype.registerComponent('custom-button', AuroButton);
AuroButton.register('custom-button');

expect(typeof customElements.get('custom-button')).to.equal(typeof AuroButton);
});
Expand Down

0 comments on commit 5ff9241

Please sign in to comment.