UNPKG

@postnord/web-components

Version:
139 lines (135 loc) 13.5 kB
/*! * Built with Stencil * By PostNord. */ import { t as transformTag, M as Mixin, r as registerInstance, g as getElement, h, a as Host } from './index-CAEP792y.js'; import { uuidv4 } from './index.js'; import { a as animateHeightFactory } from './index-CosyroON.js'; const pnRadioButtonCss = () => `${transformTag("pn-radio-button")}{position:relative;display:inline-block}${transformTag("pn-radio-button")} .pn-radio{position:relative;display:flex;flex-wrap:wrap;flex-direction:row}${transformTag("pn-radio-button")} .pn-radio-label{cursor:pointer;color:#2d2013;-webkit-tap-highlight-color:transparent}${transformTag("pn-radio-button")} .pn-radio-helpertext{color:#5e554a;margin:0 0 0.5em 0}${transformTag("pn-radio-button")} .pn-radio-helpertext:last-child{margin:0}${transformTag("pn-radio-button")} .pn-radio-content{display:flex;flex-direction:column;align-items:flex-start;font-weight:400;line-height:1.5;flex:1;margin:0 0 0 0.75em}${transformTag("pn-radio-button")} .pn-radio-button{display:flex;gap:0.75em;padding:0}${transformTag("pn-radio-button")} .pn-radio-outer{background-color:#ffffff;border:0.0625em solid #969087;border-radius:50%;height:1.5em;width:1.5em;display:flex;justify-content:center;align-items:center;outline:0.2rem solid transparent;outline-offset:0.2rem;transition-property:border-color, background-color, outline-color;transition-duration:0.2s;transition-timing-function:cubic-bezier(0.7, 0, 0.3, 1)}@media (prefers-reduced-motion: reduce){${transformTag("pn-radio-button")} .pn-radio-outer{transition-duration:0s;transition-delay:0s}}${transformTag("pn-radio-button")} .pn-radio-inner{height:0.75em;width:0.75em;background-color:#005d92;border-radius:50%;transform:scale(0);transform-origin:center center;transition-property:background-color, transform;transition-duration:0.2s;transition-timing-function:cubic-bezier(0.7, 0, 0.3, 1)}@media (prefers-reduced-motion: reduce){${transformTag("pn-radio-button")} .pn-radio-inner{transition-duration:0s;transition-delay:0s}}${transformTag("pn-radio-button")} .pn-radio[data-tile]{padding:1em;border:0.0625em solid #969087;border-radius:0.5em;background-color:#ffffff;outline:0.2rem solid transparent;outline-offset:0.2rem;transition-property:border-color, background-color, outline-color;transition-duration:0.2s;transition-timing-function:cubic-bezier(0.7, 0, 0.3, 1)}@media (prefers-reduced-motion: reduce){${transformTag("pn-radio-button")} .pn-radio[data-tile]{transition-duration:0s;transition-delay:0s}}${transformTag("pn-radio-button")} .pn-radio[data-tile] .pn-radio-outer{outline:none}${transformTag("pn-radio-button")} .pn-radio[data-invalid]{border-color:#a70707}${transformTag("pn-radio-button")} .pn-radio[data-invalid] .pn-radio-outer{border-color:#a70707}${transformTag("pn-radio-button")} .pn-radio[data-invalid] .pn-radio-inner{background-color:#a70707}${transformTag("pn-radio-button")} .pn-radio-container{flex:1 1 100%;overflow:hidden;visibility:hidden}${transformTag("pn-radio-button")} .pn-radio-container[data-open]{visibility:visible;overflow:unset}${transformTag("pn-radio-button")} .pn-radio-container[data-moving]{visibility:visible;overflow:hidden}${transformTag("pn-radio-button")} .pn-radio-area{padding:0.75em 0 0 2.25em;display:flex;flex-direction:column;flex-wrap:nowrap;gap:1em}${transformTag("pn-radio-button")} .pn-radio>${transformTag("pn-icon")}{margin-left:0.75em}${transformTag("pn-radio-button")}>input[type=radio]{cursor:pointer;opacity:0;position:absolute;margin:0;z-index:1;top:0;left:0;font-size:1em;width:1.5em;height:1.5em;-webkit-tap-highlight-color:transparent}${transformTag("pn-radio-button")}>input[type=radio][data-tile]:not([data-expand]){width:100%;height:100%}${transformTag("pn-radio-button")}>input[type=radio][data-expand]{top:1em;left:1em}${transformTag("pn-radio-button")}>input[type=radio][aria-describedby]+.pn-radio[data-tile] .pn-radio-label{font-weight:500}${transformTag("pn-radio-button")}>input[type=radio]:hover+.pn-radio[data-tile]{border-color:#005d92;background-color:#e0f8ff}${transformTag("pn-radio-button")}>input[type=radio]:hover+.pn-radio[data-tile] .pn-radio-outer{background-color:#e0f8ff}${transformTag("pn-radio-button")}>input[type=radio]:hover+.pn-radio[data-tile][data-invalid]{border-color:#a70707;background-color:#fdefee}${transformTag("pn-radio-button")}>input[type=radio]:hover+.pn-radio[data-tile][data-invalid] .pn-radio-outer{background-color:#fdefee}${transformTag("pn-radio-button")}>input[type=radio]:hover+.pn-radio[data-invalid] .pn-radio-outer{border-color:#a70707;background-color:#fdefee}${transformTag("pn-radio-button")}>input[type=radio]:hover+.pn-radio .pn-radio-outer{border-color:#005d92;background-color:#e0f8ff}${transformTag("pn-radio-button")}>input[type=radio]:checked:hover+.pn-radio[data-tile][data-invalid]{border-color:#500715}${transformTag("pn-radio-button")}>input[type=radio]:checked:hover+.pn-radio[data-invalid]>.pn-radio-button>.pn-radio-outer{border-color:#500715}${transformTag("pn-radio-button")}>input[type=radio]:focus-visible+.pn-radio .pn-radio-outer{outline-color:#005d92}${transformTag("pn-radio-button")}>input[type=radio]:focus-visible+.pn-radio[data-invalid] .pn-radio-outer{outline-color:#a70707}${transformTag("pn-radio-button")}>input[type=radio]:focus-visible+.pn-radio[data-tile]{outline-color:#005d92;border-color:#005d92}${transformTag("pn-radio-button")}>input[type=radio]:focus-visible+.pn-radio[data-tile][data-invalid]{outline-color:#a70707;border-color:#a70707}${transformTag("pn-radio-button")}>input[type=radio]:focus-visible+.pn-radio[data-tile]>.pn-radio-button>.pn-radio-outer{outline:none}${transformTag("pn-radio-button")}>input[type=radio]:checked+.pn-radio[data-tile]{border-color:#005d92;background-color:#e0f8ff}${transformTag("pn-radio-button")}>input[type=radio]:checked+.pn-radio[data-tile][data-invalid]{border-color:#a70707;background-color:#fdefee}${transformTag("pn-radio-button")}>input[type=radio]:checked+.pn-radio>.pn-radio-button>.pn-radio-outer{border-color:#005d92}${transformTag("pn-radio-button")}>input[type=radio]:checked+.pn-radio>.pn-radio-button>.pn-radio-outer>.pn-radio-inner{transform:scale(1);background-color:#005d92}${transformTag("pn-radio-button")}>input[type=radio]:checked+.pn-radio[data-invalid]>.pn-radio-button>.pn-radio-outer{border-color:#a70707}${transformTag("pn-radio-button")}>input[type=radio]:checked+.pn-radio[data-invalid]>.pn-radio-button>.pn-radio-outer>.pn-radio-inner{background-color:#a70707}${transformTag("pn-radio-button")}>input[type=radio]:disabled{pointer-events:none}${transformTag("pn-radio-button")}>input[type=radio]:disabled+.pn-radio .pn-radio-label{pointer-events:none}${transformTag("pn-radio-button")}>input[type=radio]:disabled+.pn-radio[data-tile]{background-color:#f3f2f2;border-color:#f3f2f2}${transformTag("pn-radio-button")}>input[type=radio]:disabled+.pn-radio[data-tile]>${transformTag("pn-icon")}>.pn-icon-svg path{fill:#5e554a}${transformTag("pn-radio-button")}>input[type=radio]:disabled+.pn-radio[data-tile]>.pn-radio-content>.pn-radio-label{color:#5e554a}${transformTag("pn-radio-button")}>input[type=radio]:disabled+.pn-radio>.pn-radio-button>.pn-radio-outer{background-color:#f3f2f2;border-color:#969087;outline:none}${transformTag("pn-radio-button")}>input[type=radio]:disabled+.pn-radio>.pn-radio-button>.pn-radio-outer>.pn-radio-inner{background-color:#969087}`; const PnRadioButton = class extends Mixin(animateHeightFactory) { constructor(hostRef) { super(); registerInstance(this, hostRef); } id = `pn-radio-${uuidv4()}`; idHelpertext = `${this.id}-helpertext`; contentArea; radioInput; get hostElement() { return getElement(this); } /** The radio label */ label; /** This adds an optional helpertext under the label. */ helpertext; /** This will be emitted on change and input events. */ value; /** The name of the radio group. */ name; /** Check the radio. */ checked = false; /** A unique HTML ID for the radio. */ radioid = this.id; /** Set the radio as required. @category State */ required = false; /** Disable the radio. @category State */ disabled = false; /** If set to true, color scheme will turn red, indicating that there is an issue or incorrect input related the radio. @category State */ invalid = false; /** * Turn the radio into a clickable tile. A border and padding is added. * * **Do not** use interactive elements (links/buttons) inside of the slots when using this prop. * An exception is made when using the `tile` + `expand` props together, * which allows you to use interactive elements. * * @category Tile */ tile = false; /** * Allow the radio to control the slot area "content". * When checked, the area is visible, when unchecked, the area is hidden. * * The prop `tile` must be used at the same time. * @see {@link tile} * @since v7.17.0 * @category Tile */ expand = false; /** * Add an icon on the right of your radio tile. The `tile` prop must be `true` for the icon to work. * @see {@link tile} * @category Tile */ icon = null; handleId() { this.idHelpertext = `${this.radioid}-helpertext`; } handleChecked(checked) { this.checked = checked ?? this.radioInput.checked; if (!this.displayContent()) return; if (this.checked) { this.openDropdown(this.contentArea); this.uncheckOtherRadios(); } else { this.closeDropdown(this.contentArea); } } handleChange(e) { const target = e.target; const { name } = target; const isSameRadioGroup = name === this.name; const isSameRadio = target === this.radioInput && target.checked; /** * Since content can be nested inside of this component, * double check that the event is coming from this specific radio. */ if (isSameRadioGroup) this.checked = isSameRadio; } componentWillLoad() { this.handleId(); } componentDidLoad() { requestAnimationFrame(() => this.displayContent() && this.checked && this.openDropdown(this.contentArea)); } uncheckOtherRadios() { if (!this.name) return; const selector = `pn-radio-button[name="${this.name}"]`; const list = document.querySelectorAll(selector); const radios = Array.from(list); radios.forEach(radio => radio !== this.hostElement && (radio.checked = false)); } isInvalid() { return this.invalid && !this.disabled; } displayText() { return this.displayLabel() || this.displayHelpertext(); } displayLabel() { return !!this.label; } displayHelpertext() { return !!(this.helpertext || this.hostElement.querySelector('[slot="helpertext"]')?.textContent); } displayIcon() { return this.tile && !!this.icon; } displayContent() { return this.tile && this.expand; } render() { return (h(Host, { key: 'f386b0de0377d3dbe6891a2396d32b91a808d53c' }, h("input", { key: '189e352a198275f4dbd733425166e9cb2ed417ef', type: "radio", id: this.radioid, class: "pn-radio-input", value: this.value, name: this.name, disabled: this.disabled, checked: this.checked, required: this.required, "aria-invalid": this.isInvalid()?.toString(), "aria-describedby": this.displayHelpertext() ? this.idHelpertext : null, "data-tile": this.tile, "data-expand": this.expand, ref: el => (this.radioInput = el) }), h("div", { key: '710dcfe5b1660d25a9d98eb2c3739eb092f2ab4e', class: "pn-radio", "data-tile": this.tile, "data-expand": this.expand, "data-invalid": this.isInvalid() }, h("div", { key: '2d6bd1b03ee6565b88773c60a529ae2c5be89ffd', class: "pn-radio-button" }, h("div", { key: '8b15488acb65132b2498d361d320bb804af793de', class: "pn-radio-outer" }, h("div", { key: '64ebbe9b4d18f158a43bb5d82116ef4b19325ca1', class: "pn-radio-inner" }))), h("p", { key: '3b5ed02eeae60cff45b3b4999055c6d853a6c22f', class: "pn-radio-content", hidden: !this.displayText() }, h("label", { key: 'd5f3c5f59630407a45cc48fcb26e60fe7419c73c', htmlFor: this.radioid, class: "pn-radio-label", hidden: !this.displayLabel() }, this.label), h("span", { key: 'd4329417f178f6e7bcff365a1c051ebaa51b0447', id: this.idHelpertext, class: "pn-radio-helpertext", hidden: !this.displayHelpertext() }, this.helpertext, h("slot", { key: 'defddc5970a0ea4c94c4f7b07e2cec3e7616d8c2', name: "helpertext" })), h("slot", { key: 'edaaf37351bb5d05bdc92696ac9ee2c8bc8e13f7' })), this.displayIcon() && h("pn-icon", { key: 'b9cd0ab07c691020efe39e8bb6ec39d02d5be826', icon: this.icon, color: "gray900" }), h("div", { key: '89655e67304498950e11c0388fddbbde350239a1', class: "pn-radio-container", "data-open": this.checked, hidden: !this.displayContent(), style: { height: '0px' }, ref: el => (this.contentArea = el) }, h("div", { key: 'eeebaf7c0b1fe8327810faec62877888cb6ff95d', class: "pn-radio-area" }, h("slot", { key: 'c997ee7e8ef719ff4b830df30a414cea637a54ff', name: "content" })))))); } static get watchers() { return { "radioid": [{ "handleId": 0 }], "checked": [{ "handleChecked": 0 }] }; } }; PnRadioButton.style = pnRadioButtonCss(); export { PnRadioButton as pn_radio_button };