UNPKG

@public-ui/components

Version:

Contains all web components that belong to KoliBri - The accessible HTML-Standard.

829 lines (828 loc) 34.8 kB
/*! * KoliBri - The accessible HTML-Standard */ import { __decorate } from "tslib"; import { h, Host } from "@stencil/core"; import { BaseWebComponent } from "../../internal/functional-components/base-web-component"; import { IconFC } from "../../internal/functional-components/icon/component"; import { TooltipFC } from "../../internal/functional-components/tooltip/component"; import { TooltipController } from "../../internal/functional-components/tooltip/controller"; import { devHint, setEventTarget, showExpertSlot, validateAccessKey, validateAlternativeButtonLinkRole, validateAriaControls, validateAriaCurrentValue, validateAriaDescription, validateAriaExpanded, validateAriaOwns, validateCustomClass, validateDisabled, validateDownload, validateHideLabel, validateHref, validateIcons, validateInline, validateLabelWithExpertSlot, validateLinkCallbacks, validateLinkTarget, validateShortKey, validateTooltipAlign, validateVariantClassName, } from "../../schema"; import { validateTabIndex } from "../../schema/props/tab-index"; import { createCtaRef, directClick, directFocus } from "../../utils/element-interaction"; import { dispatchDomEvent, KolEvent } from "../../utils/events"; import { onLocationChange } from "./ariaCurrentService"; import { translate } from "../../i18n"; import { SpanFC } from "../../internal/functional-components/span/component"; import { validateAccessAndShortKey } from "../../schema/validators/access-and-short-key"; import clsx from "../../utils/clsx"; export class KolLinkWc { constructor() { this.ctaRef = createCtaRef(); this.tooltipCtrl = new TooltipController(BaseWebComponent.stateLess); this.translateOpenLinkInTab = translate('kol-open-link-in-tab'); this.onClick = (event) => { var _a, _b; this.tooltipCtrl.hideTooltip(); if (this.state._disabled === true) { event.preventDefault(); } else { if (typeof ((_a = this.state._on) === null || _a === void 0 ? void 0 : _a.onClick) === 'function') { setEventTarget(event, this.ctaRef.el); (_b = this.state._on) === null || _b === void 0 ? void 0 : _b.onClick(event, this.state._href); } if (this.host) { dispatchDomEvent(this.host, KolEvent.click, this.state._href); } } }; this.getRenderValues = () => { const isExternal = typeof this.state._target === 'string' && this.state._target !== '_self'; const tagAttrs = { href: typeof this.state._href === 'string' && this.state._href.length > 0 ? this.state._href : 'javascript:void(0);', target: typeof this.state._target === 'string' && this.state._target.length > 0 ? this.state._target : undefined, rel: isExternal ? 'noopener' : undefined, download: typeof this.state._download === 'string' ? this.state._download : undefined, }; if (this.state._hideLabel === true && !this.state._label) { devHint(`[KolLink] An aria-label must be set when _hide-label is set.`); } return { isExternal, tagAttrs }; }; this._disabled = false; this._hideLabel = false; this._inline = true; this._tooltipAlign = 'right'; this.state = { _ariaCurrentValue: 'page', _href: '', _icons: {}, }; } async focus() { } async click() { } render() { var _a; const { isExternal, tagAttrs } = this.getRenderValues(); const hasExpertSlot = showExpertSlot(this.state._label); const ariaDescription = (_a = this.state._ariaDescription) === null || _a === void 0 ? void 0 : _a.trim(); return (h(Host, { key: '4ec894cf85b4e39361eb7001a0bb073a07527416' }, h("a", Object.assign({ key: '92604427272ad2f3d676ad375e18279979fd70f3', ref: this.ctaRef }, tagAttrs, { accessKey: this.state._accessKey, "aria-current": this.state._ariaCurrent, "aria-controls": this.state._ariaControls, "aria-description": ariaDescription || undefined, "aria-disabled": this.state._disabled ? 'true' : undefined, "aria-expanded": typeof this.state._ariaExpanded === 'boolean' ? String(this.state._ariaExpanded) : undefined, "aria-owns": this.state._ariaOwns, "aria-label": this.state._hideLabel && typeof this.state._label === 'string' ? `${this.state._label}${isExternal ? ` (${this.translateOpenLinkInTab})` : ''}` : undefined, "aria-keyshortcuts": this.state._shortKey, class: clsx('kol-link', { 'kol-link--disabled': this.state._disabled === true, 'kol-link--external-link': isExternal, 'kol-link--hide-label': this.state._hideLabel === true, [`kol-link--${this.state._variant}`]: this.state._variant !== undefined, 'kol-link--inline': this.state._inline === true, 'kol-link--standalone': this.state._inline === false, [this.state._customClass]: this.state._variant === 'custom' && typeof this.state._customClass === 'string' && this.state._customClass.length > 0, }) }, this.state._on, { onClick: this.onClick, onKeyPress: this.onClick, role: this.state._role, tabIndex: this.state._disabled ? -1 : this.state._tabIndex }), h(SpanFC, { key: '35b2e3835032e2e906c0513d0cd859c0ab5b241f', class: "kol-link__text", badgeText: this.state._accessKey || this.state._shortKey, icons: this.state._icons, hideLabel: this.state._hideLabel, label: hasExpertSlot ? '' : this.state._label || this.state._href }, h("slot", { key: '499674edbdda752d1c2718ec4cea4574db4eb7ad', name: "expert", slot: "expert" })), isExternal && (h(IconFC, { key: 'edbebe0db35b755c1c2eaaa93969b25d19dc3fd6', class: "kol-link__icon", label: this.state._hideLabel ? '' : this.translateOpenLinkInTab, icons: 'kolicon-link-external', "aria-hidden": this.state._hideLabel }))), this.state._hideLabel === true && !hasExpertSlot && (h("div", { key: '881fe1237ddae06da94ea578fbe96211b0f0f2e8', class: "kol-link__tooltip" }, h(TooltipFC, { key: 'e5553e15922cc7f41999f05b5539e4f93113ac84', badgeText: this.state._accessKey || this.state._shortKey || '', label: typeof this.state._label === 'string' ? this.state._label : typeof this.state._href === 'string' ? this.state._href : '', id: this.tooltipCtrl.getRenderProp('id'), refFloating: this.tooltipCtrl.setTooltipElementRef }))))); } validateAccessKey(value) { validateAccessKey(this, value); validateAccessAndShortKey(value, this._shortKey); } validateAriaCurrentValue(value) { validateAriaCurrentValue(this, value); } validateAriaControls(value) { validateAriaControls(this, value); } validateAriaDescription(value) { validateAriaDescription(this, value); } validateAriaExpanded(value) { validateAriaExpanded(this, value); } validateAriaOwns(value) { validateAriaOwns(this, value); } validateCustomClass(value) { validateCustomClass(this, value); } validateDisabled(value) { validateDisabled(this, value); } validateDownload(value) { validateDownload(this, value); } validateHideLabel(value) { validateHideLabel(this, value); } validateHref(value) { validateHref(this, value, { required: true, }); } validateIcons(value) { validateIcons(this, value); } validateInline(value) { validateInline(this, value, { defaultValue: true, }); } validateLabel(value) { validateLabelWithExpertSlot(this, value); this.tooltipCtrl.watchLabel(typeof value === 'string' ? value : undefined); } validateOn(value) { validateLinkCallbacks(this, value); } validateRole(value) { validateAlternativeButtonLinkRole(this, value); } validateShortKey(value) { validateShortKey(this, value); validateAccessAndShortKey(this._accessKey, value); } validateTabIndex(value) { validateTabIndex(this, value); } validateTarget(value) { validateLinkTarget(this, value); } validateTooltipAlign(value) { validateTooltipAlign(this, value); this.tooltipCtrl.watchAlign(value); } validateVariantClassName(value) { validateVariantClassName(this, value); } componentWillLoad() { this.validateAccessKey(this._accessKey); this.validateAriaCurrentValue(this._ariaCurrentValue); this.validateAriaControls(this._ariaControls); this.validateAriaDescription(this._ariaDescription); this.validateAriaExpanded(this._ariaExpanded); this.validateAriaOwns(this._ariaOwns); this.validateCustomClass(this._customClass); this.validateDisabled(this._disabled); this.validateDownload(this._download); this.validateHideLabel(this._hideLabel); this.validateHref(this._href); this.validateIcons(this._icons); this.validateInline(this._inline); this.validateLabel(this._label); this.validateOn(this._on); this.validateRole(this._role); this.validateShortKey(this._shortKey); this.validateTabIndex(this._tabIndex); this.validateTarget(this._target); this.validateTooltipAlign(this._tooltipAlign); this.validateVariantClassName(this._variant); this.unsubscribeOnLocationChange = onLocationChange((location) => { this.state._ariaCurrent = location === this.state._href ? this.state._ariaCurrentValue : undefined; }); validateAccessAndShortKey(this._accessKey, this._shortKey); this.tooltipCtrl.componentWillLoad({ label: typeof this.state._label === 'string' ? this.state._label : typeof this.state._href === 'string' ? this.state._href : '', align: this._tooltipAlign, }); } componentDidRender() { if (this.ctaRef.el) { this.tooltipCtrl.syncListeners(undefined, this.ctaRef.el, true); } } disconnectedCallback() { if (this.unsubscribeOnLocationChange) { this.unsubscribeOnLocationChange(); } this.tooltipCtrl.destroy(); } static get is() { return "kol-link-wc"; } static get properties() { return { "_accessKey": { "type": "string", "mutable": false, "complexType": { "original": "AccessKeyPropType", "resolved": "string | undefined", "references": { "AccessKeyPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::AccessKeyPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the key combination that can be used to trigger or focus the component's interactive element." }, "getter": false, "setter": false, "reflect": false, "attribute": "_access-key" }, "_ariaCurrentValue": { "type": "string", "mutable": false, "complexType": { "original": "AriaCurrentValuePropType", "resolved": "\"date\" | \"false\" | \"location\" | \"page\" | \"step\" | \"time\" | \"true\" | undefined", "references": { "AriaCurrentValuePropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::AriaCurrentValuePropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the value for the aria-current attribute." }, "getter": false, "setter": false, "reflect": false, "attribute": "_aria-current-value" }, "_ariaControls": { "type": "string", "mutable": false, "complexType": { "original": "string", "resolved": "string | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines which elements are controlled by this component. (https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-controls)" }, "getter": false, "setter": false, "reflect": false, "attribute": "_aria-controls" }, "_ariaDescription": { "type": "string", "mutable": false, "complexType": { "original": "AriaDescriptionPropType", "resolved": "string | undefined", "references": { "AriaDescriptionPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::AriaDescriptionPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the value for the aria-description attribute." }, "getter": false, "setter": false, "reflect": false, "attribute": "_aria-description" }, "_ariaExpanded": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "TODO", "text": ": Change type to `AriaExpandedPropType` after Stencil#4663 has been resolved." }], "text": "Marks this element as open/expanded, or that the connected element (aria-controls/aria-owns) is open/expanded." }, "getter": false, "setter": false, "reflect": false, "attribute": "_aria-expanded" }, "_ariaOwns": { "type": "string", "mutable": false, "complexType": { "original": "AriaOwnsPropType", "resolved": "string | undefined", "references": { "AriaOwnsPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::AriaOwnsPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the contextual relationship between a parent and its child elements." }, "getter": false, "setter": false, "reflect": false, "attribute": "_aria-owns" }, "_customClass": { "type": "string", "mutable": false, "complexType": { "original": "CustomClassPropType", "resolved": "string | undefined", "references": { "CustomClassPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::CustomClassPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the custom class attribute if _variant=\"custom\" is set." }, "getter": false, "setter": false, "reflect": false, "attribute": "_custom-class" }, "_disabled": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Makes the element not focusable and ignore all events." }, "getter": false, "setter": false, "reflect": false, "attribute": "_disabled", "defaultValue": "false" }, "_download": { "type": "string", "mutable": false, "complexType": { "original": "DownloadPropType", "resolved": "string | undefined", "references": { "DownloadPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::DownloadPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Tells the browser that the link contains a file. Optionally sets the filename." }, "getter": false, "setter": false, "reflect": false, "attribute": "_download" }, "_hideLabel": { "type": "boolean", "mutable": false, "complexType": { "original": "boolean", "resolved": "boolean | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [{ "name": "TODO", "text": ": Change type back to `HideLabelPropType` after Stencil#4663 has been resolved." }], "text": "Hides the caption by default and displays the caption text with a tooltip when the\ninteractive element is focused or the mouse is over it." }, "getter": false, "setter": false, "reflect": false, "attribute": "_hide-label", "defaultValue": "false" }, "_href": { "type": "string", "mutable": false, "complexType": { "original": "HrefPropType", "resolved": "string", "references": { "HrefPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::HrefPropType" } } }, "required": true, "optional": false, "docs": { "tags": [], "text": "Sets the target URI of the link or citation source." }, "getter": false, "setter": false, "reflect": false, "attribute": "_href" }, "_icons": { "type": "string", "mutable": false, "complexType": { "original": "Stringified<KoliBriIconsProp>", "resolved": "KoliBriHorizontalIcons & KoliBriVerticalIcons | string | undefined", "references": { "Stringified": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::Stringified" }, "KoliBriIconsProp": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::KoliBriIconsProp" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the icon classnames (e.g. `_icons=\"fa-solid fa-user\"`)." }, "getter": false, "setter": false, "reflect": false, "attribute": "_icons" }, "_inline": { "type": "boolean", "mutable": false, "complexType": { "original": "InlinePropType", "resolved": "boolean | undefined", "references": { "InlinePropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::InlinePropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines whether the component is displayed as a standalone block or inline without enforcing a minimum size of 44px." }, "getter": false, "setter": false, "reflect": false, "attribute": "_inline", "defaultValue": "true" }, "_label": { "type": "string", "mutable": false, "complexType": { "original": "LabelWithExpertSlotPropType", "resolved": "string | undefined", "references": { "LabelWithExpertSlotPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::LabelWithExpertSlotPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the visible or semantic label of the component (e.g. aria-label, label, headline, caption, summary, etc.). Set to `false` to enable the expert slot." }, "getter": false, "setter": false, "reflect": false, "attribute": "_label" }, "_on": { "type": "unknown", "mutable": false, "complexType": { "original": "LinkOnCallbacksPropType", "resolved": "undefined | { onClick?: EventValueOrEventCallback<Event, string> | undefined; }", "references": { "LinkOnCallbacksPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::LinkOnCallbacksPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the callback functions for links." }, "getter": false, "setter": false }, "_role": { "type": "string", "mutable": false, "complexType": { "original": "AlternativeButtonLinkRolePropType", "resolved": "\"tab\" | \"treeitem\" | undefined", "references": { "AlternativeButtonLinkRolePropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::AlternativeButtonLinkRolePropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines the role of the components primary element." }, "getter": false, "setter": false, "reflect": false, "attribute": "_role" }, "_shortKey": { "type": "string", "mutable": false, "complexType": { "original": "ShortKeyPropType", "resolved": "string | undefined", "references": { "ShortKeyPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::ShortKeyPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Adds a visual shortcut hint after the label and instructs the screen reader to read the shortcut aloud." }, "getter": false, "setter": false, "reflect": false, "attribute": "_short-key" }, "_tabIndex": { "type": "number", "mutable": false, "complexType": { "original": "number", "resolved": "number | undefined", "references": {} }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines which tab-index the primary element of the component has. (https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/tabindex)" }, "getter": false, "setter": false, "reflect": false, "attribute": "_tab-index" }, "_target": { "type": "string", "mutable": false, "complexType": { "original": "LinkTargetPropType", "resolved": "string | undefined", "references": { "LinkTargetPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::LinkTargetPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines where to open the link." }, "getter": false, "setter": false, "reflect": false, "attribute": "_target" }, "_tooltipAlign": { "type": "string", "mutable": false, "complexType": { "original": "TooltipAlignPropType", "resolved": "\"bottom\" | \"left\" | \"right\" | \"top\" | undefined", "references": { "TooltipAlignPropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::TooltipAlignPropType" } } }, "required": false, "optional": true, "docs": { "tags": [], "text": "Defines where to show the Tooltip preferably: top, right, bottom or left." }, "getter": false, "setter": false, "reflect": false, "attribute": "_tooltip-align", "defaultValue": "'right'" }, "_variant": { "type": "string", "mutable": false, "complexType": { "original": "VariantClassNamePropType", "resolved": "string | undefined", "references": { "VariantClassNamePropType": { "location": "import", "path": "../../schema", "id": "src/schema/index.ts::VariantClassNamePropType" } } }, "required": false, "optional": true, "docs": { "tags": [{ "name": "internal", "text": undefined }], "text": "Defines which button variant should be used for presentation." }, "getter": false, "setter": false, "reflect": false, "attribute": "_variant" } }; } static get states() { return { "state": {} }; } static get methods() { return { "focus": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<void>" }, "docs": { "text": "Sets focus on the internal element.", "tags": [] } }, "click": { "complexType": { "signature": "() => Promise<void>", "parameters": [], "references": { "Promise": { "location": "global", "id": "global::Promise" } }, "return": "Promise<void>" }, "docs": { "text": "Clicks the primary interactive element inside this component.", "tags": [] } } }; } static get elementRef() { return "host"; } static get watchers() { return [{ "propName": "_accessKey", "methodName": "validateAccessKey" }, { "propName": "_ariaCurrentValue", "methodName": "validateAriaCurrentValue" }, { "propName": "_ariaControls", "methodName": "validateAriaControls" }, { "propName": "_ariaDescription", "methodName": "validateAriaDescription" }, { "propName": "_ariaExpanded", "methodName": "validateAriaExpanded" }, { "propName": "_ariaOwns", "methodName": "validateAriaOwns" }, { "propName": "_customClass", "methodName": "validateCustomClass" }, { "propName": "_disabled", "methodName": "validateDisabled" }, { "propName": "_download", "methodName": "validateDownload" }, { "propName": "_hideLabel", "methodName": "validateHideLabel" }, { "propName": "_href", "methodName": "validateHref" }, { "propName": "_icons", "methodName": "validateIcons" }, { "propName": "_inline", "methodName": "validateInline" }, { "propName": "_label", "methodName": "validateLabel" }, { "propName": "_on", "methodName": "validateOn" }, { "propName": "_role", "methodName": "validateRole" }, { "propName": "_shortKey", "methodName": "validateShortKey" }, { "propName": "_tabIndex", "methodName": "validateTabIndex" }, { "propName": "_target", "methodName": "validateTarget" }, { "propName": "_tooltipAlign", "methodName": "validateTooltipAlign" }, { "propName": "_variant", "methodName": "validateVariantClassName" }]; } } __decorate([ directFocus('ctaRef') ], KolLinkWc.prototype, "focus", null); __decorate([ directClick('ctaRef') ], KolLinkWc.prototype, "click", null); //# sourceMappingURL=component.js.map