uikit/dist/remote/SegmentedSelector.js
Mohamadzadeh 9709e96ea1
All checks were successful
Build and Deploy Next.js + Nginx Docker Image / build-and-deploy (push) Successful in 51s
Build and Deploy Next.js + Nginx Docker Image / deploy (push) Successful in 7s
remote-ui-segmented-selector
2026-10-19 17:27:38 +03:30

212 lines
7.5 KiB
JavaScript

const R = "uikit-segmented-selector", H = "__UIKIT_REACT_REMOTE_DEPS__", I = `
: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;
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;
}
.selector[data-mode="options"] .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: 16px; height: 16px; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 16px; }
.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[data-side="top"]::after { bottom: -4px; }
.tooltip[data-side="bottom"]::after { top: -4px; }
@media (min-width: 62em) {
.selector { height: 44px; border-radius: 12px; }
.option { border-radius: 8px; }
.selector[data-mode="options"] .option { padding-inline: 8px; }
.title { font-size: 12px; }
.icon { width: 20px; height: 20px; font-size: 20px; }
}
@media (prefers-reduced-motion: reduce) { .option { transition: none; } }
`;
function P() {
return typeof window > "u" ? {} : window[H] || {};
}
function q(o) {
const t = { ...o.style || {} }, c = { w: "width", h: "height", minW: "minWidth", maxW: "maxWidth", minH: "minHeight", maxH: "maxHeight", bg: "background", gap: "gap" };
for (const [r, p] of Object.entries(c))
o[r] !== void 0 && o[r] !== null && typeof o[r] != "object" && (t[p] = typeof o[r] == "number" && !["gap"].includes(p) ? `${o[r]}px` : o[r]);
return t;
}
function F(o) {
const t = o.createElement, { useEffect: c, useLayoutEffect: r, useRef: p, useState: y } = o;
function z(_) {
const {
items: v = [],
mode: E = "text",
value: b,
onChange: L,
defaultValue: x,
isDisabled: k = !1,
renderItem: S,
ariaLabel: $,
className: T,
dir: N
} = _, m = Array.isArray(v) ? v : [], [j, D] = y(x), [s, u] = y(null), g = p(null), A = p([]), C = b !== void 0 ? m.findIndex((e) => Object.is(e?.value, b)) : x !== void 0 ? m.findIndex((e) => Object.is(e?.value, j)) : -1;
c(() => {
if (!s) return;
const e = () => u(null);
return window.addEventListener("scroll", e, !0), window.addEventListener("resize", e), () => {
window.removeEventListener("scroll", e, !0), window.removeEventListener("resize", e);
};
}, [s]), r(() => {
if (!s || !g.current || !s.anchor?.isConnected) return;
const e = g.current, n = s.anchor.getBoundingClientRect(), i = e.getBoundingClientRect(), d = window.innerWidth, h = n.top >= i.height + 14, w = h ? n.top - i.height - 10 : n.bottom + 10, f = Math.max(8, Math.min(n.left + n.width / 2 - i.width / 2, d - i.width - 8));
e.style.left = `${f}px`, e.style.top = `${w}px`, e.dataset.side = h ? "top" : "bottom", e.style.setProperty("--tip-arrow-x", `${Math.max(8, Math.min(n.left + n.width / 2 - f, i.width - 8))}px`), e.dataset.visible = "true";
}, [s]);
function O(e, n) {
k || e.isDisabled || n === C || (b === void 0 && x !== void 0 && D(e.value), L?.(e.value));
}
const V = m.map((e, n) => {
if (!e) return null;
const i = String(e.title ?? e.label ?? ""), d = String(e.tooltip || i), h = n === C, w = !!(k || e.isDisabled), f = E === "icon" && !!e.icon;
let a = e.icon;
f && !o.isValidElement(a) && (typeof a == "function" || typeof a == "object" && a?.$$typeof) && (a = t(a));
const W = f ? t("span", { className: "icon", "aria-hidden": "true" }, a) : t("span", { className: "title" }, i), M = t("button", {
key: `option-${n}`,
ref: (l) => {
A.current[n] = l;
},
className: "option",
type: "button",
disabled: w,
"aria-label": i,
"aria-pressed": h,
"data-selected": String(h),
onClick: () => O(e, n)
}, W), B = typeof S == "function" ? S(M, e, n) : M;
return t("span", {
key: e.value ?? n,
className: "item-wrap",
onMouseEnter: (l) => {
d && u({ label: d, anchor: l.currentTarget.querySelector("button") || l.currentTarget });
},
onMouseLeave: () => u(null),
onFocus: (l) => {
d && u({ label: d, anchor: l.currentTarget.querySelector("button") || l.currentTarget });
},
onBlur: () => u(null)
}, B);
});
return t(
o.Fragment,
null,
t("div", {
className: `selector${T ? ` ${T}` : ""}`,
role: "group",
"aria-label": $,
"data-mode": "options",
"data-visual": E === "icon" ? "icon" : "text",
dir: N,
style: q(_)
}, V),
s && t("div", { role: "tooltip", className: "tooltip", ref: g }, s.label)
);
}
return z;
}
class K extends HTMLElement {
constructor() {
super(), this._props = {}, this._shadow = this.attachShadow({ mode: "open" });
const t = document.createElement("style");
t.textContent = I, 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: c } = P();
if (!t || !c) {
console.error("[uikit/remote/ui] SegmentedSelector requires host React and ReactDOM.createRoot.");
return;
}
this._root || (this._root = c(this._mount), this._view = F(t)), this._root.render(t.createElement(this._view, this._props));
}
}
customElements.get(R) || customElements.define(R, K);