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