UNPKG

@nysds/nys-backtotop

Version:

The Back To Top component from the NYS Design System.

410 lines (405 loc) 13.9 kB
import { css as C, LitElement as O, html as A } from "lit"; /*! * ▒█▄░▒█ ▒█░░▒█ ▒█▀▀▀█ ▒█▀▀▄ ▒█▀▀▀█ * ▒█▒█▒█ ▒█▄▄▄█ ░▀▀▀▄▄ ▒█░▒█ ░▀▀▀▄▄ * ▒█░░▀█ ░░▒█░░ ▒█▄▄▄█ ▒█▄▄▀ ▒█▄▄▄█ * * Back To Top Component * Part of the New York State Design System * Repository: https://github.com/its-hcd/nysds * License: MIT */ /** * @license * Copyright 2019 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const p = globalThis, b = p.ShadowRoot && (p.ShadyCSS === void 0 || p.ShadyCSS.nativeShadow) && "adoptedStyleSheets" in Document.prototype && "replace" in CSSStyleSheet.prototype, P = Symbol(), v = /* @__PURE__ */ new WeakMap(); let R = class { constructor(t, e, s) { if (this._$cssResult$ = !0, s !== P) throw Error("CSSResult is not constructable. Use `unsafeCSS` or `css` instead."); this.cssText = t, this.t = e; } get styleSheet() { let t = this.o; const e = this.t; if (b && t === void 0) { const s = e !== void 0 && e.length === 1; s && (t = v.get(e)), t === void 0 && ((this.o = t = new CSSStyleSheet()).replaceSync(this.cssText), s && v.set(e, t)); } return t; } toString() { return this.cssText; } }; const k = (r) => new R(typeof r == "string" ? r : r + "", void 0, P), x = (r, t) => { if (b) r.adoptedStyleSheets = t.map((e) => e instanceof CSSStyleSheet ? e : e.styleSheet); else for (const e of t) { const s = document.createElement("style"), i = p.litNonce; i !== void 0 && s.setAttribute("nonce", i), s.textContent = e.cssText, r.appendChild(s); } }, S = b ? (r) => r : (r) => r instanceof CSSStyleSheet ? ((t) => { let e = ""; for (const s of t.cssRules) e += s.cssText; return k(e); })(r) : r; /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ const { is: M, defineProperty: z, getOwnPropertyDescriptor: T, getOwnPropertyNames: L, getOwnPropertySymbols: j, getPrototypeOf: D } = Object, h = globalThis, g = h.trustedTypes, q = g ? g.emptyScript : "", m = h.reactiveElementPolyfillSupport, d = (r, t) => r, u = { toAttribute(r, t) { switch (t) { case Boolean: r = r ? q : null; break; case Object: case Array: r = r == null ? r : JSON.stringify(r); } return r; }, fromAttribute(r, t) { let e = r; switch (t) { case Boolean: e = r !== null; break; case Number: e = r === null ? null : Number(r); break; case Object: case Array: try { e = JSON.parse(r); } catch { e = null; } } return e; } }, _ = (r, t) => !M(r, t), w = { attribute: !0, type: String, converter: u, reflect: !1, hasChanged: _ }; Symbol.metadata ?? (Symbol.metadata = Symbol("metadata")), h.litPropertyMetadata ?? (h.litPropertyMetadata = /* @__PURE__ */ new WeakMap()); class c extends HTMLElement { static addInitializer(t) { this._$Ei(), (this.l ?? (this.l = [])).push(t); } static get observedAttributes() { return this.finalize(), this._$Eh && [...this._$Eh.keys()]; } static createProperty(t, e = w) { if (e.state && (e.attribute = !1), this._$Ei(), this.elementProperties.set(t, e), !e.noAccessor) { const s = Symbol(), i = this.getPropertyDescriptor(t, s, e); i !== void 0 && z(this.prototype, t, i); } } static getPropertyDescriptor(t, e, s) { const { get: i, set: o } = T(this.prototype, t) ?? { get() { return this[e]; }, set(n) { this[e] = n; } }; return { get() { return i == null ? void 0 : i.call(this); }, set(n) { const a = i == null ? void 0 : i.call(this); o.call(this, n), this.requestUpdate(t, a, s); }, configurable: !0, enumerable: !0 }; } static getPropertyOptions(t) { return this.elementProperties.get(t) ?? w; } static _$Ei() { if (this.hasOwnProperty(d("elementProperties"))) return; const t = D(this); t.finalize(), t.l !== void 0 && (this.l = [...t.l]), this.elementProperties = new Map(t.elementProperties); } static finalize() { if (this.hasOwnProperty(d("finalized"))) return; if (this.finalized = !0, this._$Ei(), this.hasOwnProperty(d("properties"))) { const e = this.properties, s = [...L(e), ...j(e)]; for (const i of s) this.createProperty(i, e[i]); } const t = this[Symbol.metadata]; if (t !== null) { const e = litPropertyMetadata.get(t); if (e !== void 0) for (const [s, i] of e) this.elementProperties.set(s, i); } this._$Eh = /* @__PURE__ */ new Map(); for (const [e, s] of this.elementProperties) { const i = this._$Eu(e, s); i !== void 0 && this._$Eh.set(i, e); } this.elementStyles = this.finalizeStyles(this.styles); } static finalizeStyles(t) { const e = []; if (Array.isArray(t)) { const s = new Set(t.flat(1 / 0).reverse()); for (const i of s) e.unshift(S(i)); } else t !== void 0 && e.push(S(t)); return e; } static _$Eu(t, e) { const s = e.attribute; return s === !1 ? void 0 : typeof s == "string" ? s : typeof t == "string" ? t.toLowerCase() : void 0; } constructor() { super(), this._$Ep = void 0, this.isUpdatePending = !1, this.hasUpdated = !1, this._$Em = null, this._$Ev(); } _$Ev() { var t; this._$ES = new Promise((e) => this.enableUpdating = e), this._$AL = /* @__PURE__ */ new Map(), this._$E_(), this.requestUpdate(), (t = this.constructor.l) == null || t.forEach((e) => e(this)); } addController(t) { var e; (this._$EO ?? (this._$EO = /* @__PURE__ */ new Set())).add(t), this.renderRoot !== void 0 && this.isConnected && ((e = t.hostConnected) == null || e.call(t)); } removeController(t) { var e; (e = this._$EO) == null || e.delete(t); } _$E_() { const t = /* @__PURE__ */ new Map(), e = this.constructor.elementProperties; for (const s of e.keys()) this.hasOwnProperty(s) && (t.set(s, this[s]), delete this[s]); t.size > 0 && (this._$Ep = t); } createRenderRoot() { const t = this.shadowRoot ?? this.attachShadow(this.constructor.shadowRootOptions); return x(t, this.constructor.elementStyles), t; } connectedCallback() { var t; this.renderRoot ?? (this.renderRoot = this.createRenderRoot()), this.enableUpdating(!0), (t = this._$EO) == null || t.forEach((e) => { var s; return (s = e.hostConnected) == null ? void 0 : s.call(e); }); } enableUpdating(t) { } disconnectedCallback() { var t; (t = this._$EO) == null || t.forEach((e) => { var s; return (s = e.hostDisconnected) == null ? void 0 : s.call(e); }); } attributeChangedCallback(t, e, s) { this._$AK(t, s); } _$EC(t, e) { var o; const s = this.constructor.elementProperties.get(t), i = this.constructor._$Eu(t, s); if (i !== void 0 && s.reflect === !0) { const n = (((o = s.converter) == null ? void 0 : o.toAttribute) !== void 0 ? s.converter : u).toAttribute(e, s.type); this._$Em = t, n == null ? this.removeAttribute(i) : this.setAttribute(i, n), this._$Em = null; } } _$AK(t, e) { var o; const s = this.constructor, i = s._$Eh.get(t); if (i !== void 0 && this._$Em !== i) { const n = s.getPropertyOptions(i), a = typeof n.converter == "function" ? { fromAttribute: n.converter } : ((o = n.converter) == null ? void 0 : o.fromAttribute) !== void 0 ? n.converter : u; this._$Em = i, this[i] = a.fromAttribute(e, n.type), this._$Em = null; } } requestUpdate(t, e, s) { if (t !== void 0) { if (s ?? (s = this.constructor.getPropertyOptions(t)), !(s.hasChanged ?? _)(this[t], e)) return; this.P(t, e, s); } this.isUpdatePending === !1 && (this._$ES = this._$ET()); } P(t, e, s) { this._$AL.has(t) || this._$AL.set(t, e), s.reflect === !0 && this._$Em !== t && (this._$Ej ?? (this._$Ej = /* @__PURE__ */ new Set())).add(t); } async _$ET() { this.isUpdatePending = !0; try { await this._$ES; } catch (e) { Promise.reject(e); } const t = this.scheduleUpdate(); return t != null && await t, !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 i = this.constructor.elementProperties; if (i.size > 0) for (const [o, n] of i) n.wrapped !== !0 || this._$AL.has(o) || this[o] === void 0 || this.P(o, this[o], n); } let t = !1; const e = this._$AL; try { t = this.shouldUpdate(e), t ? (this.willUpdate(e), (s = this._$EO) == null || s.forEach((i) => { var o; return (o = i.hostUpdate) == null ? void 0 : o.call(i); }), this.update(e)) : this._$EU(); } catch (i) { throw t = !1, this._$EU(), i; } t && this._$AE(e); } willUpdate(t) { } _$AE(t) { var e; (e = this._$EO) == null || e.forEach((s) => { var i; return (i = s.hostUpdated) == null ? void 0 : i.call(s); }), this.hasUpdated || (this.hasUpdated = !0, this.firstUpdated(t)), this.updated(t); } _$EU() { this._$AL = /* @__PURE__ */ new Map(), this.isUpdatePending = !1; } get updateComplete() { return this.getUpdateComplete(); } getUpdateComplete() { return this._$ES; } shouldUpdate(t) { return !0; } update(t) { this._$Ej && (this._$Ej = this._$Ej.forEach((e) => this._$EC(e, this[e]))), this._$EU(); } updated(t) { } firstUpdated(t) { } } 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 H = { attribute: !0, type: String, converter: u, reflect: !1, hasChanged: _ }, V = (r = H, t, e) => { const { kind: s, metadata: i } = e; let o = globalThis.litPropertyMetadata.get(i); if (o === void 0 && globalThis.litPropertyMetadata.set(i, o = /* @__PURE__ */ new Map()), o.set(e.name, r), s === "accessor") { const { name: n } = e; return { set(a) { const y = t.get.call(this); t.set.call(this, a), this.requestUpdate(n, y, r); }, init(a) { return a !== void 0 && this.P(n, void 0, r), a; } }; } if (s === "setter") { const { name: n } = e; return function(a) { const y = this[n]; t.call(this, a), this.requestUpdate(n, y, r); }; } throw Error("Unsupported decorator location: " + s); }; function $(r) { return (t, e) => typeof e == "object" ? V(r, t, e) : ((s, i, o) => { const n = i.hasOwnProperty(o); return i.constructor.createProperty(o, n ? { ...s, wrapped: !0 } : s), n ? Object.getOwnPropertyDescriptor(i, o) : void 0; })(r, t, e); } /** * @license * Copyright 2017 Google LLC * SPDX-License-Identifier: BSD-3-Clause */ function U(r) { return $({ ...r, state: !0, attribute: !1 }); } const N = C` :host { } .nys-backtotop { --_nys-button-radius-left: var(--nys-radius-round, 1776px); --_nys-button-radius-right: var(--nys-radius-round, 1776px); --_nys-button-padding-y: var(--nys-space-100, 8px); --_nys-button-padding-x: var(--nys-space-200, 16px); position: fixed; bottom: 1rem; right: 1rem; z-index: 9999; display: none; } .left { left: 1rem; right: auto; } .visible { display: inline-flex; } `; var Q = Object.defineProperty, f = (r, t, e, s) => { for (var i = void 0, o = r.length - 1, n; o >= 0; o--) (n = r[o]) && (i = n(t, e, i) || i); return i && Q(t, e, i), i; }; const E = class E extends O { constructor() { super(), this.position = "right", this.visible = !1, this.isMobile = !1, this.forceVisible = !1, this._handleScroll = this._handleScroll.bind(this), this._handleResize = this._handleResize.bind(this), this.mediaQuery = window.matchMedia("(max-width: 480px)"); } connectedCallback() { super.connectedCallback(), this.forceVisible = this.hasAttribute("visible"), window.addEventListener("scroll", this._handleScroll), this.mediaQuery.addEventListener("change", this._handleResize), this._handleResize(); } disconnectedCallback() { window.removeEventListener("scroll", this._handleScroll), this.mediaQuery.removeEventListener("change", this._handleResize), super.disconnectedCallback(); } _handleScroll() { if (this.forceVisible) return; const t = window.innerHeight, e = document.documentElement.scrollHeight; this.visible = e >= t * 4 && window.scrollY > t * 1.5; } _scrollToTop() { window.scrollTo({ top: 0, behavior: "smooth" }); } _handleResize() { this.isMobile = this.mediaQuery.matches; } render() { const t = [ "nys-backtotop", this.position, this.visible ? "visible" : "" ].filter(Boolean).join(" "); return A`<nys-button id="nys-backtotop" prefixIcon="chevron_up" variant="outline" label="Back to top" size="sm" class="${t}" .onClick=${this._scrollToTop} ?circle=${this.isMobile} ></nys-button>`; } }; E.styles = N; let l = E; f([ $({ type: String }) ], l.prototype, "position"); f([ $({ type: Boolean, reflect: !0 }) ], l.prototype, "visible"); f([ U() ], l.prototype, "isMobile"); f([ U() ], l.prototype, "forceVisible"); customElements.get("nys-backtotop") || customElements.define("nys-backtotop", l); export { l as NysBacktotop }; //# sourceMappingURL=nys-backtotop.js.map