Skip to content

Commit

Permalink
feat(api): add register static method #77
Browse files Browse the repository at this point in the history
- Update dependency versions

- `AuroCard.register` is to easily register the element without extra importing
- `import "@aurodesignsystem/auro-card"` will still register this element to <auro-card>
- `import { AuroCard } from '../src/auro-card` wont register this element until `AuroCard.register` gets called

- Fixed the typo on Recommended Use and Version Control: `clement` -> `element`
  • Loading branch information
sun-mota committed Oct 23, 2024
1 parent a24cf0c commit e31c4be
Show file tree
Hide file tree
Showing 12 changed files with 4,290 additions and 2,048 deletions.
9 changes: 1 addition & 8 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,7 @@
Prism.highlightAll();
});
</script>
<script type="module" data-demo-script="true" src="../index.js"></script>

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

RuntimeUtils.default.prototype.registerComponent('custom-card', AuroCard);
</script>
<script type="module" data-demo-script="true" src="./index.min.js"></script>

<!-- 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>
Expand Down
5 changes: 5 additions & 0 deletions demo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* eslint-disable jsdoc/require-jsdoc, no-magic-numbers, no-param-reassign */
import { AuroCard } from '../src/auro-card.js';

AuroCard.register();
AuroCard.register('custom-card');
7 changes: 3 additions & 4 deletions demo/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -778,15 +778,14 @@ Note that this example also uses a `style` tag to set most of the styles - this

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

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

RuntimeUtils.default.prototype.registerComponent('custom-card', AuroCard);
AuroCard.register('custom-card');
```

This will create a new custom element that you can use in your HTML that will function identically to the `auro-card` element.
Expand Down
148 changes: 148 additions & 0 deletions demo/index.min.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import { css, LitElement } from 'lit';
import { html } from 'lit/static-html.js';
import { ifDefined } from 'lit/directives/if-defined.js';
import AuroLibraryRuntimeUtils from '@aurodesignsystem/auro-library/scripts/utils/runtimeUtils.mjs';
import { AuroDependencyVersioning } from '@aurodesignsystem/auro-library/scripts/runtime/dependencyTagVersioning.mjs';
import { AuroHyperlink } from '@aurodesignsystem/auro-hyperlink/src/auro-hyperlink.js';

var hyperlinkVersion = '4.0.2';

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}:host{display:inline-flex;flex-direction:column}:host ::slotted(img){display:block;object-fit:cover;width:100%}:host::part(imageWrapper){display:flex;justify-content:center;align-items:center}:host .imageWrapper{display:flex;justify-content:center;align-items:center}:host .content{padding:var(--ds-size-200, 1rem) 0;padding-top:0}:host([center]){text-align:center}:host([variant=inset-content])::part(content),:host([href])::part(content){padding:var(--ds-size-200, 1rem);padding-top:0;overflow:hidden;text-wrap:wrap}:host([variant=inset-container]){padding:var(--ds-size-400, 2rem) var(--ds-size-200, 1rem)}:host([variant=inset-stretch])::part(imageWrapper){margin-top:var(--ds-size-400, 2rem)}:host([variant=inset-stretch])::part(content){margin-bottom:var(--ds-size-400, 2rem);padding-bottom:0}:host([border]){border-width:1px;border-style:solid;border-radius:var(--ds-border-radius, 0.375rem);overflow:hidden}:host([href]) [auro-hyperlink]{border-radius:var(--ds-border-radius, 0.375rem);overflow:hidden;box-shadow:var(--ds-elevation-100, 0px 0px 5px rgba(0, 0, 0, 0.15));position:relative}:host([href]) [auro-hyperlink]::part(link){text-decoration:none;color:inherit}:host([href]) [auro-hyperlink]::part(targetIcon){position:absolute;top:unset;bottom:var(--ds-size-50, 0.25rem);right:var(--ds-size-50, 0.25rem)}:host([href]) [auro-hyperlink]:focus{outline-width:1px;outline-style:solid;border-radius:var(--ds-border-radius, 0.375rem);color:inherit}:host([href]) [auro-hyperlink]:hover{outline-width:1px;outline-style:solid;outline-offset:-1px;box-shadow:var(--ds-elevation-200, 0px 0px 5px rgba(0, 0, 0, 0.15));color:inherit}`;

var tokensCss = css`:host{--ds-auro-card-border-color: var(--ds-color-border-divider-default, rgba(0, 0, 0, 0.12));--ds-auro-card-link-border-color: transparent}`;

var colorCss = css`:host([border]){border-color:var(--ds-auro-card-border-color)}:host([href]) [auro-hyperlink]{outline-color:var(--ds-auro-card-link-border-color);color:inherit}:host([href]) [auro-hyperlink]:focus{--ds-auro-card-link-border-color: var(--ds-color-border-ui-active-default, #225296)}`;

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


// See https://git.io/JJ6SJ for "How to document your components using JSDoc"
/**
* The `<auro-card>` element provides users a flexible way to convey a summary of information.
*
* @attr {String} variant - Sets the variant of the card. Options `inset-content`, `inset-container`, `inset-stretch`.
* @attr {Boolean} border - Adds desired UI border to the `auro-card` element.
* @attr {Boolean} center - Centers content within the scope of the `auro-card`.
* @attr {String} href - Sets the card to function as a hyperlink to the provided href value & disables the default CTA slot.
* @attr {String} rel - Sets rel attribute on the [auro-hyperlink](https://auro.alaskaair.com/components/auro/hyperlink/api#rel).
* @attr {String} role - Sets role attribute on the [auro-hyperlink](https://auro.alaskaair.com/components/auro/hyperlink/api#role).
* @attr {String} target - Sets target attribute on the [auro-hyperlink](https://auro.alaskaair.com/components/auro/hyperlink/api#target).
* @slot image - Content slot for image content.
* @slot header - Content slot for string text header content.
* @slot description - Content slot for string text description content.
* @slot cta - Content slot for call-to-action content.
* @csspart content - Apply styles to the outer string text container.
* @csspart link - Apply styles to the [auro-hyperlink](https://auro.alaskaair.com/components/auro/hyperlink/api#link) element.
* @csspart imageWrapper - Apply styles to the outer image slot container.
* @csspart image - Apply styles to the named image slot container.
* @csspart header - Apply styles to the named header slot container.
* @csspart description - Apply styles to the named description slot container.
* @csspart cta - Apply styles to the named cta slot container.
*/

class AuroCard extends LitElement {
constructor () {
super();

// Properly defines the role of this new custom element for screen readers.
this.setAttribute('role', 'article');

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

const versioning = new AuroDependencyVersioning();

/**
* @private
*/
this.hyperlinkTag = versioning.generateTag('auro-hyperlink', hyperlinkVersion, AuroHyperlink);
}

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

static get properties() {
return {
variant: {
type: String,
reflect: true,
},
href: {
type: String,
reflect: true,
},
rel: {
type: String,
reflect: true,
},
role: {
type: String,
reflect: true,
},
target: {
type: String,
reflect: true,
}
};
}

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

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

// function that renders the HTML and CSS into the scope of the component
render() {
const cardContent = html`
<div class="imageWrapper" part="imageWrapper">
<slot name="image" part="image"></slot>
</div>
<div id="content-within" class="content" part="content">
<slot name="header" part="header"></slot>
<slot name="description" part="description"></slot>
<slot name="cta" part="cta"></slot>
</div>
`;

return html`
${this.href
? html`
<${this.hyperlinkTag}
aria-labelledby="content-within"
href="${ifDefined(this.href ? this.href : undefined)}"
rel="${ifDefined(this.rel ? this.rel : undefined)}"
role="${ifDefined(this.role ? this.role : undefined)}"
target="${ifDefined(this.target ? this.target : undefined)}"
>
${cardContent}
</${this.hyperlinkTag}>`
: cardContent
}
`;
}
}

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

AuroCard.register();
AuroCard.register('custom-card');
7 changes: 3 additions & 4 deletions docs/partials/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,14 @@ Note that this example also uses a `style` tag to set most of the styles - this

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

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

RuntimeUtils.default.prototype.registerComponent('custom-card', AuroCard);
AuroCard.register('custom-card');
```

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

RuntimeUtils.default.prototype.registerComponent('custom-card', AuroCard);
AuroCard.register();
Loading

0 comments on commit e31c4be

Please sign in to comment.