Skip to content

Commit

Permalink
feat(api): add register static method
Browse files Browse the repository at this point in the history
`AuroToast.register` is to easily register the element without extra importing
`import "@aurodesignsystem/auro-toast"` will still register this element to <auro-toast>
`import { AuroToast } from '../src/auro-toast.js` wont register this element until `AuroToast.register` gets called

Fixed the typo on Recommended Use and Version Control.
`clement` -> `element`
  • Loading branch information
sun-mota committed Oct 18, 2024
1 parent f243a9d commit f5bee45
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 26 deletions.
1 change: 1 addition & 0 deletions demo/api.min.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ function initExamples(initCount) {
}
}

export { initExamples };
7 changes: 0 additions & 7 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,6 @@
<!-- If additional elements are needed for the demo, add them here. -->
<script src="https://cdn.jsdelivr.net/npm/@aurodesignsystem/auro-accordion@latest/dist/auro-accordion__bundled.js" type="module"></script>
<script type="module" src="../index.js"></script>
<script type="module">
import { AuroToast } from '../src/auro-toast.js';
import * as RuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';

RuntimeUtils.default.prototype.registerComponent('custom-toast', AuroToast);
</script>
<script type="module" src="../src/auro-toaster.js"></script>
<script type="module">
import { initExamples } from "./index.min.js"
initExamples();
Expand Down
4 changes: 4 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { initErrorExample } from '../apiExamples/error';
import { initSuccessExample } from '../apiExamples/success';
import { initMultipleToastsExample } from '../apiExamples/multipleToasts';

import { AuroToast } from '../src/auro-toast.js';

AuroToast.register('custom-toast');

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 @@ -234,15 +234,14 @@ The following example is for the HTML template.

## 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-toast` 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-toast` 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 `AuroToast.register(name)` method and pass in a unique name.

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

RuntimeUtils.default.prototype.registerComponent('custom-toast', AuroToast);
AuroToast.register('custom-toast');
```

This will create a new custom element that you can use in your HTML that will function identically to the `auro-toast` element.
Expand Down
259 changes: 259 additions & 0 deletions demo/index.min.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { css, LitElement } from 'lit';
import { html } from 'lit/static-html.js';
import closeIcon from '@alaskaairux/icons/dist/icons/interface/x-lg.mjs';
import successIcon from '@alaskaairux/icons/dist/icons/interface/check-stroke.mjs';
import errorIcon from '@alaskaairux/icons/dist/icons/alert/error-stroke.mjs';
import infoIcon from '@alaskaairux/icons/dist/icons/alert/information-stroke.mjs';
import AuroLibraryRuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';
import { AuroDependencyVersioning } from '@aurodesignsystem/auro-library/scripts/runtime/dependencyTagVersioning.mjs';
import { AuroButton } from '@aurodesignsystem/auro-button/src/auro-button.js';
import { AuroIcon } from '@aurodesignsystem/auro-icon/src/auro-icon.js';

/* eslint-disable jsdoc/require-jsdoc */

function initBasicExample() {
Expand Down Expand Up @@ -95,9 +106,256 @@ function initMultipleToastsExample() {
});
}

var styleCss = css`*,*:before,*:after{box-sizing:border-box}@media(prefers-reduced-motion: reduce){*,*:before,*:after{animation-duration:.01ms !important;animation-iteration-count:1 !important;transition-duration:.01ms !important}}*:focus-visible{outline:0}*:focus-visible{outline:0}:focus:not(:focus-visible){outline:3px solid transparent}.util_displayInline{display:inline}.util_displayInlineBlock{display:inline-block}.util_displayBlock{display:block}.util_displayFlex{display:flex}.util_displayHidden{display:none}.util_displayHiddenVisually{position:absolute;overflow:hidden;clip:rect(1px, 1px, 1px, 1px);width:1px;height:1px;padding:0;border:0}:host([visible]){display:inline-flex;min-width:100%;min-height:calc(var(--ds-size-700, 3.5rem) + var(--ds-size-50, 0.25rem));padding:var(--ds-size-200, 1rem) var(--ds-size-150, 0.75rem);border-radius:var(--ds-size-100, 0.5rem);box-shadow:var(--ds-elevation-200, 0px 0px 10px rgba(0, 0, 0, 0.15));gap:var(--ds-size-200, 1rem);pointer-events:auto}@media(576px <= width < 1024px){:host([visible]){min-width:33%;max-width:50%}}@media(width >= 1024px){:host([visible]){min-width:200px;max-width:500px}}:host([visible]) .toastContainer{display:flex;width:100%;flex-direction:row;align-items:center;gap:var(--ds-size-200, 1rem)}:host([visible]) .closeButton{width:var(--ds-size-300, 1.5rem);height:var(--ds-size-300, 1.5rem);padding-right:0;padding-left:0;border:var(--ds-size-25, 0.125rem) solid transparent;margin-left:auto}:host([visible]) .closeButton:hover{border-radius:50%;cursor:pointer}:host([visible]) .closeButtonIcon{width:var(--ds-size-300, 1.5rem);height:var(--ds-size-300, 1.5rem)}:host([visible]) .hidden{opacity:0;transition:visibility 0s 300ms,opacity 300ms ease-out;visibility:hidden}:host([visible]) [auro-icon]{width:var(--ds-size-300, 1.5rem);height:var(--ds-size-300, 1.5rem)}:host([visible]) .message{flex:1;align-self:center}`;

var colorCss = css`:host([visible]){background-color:var(--ds-auro-toast-container-color);color:var(--ds-auro-toast-text-color)}:host([visible]) .closeButton:hover{background-color:var(--ds-auro-toast-close-button-hover-container-color)}:host([variant=error]){--ds-auro-toast-container-color: var(--ds-color-container-error-default, #fff4f4);--ds-auro-toast-text-color: var(--ds-color-text-primary-default, #2a2a2a)}:host([variant=success]){--ds-auro-toast-container-color: var(--ds-color-container-success-default, #eef8f5);--ds-auro-toast-text-color: var(--ds-color-text-primary-default, #2a2a2a)}`;

var tokensCss = css`:host{--ds-auro-toast-container-color: var(--ds-color-container-subtle-inverse, #393d43);--ds-auro-toast-text-color: var(--ds-color-text-primary-inverse, #ffffff);--ds-auro-toast-close-button-hover-container-color: var(--ds-color-container-ui-secondary-hover-default, rgba(0, 0, 0, 0.03))}`;

var buttonVersion = '8.0.0';

var iconVersion = '5.0.0';

// Copyright (c) 2023 Alaska Airlines. All right reserved. Licensed under the Apache-2.0 license
// See LICENSE in the project root for license information.


const TIME_TIL_FADE_OUT = 5000;
const FADE_OUT_DURATION = 300;

// See https://git.io/JJ6SJ for "How to document your components using JSDoc"
/**
* The auro-toast element provides users a way to display short, temporary messages.
*
* @attr {Boolean} visible - Sets state of toast to visible
* @attr {String} variant - Component will render visually based on which variant value is set; currently supports `error`, `success`
* @attr {Boolean} noIcon - Removes icon from the toast UI
* @fires onToastClose - Notifies that the toast has been closed
*/

// build the component class
class AuroToast extends LitElement {
constructor() {
super();

/**
* @private
*/
this.closeDom = new DOMParser().parseFromString(closeIcon.svg, 'text/html');

/**
* @private
*/
this.closeSvg = this.closeDom.body.firstChild;
this.closeSvg.setAttribute('slot', 'svg');

/**
* @private
*/
this.successDom = new DOMParser().parseFromString(successIcon.svg, 'text/html');

/**
* @private
*/
this.successSvg = this.successDom.body.firstChild;

this.successSvg.setAttribute('slot', 'svg');

/**
* @private
*/
this.errorDom = new DOMParser().parseFromString(errorIcon.svg, 'text/html');

/**
* @private
*/
this.errorSvg = this.errorDom.body.firstChild;
this.errorSvg.setAttribute('slot', 'svg');

/**
* @private
*/
this.infoDom = new DOMParser().parseFromString(infoIcon.svg, 'text/html');

/**
* @private
*/
this.infoSvg = this.infoDom.body.firstChild;
this.infoSvg.setAttribute('slot', 'svg');

const versioning = new AuroDependencyVersioning();

/**
* @private
*/
this.buttonTag = versioning.generateTag('auro-button', buttonVersion, AuroButton);

/**
* @private
*/
this.iconTag = versioning.generateTag('auro-icon', iconVersion, AuroIcon);

/**
* @private
*/
this.runtimeUtils = new AuroLibraryRuntimeUtils();

/**
* @private
*/
this.fadeOutTimer = undefined;
}

// This function is to define props used within the scope of this component
// Be sure to review https://lit.dev/docs/components/properties/
// to understand how to use reflected attributes with your property settings.
static get properties() {
return {
...super.properties,
visible: {
type: Boolean,
reflect: true
},
variant: {
type: String,
reflect: true
},
noIcon: {
type: Boolean
}
};
}

static get styles() {
return [
styleCss,
colorCss,
tokensCss
];
}

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

firstUpdated() {
// Add the tag name as an attribute if it is different than the component name
this.runtimeUtils.handleComponentTagRename(this, 'auro-toast');
}

/**
* @private
* @returns {void}
*/
handleOnClose() {
this.fadeOutToast();
clearTimeout(this.fadeOutTimer);
}

/**
* @private
* @returns {void}
*/
fadeOutToast() {
const toastContainer = this.shadowRoot.querySelector('.toastContainer');
toastContainer.classList.add('hidden');

setTimeout(() => {
this.closeToast();
}, FADE_OUT_DURATION);
}

/**
* @private
* @returns {void}
*/
closeToast() {
clearTimeout(this.fadeOutTimer);
this.visible = false;

/**
* Emits closed toast event.
*
* @event onToastClose
* @type {object}
* @property {boolean} false - Sets visibility value for the toast element.
*/
this.dispatchEvent(new CustomEvent('onToastClose', {
bubbles: true,
composed: true,
detail: this
}));
}

/**
* For mobile, set the onclick function so the toast can be dismissed if it is tapped on anywhere inside the toast.
* @private
* @returns {void}
*/
setOnClick() {
const mobileBreakPoint = 767;
if (window.innerWidth < mobileBreakPoint) {
this.onclick = () => {
this.fadeOutToast();
};
}
}

connectedCallback() {
super.connectedCallback();
this.setOnClick();
}

updated(changedProperties) {
if (changedProperties.has('variant')) {
clearTimeout(this.fadeOutTimer);
}
// do not auto dismiss for error toasts
if (this.visible && this.variant !== 'error') {
this.fadeOutTimer = setTimeout(() => {
this.fadeOutToast();
}, TIME_TIL_FADE_OUT);
}
}

render() {
return this.visible ? html`
<div aria-live="polite" class="toastContainer">
${this.noIcon ? undefined : html`
<${this.iconTag} customSize customColor customSvg class="typeIcon">
${this.variant === 'success' ? this.successSvg : undefined}
${this.variant === 'error' ? this.errorSvg : undefined}
${this.variant !== 'success' && this.variant !== 'error' ? this.infoSvg : undefined}
</${this.iconTag}>
`}
<div class="message"><slot></slot></div>
<${this.buttonTag}
variant="flat"
?onDark=${this.getAttribute('variant') !== 'error' && this.getAttribute('variant') !== 'success'}
@click="${this.handleOnClose}" part="close-button">
<${this.iconTag} customSize customColor customSvg>
${this.closeSvg}
</${this.iconTag}>
<span class="util_displayHiddenVisually">Close</span>
</${this.buttonTag}>
</div>
` : undefined;
}
}

/* eslint-disable jsdoc/require-jsdoc, no-magic-numbers, no-param-reassign */


AuroToast.register('custom-toast');

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

Expand All @@ -117,3 +375,4 @@ function initExamples(initCount) {
}
}

export { initExamples };
7 changes: 3 additions & 4 deletions docs/partials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,14 @@ The following example is for the HTML template.

## 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-toast` 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-toast` 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 `AuroToast.register(name)` method and pass in a unique name.

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

RuntimeUtils.default.prototype.registerComponent('custom-toast', AuroToast);
AuroToast.register('custom-toast');
```

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

RuntimeUtils.default.prototype.registerComponent('custom-toast', AuroToast);
AuroToast.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 @@ -140,7 +140,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 build:demoScripts bundler postinstall",
"build:ci": "npm-run-all sweep build:release",
Expand Down
Loading

0 comments on commit f5bee45

Please sign in to comment.