@postnord/web-components
Version:
PostNord Web Components
136 lines (132 loc) • 12.8 kB
JavaScript
/*!
* Built with Stencil
* By PostNord.
*/
import { t as transformTag, r as registerInstance, g as getElement, f as forceUpdate, h, a as Host } from './index-XKg-ydzH.js';
import { a as alert_exclamation_circle } from './alert_exclamation_circle-B88w-Zxn.js';
import { c as chevron_down } from './chevron_down-DrYFvs2M.js';
import { uuidv4 } from './index.js';
const pnSelectCss = () => `${transformTag("pn-select")}{display:inline-block}${transformTag("pn-select")} .pn-select-label{cursor:pointer;display:flex;justify-content:space-between;align-items:flex-end;font-weight:400;color:#2d2013;margin:0 0 0.25em 0;gap:0.5em;-webkit-tap-highlight-color:transparent;transition-property:color, transform;transition-duration:0.2s;transition-timing-function:cubic-bezier(0.7, 0, 0.3, 1)} (prefers-reduced-motion: reduce){${transformTag("pn-select")} .pn-select-label{transition-duration:0s;transition-delay:0s}}${transformTag("pn-select")} .pn-select-label[data-compact]{position:absolute;top:1.5625em;left:calc(var(--pn-input-offset-left, 0px) + 0.75em + 0.0625em);margin:0;pointer-events:none;align-items:center;height:1.5em;max-width:calc(100% - (var(--pn-input-offset-left, 0px) + 0.75em) - (var(--pn-input-offset-right, 0px) + 0.75em));transform:translateY(-50%) scale(1) translateZ(0);transform-origin:0 0;transition-delay:0.2s}${transformTag("pn-select")} .pn-select-label[data-compact]>span{font-size:1em;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;line-height:1.5em}${transformTag("pn-select")} .pn-select-label>span{font-size:0.875em}${transformTag("pn-select")} .pn-select-label[data-compact][data-icon]{--pn-input-offset-left:calc(2em)}${transformTag("pn-select")} .pn-select-input{position:relative}${transformTag("pn-select")} .pn-select-icon{pointer-events:none;position:absolute;top:50%;transform:translateY(-50%);width:1.5em;height:1.5em;border-radius:50%;background-color:transparent}${transformTag("pn-select")} .pn-select-icon .pn-icon-svg{transition-property:transform;transition-duration:0.2s;transition-timing-function:cubic-bezier(0.7, 0, 0.3, 1)} (prefers-reduced-motion: reduce){${transformTag("pn-select")} .pn-select-icon .pn-icon-svg{transition-duration:0s;transition-delay:0s}}${transformTag("pn-select")} .pn-select-icon[data-default]{right:1em}${transformTag("pn-select")} .pn-select-icon[data-custom]{left:0.75em}${transformTag("pn-select")} .pn-select-icon[data-custom]~.pn-select-element{padding-left:2.75em}${transformTag("pn-select")} .pn-select-icon[data-error]{right:3.5em}${transformTag("pn-select")} .pn-select-icon[data-error]~.pn-select-element{padding-right:5.5em}${transformTag("pn-select")} .pn-select-element{cursor:pointer;-webkit-appearance:none;-moz-appearance:none;appearance:none;width:100%;text-overflow:ellipsis;white-space:nowrap;color:#2d2013;background-color:#ffffff;border:0.0625em solid #969087;border-radius:0.5em;padding:0.75em 3em 0.75em 0.75em;font-family:inherit;font-size:1em;font-weight:500;line-height:1.5em;-webkit-font-smoothing:antialiased;-webkit-tap-highlight-color:transparent}${transformTag("pn-select")} .pn-select-element:-webkit-autofill,${transformTag("pn-select")} .pn-select-element:-webkit-autofill:hover,${transformTag("pn-select")} .pn-select-element:-webkit-autofill:focus,${transformTag("pn-select")} .pn-select-element:-webkit-autofill:active{-webkit-box-shadow:0 0 0 10em #e0f8ff inset;-webkit-text-fill-color:#2d2013}${transformTag("pn-select")} .pn-select-element{outline:0.2rem solid transparent;outline-offset:0.2rem}${transformTag("pn-select")} .pn-select-element:focus-visible{outline-color:#005d92;background-color:#ffffff;border-color:#005d92}${transformTag("pn-select")} .pn-select-element{transition-property:outline-color, background-color, border-color, color;transition-duration:0.2s;transition-timing-function:cubic-bezier(0.7, 0, 0.3, 1)} (prefers-reduced-motion: reduce){${transformTag("pn-select")} .pn-select-element{transition-duration:0s;transition-delay:0s}}${transformTag("pn-select")} .pn-select-element[data-compact]{text-align:left;padding-top:1.125em;padding-bottom:0.375em;max-width:unset}${transformTag("pn-select")} .pn-select-element[data-compact]::placeholder{color:transparent}${transformTag("pn-select")} .pn-select-element[data-compact]:focus+.pn-select-label,${transformTag("pn-select")} .pn-select-element[data-compact]:not(:placeholder-shown)+.pn-select-label{transform:translateY(-85%) scale(0.75);transition-delay:0s}${transformTag("pn-select")} .pn-select-element::placeholder{color:#5e554a;font-weight:normal}${transformTag("pn-select")} .pn-select-element:hover{border-color:#005d92}${transformTag("pn-select")} .pn-select-element:disabled{color:#5e554a;background-color:#f3f2f2;border-color:#f3f2f2}${transformTag("pn-select")} .pn-select-element:open~.pn-select-icon .pn-icon-svg{transform:rotate(180deg)}${transformTag("pn-select")} .pn-select-element:disabled{color:#5e554a;background-color:#f3f2f2;border-color:#f3f2f2;pointer-events:none}${transformTag("pn-select")} .pn-select-element:disabled:hover{background-color:#f3f2f2}${transformTag("pn-select")} .pn-select-element:has(option[value=""]:checked){color:#5e554a;font-weight:normal}${transformTag("pn-select")} .pn-select-element:hover{color:#2d2013;background-color:#effbff}${transformTag("pn-select")} .pn-select-no-results{margin:0;padding:0.75em}${transformTag("pn-select")} .pn-select-text{color:#5e554a;font-size:0.875em;font-weight:400;margin:0.25em 0 0 0;display:flex;flex-direction:column;gap:0.25em}${transformTag("pn-select")} .pn-select-text[role=alert]{color:#a70707}${transformTag("pn-select")} .pn-select[data-error]>.pn-select-label{color:#a70707}${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element{border-color:#a70707}${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:hover{border-color:#500715}${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:focus-visible{background-color:#ffffff;border-color:#a70707;outline-color:#a70707}${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:-webkit-autofill,${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:-webkit-autofill:hover,${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:-webkit-autofill:focus,${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:-webkit-autofill:active{-webkit-box-shadow:0 0 0 10em #fef7f6 inset;-webkit-text-fill-color:#2d2013}${transformTag("pn-select")} .pn-select[data-error]>.pn-select-input>.pn-select-element:hover{background-color:#fdefee}`;
const PnSelect = class {
constructor(hostRef) {
registerInstance(this, hostRef);
}
id = `pn-select-${uuidv4()}`;
idHelper = `${this.id}-text`;
idError = `${this.id}-error`;
mo;
get hostElement() { return getElement(this); }
/** Label placed above the select. */
label;
/** Display a helper text underneath the select. */
helpertext;
/** HTML select name. @category Native attributes */
name;
/** HTML form name. @category Native attributes */
form;
/** HTML autocomplete. @category Native attributes */
autocomplete;
/** Set the select as required. @category Native attributes */
required = false;
/** Disable the select. @category Native attributes */
disabled = false;
/** Use the compact label variant. @since v7.21.0 @category Features */
compact = false;
/** Display an icon to the left of the select input. @category Features */
icon;
/** Trigger the invalid state. @category Features */
invalid = false;
/** Display an error message and trigger the invalid state. @category Features */
error;
/** Set a custom ID for the select. @since v7.25.0 @category HTML attributes */
pnId;
/**
* Provide the label via an aria attribute.
* We strongly recommend you use the `label` prop instead.
* @since v7.25.0
* @category HTML attributes
*/
pnAriaLabel;
/**
* Provide the label from another element via its ID.
* We strongly recommend you use the `label` prop instead.
* @since v7.25.0
* @category HTML attributes
*/
pnAriaLabelledby;
/** Set a custom ID for the select. @deprecated Use `pn-id` instead. @category HTML attributes */
selectId;
handleId() {
this.idHelper = `${this.getId()}-text`;
this.idError = `${this.getId()}-error`;
}
connectedCallback() {
this.mo = new MutationObserver(() => forceUpdate(this.hostElement));
this.mo.observe(this.hostElement, { childList: true, subtree: true });
}
disconnectedCallback() {
if (this.mo)
this.mo.disconnect();
}
hasHelperText() {
return this.helpertext?.length > 0 || this.checkSlottedHelper();
}
/** If any `error` text is present, either via prop/slot. */
hasErrorMessage() {
return this.error?.length > 0 || this.checkSlottedError();
}
/** If any `error` is active, either via the prop `invalid` or `error` prop/slot. */
hasError() {
return this.hasErrorMessage() || this.invalid || this.checkSlottedError();
}
checkSlottedHelper() {
return !!this.hostElement.querySelector('[slot=helpertext]');
}
checkSlottedError() {
return !!this.hostElement.querySelector('[slot=error]');
}
hideHelpertext() {
return this.hasErrorMessage() || !this.hasHelperText();
}
hideError() {
return !this.hasErrorMessage();
}
getId() {
return this.pnId || this.selectId || this.id;
}
displayLabel() {
return !!this.label;
}
getAriaLabel() {
return !this.displayLabel() && !this.pnAriaLabelledby ? this.pnAriaLabel : null;
}
getAriaLabelledby() {
return !this.displayLabel() && !this.pnAriaLabel ? this.pnAriaLabelledby : null;
}
getAriaDescribedby() {
const list = [];
if (this.hasErrorMessage())
list.push(this.idError);
else if (this.hasHelperText())
list.push(this.idHelper);
return list.length ? list.join(' ') : null;
}
renderLabel() {
if (!this.label)
return null;
return (h("label", { htmlFor: this.getId(), class: "pn-select-label", "data-compact": this.compact, "data-icon": !!this.icon }, h("span", null, this.label)));
}
render() {
return (h(Host, { key: 'c00c913a1786719ef71800ed15adaf0dd47577ff' }, h("div", { key: 'c2ee0553f2c130e12e7bc5bcfb611d457071cdec', class: "pn-select", "data-error": this.hasError() }, !this.compact && this.renderLabel(), h("div", { key: '7e8d7a1ab89e30b870883ff50bec31dd2a38690d', class: "pn-select-input" }, this.icon && h("pn-icon", { key: '772eb356db3cdf4fd04a67e1666ddb98799d40c0', class: "pn-select-icon", icon: this.icon, "data-custom": true, "aria-hidden": "true" }), this.hasError() && (h("pn-icon", { key: '03ccbd0952abb83cc4828ad6bfca777e1544f7a6', class: "pn-select-icon", icon: alert_exclamation_circle, color: "warning", "data-error": true, "aria-hidden": "true" })), h("select", { key: '46db113874c38b6f2ae6a42fbc63d9a9654a9dc1', id: this.getId(), class: "pn-select-element", name: this.name, form: this.form, autocomplete: this.autocomplete, "aria-label": this.getAriaLabel(), "aria-labelledby": this.getAriaLabelledby(), "aria-describedby": this.getAriaDescribedby(), "aria-invalid": this.hasError().toString(), disabled: this.disabled, "data-compact": this.compact }, h("slot", { key: '8253ace83c8f6454d8bae7550895768d4e53f809' })), this.compact && this.renderLabel(), h("pn-icon", { key: '88b094bd13b0d9cf912103427722e8be9ea2b687', class: "pn-select-icon", icon: chevron_down, color: "blue700", "data-default": true, "aria-hidden": "true" })), h("p", { key: '03009e2b484ade6fadb9e20f161cfc50964db43a', id: this.idHelper, class: "pn-select-text", hidden: this.hideHelpertext() }, h("span", { key: '2620e53c70c4ab95efc9924faa1a3ea298ec741b' }, this.helpertext), h("slot", { key: 'bd9798ba17632021ce177292cc022f57ae52193f', name: "helpertext" })), h("p", { key: '6ba88b6a3884430e7c263803ece5000adb08abe7', id: this.idError, class: "pn-select-text", role: "alert", hidden: this.hideError() }, h("span", { key: 'dcc53f692bce2eaa5460075b9a1e191f29a777ad' }, this.error), h("slot", { key: '0cf932dadffd1df3958e400a08c090483fc7c397', name: "error" })))));
}
static get watchers() { return {
"selectId": [{
"handleId": 0
}],
"pnId": [{
"handleId": 1
}]
}; }
};
PnSelect.style = pnSelectCss();
export { PnSelect as pn_select };