UNPKG

@public-ui/components

Version:

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

328 lines (321 loc) 24.8 kB
/*! * KoliBri - The accessible HTML-Standard */ 'use strict'; var index = require('./index-BrhW8s5h.js'); var tslib_es6 = require('./tslib.es6-Cm0ytgPY.js'); var common = require('./common-DPb6NWR4.js'); var align = require('./align-B8NMKvjk.js'); var label = require('./label-8vcJJEVI.js'); var componentNames = require('./component-names-5KS_pYRF.js'); var i18n = require('./i18n-Cjy0vgJA.js'); var keyboard = require('./keyboard-BfFtSnNy.js'); var clsx = require('./clsx-Bm_HQUnh.js'); var elementInteraction = require('./element-interaction-CXkAVgKs.js'); var events = require('./events-Jc2wxPjR.js'); require('./i18n-CgUN6lev.js'); require('./element-focus-DeVevBcF.js'); const tabBehaviorPropTypeOptions = ['select-automatic', 'select-manual']; const validateTabBehavior = (component, value) => { common.watchValidator(component, `_behavior`, (value) => typeof value === 'string' && tabBehaviorPropTypeOptions.includes(value), new Set([`KoliBriTabBehavior {${tabBehaviorPropTypeOptions.join(', ')}`]), value); }; const validateHasCreateButton = (component, value) => { common.watchBoolean(component, '_hasCreateButton', value); }; const defaultStyleCss = "/* forward the rem function */\n/*\n* This file defines the layer order for all CSS layers used in KoliBri.\n* The order is important as it determines the cascade priority.\n*\n* Layer order (lowest to highest priority):\n* 1. kol-a11y - Accessibility defaults and requirements\n* 2. kol-global - Global component styles and resets\n* 3. kol-component - Component-specific styles\n* 4. kol-theme-global - Theme-specific global styles\n* 5. kol-theme-component - Theme-specific component styles\n*/\n@layer kol-a11y, kol-global, kol-component, kol-theme-global, kol-theme-component;\n/*\n * This file contains all rules for accessibility.\n */\n@layer kol-a11y {\n :host {\n /*\n * Minimum size of interactive elements.\n */\n --a11y-min-size: calc(44 * 1rem / var(--kolibri-root-font-size, 16));\n /*\n * No element should be used without verifying the contrast ratio of its background and font colors.\n * By initially setting the background color to white and the font color to black,\n * the contrast ratio is ensured and explicit adjustment is forced.\n */\n color: black;\n background-color: white;\n /*\n * Verdana is an accessible font that can be used without requiring additional loading time.\n */\n font-family: Verdana;\n /*\n * Letter spacing is required for all texts.\n */\n letter-spacing: inherit;\n /*\n * Word spacing is required for all texts.\n */\n word-spacing: inherit;\n /*\n * Text should be aligned left by default to provide a predictable starting point.\n */\n text-align: left;\n }\n * {\n /*\n * This rule enables the word dividing for all texts. That is important for high zoom levels.\n */\n hyphens: auto;\n /*\n * This rule enables the word dividing for all texts. That is important for high zoom levels.\n */\n word-break: break-word;\n }\n /*\n * All interactive elements should have a minimum size of to-rem(44).\n */\n /* input:not([type='checkbox'], [type='radio'], [type='range']), */\n /* option, */\n /* select, */\n /* textarea, */\n button,\n .kol-input .input {\n min-width: var(--a11y-min-size);\n min-height: var(--a11y-min-size);\n }\n /*\n * Some interactive elements should not inherit the font-family and font-size.\n */\n a,\n button,\n h1,\n h2,\n h3,\n h4,\n h5,\n h6,\n input,\n option,\n select,\n textarea {\n /*\n * All elements should inherit the text color from his parent element.\n */\n color: inherit;\n /*\n * All elements should inherit the font family from his parent element.\n */\n font-family: inherit;\n /*\n * All elements should inherit the font size from his parent element.\n */\n font-size: inherit;\n /*\n * Letter spacing is required for all texts.\n */\n letter-spacing: inherit;\n /*\n * Word spacing is required for all texts.\n */\n word-spacing: inherit;\n }\n /**\n * Sometimes we need the semantic element for accessibility reasons,\n * but we don't want to show it.\n *\n * - https://www.a11yproject.com/posts/how-to-hide-content/\n */\n .visually-hidden {\n position: fixed;\n top: 0;\n left: 0;\n width: 1px;\n height: 1px;\n overflow: hidden;\n white-space: nowrap;\n clip-path: inset(50%);\n }\n}\n@layer kol-global {\n /*\n * Dieses CSS stellt sicher, dass der Standard-Style\n * von A und Button resettet werden.\n */\n :is(a, button) {\n background-color: transparent;\n width: 100%;\n margin: 0;\n padding: 0;\n border: none;\n /* 100% needed for custom width from outside */\n }\n /*\n * Ensure elements with hidden attribute to be actually not visible\n * @see https://meowni.ca/hidden.is.a.lie.html\n */\n [hidden] {\n display: none !important;\n }\n .badge-text-hint {\n color: black;\n background-color: white;\n }\n}\n@layer kol-global {\n :host {\n /*\n * The max-width is needed to prevent the table from overflowing the\n * parent node, if the table is wider than the parent node.\n */\n max-width: 100%;\n font-size: calc(16 * 1rem / var(--kolibri-root-font-size, 16));\n }\n * {\n /*\n * We prefer to box-sizing: border-box for all elements.\n */\n box-sizing: border-box;\n }\n .kol-span {\n /* KolSpan is a layout component with icons in all directions and a label text in the middle. */\n display: flex;\n flex-flow: column;\n align-items: center;\n justify-content: center;\n /* The sub span in KolSpan is the horizontal span with icon left and right and the label text in the middle. */\n }\n .kol-span__container {\n display: flex;\n align-items: center;\n }\n a,\n button {\n cursor: pointer;\n }\n .kol-span .kol-span__label--hide-label .kol-span__label {\n display: none;\n }\n /* Reset browser agent style. */\n button:disabled {\n color: unset;\n }\n .disabled label,\n .disabled:focus-within label,\n [aria-disabled=true],\n [aria-disabled=true]:focus,\n [disabled],\n [disabled]:focus {\n outline: none;\n cursor: not-allowed;\n }\n [aria-disabled=true]:focus .kol-span,\n [disabled]:focus .kol-span {\n outline: none !important;\n }\n .hastooltip {\n z-index: 900 !important;\n }\n}\n@layer kol-component {\n :host {\n display: block;\n }\n}\n@font-face {\n font-family: \"kolicons\";\n src: url(\"kolicons.eot?t=1780571516250\"); /* IE9*/\n src: url(\"kolicons.eot?t=1780571516250#iefix\") format(\"embedded-opentype\"), url(\"kolicons.woff2?t=1780571516250\") format(\"woff2\"), url(\"kolicons.woff?t=1780571516250\") format(\"woff\"), url(\"kolicons.ttf?t=1780571516250\") format(\"truetype\"), url(\"kolicons.svg?t=1780571516250#kolicons\") format(\"svg\"); /* iOS 4.1- */\n}\n@layer kol-component {\n [class^=kolicon-], [class*=\" kolicon-\"] {\n font-family: \"kolicons\";\n font-style: normal;\n font-weight: 400;\n line-height: 1em;\n -webkit-font-smoothing: antialiased;\n -moz-osx-font-smoothing: grayscale;\n }\n .kolicon-alert-error::before {\n content: \"\\ea01\";\n }\n .kolicon-alert-info::before {\n content: \"\\ea02\";\n }\n .kolicon-alert-success::before {\n content: \"\\ea03\";\n }\n .kolicon-alert-warning::before {\n content: \"\\ea04\";\n }\n .kolicon-check::before {\n content: \"\\ea05\";\n }\n .kolicon-chevron-double-left::before {\n content: \"\\ea06\";\n }\n .kolicon-chevron-double-right::before {\n content: \"\\ea07\";\n }\n .kolicon-chevron-down::before {\n content: \"\\ea08\";\n }\n .kolicon-chevron-left::before {\n content: \"\\ea09\";\n }\n .kolicon-chevron-right::before {\n content: \"\\ea0a\";\n }\n .kolicon-chevron-up::before {\n content: \"\\ea0b\";\n }\n .kolicon-cogwheel::before {\n content: \"\\ea0c\";\n }\n .kolicon-cross::before {\n content: \"\\ea0d\";\n }\n .kolicon-eye-closed::before {\n content: \"\\ea0e\";\n }\n .kolicon-eye::before {\n content: \"\\ea0f\";\n }\n .kolicon-house::before {\n content: \"\\ea10\";\n }\n .kolicon-kolibri::before {\n content: \"\\ea11\";\n }\n .kolicon-link-external::before {\n content: \"\\ea12\";\n }\n .kolicon-link::before {\n content: \"\\ea13\";\n }\n .kolicon-minus::before {\n content: \"\\ea14\";\n }\n .kolicon-plus::before {\n content: \"\\ea15\";\n }\n .kolicon-settings::before {\n content: \"\\ea16\";\n }\n .kolicon-sort-asc::before {\n content: \"\\ea17\";\n }\n .kolicon-sort-desc::before {\n content: \"\\ea18\";\n }\n .kolicon-sort-neutral::before {\n content: \"\\ea19\";\n }\n .kolicon-up::before {\n content: \"\\ea1a\";\n }\n .kolicon-version::before {\n content: \"\\ea1b\";\n }\n}\n@layer kol-component {\n .kol-icon {\n color: inherit;\n display: inline-block;\n font-size: inherit;\n font-weight: inherit;\n line-height: inherit;\n }\n .kol-tooltip {\n display: contents;\n }\n .kol-tooltip__floating {\n opacity: 0;\n display: none;\n position: fixed;\n /* Avoid layout interference - see https://floating-ui.com/docs/computePosition */\n top: 0;\n left: 0;\n /* Can be used to specify the tooltip-width from the outside. Unset by default. */\n width: var(--kol-tooltip-width, max-content);\n min-width: calc(8 * 1rem / var(--kolibri-root-font-size, 16));\n max-width: 90vw;\n max-height: 90vh;\n animation-direction: normal;\n /* Can be used to specify the animation duration from the outside. 250ms by default. */\n animation-duration: var(--kolibri-tooltip-animation-duration, 250ms);\n animation-fill-mode: forwards;\n animation-iteration-count: 1;\n animation-timing-function: ease-in;\n }\n .kol-tooltip__floating.hide {\n animation-name: hideTooltip;\n }\n .kol-tooltip__floating.show {\n animation-name: showTooltip;\n }\n .kol-tooltip__arrow {\n transform: rotate(45deg);\n color: black;\n background-color: white;\n position: absolute;\n z-index: 999;\n width: calc(10 * 1rem / var(--kolibri-root-font-size, 16));\n height: calc(10 * 1rem / var(--kolibri-root-font-size, 16));\n }\n .kol-tooltip__content {\n color: black;\n background-color: white;\n position: relative;\n z-index: 1000;\n }\n @keyframes hideTooltip {\n 0% {\n opacity: 1;\n }\n 100% {\n opacity: 0;\n display: none;\n }\n }\n @keyframes showTooltip {\n 0% {\n opacity: 0;\n }\n 100% {\n opacity: 1;\n }\n }\n}\n@layer kol-component {\n .kol-icon {\n color: inherit;\n display: inline-block;\n font-size: inherit;\n font-weight: inherit;\n line-height: inherit;\n }\n .kol-tabs {\n display: var(--display);\n grid-template-columns: var(--grid-template-columns);\n grid-template-rows: var(--grid-template-rows);\n }\n .kol-tabs--align-right {\n --display: grid;\n --grid-template-columns: 1fr auto;\n --button-group-flex-direction: column;\n --button-group-order: 2;\n }\n .kol-tabs--align-left {\n --display: grid;\n --grid-template-columns: auto 1fr;\n --button-group-flex-direction: column;\n --button-group-order: 0;\n }\n .kol-tabs--align-bottom {\n --display: grid;\n --grid-template-rows: 1fr auto;\n --button-group-order: 2;\n }\n .kol-tabs__tabs-align-top {\n --display: grid;\n --grid-template-rows: auto 1fr;\n --button-group-order: 0;\n }\n .kol-tabs__button-group {\n display: flex;\n order: var(--button-group-order);\n flex-direction: var(--button-group-flex-direction);\n flex-wrap: wrap;\n }\n .kol-tabs__button-group .kol-button {\n border-bottom-color: transparent;\n display: block;\n border-bottom-style: solid;\n }\n .kol-tabs__panel {\n height: 100%;\n }\n}"; const KolTabs = class { constructor(hostRef) { index.registerInstance(this, hostRef); this.onCreateLabel = `${i18n.translate('kol-new')} `; this.ctaRef = elementInteraction.createCtaRef(); this.nextPossibleTabIndex = (tabs, offset, step = 1) => { const nextOffset = offset + step; if (nextOffset < tabs.length) { if (tabs[nextOffset]._disabled) { return this.nextPossibleTabIndex(tabs, offset, step + 1); } return nextOffset; } return offset; }; this.prevPossibleTabIndex = (tabs, offset, step = 1) => { const nextOffset = offset - step; if (nextOffset >= 0) { if (tabs[nextOffset]._disabled) { return this.prevPossibleTabIndex(tabs, offset, step + 1); } return nextOffset; } return offset; }; this.onKeyDown = (event) => { switch (event.key) { case keyboard.KeyboardKey.ArrowRight: this.goToNextTab(event); break; case keyboard.KeyboardKey.ArrowLeft: this.goToPreviousTab(event); break; case keyboard.KeyboardKey.Space: case keyboard.KeyboardKey.Enter: this.activateFocusedTab(event); break; } }; this.onClickSelect = (event, index) => { this.selectNextTabEvent(event, index); }; this.onMouseDown = (event) => { event.preventDefault(); }; this.callbacks = { onClick: this.onClickSelect, onMouseDown: this.onMouseDown, }; this.catchTabPanelHost = (el) => { this.tabPanelHost = el; }; this._align = 'top'; this._hasCreateButton = false; this._selected = 0; this.state = { _align: 'top', _label: '', _selected: 0, _tabs: [], }; this.selectNextNotDisabledTab = (selected, tabs, upOrDown = true, initialSelected) => { if (selected > tabs.length - 1) { selected = tabs.length - 1; } if (selected < 0) { selected = 0; } if (Array.isArray(tabs) && tabs[selected]) { if (tabs[selected]._disabled) { if (upOrDown === true) { if (selected < tabs.length - 1) { return this.selectNextNotDisabledTab(selected + 1, tabs, true, initialSelected || selected); } else { selected = initialSelected || selected; upOrDown = false; } } if (upOrDown === false) { if (selected > 0) { return this.selectNextNotDisabledTab(selected - 1, tabs, false, initialSelected || selected); } else { common.devHint(`[KolTabs] All tabs are disabled, and therefore no tab can be displayed.`); } } } } return selected; }; this.syncSelectedAndTabs = (nextValue, nextState, _component, key) => { let selected; if (key === '_selected') { selected = nextValue; } else { selected = this.state._selected; } let tabs; if (key === '_tabs') { tabs = nextValue; } else { tabs = this.state._tabs; } if (tabs.length > 0) { nextState.set('_selected', this.selectNextNotDisabledTab(selected, tabs)); } }; this.refreshTabPanels = () => { var _a, _b, _c, _d; if (!this.tabPanelHost) return; while (this.tabPanelHost.firstChild) { this.tabPanelHost.removeChild(this.tabPanelHost.firstChild); } for (let i = 0; i < ((_a = this.state._tabs) === null || _a === void 0 ? void 0 : _a.length); i++) { const div = document.createElement('div'); div.setAttribute('aria-labelledby', `${this.state._label.replace(/\s/g, '-')}-tab-${i}`); div.setAttribute('id', `tabpanel-${i}`); div.setAttribute('role', 'tabpanel'); div.setAttribute('hidden', ''); div.setAttribute('tabindex', '0'); const slot = document.createElement('slot'); slot.setAttribute('name', `tabpanel-slot-${i}`); div.appendChild(slot); (_b = this.tabPanelHost) === null || _b === void 0 ? void 0 : _b.appendChild(div); if (typeof HTMLCollection !== 'undefined' && ((_c = this.host) === null || _c === void 0 ? void 0 : _c.children) instanceof HTMLCollection && ((_d = this.host) === null || _d === void 0 ? void 0 : _d.children[i])) { this.host.children[i].setAttribute('slot', `tabpanel-slot-${i}`); } } this.updateVisiblePanel(); }; this.updateVisiblePanel = () => { if (!this.tabPanelHost) return; Array.from(this.tabPanelHost.children).forEach((child, i) => { if (i === this.state._selected) { child.removeAttribute('hidden'); } else { child.setAttribute('hidden', ''); } }); }; this.onCreate = (event) => { var _a, _b; event.preventDefault(); (_b = (_a = this.state._on) === null || _a === void 0 ? void 0 : _a.onCreate) === null || _b === void 0 ? void 0 : _b.call(_a, event); if (this.host) { events.dispatchDomEvent(this.host, events.KolEvent.create); } }; this.onBlur = () => { this.currentFocusIndex = undefined; }; } getCurrentFocusIndex() { if (typeof this.currentFocusIndex === 'number') { return this.currentFocusIndex; } return this.state._selected; } getKeyboardTabChangeMode() { if (this._behavior === 'select-manual') { return 'selectFocusOnly'; } return 'activateCompletely'; } goToNextTab(event) { const nextFocusIndex = this.nextPossibleTabIndex(this.state._tabs, this.getCurrentFocusIndex()); this.selectNextTabEvent(event, nextFocusIndex, this.getKeyboardTabChangeMode()); } goToPreviousTab(event) { const nextFocusIndex = this.prevPossibleTabIndex(this.state._tabs, this.getCurrentFocusIndex()); this.selectNextTabEvent(event, nextFocusIndex, this.getKeyboardTabChangeMode()); } activateFocusedTab(event) { if (typeof this.currentFocusIndex === 'number') { this.onSelect(event, this.currentFocusIndex); } } selectNextTabEvent(event, nextTabIndex, changeMode = 'activateCompletely') { var _a, _b; this.currentFocusIndex = nextTabIndex; this.focusTabById(nextTabIndex); if (changeMode === 'activateCompletely') { this._selected = nextTabIndex; const tab = this.state._tabs[nextTabIndex]; (_b = (_a = tab._on) === null || _a === void 0 ? void 0 : _a.onSelect) === null || _b === void 0 ? void 0 : _b.call(_a, event, nextTabIndex); this.onSelect(event, nextTabIndex); } } async focus() { } async click() { } renderButtonGroup() { return (index.h("div", { "aria-label": this.state._label, class: "kol-tabs__button-group", role: "tablist", onKeyDown: this.onKeyDown, onBlur: this.onBlur }, this.state._tabs.map((button, index$1) => (index.h(componentNames.KolButtonWcTag, { ref: this.state._selected === index$1 ? this.ctaRef : undefined, _disabled: button._disabled, _icons: button._icons, _hideLabel: button._hideLabel, _label: button._label, _on: this.callbacks, _tabIndex: this.state._selected === index$1 ? 0 : -1, _tooltipAlign: button._tooltipAlign, _variant: this.state._selected === index$1 ? 'custom' : undefined, _customClass: this.state._selected === index$1 ? 'selected' : '', _ariaControls: `tabpanel-${index$1}`, _ariaSelected: this.state._selected === index$1, _id: `${this.state._label.replace(/\s/g, '-')}-tab-${index$1}`, _role: "tab", _value: index$1 }))), this.state._hasCreateButton && (index.h(componentNames.KolButtonWcTag, { class: "kol-tabs__button-create", _label: this.onCreateLabel, _on: { onClick: this.onCreate, }, _icons: "kolicon-plus", "data-testid": "tabs-create-button" })))); } render() { return (index.h("div", { key: '03faa28ca436d156581f60b422a46eca57ff9d48', ref: (el) => { this.tabPanelsElement = el; }, class: clsx.clsx('kol-tabs', `kol-tabs--align-${this.state._align}`) }, this.renderButtonGroup(), index.h("div", { key: '61e93a5f157c7d31a60ec9fef085422955cb7311', class: "kol-tabs__content", ref: this.catchTabPanelHost }))); } validateAlign(value) { align.validateAlign(this, value); } validateBehavior(value) { validateTabBehavior(this, value); } validateHasCreateButton(value) { validateHasCreateButton(this, value); } validateLabel(value) { label.validateLabel(this, value, { required: true, }); } validateOn(value) { if (typeof value === 'object' && value !== null) { const callbacks = {}; if (typeof value.onCreate === 'function') { callbacks.onCreate = value.onCreate; } if (typeof value.onSelect === 'function') { callbacks.onSelect = value.onSelect; } common.setState(this, '_on', callbacks); } } validateSelected(value) { common.watchNumber(this, '_selected', value, { hooks: { beforePatch: this.syncSelectedAndTabs, }, }); } validateTabs(value) { common.watchJsonArrayString(this, '_tabs', (item) => typeof item === 'object' && item !== null && typeof item._label === 'string' && item._label.length > 0, value, undefined, { hooks: { beforePatch: this.syncSelectedAndTabs, afterPatch: this.refreshTabPanels, }, }); common.uiUxHintMillerscheZahl('KolTabs', this.state._tabs.length); } componentWillLoad() { this.validateAlign(this._align); this.validateLabel(this._label); this.validateOn(this._on); this.validateSelected(this._selected); this.validateTabs(this._tabs); this.validateBehavior(this._behavior); this.validateHasCreateButton(this._hasCreateButton); } componentDidRender() { this.refreshTabPanels(); } focusTabById(index) { if (this.tabPanelsElement) { const button = common.koliBriQuerySelector(`button#${this.state._label.replace(/\s/g, '-')}-tab-${index}`, this.tabPanelsElement); button === null || button === void 0 ? void 0 : button.focus(); } } onSelect(event, index) { var _a, _b; (_b = (_a = this._on) === null || _a === void 0 ? void 0 : _a.onSelect) === null || _b === void 0 ? void 0 : _b.call(_a, event, index); if (this.host) { events.dispatchDomEvent(this.host, events.KolEvent.select, index); } this.focusTabById(index); } get host() { return index.getElement(this); } static get watchers() { return { "_align": ["validateAlign"], "_behavior": ["validateBehavior"], "_hasCreateButton": ["validateHasCreateButton"], "_label": ["validateLabel"], "_on": ["validateOn"], "_selected": ["validateSelected"], "_tabs": ["validateTabs"] }; } }; tslib_es6.__decorate([ elementInteraction.delegateFocus('ctaRef') ], KolTabs.prototype, "focus", null); tslib_es6.__decorate([ elementInteraction.delegateClick('ctaRef') ], KolTabs.prototype, "click", null); KolTabs.style = { default: defaultStyleCss }; exports.kol_tabs = KolTabs; //# sourceMappingURL=kol-tabs.entry.cjs.js.map