UNPKG

ranui

Version:

A framework-agnostic Web Components UI library built on native custom elements, with TypeScript types, light/dark theming, SSR and PWA support.

185 lines (184 loc) 10.6 kB
import { a as b, c, n as f, o as v, r as d, t as p } from "./utils-D1iZnChu.js"; import { t as u } from "./events-C59wGdni.js"; import { _ as g, i as h, p as y } from "./factory-ZNhcQ4tf.js"; import { a as A, n as _ } from "./ssr-DSGwPtEd.js"; var w = '.remove-wap-active-focus{outline:0;-webkit-tap-highlight-color:transparent}.remove-wap-active-focus:active,.remove-wap-active-focus:focus{outline:0;-webkit-tap-highlight-color:transparent}.ran-tab ::slotted(r-tab){box-sizing:border-box;width:100%;height:100%;padding:10px;flex-shrink:0;overflow:auto;text-align:start}.ran-tab-header{position:relative;overflow:hidden;scroll-behavior:smooth}.ran-tab-header-nav{display:flex;flex-flow:row nowrap;justify-content:flex-start;align-items:center}.ran-tab-header-nav::-webkit-scrollbar{display:none}.ran-tab-header-nav-item{flex-shrink:0}.ran-tab-header-nav .active{color:var(--ran-tab-active-color, var(--ran-color-primary, #006bff))}.ran-tab-header-line{position:absolute;width:0;margin-top:-2px;height:2px;border-radius:2px;background:var(--ran-tab-line-color, var(--ran-color-primary, #006bff));transition:var(--ran-tab-line-transition, left .2s, width .2s, transform .2s)}.ran-tab-content{overflow:hidden}.ran-tab-content-wrap{display:flex;width:100%;height:100%;transition:var(--ran-tab-content-transition, transform .2s)}:host([type="line"]) .ran-tab-header-nav-item{border:solid 1px transparent}:host([type="line"]) .ran-tab-header-nav .active{color:var(--ran-tab-line-color, var(--ran-color-primary, #006bff));border:solid 1px transparent;border-color:var(--ran-tab-border-color, var(--ran-color-border, rgba(0, 0, 0, .2))) var(--ran-tab-border-color, var(--ran-color-border, rgba(0, 0, 0, .2))) transparent;border-radius:var(--ran-tab-border-radius, var(--ran-radius-sm, .25em)) var(--ran-tab-border-radius, var(--ran-radius-sm, .25em)) 0 0}:host([type="line"]) .ran-tab-header-line{transition:none;visibility:hidden}:host([type="line"]) .ran-tab-header-line:after,:host([type="line"]) .ran-tab-header-line:before{content:"";position:absolute;visibility:visible;width:9999px;height:1px;bottom:0;background:var(--ran-tab-border-color, var(--ran-color-border, rgba(0, 0, 0, .2)))}:host([type="line"]) .ran-tab-header-line:after{left:100%}:host([type="line"]) .ran-tab-header-line:before{right:100%}:host([align="center"]) .ran-tab-header-nav{justify-content:center}:host([align="center"]) .ran-tab-header-line{left:50%}:host([align="end"]) .ran-tab-header-nav{justify-content:flex-end}', x = 0, m = class extends _ { static get observedAttributes() { return [ "active", "type", "align", "effect", "sheet" ]; } _events = new u(); _container; _header; _nav; _line; _content; _wrap; _slot; _shadowDom; _tabsId; tabHeaderKeyMapIndex; constructor() { super(), this._tabsId = ++x, this._shadowDom = f(this, w), this.tabHeaderKeyMapIndex = {}; const t = p(this._shadowDom, ".ran-tab", () => h().class("ran-tab").part("tabs").children(h().class("ran-tab-header").part("header").children(h().class("ran-tab-header-nav").part("nav"), h().class("ran-tab-header-line").part("indicator")), h().class("ran-tab-content").part("content").children(h().class("ran-tab-content-wrap").part("content-wrap").children(y()))).build()); this._container = t, this._header = t.querySelector(".ran-tab-header"), this._nav = t.querySelector(".ran-tab-header-nav"), this._line = t.querySelector(".ran-tab-header-line"), this._content = t.querySelector(".ran-tab-content"), this._wrap = t.querySelector(".ran-tab-content-wrap"), this._slot = t.querySelector("slot"), this._nav.setAttribute("role", "tablist"); } get align() { return d(this, "align", "start"); } set align(t) { this.setAttribute("align", t); } get type() { return d(this, "type", "flat"); } set type(t) { this.setAttribute("type", t); } get active() { return this.getAttribute("active"); } set active(t) { t ? (this.setAttribute("active", t), this.setTabLine(t), this.setTabContent(t), this.syncTabsAria()) : this.removeAttribute("active"); } get effect() { return this.getAttribute("effect"); } set effect(t) { !t || t === "false" ? this.removeAttribute("effect") : this.setAttribute("effect", t); } get sheet() { return d(this, "sheet"); } set sheet(t) { b(this, "sheet", t); } handlerExternalCss = () => { v(this, this._shadowDom, "sheet", null, this.sheet); }; initTabHeaderKeyMapIndex = (t, i) => { if (this.tabHeaderKeyMapIndex[t]) throw new Error("tab 组件的 key 值存在重复,或者某个 tab 组件缺少 key 属性"); this.tabHeaderKeyMapIndex[t] = i; }; createTabHeader(t, i) { const e = t.getAttribute("label") || "", a = t.getAttribute("icon") || "", r = t.getAttribute("iconSize") || "", n = t.getAttribute("r-key") || `${i}`, o = t.getAttribute("type") || "text"; this.initTabHeaderKeyMapIndex(n, i); const s = g("r-button").class("tab-header-nav-item").attr("type", o).attr("r-key", n).text(e); return a && s.attr("icon", a), r && s.attr("iconSize", r), c(t) && s.attr("disabled", ""), this.effect && (t.setAttribute("effect", this.effect), this._line.style.setProperty("display", "none")), t.setAttribute("r-key", n), s.build(); } initTabLineAlignCenter = () => { const { length: t } = this._nav.children; let i = 0; for (let e = 0; e < t; e++) { const { width: a = 0 } = this._nav.children[e].getBoundingClientRect(); i += a; } this._line.style.setProperty("left", `calc(50% - ${i / 2}px)`); }; initTabLineAlignEnd = () => { const { length: t } = this._nav.children; let i = 0; for (let e = 0; e < t; e++) { const { width: a = 0 } = this._nav.children[e].getBoundingClientRect(); i += a; } this._line.style.setProperty("left", `calc(100% - ${i}px)`); }; setTabLine = (t) => { if (t) { const i = this.tabHeaderKeyMapIndex[t], { width: e = 0 } = this._nav.children[i].getBoundingClientRect(); this._line.style.setProperty("width", `${e}px`); let a = 0; for (let r = 0; r < i; r++) { const { width: n = 0 } = this._nav.children[r].getBoundingClientRect(); a += n; } this._line.style.setProperty("transform", `translateX(${a}px)`); } }; setTabContent = (t) => { if (t) { const i = this.tabHeaderKeyMapIndex[t]; this._wrap.style.setProperty("transform", `translateX(${i * -100}%)`); } }; clickTabHead = (t) => { const i = t.currentTarget ?? t.target, e = i.getAttribute("r-key"); !c(i) && e && this.activateKey(e); }; activateKey = (t) => { this.setAttribute("active", t), this.setTabLine(t), this.setTabContent(t), this._nav.querySelectorAll(".active").forEach((e) => e.classList.remove("active")); const i = this.tabHeaderKeyMapIndex[t]; this._nav.children[i]?.classList.add("active"), this.syncTabsAria(); }; tabFocusable = (t) => t._btn ?? t; tabIdFor = (t) => `ran-tab-${this._tabsId}-${t}`; panelIdFor = (t) => `ran-tabpanel-${this._tabsId}-${t}`; syncTabsAria = () => { const t = this.active, i = this._slot?.assignedElements?.() ?? []; [...this._nav.children].forEach((e, a) => { const r = e.getAttribute("r-key") ?? `${a}`, n = r === t, o = c(e), s = this.tabFocusable(e); s.setAttribute("role", "tab"), s.id = this.tabIdFor(r), s.setAttribute("aria-controls", this.panelIdFor(r)), s.setAttribute("aria-selected", n ? "true" : "false"), s.tabIndex = n && !o ? 0 : -1; const l = i[a]; l && (l.setAttribute("role", "tabpanel"), l.id = this.panelIdFor(r), l.setAttribute("aria-labelledby", this.tabIdFor(r)), l.setAttribute("tabindex", n ? "0" : "-1"), n ? l.removeAttribute("aria-hidden") : l.setAttribute("aria-hidden", "true")); }); }; onNavKeydown = (t) => { if (![ "ArrowRight", "ArrowLeft", "ArrowDown", "ArrowUp", "Home", "End" ].includes(t.key)) return; const i = [...this._nav.children].filter((o) => !c(o)); if (!i.length) return; const e = t.target?.closest?.(".tab-header-nav-item") ?? t.target; let a = i.findIndex((o) => o === e); a < 0 && (a = i.findIndex((o) => o.getAttribute("r-key") === this.active)), a < 0 && (a = 0); let r = a; t.key === "ArrowRight" || t.key === "ArrowDown" ? r = (a + 1) % i.length : t.key === "ArrowLeft" || t.key === "ArrowUp" ? r = (a - 1 + i.length) % i.length : t.key === "Home" ? r = 0 : t.key === "End" && (r = i.length - 1), t.preventDefault(); const n = i[r].getAttribute("r-key"); n && (this.activateKey(n), this.tabFocusable(i[r]).focus()); }; updateAttribute = (t, i, e = "") => { const a = this.tabHeaderKeyMapIndex[t]; t && e && this._nav.children[a] ? this._nav.children[a]?.setAttribute(i, e) : this._nav.children[a]?.removeAttribute(i); }; initActive = () => { const t = [...this._nav.children], i = t.filter((n) => !c(n)); let e; if (this.active != null && (e = i.find((n) => n.getAttribute("r-key") === this.active), e?.setAttribute("r-key", this.active)), e || (e = i.shift()), !e) return; const a = t.findIndex((n) => n === e), r = e?.getAttribute("r-key") || `${a}`; r != null && (this.setAttribute("active", `${r}`), e.classList.add("active"), this.setTabContent(r), setTimeout(() => { this.setTabLine(r); }, 200)); }; listenSlotChange = () => { this._slot.assignedElements().forEach((t, i) => { const e = this.createTabHeader(t, i); this._nav.appendChild(e), e.addEventListener("click", this.clickTabHead); }), this.initActive(), this.syncTabsAria(), this.align && (this.align === "center" && this.initTabLineAlignCenter(), this.align === "end" && this.initTabLineAlignEnd()); }; connectedCallback() { this.handlerExternalCss(), this._events.on(this._slot, "slotchange", this.listenSlotChange), this._events.on(this._nav, "keydown", this.onNavKeydown); } disconnectedCallback() { this._events.abort(); } attributeChangedCallback(t, i, e) { i !== e && (this.dispatchEvent(new CustomEvent("change", { detail: { active: this.active } })), t === "align" && (e === "center" && this.initTabLineAlignCenter(), e === "end" && this.initTabLineAlignEnd()), t === "effect" && [...this._nav.children].forEach((a) => { !this.effect || this.effect === "false" ? a.removeAttribute("effect") : a.setAttribute("effect", e); }), t === "active" && this.setAttribute(t, e), t === "sheet" && this.handlerExternalCss()); } }; A("r-tabs", m); export { m as t };