UNPKG

@nysds/nys-errormessage

Version:

The Errormessage component from the NYS Design System.

396 lines (391 loc) 13.7 kB
import { css as C, LitElement as A, html as E } from "lit"; /*! * Error Message * Part of the New York State Design System * A design system for New York State's digital products. * Repository: https://github.com/its-hcd/nysds * License: MIT */ /** * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const p = globalThis, g = p.ShadowRoot && (p.ShadyCSS === void 0 || p.ShadyCSS.nativeShadow) && "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, U = Symbol(), S = /* @__PURE__ */ new WeakMap(); let O = class { constructor(e, t, s) { if (this._$cssResult$ = !0, s !== U) throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead."); this.cssText = e, this.t = t; } get styleSheet() { let e = this.o; const t = this.t; if (g && e === void 0) { const s = t !== void 0 && t.length === 1; s && (e = S.get(t)), e === void 0 && ((this.o = e = new CSSStyleSheet()).replaceSync(this.cssText), s && S.set(t, e)); } return e; } toString() { return this.cssText; } }; const x = (i) => new O(typeof i == "string" ? i : i + "", void 0, U), R = (i, e) => { if (g) i.adoptedStyleSheets = e.map((t) => t instanceof CSSStyleSheet ? t : t.styleSheet); else for (const t of e) { const s = document.createElement("style"), r = p.litNonce; r !== void 0 && s.setAttribute("nonce", r), s.textContent = t.cssText, i.appendChild(s); } }, w = g ? (i) => i : (i) => i instanceof CSSStyleSheet ? ((e) => { let t = ""; for (const s of e.cssRules) t += s.cssText; return x(t); })(i) : i; /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const { is: z, defineProperty: M, getOwnPropertyDescriptor: T, getOwnPropertyNames: k, getOwnPropertySymbols: D, getPrototypeOf: j } = Object, h = globalThis, b = h.trustedTypes, L = b ? b.emptyScript : "", m = h.reactiveElementPolyfillSupport, d = (i, e) => i, u = { toAttribute(i, e) { switch (e) { case Boolean: i = i ? L : null; break; case Object: case Array: i = i == null ? i : JSON.stringify(i); } return i; }, fromAttribute(i, e) { let t = i; switch (e) { case Boolean: t = i !== null; break; case Number: t = i === null ? null : Number(i); break; case Object: case Array: try { t = JSON.parse(i); } catch { t = null; } } return t; } }, v = (i, e) => !z(i, e), P = { attribute: !0, type: String, converter: u, reflect: !1, hasChanged: v }; Symbol.metadata ?? (Symbol.metadata = Symbol("metadata")), h.litPropertyMetadata ?? (h.litPropertyMetadata = /* @__PURE__ */ new WeakMap()); class c extends HTMLElement { static addInitializer(e) { this._$Ei(), (this.l ?? (this.l = [])).push(e); } static get observedAttributes() { return this.finalize(), this._$Eh && [...this._$Eh.keys()]; } static createProperty(e, t = P) { if (t.state && (t.attribute = !1), this._$Ei(), this.elementProperties.set(e, t), !t.noAccessor) { const s = Symbol(), r = this.getPropertyDescriptor(e, s, t); r !== void 0 && M(this.prototype, e, r); } } static getPropertyDescriptor(e, t, s) { const { get: r, set: o } = T(this.prototype, e) ?? { get() { return this[t]; }, set(n) { this[t] = n; } }; return { get() { return r == null ? void 0 : r.call(this); }, set(n) { const a = r == null ? void 0 : r.call(this); o.call(this, n), this.requestUpdate(e, a, s); }, configurable: !0, enumerable: !0 }; } static getPropertyOptions(e) { return this.elementProperties.get(e) ?? P; } static _$Ei() { if (this.hasOwnProperty(d("elementProperties"))) return; const e = j(this); e.finalize(), e.l !== void 0 && (this.l = [...e.l]), this.elementProperties = new Map(e.elementProperties); } static finalize() { if (this.hasOwnProperty(d("finalized"))) return; if (this.finalized = !0, this._$Ei(), this.hasOwnProperty(d("properties"))) { const t = this.properties, s = [...k(t), ...D(t)]; for (const r of s) this.createProperty(r, t[r]); } const e = this[Symbol.metadata]; if (e !== null) { const t = litPropertyMetadata.get(e); if (t !== void 0) for (const [s, r] of t) this.elementProperties.set(s, r); } this._$Eh = /* @__PURE__ */ new Map(); for (const [t, s] of this.elementProperties) { const r = this._$Eu(t, s); r !== void 0 && this._$Eh.set(r, t); } this.elementStyles = this.finalizeStyles(this.styles); } static finalizeStyles(e) { const t = []; if (Array.isArray(e)) { const s = new Set(e.flat(1 / 0).reverse()); for (const r of s) t.unshift(w(r)); } else e !== void 0 && t.push(w(e)); return t; } static _$Eu(e, t) { const s = t.attribute; return s === !1 ? void 0 : typeof s == "string" ? s : typeof e == "string" ? e.toLowerCase() : void 0; } constructor() { super(), this._$Ep = void 0, this.isUpdatePending = !1, this.hasUpdated = !1, this._$Em = null, this._$Ev(); } _$Ev() { var e; this._$ES = new Promise((t) => this.enableUpdating = t), this._$AL = /* @__PURE__ */ new Map(), this._$E_(), this.requestUpdate(), (e = this.constructor.l) == null || e.forEach((t) => t(this)); } addController(e) { var t; (this._$EO ?? (this._$EO = /* @__PURE__ */ new Set())).add(e), this.renderRoot !== void 0 && this.isConnected && ((t = e.hostConnected) == null || t.call(e)); } removeController(e) { var t; (t = this._$EO) == null || t.delete(e); } _$E_() { const e = /* @__PURE__ */ new Map(), t = this.constructor.elementProperties; for (const s of t.keys()) this.hasOwnProperty(s) && (e.set(s, this[s]), delete this[s]); e.size > 0 && (this._$Ep = e); } createRenderRoot() { const e = this.shadowRoot ?? this.attachShadow(this.constructor.shadowRootOptions); return R(e, this.constructor.elementStyles), e; } connectedCallback() { var e; this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this.enableUpdating(!0), (e = this._$EO) == null || e.forEach((t) => { var s; return (s = t.hostConnected) == null ? void 0 : s.call(t); }); } enableUpdating(e) { } disconnectedCallback() { var e; (e = this._$EO) == null || e.forEach((t) => { var s; return (s = t.hostDisconnected) == null ? void 0 : s.call(t); }); } attributeChangedCallback(e, t, s) { this._$AK(e, s); } _$EC(e, t) { var o; const s = this.constructor.elementProperties.get(e), r = this.constructor._$Eu(e, s); if (r !== void 0 && s.reflect === !0) { const n = (((o = s.converter) == null ? void 0 : o.toAttribute) !== void 0 ? s.converter : u).toAttribute(t, s.type); this._$Em = e, n == null ? this.removeAttribute(r) : this.setAttribute(r, n), this._$Em = null; } } _$AK(e, t) { var o; const s = this.constructor, r = s._$Eh.get(e); if (r !== void 0 && this._$Em !== r) { const n = s.getPropertyOptions(r), a = typeof n.converter == "function" ? { fromAttribute: n.converter } : ((o = n.converter) == null ? void 0 : o.fromAttribute) !== void 0 ? n.converter : u; this._$Em = r, this[r] = a.fromAttribute(t, n.type), this._$Em = null; } } requestUpdate(e, t, s) { if (e !== void 0) { if (s ?? (s = this.constructor.getPropertyOptions(e)), !(s.hasChanged ?? v)(this[e], t)) return; this.P(e, t, s); } this.isUpdatePending === !1 && (this._$ES = this._$ET()); } P(e, t, s) { this._$AL.has(e) || this._$AL.set(e, t), s.reflect === !0 && this._$Em !== e && (this._$Ej ?? (this._$Ej = /* @__PURE__ */ new Set())).add(e); } async _$ET() { this.isUpdatePending = !0; try { await this._$ES; } catch (t) { Promise.reject(t); } const e = this.scheduleUpdate(); return e != null && await e, !this.isUpdatePending; } scheduleUpdate() { return this.performUpdate(); } performUpdate() { var s; if (!this.isUpdatePending) return; if (!this.hasUpdated) { if (this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this._$Ep) { for (const [o, n] of this._$Ep) this[o] = n; this._$Ep = void 0; } const r = this.constructor.elementProperties; if (r.size > 0) for (const [o, n] of r) n.wrapped !== !0 || this._$AL.has(o) || this[o] === void 0 || this.P(o, this[o], n); } let e = !1; const t = this._$AL; try { e = this.shouldUpdate(t), e ? (this.willUpdate(t), (s = this._$EO) == null || s.forEach((r) => { var o; return (o = r.hostUpdate) == null ? void 0 : o.call(r); }), this.update(t)) : this._$EU(); } catch (r) { throw e = !1, this._$EU(), r; } e && this._$AE(t); } willUpdate(e) { } _$AE(e) { var t; (t = this._$EO) == null || t.forEach((s) => { var r; return (r = s.hostUpdated) == null ? void 0 : r.call(s); }), this.hasUpdated || (this.hasUpdated = !0, this.firstUpdated(e)), this.updated(e); } _$EU() { this._$AL = /* @__PURE__ */ new Map(), this.isUpdatePending = !1; } get updateComplete() { return this.getUpdateComplete(); } getUpdateComplete() { return this._$ES; } shouldUpdate(e) { return !0; } update(e) { this._$Ej && (this._$Ej = this._$Ej.forEach((t) => this._$EC(t, this[t]))), this._$EU(); } updated(e) { } firstUpdated(e) { } } c.elementStyles = [], c.shadowRootOptions = { mode: "open" }, c[d("elementProperties")] = /* @__PURE__ */ new Map(), c[d("finalized")] = /* @__PURE__ */ new Map(), m == null || m({ ReactiveElement: c }), (h.reactiveElementVersions ?? (h.reactiveElementVersions = [])).push("2.0.4"); /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const q = { attribute: !0, type: String, converter: u, reflect: !1, hasChanged: v }, N = (i = q, e, t) => { const { kind: s, metadata: r } = t; let o = globalThis.litPropertyMetadata.get(r); if (o === void 0 && globalThis.litPropertyMetadata.set(r, o = /* @__PURE__ */ new Map()), o.set(t.name, i), s === "accessor") { const { name: n } = t; return { set(a) { const y = e.get.call(this); e.set.call(this, a), this.requestUpdate(n, y, i); }, init(a) { return a !== void 0 && this.P(n, void 0, i), a; } }; } if (s === "setter") { const { name: n } = t; return function(a) { const y = this[n]; e.call(this, a), this.requestUpdate(n, y, i); }; } throw Error("Unsupported decorator location: " + s); }; function _(i) { return (e, t) => typeof t == "object" ? N(i, e, t) : ((s, r, o) => { const n = r.hasOwnProperty(o); return r.constructor.createProperty(o, n ? { ...s, wrapped: !0 } : s), n ? Object.getOwnPropertyDescriptor(r, o) : void 0; })(i, e, t); } const B = C` :host { --_nys-errormessage-font-family: var( --nys-font-family-ui, var( --nys-font-family-sans, "Proxima Nova", "Helvetica Neue", "Helvetica", "Arial", sans-serif ) ); --_nys-errormessage-font-weight: var(--nys-font-weight-regular, 400); --_nys-errormessage-font-size: var(--nys-font-size-ui-md, 16px); --_nys-errormessage-line-height: var(--nys-font-lineheight-ui-md, 24px); --_nys-errormessage-letter-spacing: var( --nys-font-letterspacing-ui-md, 0.044px ); --_nys-errormessage-color: var( --nys-color-danger, var(--nys-color-red-600, #b52c2c) ); /* Spacing */ --_nys-errormessage-gap: var(--nys-space-100, 8px); --_nys-errormessage-divider-gap: var(--nys-space-50, 4px); --_nys-errormessage-divider-width: var(--nys-border-width-sm, 1px); --_nys-errormessage-margin-top: 0; } .nys-errormessage { display: flex; align-items: center; gap: var(--_nys-errormessage-gap); font-family: var(--_nys-errormessage-font-family); font-weight: var(--_nys-errormessage-font-weight); font-size: var(--_nys-errormessage-font-size); line-height: var(--_nys-errormessage-line-height); letter-spacing: var(--_nys-errormessage-letter-spacing); color: var(--_nys-errormessage-color); margin-top: var(--_nys-errormessage-margin-top); } .nys-errormessage[showDivider] { padding-top: var(--_nys-errormessage-divider-gap); margin-top: var(--_nys-errormessage-divider-gap); border-top: var(--_nys-errormessage-divider-width) solid var(--_nys-errormessage-color); } `; var H = Object.defineProperty, $ = (i, e, t, s) => { for (var r = void 0, o = i.length - 1, n; o >= 0; o--) (n = i[o]) && (r = n(e, t, r) || r); return r && H(e, t, r), r; }; const f = class f extends A { // allows use of elementInternals' API constructor() { super(), this.showError = !1, this.errorMessage = "", this.showDivider = !1, this._internals = this.attachInternals(); } render() { return E`${this.showError ? E`<div class="nys-errormessage" ?showDivider=${this.showDivider}> <nys-icon name="error" size="2xl"></nys-icon> ${this._internals.validationMessage || this.errorMessage} </div>` : ""}`; } }; f.styles = B, f.formAssociated = !0; let l = f; $([ _({ type: Boolean }) ], l.prototype, "showError"); $([ _({ type: String }) ], l.prototype, "errorMessage"); $([ _({ type: Boolean, reflect: !0 }) ], l.prototype, "showDivider"); customElements.get("nys-errormessage") || customElements.define("nys-errormessage", l); export { l as NysErrorMessage }; //# sourceMappingURL=nys-errormessage.js.map