@public-ui/components
Version:
Contains all web components that belong to KoliBri - The accessible HTML-Standard.
307 lines (293 loc) • 19.5 kB
JavaScript
/*!
* KoliBri - The accessible HTML-Standard
*/
import { h, proxyCustomElement, Host } from '@stencil/core/internal/client';
import { B as BaseWebComponent } from './variant-quote.js';
import { t as translate } from './i18n.js';
import './common.js';
import './prop.validators.js';
import { c as createPropDefinition, b as normalizeNumber, n as normalizeString } from './normalizers.js';
import { l as labelProp } from './label2.js';
import { m as maxProp, u as unitProp, c as clampedNumberValueProp } from './value-number-clamped.js';
import { B as BaseController } from './base-controller.js';
const highProp = createPropDefinition('high', 0, normalizeNumber);
const lowProp = createPropDefinition('low', 0, normalizeNumber);
const minProp = createPropDefinition('min', 0, normalizeNumber);
const optimumProp = createPropDefinition('optimum', 0, normalizeNumber);
const orientationOptions = ['horizontal', 'vertical'];
const orientationProp = createPropDefinition('orientation', 'horizontal', (value) => {
const str = normalizeString(value);
if (orientationOptions.includes(str)) {
return str;
}
throw new Error(`Invalid orientation: ${str}`);
});
const numberValueProp = createPropDefinition('value', 0, normalizeNumber, (v) => v >= 0);
function getMeterState(value, min, max, low, high, optimum) {
const effectiveLow = low !== null && low !== void 0 ? low : min;
const effectiveHigh = high !== null && high !== void 0 ? high : max;
if (optimum === undefined) {
const inMidRegion = value >= effectiveLow && value <= effectiveHigh;
return inMidRegion ? 'optimum' : 'suboptimal';
}
const inLowRegion = value < effectiveLow;
const inHighRegion = value > effectiveHigh;
const inMidRegion = !inLowRegion && !inHighRegion;
const optimumInLow = optimum < effectiveLow;
const optimumInHigh = optimum > effectiveHigh;
if (optimumInLow) {
if (inLowRegion)
return 'optimum';
if (inMidRegion)
return 'suboptimal';
return 'critical';
}
else if (optimumInHigh) {
if (inHighRegion)
return 'optimum';
if (inMidRegion)
return 'suboptimal';
return 'critical';
}
else {
if (inMidRegion)
return 'optimum';
return 'suboptimal';
}
}
const MeterFC = (props) => {
const { high, label, low, liveValue, max, min, optimum, orientation, unit, value } = props;
const isVertical = orientation === 'vertical';
const isPercentage = unit === '%';
const displayValue = isPercentage ? Math.round(((value - min) / (max - min)) * 100) : value;
const liveMeterValue = isPercentage ? `${Math.round(((liveValue - min) / (max - min)) * 100)}` : liveValue;
const state = getMeterState(value, min, max, low, high, optimum);
const hasStateClassification = low !== undefined || high !== undefined;
const stateLabel = hasStateClassification ? translate(`kol-meter-state-${state}`) : '';
const liveValueText = isPercentage
? translate('kol-live-value', { placeholders: { value: String(liveMeterValue), unit } })
: translate('kol-live-value-bounded', { placeholders: { value: String(liveMeterValue), max: String(max), unit } });
const liveValueWithState = hasStateClassification ? `${liveValueText} – ${stateLabel}` : liveValueText;
const charCount = max.toString().length > min.toString().length ? max.toString().length + 'ch' : min.toString().length + 'ch';
return (h("div", { class: { 'kol-meter': true, 'kol-meter--vertical': isVertical } }, h("div", { class: "kol-meter__bar" }, h("div", { class: "kol-meter__bar-label" }, label, hasStateClassification && (h("span", { class: `kol-meter__bar-state kol-meter__bar-state--${state}` }, ' – ', stateLabel))), h("div", { class: "kol-meter__bar-track" }, h("meter", { "aria-label": label, high: high, low: low, max: max, min: min, optimum: optimum, value: value })), h("span", { class: "kol-meter__value-unit" }, h("span", { class: "kol-meter__value", style: { 'min-width': charCount } }, displayValue), h("span", { class: "kol-meter__unit" }, unit))), h("span", { "aria-live": "polite", "aria-relevant": "additions text", class: "visually-hidden" }, liveValueWithState)));
};
const meterPropsConfig = {
optional: [minProp, orientationProp, unitProp],
required: [labelProp, maxProp, numberValueProp],
};
class MeterController extends BaseController {
constructor(stateAccess) {
super(stateAccess, meterPropsConfig);
this.meterData = { high: undefined, low: undefined, optimum: undefined };
}
componentWillLoad(props) {
const { high, label, low, max, min, optimum, orientation, unit, value } = props;
this.watchHigh(high);
this.watchLabel(label);
this.watchLow(low);
this.watchMax(max);
this.watchMin(min);
this.watchOptimum(optimum);
this.watchOrientation(orientation);
this.watchUnit(unit);
this.watchValue(value);
this.setState('liveValue', this.getRenderProp('value'));
this.startLiveValueInterval();
}
destroy() {
if (this.interval) {
clearInterval(this.interval);
this.interval = undefined;
}
}
getMeterData() {
return this.meterData;
}
watchHigh(value) {
if (value === undefined) {
this.meterData.high = undefined;
}
else {
highProp.apply(value, (v) => {
this.meterData.high = v;
});
}
}
watchLabel(value) {
labelProp.apply(value, (v) => {
this.setRenderProp('label', v);
});
}
watchLow(value) {
if (value === undefined) {
this.meterData.low = undefined;
}
else {
lowProp.apply(value, (v) => {
this.meterData.low = v;
});
}
}
watchMax(value) {
maxProp.apply(value, (v) => {
this.setRenderProp('max', v);
this.watchValue(this.getRawProp('value'));
});
}
watchMin(value) {
minProp.apply(value, (v) => {
this.setRenderProp('min', v);
this.watchValue(this.getRawProp('value'));
});
}
watchOptimum(value) {
if (value === undefined) {
this.meterData.optimum = undefined;
}
else {
optimumProp.apply(value, (v) => {
this.meterData.optimum = v;
});
}
}
watchOrientation(value) {
orientationProp.apply(value, (v) => {
this.setRenderProp('orientation', v);
});
}
watchUnit(value) {
unitProp.apply(value, (v) => {
this.setRenderProp('unit', v);
});
}
watchValue(value) {
this.setRawProp('value', value);
clampedNumberValueProp.apply(value, (v) => {
this.setRenderProp('value', v);
}, { min: this.getRenderProp('min'), max: this.getRenderProp('max') });
}
startLiveValueInterval() {
this.interval = setInterval(() => {
const value = this.getRenderProp('value');
if (this.getState('liveValue') !== value) {
this.setState('liveValue', value);
}
}, 5000);
}
}
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 .kol-meter {\n --color-optimal: var(--kol-meter-color-optimal, #2ea32e);\n --color-suboptimal: var(--kol-meter-color-suboptimal, #c8a000);\n --color-critical: var(--kol-meter-color-critical, #c00);\n --vertical-height: var(--kol-meter-vertical-height, 128);\n --horizontal-height: var(--kol-meter-horizontal-height, 12);\n }\n .kol-meter__bar {\n display: grid;\n align-items: center;\n grid-template-areas: \"label label\" \"bar value\";\n grid-template-columns: 1fr auto;\n }\n .kol-meter__bar-label {\n grid-column-end: 2;\n grid-area: label;\n }\n .kol-meter__bar-track {\n display: flex;\n min-width: 100%;\n grid-area: bar;\n }\n .kol-meter__value-unit {\n grid-area: value;\n }\n .kol-meter:not(.kol-meter--vertical) .kol-meter__value {\n display: inline-block;\n text-align: right;\n }\n .kol-meter--vertical {\n min-height: 100%;\n }\n .kol-meter--vertical .kol-meter__bar {\n min-height: 100%;\n align-items: end;\n justify-items: center;\n grid-template-areas: \"label\" \"bar\" \"value\";\n grid-template-columns: 1fr;\n grid-template-rows: min-content 1fr min-content;\n }\n .kol-meter--vertical .kol-meter__bar-label {\n text-align: center;\n }\n .kol-meter--vertical .kol-meter__bar-track {\n position: relative;\n min-width: 0;\n min-height: calc(var(--vertical-height) * 1rem / var(--kolibri-root-font-size, 16));\n align-items: center;\n }\n .kol-meter--vertical .kol-meter__bar meter {\n transform: rotate(-90deg);\n transform-origin: left bottom;\n position: absolute;\n bottom: 0;\n width: calc(var(--vertical-height) * 1rem / var(--kolibri-root-font-size, 16));\n }\n .kol-meter {\n /* Base meter styling */\n }\n .kol-meter meter {\n width: 100%;\n height: calc(var(--horizontal-height) * 1rem / var(--kolibri-root-font-size, 16));\n /* Remove default appearance in some browsers */\n -webkit-appearance: none;\n -moz-appearance: none;\n appearance: none;\n }\n .kol-meter {\n /* Chrome, Safari, Edge (WebKit/Blink) */\n }\n .kol-meter meter::-webkit-meter-bar {\n background: white;\n height: calc(var(--horizontal-height) * 1rem / var(--kolibri-root-font-size, 16));\n border: 1px solid black;\n }\n .kol-meter meter::-webkit-meter-optimum-value {\n background: var(--color-optimal);\n }\n .kol-meter meter::-webkit-meter-suboptimum-value {\n background: var(--color-suboptimal);\n }\n .kol-meter meter::-webkit-meter-even-less-good-value {\n background: var(--color-critical);\n }\n .kol-meter {\n /* Firefox */\n }\n @supports (selector(::-moz-meter-bar)) {\n .kol-meter meter {\n background: white;\n border: 1px solid black;\n }\n }\n .kol-meter meter::-moz-meter-bar {\n background: var(--color-optimal);\n }\n .kol-meter meter:-moz-meter-sub-optimum::-moz-meter-bar {\n background: var(--color-suboptimal);\n }\n .kol-meter meter:-moz-meter-sub-sub-optimum::-moz-meter-bar {\n background: var(--color-critical);\n }\n}";
const KolMeter$1 = proxyCustomElement(class KolMeter extends BaseWebComponent {
constructor(registerHost) {
super(false);
if (registerHost !== false) {
this.__registerHost();
}
this.__attachShadow();
this.ctrl = new MeterController(this.stateAccess);
this._max = 1;
this._min = 0;
this._orientation = 'horizontal';
this._unit = '%';
this.liveValue = 0;
}
watchHigh(value) {
this.ctrl.watchHigh(value);
}
watchLabel(value) {
this.ctrl.watchLabel(value);
}
watchLow(value) {
this.ctrl.watchLow(value);
}
watchMax(value) {
this.ctrl.watchMax(value);
}
watchMin(value) {
this.ctrl.watchMin(value);
}
watchOptimum(value) {
this.ctrl.watchOptimum(value);
}
watchOrientation(value) {
this.ctrl.watchOrientation(value);
}
watchUnit(value) {
this.ctrl.watchUnit(value);
}
watchValue(value) {
this.ctrl.watchValue(value);
}
componentWillLoad() {
this.ctrl.componentWillLoad({
high: this._high,
label: this._label,
low: this._low,
max: this._max,
min: this._min,
optimum: this._optimum,
orientation: this._orientation,
unit: this._unit,
value: this._value,
});
}
disconnectedCallback() {
this.ctrl.destroy();
}
render() {
const { high, low, optimum } = this.ctrl.getMeterData();
return (h(Host, { key: 'f984ca58fde313ec89e7c3706bcc8abc78ccd0c9' }, h(MeterFC, { key: 'cc748329ed165491feb61315348e0b44c8797197', high: high, label: this.ctrl.getRenderProp('label'), low: low, liveValue: this.liveValue, max: this.ctrl.getRenderProp('max'), min: this.ctrl.getRenderProp('min'), optimum: optimum, orientation: this.ctrl.getRenderProp('orientation'), unit: this.ctrl.getRenderProp('unit'), value: this.ctrl.getRenderProp('value') })));
}
static get watchers() { return {
"_high": ["watchHigh"],
"_label": ["watchLabel"],
"_low": ["watchLow"],
"_max": ["watchMax"],
"_min": ["watchMin"],
"_optimum": ["watchOptimum"],
"_orientation": ["watchOrientation"],
"_unit": ["watchUnit"],
"_value": ["watchValue"]
}; }
static get style() { return {
default: defaultStyleCss
}; }
}, [801, "kol-meter", {
"_high": [2],
"_label": [1],
"_low": [2],
"_max": [2],
"_min": [2],
"_optimum": [2],
"_orientation": [1],
"_unit": [1],
"_value": [2],
"liveValue": [32]
}, undefined, {
"_high": ["watchHigh"],
"_label": ["watchLabel"],
"_low": ["watchLow"],
"_max": ["watchMax"],
"_min": ["watchMin"],
"_optimum": ["watchOptimum"],
"_orientation": ["watchOrientation"],
"_unit": ["watchUnit"],
"_value": ["watchValue"]
}]);
function defineCustomElement$1() {
if (typeof customElements === "undefined") {
return;
}
const components = ["kol-meter"];
components.forEach(tagName => { switch (tagName) {
case "kol-meter":
if (!customElements.get(tagName)) {
customElements.define(tagName, KolMeter$1);
}
break;
} });
}
const KolMeter = KolMeter$1;
const defineCustomElement = defineCustomElement$1;
export { KolMeter, defineCustomElement };
//# sourceMappingURL=kol-meter.js.map
//# sourceMappingURL=kol-meter.js.map