some fixes
This commit is contained in:
parent
9709e96ea1
commit
dd0604e2d6
11
README.md
11
README.md
@ -1021,7 +1021,7 @@ The remote module URLs stay unchanged, for example `/AppHeader.js`, `/AppSidebar
|
||||
## Remote SegmentedSelector (Chakra-independent)
|
||||
|
||||
`SegmentedSelector` is served from `https://uikit.d.aiengines.ir/SegmentedSelector.js`.
|
||||
Its visual implementation, keyboard navigation and tooltips live in Shadow DOM; the
|
||||
Its visual implementation, option selection and tooltips live in Shadow DOM; the
|
||||
remote relies only on host React, ReactDOM (`createRoot`) and the inherited font.
|
||||
It does **not** import Chakra, Emotion, icon packages, or other host dependencies.
|
||||
Releasing a compatible update to `SegmentedSelector.js` updates consuming applications
|
||||
@ -1054,8 +1054,13 @@ function Example() {
|
||||
|
||||
- Original component geometry preserved: outer radius 10px mobile/12px desktop,
|
||||
item radius 7px/8px, `options` height 36px/44px, outer padding and gap 4px,
|
||||
text size 11px/12px, icon size 16px/20px, breakpoint 992px.
|
||||
- Tooltip uses `item.tooltip || item.title` and includes an arrow. Icons can be
|
||||
text size 11px/12px, icon size 14px/16px, breakpoint 992px.
|
||||
Icon option tiles are square (26px mobile and 34px desktop) and do not resize
|
||||
when selected.
|
||||
- Tooltip always appears below the hovered/focused item, uses
|
||||
`item.tooltip || item.title`, and includes an upward-pointing arrow.
|
||||
Tooltip visibility updates do not rerender the selection controls, avoiding
|
||||
transient layout changes in text mode. Icons can be
|
||||
React components or React elements; the destination's icon library is **not**
|
||||
imported by the remote (only the supplied React element is rendered).
|
||||
- Selection behavior is always `options`: individual buttons use `value/onChange`
|
||||
|
||||
145
dist/remote/SegmentedSelector.js
vendored
145
dist/remote/SegmentedSelector.js
vendored
@ -1,4 +1,4 @@
|
||||
const R = "uikit-segmented-selector", H = "__UIKIT_REACT_REMOTE_DEPS__", I = `
|
||||
const M = "uikit-segmented-selector", W = "__UIKIT_REACT_REMOTE_DEPS__", q = `
|
||||
:host {
|
||||
display: inline-block;
|
||||
box-sizing: border-box;
|
||||
@ -14,6 +14,7 @@ const R = "uikit-segmented-selector", H = "__UIKIT_REACT_REMOTE_DEPS__", I = `
|
||||
align-items: stretch;
|
||||
width: fit-content;
|
||||
height: 36px;
|
||||
flex-wrap: nowrap;
|
||||
padding: 4px;
|
||||
gap: 4px;
|
||||
flex-shrink: 0;
|
||||
@ -46,13 +47,17 @@ const R = "uikit-segmented-selector", H = "__UIKIT_REACT_REMOTE_DEPS__", I = `
|
||||
white-space: nowrap;
|
||||
transition: background-color 160ms ease, color 160ms ease, box-shadow 160ms ease;
|
||||
}
|
||||
.selector[data-mode="options"] .option { padding-inline: 6px; }
|
||||
/* 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: 16px; height: 16px; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 16px; }
|
||||
.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;
|
||||
@ -82,108 +87,118 @@ const R = "uikit-segmented-selector", H = "__UIKIT_REACT_REMOTE_DEPS__", I = `
|
||||
background: #2d3748;
|
||||
transform: translateX(-50%) rotate(45deg);
|
||||
}
|
||||
.tooltip[data-side="top"]::after { bottom: -4px; }
|
||||
.tooltip[data-side="bottom"]::after { top: -4px; }
|
||||
.tooltip::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; }
|
||||
.selector[data-mode="options"][data-visual="text"] .option { padding-inline: 8px; }
|
||||
.title { font-size: 12px; }
|
||||
.icon { width: 20px; height: 20px; font-size: 20px; }
|
||||
.icon { width: 16px; height: 16px; font-size: 16px; }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) { .option { transition: none; } }
|
||||
`;
|
||||
function P() {
|
||||
return typeof window > "u" ? {} : window[H] || {};
|
||||
function B() {
|
||||
return typeof window > "u" ? {} : window[W] || {};
|
||||
}
|
||||
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]);
|
||||
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 F(o) {
|
||||
const t = o.createElement, { useEffect: c, useLayoutEffect: r, useRef: p, useState: y } = o;
|
||||
function z(_) {
|
||||
function P(n) {
|
||||
const t = n.createElement, { useEffect: d, useRef: s, useState: u } = n;
|
||||
function R(w) {
|
||||
const {
|
||||
items: v = [],
|
||||
mode: E = "text",
|
||||
value: b,
|
||||
onChange: L,
|
||||
mode: y = "text",
|
||||
value: f,
|
||||
onChange: z,
|
||||
defaultValue: x,
|
||||
isDisabled: k = !1,
|
||||
renderItem: S,
|
||||
isDisabled: _ = !1,
|
||||
renderItem: E,
|
||||
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);
|
||||
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);
|
||||
};
|
||||
}, [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));
|
||||
}, []);
|
||||
function A(e, i) {
|
||||
_ || e.isDisabled || i === S || (f === void 0 && x !== void 0 && j(e.value), z?.(e.value));
|
||||
}
|
||||
const V = m.map((e, n) => {
|
||||
const H = b.map((e, i) => {
|
||||
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;
|
||||
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;
|
||||
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}`,
|
||||
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) => {
|
||||
A.current[n] = l;
|
||||
D.current[i] = 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;
|
||||
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 ?? n,
|
||||
key: e.value ?? i,
|
||||
className: "item-wrap",
|
||||
onMouseEnter: (l) => {
|
||||
d && u({ label: d, anchor: l.currentTarget.querySelector("button") || l.currentTarget });
|
||||
C(r, l.currentTarget.querySelector("button") || l.currentTarget);
|
||||
},
|
||||
onMouseLeave: () => u(null),
|
||||
onMouseLeave: m,
|
||||
onFocus: (l) => {
|
||||
d && u({ label: d, anchor: l.currentTarget.querySelector("button") || l.currentTarget });
|
||||
C(r, l.currentTarget.querySelector("button") || l.currentTarget);
|
||||
},
|
||||
onBlur: () => u(null)
|
||||
}, B);
|
||||
onBlur: m
|
||||
}, V);
|
||||
});
|
||||
return t(
|
||||
o.Fragment,
|
||||
n.Fragment,
|
||||
null,
|
||||
t("div", {
|
||||
className: `selector${T ? ` ${T}` : ""}`,
|
||||
className: `selector${k ? ` ${k}` : ""}`,
|
||||
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)
|
||||
"data-visual": y === "icon" ? "icon" : "text",
|
||||
dir: L,
|
||||
style: I(w)
|
||||
}, H),
|
||||
t("div", { role: "tooltip", className: "tooltip", ref: h, "data-visible": "false" })
|
||||
);
|
||||
}
|
||||
return z;
|
||||
return R;
|
||||
}
|
||||
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;
|
||||
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();
|
||||
@ -200,12 +215,12 @@ class K extends HTMLElement {
|
||||
return this._props;
|
||||
}
|
||||
_render() {
|
||||
const { React: t, createRoot: c } = P();
|
||||
if (!t || !c) {
|
||||
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 = c(this._mount), this._view = F(t)), this._root.render(t.createElement(this._view, this._props));
|
||||
this._root || (this._root = d(this._mount), this._view = P(t)), this._root.render(t.createElement(this._view, this._props));
|
||||
}
|
||||
}
|
||||
customElements.get(R) || customElements.define(R, K);
|
||||
customElements.get(M) || customElements.define(M, K);
|
||||
|
||||
@ -22,6 +22,7 @@ const STYLES = `
|
||||
align-items: stretch;
|
||||
width: fit-content;
|
||||
height: 36px;
|
||||
flex-wrap: nowrap;
|
||||
padding: 4px;
|
||||
gap: 4px;
|
||||
flex-shrink: 0;
|
||||
@ -54,13 +55,17 @@ const STYLES = `
|
||||
white-space: nowrap;
|
||||
transition: background-color 160ms ease, color 160ms ease, box-shadow 160ms ease;
|
||||
}
|
||||
.selector[data-mode="options"] .option { padding-inline: 6px; }
|
||||
/* 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: 16px; height: 16px; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; font-size: 16px; }
|
||||
.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;
|
||||
@ -90,14 +95,13 @@ const STYLES = `
|
||||
background: #2d3748;
|
||||
transform: translateX(-50%) rotate(45deg);
|
||||
}
|
||||
.tooltip[data-side="top"]::after { bottom: -4px; }
|
||||
.tooltip[data-side="bottom"]::after { top: -4px; }
|
||||
.tooltip::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; }
|
||||
.selector[data-mode="options"][data-visual="text"] .option { padding-inline: 8px; }
|
||||
.title { font-size: 12px; }
|
||||
.icon { width: 20px; height: 20px; font-size: 20px; }
|
||||
.icon { width: 16px; height: 16px; font-size: 16px; }
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) { .option { transition: none; } }
|
||||
`;
|
||||
@ -120,7 +124,7 @@ function normalizeInlineStyle(props) {
|
||||
|
||||
function createView(React) {
|
||||
const h = React.createElement;
|
||||
const { useEffect, useLayoutEffect, useRef, useState } = React;
|
||||
const { useEffect, useRef, useState } = React;
|
||||
|
||||
function View(props) {
|
||||
const {
|
||||
@ -130,7 +134,6 @@ function createView(React) {
|
||||
// Selection always uses independent option buttons, never tab semantics.
|
||||
const safeItems = Array.isArray(items) ? items : [];
|
||||
const [internalValue, setInternalValue] = useState(defaultValue);
|
||||
const [tip, setTip] = useState(null);
|
||||
const tooltipRef = useRef(null);
|
||||
const buttonRefs = useRef([]);
|
||||
const selectedIndex = value !== undefined
|
||||
@ -139,32 +142,41 @@ function createView(React) {
|
||||
? safeItems.findIndex((item) => Object.is(item?.value, internalValue))
|
||||
: -1;
|
||||
|
||||
// Keep the tooltip outside React state. Focusing/hovering an item should
|
||||
// never rerender the selector (which could briefly disturb text layout).
|
||||
function hideTooltip() {
|
||||
if (tooltipRef.current) tooltipRef.current.dataset.visible = "false";
|
||||
}
|
||||
|
||||
function showTooltip(label, anchor) {
|
||||
const tooltip = tooltipRef.current;
|
||||
if (!label || !anchor?.isConnected || !tooltip) return;
|
||||
tooltip.textContent = label;
|
||||
tooltip.dataset.visible = "false";
|
||||
const rect = anchor.getBoundingClientRect();
|
||||
const box = tooltip.getBoundingClientRect();
|
||||
const viewportWidth = window.innerWidth;
|
||||
const left = Math.max(8, Math.min(
|
||||
rect.left + rect.width / 2 - box.width / 2,
|
||||
viewportWidth - box.width - 8,
|
||||
));
|
||||
tooltip.style.left = `${left}px`;
|
||||
tooltip.style.top = `${rect.bottom + 10}px`;
|
||||
tooltip.style.setProperty("--tip-arrow-x", `${Math.max(
|
||||
8, Math.min(rect.left + rect.width / 2 - left, box.width - 8),
|
||||
)}px`);
|
||||
tooltip.dataset.visible = "true";
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (!tip) return;
|
||||
const hide = () => setTip(null);
|
||||
const hide = () => hideTooltip();
|
||||
window.addEventListener("scroll", hide, true);
|
||||
window.addEventListener("resize", hide);
|
||||
return () => {
|
||||
window.removeEventListener("scroll", hide, true);
|
||||
window.removeEventListener("resize", hide);
|
||||
};
|
||||
}, [tip]);
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (!tip || !tooltipRef.current || !tip.anchor?.isConnected) return;
|
||||
const tooltip = tooltipRef.current;
|
||||
const rect = tip.anchor.getBoundingClientRect();
|
||||
const box = tooltip.getBoundingClientRect();
|
||||
const viewportWidth = window.innerWidth;
|
||||
const placeTop = rect.top >= box.height + 14;
|
||||
const top = placeTop ? rect.top - box.height - 10 : rect.bottom + 10;
|
||||
const left = Math.max(8, Math.min(rect.left + rect.width / 2 - box.width / 2, viewportWidth - box.width - 8));
|
||||
tooltip.style.left = `${left}px`;
|
||||
tooltip.style.top = `${top}px`;
|
||||
tooltip.dataset.side = placeTop ? "top" : "bottom";
|
||||
tooltip.style.setProperty("--tip-arrow-x", `${Math.max(8, Math.min(rect.left + rect.width / 2 - left, box.width - 8))}px`);
|
||||
tooltip.dataset.visible = "true";
|
||||
}, [tip]);
|
||||
}, []);
|
||||
|
||||
function activate(item, itemIndex) {
|
||||
if (isDisabled || item.isDisabled || itemIndex === selectedIndex) return;
|
||||
@ -203,13 +215,13 @@ function createView(React) {
|
||||
key: item.value ?? itemIndex,
|
||||
className: "item-wrap",
|
||||
onMouseEnter: (event) => {
|
||||
if (tooltip) setTip({ label: tooltip, anchor: event.currentTarget.querySelector("button") || event.currentTarget });
|
||||
showTooltip(tooltip, event.currentTarget.querySelector("button") || event.currentTarget);
|
||||
},
|
||||
onMouseLeave: () => setTip(null),
|
||||
onMouseLeave: hideTooltip,
|
||||
onFocus: (event) => {
|
||||
if (tooltip) setTip({ label: tooltip, anchor: event.currentTarget.querySelector("button") || event.currentTarget });
|
||||
showTooltip(tooltip, event.currentTarget.querySelector("button") || event.currentTarget);
|
||||
},
|
||||
onBlur: () => setTip(null),
|
||||
onBlur: hideTooltip,
|
||||
}, renderedControl);
|
||||
});
|
||||
|
||||
@ -223,7 +235,7 @@ function createView(React) {
|
||||
dir,
|
||||
style: normalizeInlineStyle(props),
|
||||
}, controls),
|
||||
tip && h("div", { role: "tooltip", className: "tooltip", ref: tooltipRef }, tip.label),
|
||||
h("div", { role: "tooltip", className: "tooltip", ref: tooltipRef, "data-visible": "false" }),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user