Skip to content

Commit

Permalink
feat(api): add register static method #26
Browse files Browse the repository at this point in the history
- `AuroDialog.register` is to easily register the element without extra importing
- `import '@aurodesignsystem/auro-dialog'` will still register this element to `<auro-dialog>`
- `import { AuroDialog } from '@aurodesignsystem/auro-dialog/src/auro-dialog.js` wont register this element until `AuroDialog.register` gets called
  • Loading branch information
sun-mota committed Oct 25, 2024
1 parent 012dc33 commit a5882af
Show file tree
Hide file tree
Showing 10 changed files with 367 additions and 25 deletions.
5 changes: 0 additions & 5 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@
</script>

<script type="module" data-demo-script="true">
import { AuroDialog } from '../src/auro-dialog.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-dialog', AuroDialog);

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

initExamples();
Expand Down
5 changes: 5 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { initBasicExample } from "../apiExamples/basic";
import { initCustomExample } from "../apiExamples/custom";

import { AuroDialog } from '../src/auro-dialog.js';

AuroDialog.register();
AuroDialog.register('custom-dialog');

export function initExamples(initCount) {
initCount = initCount || 0;

Expand Down
7 changes: 3 additions & 4 deletions demo/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,14 @@ Having a closing statement about your example helps to really complete the thoug

## Recommended Use and Version Control

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-dialog` custom element is defined automatically.
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 element. 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-dialog` 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 `AuroDialog.register(name)` method and pass in a unique name.

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

RuntimeUtils.default.prototype.registerComponent('custom-dialog', AuroDialog);
AuroDialog.register('custom-dialog');
```

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

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions docs/partials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,14 @@ Having a closing statement about your example helps to really complete the thoug

## Recommended Use and Version Control

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-dialog` custom element is defined automatically.
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 element. 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-dialog` 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 `AuroDialog.register(name)` method and pass in a unique name.

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

RuntimeUtils.default.prototype.registerComponent('custom-dialog', AuroDialog);
AuroDialog.register('custom-dialog');
```

This will create a new custom element that you can use in your HTML that will function identically to the `auro-dialog` 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 { AuroDialog } from './src/auro-dialog.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-dialog', AuroDialog);
AuroDialog.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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
"web components"
],
"scripts": {
"build": "npm-run-all build:version build:sass sass:render types",
"build": "npm-run-all build:version build:sass sass:render bundler types",
"build:test": "npm-run-all test linters",
"build:release": "npm-run-all build build:test build:api build:docs bundler postinstall",
"build:ci": "npm-run-all sweep build:release",
Expand Down
18 changes: 12 additions & 6 deletions src/auro-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

// ---------------------------------------------------------------------

import AuroLibraryRuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';
import ComponentBase from './componentBase.js';

// build the component class
Expand All @@ -26,11 +27,16 @@ export class AuroDialog extends ComponentBase {
static get styles() {
return [super.styles];
}
}

/* istanbul ignore else */
// define the name of the custom component
if (!customElements.get("auro-dialog")) {
customElements.define("auro-dialog", AuroDialog);
/**
* This will register this element with the browser.
* @param {string} [name="auro-dialog"] - The name of element that you want to register to.
*
* @example
* AuroDialog.register("custom-dialog") // this will register this element to <custom-dialog/>
*
*/
static register(name = "auro-dialog") {
AuroLibraryRuntimeUtils.prototype.registerComponent(name, AuroDialog);
}
}

2 changes: 1 addition & 1 deletion test/auro-dialog.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { fixture, html, expect, oneEvent, waitUntil, elementUpdated } from '@open-wc/testing';
import '../src/auro-dialog.js';
import '../index.js';
// import './nested-shadow.js';

describe('auro-dialog', () => {
Expand Down

0 comments on commit a5882af

Please sign in to comment.