UNPKG

@esri/calcite-components

Version:

Web Components for Esri's Calcite Design System.

199 lines (198 loc) • 6.25 kB
import { Component, Element, h, Host, Method, Prop } from "@stencil/core"; import { focusElement, getElementDir } from "../../utils/dom"; /** @slot default text slot for link text */ /** Any attributes placed on <calcite-link> component will propagate to the rendered child */ /** Passing a 'href' will render an anchor link, instead of a span. Role will be set to link, or link, depending on this. */ /** It is the consumers responsibility to add aria information, rel, target, for links, and any link attributes for form submission */ export class CalciteLink { constructor() { /** the node type of the rendered child element */ this.childElType = "span"; this.storeTagRef = (el) => { this.childEl = el; }; } //-------------------------------------------------------------------------- // // Lifecycle // //-------------------------------------------------------------------------- connectedCallback() { this.childElType = this.href ? "a" : "span"; } render() { const dir = getElementDir(this.el); const iconStartEl = (h("calcite-icon", { class: "calcite-link--icon icon-start", dir: dir, flipRtl: this.iconFlipRtl === "start" || this.iconFlipRtl === "both", icon: this.iconStart, scale: "s" })); const iconEndEl = (h("calcite-icon", { class: "calcite-link--icon icon-end", dir: dir, flipRtl: this.iconFlipRtl === "end" || this.iconFlipRtl === "both", icon: this.iconEnd, scale: "s" })); const attributes = this.getAttributes(); const Tag = this.childElType; const role = this.childElType === "span" ? "link" : null; const tabIndex = this.disabled ? -1 : this.childElType === "span" ? 0 : null; return (h(Host, { dir: dir, role: "presentation" }, h(Tag, Object.assign({}, attributes, { href: Tag === "a" && this.href, ref: this.storeTagRef, role: role, tabIndex: tabIndex }), this.iconStart ? iconStartEl : null, h("slot", null), this.iconEnd ? iconEndEl : null))); } //-------------------------------------------------------------------------- // // Public Methods // //-------------------------------------------------------------------------- async setFocus() { focusElement(this.childEl); } //-------------------------------------------------------------------------- // // Private Methods // //-------------------------------------------------------------------------- getAttributes() { // spread attributes from the component to rendered child, filtering out props const props = ["dir", "icon-end", "icon-start", "id", "theme"]; return Array.from(this.el.attributes) .filter((a) => a && !props.includes(a.name)) .reduce((acc, { name, value }) => (Object.assign(Object.assign({}, acc), { [name]: value })), {}); } static get is() { return "calcite-link"; } static get encapsulation() { return "shadow"; } static get originalStyleUrls() { return { "$": ["calcite-link.scss"] }; } static get styleUrls() { return { "$": ["calcite-link.css"] }; } static get properties() { return { "disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "is the link disabled" }, "attribute": "disabled", "reflect": true }, "href": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "optionally pass a href - used to determine if the component should render as a link or an anchor" }, "attribute": "href", "reflect": true }, "iconEnd": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "optionally pass an icon to display at the end of a button - accepts calcite ui icon names" }, "attribute": "icon-end", "reflect": true }, "iconFlipRtl": { "type": "string", "mutable": false, "complexType": { "original": "FlipContext", "resolved": "\"both\" | \"end\" | \"start\"", "references": { "FlipContext": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "flip the icon(s) in rtl" }, "attribute": "icon-flip-rtl", "reflect": true }, "iconStart": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "optionally pass an icon to display at the start of a button - accepts calcite ui icon names" }, "attribute": "icon-start", "reflect": true }, "theme": { "type": "string", "mutable": false, "complexType": { "original": "Theme", "resolved": "\"dark\" | \"light\"", "references": { "Theme": { "location": "import", "path": "../interfaces" } } }, "required": false, "optional": false, "docs": { "tags": [], "text": "Select theme (light or dark)" }, "attribute": "theme", "reflect": true } }; } static get methods() { return { "setFocus": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global" } }, "return": "Promise<void>" }, "docs": { "text": "", "tags": [] } } }; } static get elementRef() { return "el"; } }