uikit/dist/remote/SegmentedSelector.js
Mohamadzadeh 307d178ed3
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Has been skipped
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Has been skipped
add TabSelector and SegmentedSelector
2026-11-21 11:19:33 +03:30

263 lines
10 KiB
JavaScript

const N = "uikit-segmented-selector", Q = "__UIKIT_REACT_REMOTE_DEPS__", Z = `
:host {
display: inline-block;
box-sizing: border-box;
max-width: 100%;
vertical-align: middle;
font-family: inherit;
direction: inherit;
}
*, *::before, *::after { box-sizing: border-box; }
.selector {
display: flex;
flex-direction: row;
align-items: stretch;
width: fit-content;
height: 36px;
flex-wrap: nowrap;
padding: 4px;
gap: 4px;
flex-shrink: 0;
background: #edf2f7;
border: 1px solid #e2e8f0;
border-radius: 10px;
box-shadow: inset 0 2px 4px 0 rgba(0, 0, 0, 0.06);
color: #718096;
font-family: inherit;
}
.item-wrap { display: contents; }
.option {
-webkit-appearance: none;
appearance: none;
display: inline-flex;
flex: 0 0 auto;
min-width: 0;
height: 100%;
padding: 0 6px;
margin: 0;
justify-content: center;
align-items: center;
border: 0;
border-radius: 7px;
background: transparent;
color: #718096;
font: inherit;
text-decoration: none;
cursor: pointer;
white-space: nowrap;
transition: background-color 160ms ease, color 160ms ease, box-shadow 160ms ease;
}
/* Keep text options at their intrinsic width while the selected value changes. */
.selector[data-visual="text"] .option { flex: 0 0 auto; }
/* Height determines width too, so both selected and unselected icon tiles stay square. */
.selector[data-visual="icon"] .option { aspect-ratio: 1 / 1; padding: 0; }
.selector[data-mode="options"][data-visual="text"] .option { padding-inline: 6px; }
.option[data-selected="true"] { background: #fff; color: #2b6cb0; box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.06); }
.option:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(66, 153, 225, 0.6); }
.option:disabled { cursor: not-allowed; opacity: .4; }
.option:disabled:hover { background: transparent; color: #718096; }
.title { font-size: 11px; font-weight: 800; line-height: 1; white-space: nowrap; }
.icon { width: 14px; height: 14px; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 14px; }
.icon > svg, .icon > * { display: block; width: 100%; height: 100%; }
.tooltip {
position: fixed;
z-index: 2147483000;
width: max-content;
max-width: min(280px, calc(100vw - 16px));
padding: 4px 8px;
background: #2d3748;
border-radius: 2px;
box-shadow: 0 1px 2px rgba(0,0,0,.08);
color: #fff;
font-family: inherit;
font-size: 14px;
font-weight: 500;
line-height: 1.5;
white-space: normal;
pointer-events: none;
visibility: hidden;
}
.tooltip[data-visible="true"] { visibility: visible; }
.tooltip::after {
content: "";
position: absolute;
left: var(--tip-arrow-x, 50%);
width: 8px;
height: 8px;
background: #2d3748;
transform: translateX(-50%) rotate(45deg);
}
.tooltip::after { top: -4px; }
@media (min-width: 62em) {
.selector { height: 44px; border-radius: 12px; }
.option { border-radius: 8px; }
.selector[data-mode="options"][data-visual="text"] .option { padding-inline: 8px; }
.title { font-size: 12px; }
.icon { width: 16px; height: 16px; font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) { .option { transition: none; } }
.selector { height:max(36px, var(--ss-height-mobile)); width:var(--ss-width-mobile); background:#fff; border:0; outline:1px solid #2176ff; outline-offset:-1px; border-radius:8px; box-shadow:none; align-items:center; }
.option { height:28px; min-height:28px; border-radius:6px; padding:0 8px; color:#3182ce; }
.option:hover { background:#ebf8ff; }
.option[data-selected="true"] { background:#3182ce; color:#fff; box-shadow:none; }
.selector[data-visual="icon"] .option { width:36px; min-width:36px; aspect-ratio:auto; padding:4px; }
.icon { width:18px; height:18px; }
@media (min-width:62em) { .selector { height:max(36px, var(--ss-height-desktop)); width:var(--ss-width-desktop); border-radius:8px; } .option { border-radius:6px; } .icon { width:18px; height:18px; } }
`;
function ee() {
return typeof window > "u" ? {} : window[Q] || {};
}
function v(n, t) {
return n == null || typeof n == "object" ? t : typeof n == "number" ? `${n * 4}px` : n === "full" ? "100%" : String(n);
}
function te(n) {
const t = { ...n.style || {} }, p = { w: "width", h: "height", minW: "minWidth", maxW: "maxWidth", minH: "minHeight", maxH: "maxHeight", bg: "background", gap: "gap" };
for (const [a, w] of Object.entries(p))
n[a] !== void 0 && n[a] !== null && typeof n[a] != "object" && (t[w] = typeof n[a] == "number" && !["gap"].includes(w) ? `${n[a]}px` : n[a]);
return t;
}
function ne(n) {
const t = n.createElement, { useEffect: p, useRef: a, useState: w } = n;
function W(o) {
const {
items: M = [],
mode: R = "text",
value: _,
onChange: I,
defaultValue: k,
isDisabled: E = !1,
renderItem: D,
ariaLabel: O,
className: z,
dir: S = "rtl",
selectionMode: V = "options",
showTooltip: q = !0,
fullWidth: L = !1,
compact: B = !1
} = o, c = V === "tabs", f = Array.isArray(M) ? M : [], [P, K] = w(k), m = a(null), $ = a([]), b = _ !== void 0 ? f.findIndex((e) => Object.is(e?.value, _)) : k !== void 0 ? f.findIndex((e) => Object.is(e?.value, P)) : -1;
function T() {
m.current && (m.current.dataset.visible = "false");
}
function A(e, s) {
const r = m.current;
if (!q || !e || !s?.isConnected || !r) return;
r.textContent = e, r.dataset.visible = "false";
const d = s.getBoundingClientRect(), h = r.getBoundingClientRect(), y = window.innerWidth, g = Math.max(8, Math.min(
d.left + d.width / 2 - h.width / 2,
y - h.width - 8
));
r.style.left = `${g}px`, r.style.top = `${d.bottom + 10}px`, r.style.setProperty("--tip-arrow-x", `${Math.max(
8,
Math.min(d.left + d.width / 2 - g, h.width - 8)
)}px`), r.dataset.visible = "true";
}
p(() => {
const e = () => T();
return window.addEventListener("scroll", e, !0), window.addEventListener("resize", e), () => {
window.removeEventListener("scroll", e, !0), window.removeEventListener("resize", e);
};
}, []);
function j(e, s) {
E || e.isDisabled || s === b || (_ === void 0 && k !== void 0 && K(e.value), I?.(e.value));
}
const F = f.map((e, s) => {
if (!e) return null;
const r = String(e.title ?? e.label ?? ""), d = String(e.tooltip || r), h = s === b, y = !!(E || e.isDisabled), g = R === "icon" && !!e.icon;
let u = e.icon;
g && !n.isValidElement(u) && (typeof u == "function" || typeof u == "object" && u?.$$typeof) && (u = t(u));
const Y = g ? t("span", { className: "icon", "aria-hidden": "true" }, u) : t("span", { className: "title" }, r), H = t("button", {
key: `option-${s}`,
ref: (i) => {
$.current[s] = i;
},
className: "option",
type: "button",
disabled: y,
"aria-label": r,
"aria-pressed": c ? void 0 : h,
"aria-selected": c ? h : void 0,
role: c ? "tab" : void 0,
tabIndex: c ? s === (b >= 0 && !f[b]?.isDisabled ? b : f.findIndex((i) => i && !i.isDisabled)) ? 0 : -1 : void 0,
onKeyDown: (i) => {
if (!c || y || !["ArrowLeft", "ArrowRight", "Home", "End"].includes(i.key)) return;
const l = f.map((x, J) => ({ entry: x, index: J })).filter(({ entry: x }) => x && !x.isDisabled && !E);
if (!l.length) return;
i.preventDefault();
const U = l.findIndex(({ index: x }) => x === s), X = i.key === "ArrowLeft" ? S === "ltr" ? -1 : 1 : S === "ltr" ? 1 : -1, C = i.key === "Home" ? l[0] : i.key === "End" ? l[l.length - 1] : l[(U + X + l.length) % l.length];
$.current[C.index]?.focus(), j(C.entry, C.index);
},
"data-selected": String(h),
onClick: () => j(e, s)
}, Y), G = typeof D == "function" ? D(H, e, s) : H;
return t("span", {
key: e.value ?? s,
className: "item-wrap",
onMouseEnter: (i) => {
A(d, i.currentTarget.querySelector("button") || i.currentTarget);
},
onMouseLeave: T,
onFocus: (i) => {
A(d, i.currentTarget.querySelector("button") || i.currentTarget);
},
onBlur: T
}, G);
});
return t(
n.Fragment,
null,
t("div", {
className: `selector${z ? ` ${z}` : ""}`,
role: c ? "tablist" : "group",
"aria-label": O,
"data-mode": c ? "tabs" : "options",
"data-compact": B || void 0,
"data-visual": R === "icon" ? "icon" : "text",
dir: S,
style: {
...te(o),
"--ss-width-mobile": v(o.w?.base ?? o.w, L ? "100%" : "fit-content"),
"--ss-width-desktop": v(o.w?.lg ?? o.w?.base ?? o.w, L ? "100%" : "fit-content"),
"--ss-height-mobile": v(o.h?.base ?? o.h, "36px"),
"--ss-height-desktop": v(o.h?.lg ?? o.h?.base ?? o.h, "36px"),
width: void 0,
height: void 0,
...o.style
}
}, F),
t("div", { role: "tooltip", className: "tooltip", ref: m, "data-visible": "false" })
);
}
return W;
}
class ie extends HTMLElement {
constructor() {
super(), this._props = {}, this._shadow = this.attachShadow({ mode: "open" });
const t = document.createElement("style");
t.textContent = Z, this._shadow.append(t), this._mount = document.createElement("div"), this._mount.style.display = "contents", this._shadow.append(this._mount), this._root = null, this._view = null;
}
connectedCallback() {
this._render();
}
disconnectedCallback() {
queueMicrotask(() => {
this.isConnected || !this._root || (this._root.unmount(), this._root = null, this._view = null);
});
}
set componentProps(t) {
this._props = t || {}, this.isConnected && this._render();
}
get componentProps() {
return this._props;
}
_render() {
const { React: t, createRoot: p } = ee();
if (!t || !p) {
console.error("[uikit/remote/ui] SegmentedSelector requires host React and ReactDOM.createRoot.");
return;
}
this._root || (this._root = p(this._mount), this._view = ne(t)), this._root.render(t.createElement(this._view, this._props));
}
}
customElements.get(N) || customElements.define(N, ie);