feat: let skins dock the recovery capsule into their layout with runtime fallback

- chrome.json#slots names an existing skin data-ws-part as anchor for host.recovery
  (align start/center/end, optional compact); host/trusted parts rejected as anchors
- reusable host-slot-placement module: positions the closed-shadow top-layer
  capsule over the anchor, re-checks visibility/size/on-screen/elementFromPoint,
  snaps back to the default corner on any failure with anti-flap re-slotting
- --skin-recovery-* tokens (accent/background/foreground/radius/font/size tier)
  sanitized with 28px minimum hit target and WCAG contrast fallback
- compact mode only after 5s full display post-activation; shortcut always full
- no new skin CSS compile-time restrictions; docs and host-chrome-demo updated
This commit is contained in:
OddTurtleHead committed 2026-11-15 15:00:24 +08:00
1 parent fa720bf260
commit aefa0bc163
18 files changed
+1547 -254

No files matched your search

+100 -13
View File
@@ -35,6 +35,8 @@
popover="manual"
@keydown.escape="shortcutOpen = false"
class="ws-host-recovery"
:class="{ 'ws-host-recovery-compact': compact }"
:tabindex="compact ? 0 : undefined"
data-ws-host-recovery
role="region"
:aria-label="copy.recovery"
@@ -42,12 +44,14 @@
<span class="ws-host-recovery-mark" aria-hidden="true">◈</span>
<button
type="button"
class="ws-host-recovery-action"
data-ws-recovery-action="built-in"
:title="copy.shortcut"
@click="useBuiltInSkin"
>{{ copy.useBuiltIn }}</button>
<button
type="button"
class="ws-host-recovery-action"
v-if="pluginLibraryAvailable"
data-ws-recovery-action="library"
:title="copy.library"
@@ -62,6 +66,8 @@ import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from "vue"
import type { SiteLanguage } from "../../../src/site-copy.js";
import { hostChromeCopy } from "../i18n/host-chrome.js";
import {
activeHostSlot,
hostChromeActiveSkinId,
isHostPartHidden,
isHostPartVisuallyReachable,
isRecoveryShortcut,
@@ -74,6 +80,7 @@ import {
syncRecoveryPopover,
type HostOwnedLayer,
} from "../services/host-owned-layer.js";
import { bindHostSlot, type HostSlotBinding } from "../services/host-slot-placement.js";
import { shouldShowRecovery } from "../services/host-chrome-visibility.js";
import { thirdPartyAllowed } from "../services/instance-authorization.js";
@@ -94,35 +101,52 @@ const RECOVERY_LAYER_CSS = `
max-width: calc(100vw - 24px);
padding: 4px 5px 4px 9px;
border: 1px solid #9db6b2;
border-radius: 999px;
background: color-mix(in srgb, #ffffff 92%, transparent);
color: #31524f;
border-radius: var(--ws-recovery-radius, 999px);
background: var(--ws-recovery-background, color-mix(in srgb, #ffffff 92%, transparent));
color: var(--ws-recovery-foreground, #31524f);
font-family: var(--ws-recovery-font, ui-sans-serif, system-ui, sans-serif);
font-size: var(--ws-recovery-font-size, 11px);
box-shadow: 0 6px 18px #0f2a2626;
pointer-events: auto;
}
.ws-host-recovery:popover-open { display: flex; }
.ws-host-recovery-mark { font-size: 12px; line-height: 1; }
.ws-host-recovery-mark {
display: inline-flex;
align-items: center;
justify-content: center;
font-size: 12px;
line-height: 1;
}
.ws-host-recovery button {
display: inline-flex;
align-items: center;
min-height: 28px;
min-height: var(--ws-recovery-hit, 28px);
padding: 4px 9px;
border: 1px solid #9db6b2;
border-radius: 999px;
background: #fff;
color: #31524f;
border-radius: var(--ws-recovery-radius, 999px);
background: var(--ws-recovery-background, #fff);
color: var(--ws-recovery-foreground, #31524f);
font: inherit;
cursor: pointer;
white-space: nowrap;
}
.ws-host-recovery button[data-ws-recovery-action="built-in"] {
border-color: #b45309;
border-color: var(--ws-recovery-accent, #b45309);
background: #fff7ed;
color: #7c2d12;
}
.ws-host-recovery button:hover { filter: brightness(.96); }
.ws-host-recovery button:focus-visible { outline: 2px solid #4c8f88; outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) { .ws-host-recovery button { transition: none; } }
.ws-host-recovery.ws-host-recovery-compact:not(:hover):not(:focus-within) { gap: 0; padding: 4px; }
.ws-host-recovery.ws-host-recovery-compact:not(:hover):not(:focus-within) .ws-host-recovery-action { display: none; }
.ws-host-recovery.ws-host-recovery-compact:not(:hover):not(:focus-within) .ws-host-recovery-mark {
min-width: var(--ws-recovery-hit, 28px);
min-height: var(--ws-recovery-hit, 28px);
}
.ws-host-recovery.ws-host-recovery-compact:focus-visible { outline: 2px solid var(--ws-recovery-accent, #4c8f88); outline-offset: 2px; }
@media (prefers-reduced-motion: reduce) {
.ws-host-recovery, .ws-host-recovery button { transition: none !important; }
}
`;
const layer: HostOwnedLayer | null = createHostOwnedLayer("recovery", RECOVERY_LAYER_CSS);
@@ -148,6 +172,49 @@ const shortcutOpen = ref(false);
const recoveryPopover = ref<HTMLElement | null>(null);
const recoveryVisible = computed(() => showRecovery.value || (shortcutOpen.value && thirdPartyAllowed.value));
/**
* Optional anchor from the active skin's chrome.json. Only third-party skins
* can declare it; the host still owns the capsule and the module falls back to
* the default corner whenever the anchor stops being usable.
*/
const slotConfig = computed(() => activeHostSlot("host.recovery"));
const slotMode = ref<"floating" | "slotted">("floating");
const compactHold = ref(true);
let compactHoldTimer: ReturnType<typeof setTimeout> | undefined;
watch(hostChromeActiveSkinId, () => {
// Keep the full capsule visible for a moment after a skin is activated, so
// the entry stays discoverable before it folds into the skin's layout.
compactHold.value = true;
if (compactHoldTimer) clearTimeout(compactHoldTimer);
compactHoldTimer = setTimeout(() => { compactHold.value = false; }, 5000);
}, { immediate: true });
const compact = computed(() => Boolean(slotConfig.value?.compact)
&& slotMode.value === "slotted"
&& !compactHold.value
&& !shortcutOpen.value);
let slotBinding: HostSlotBinding | undefined;
function syncSlotBinding() {
slotBinding?.dispose();
slotBinding = undefined;
const control = recoveryPopover.value;
const config = slotConfig.value;
if (!control || !config || !layer) {
slotMode.value = "floating";
return;
}
slotBinding = bindHostSlot({
layer,
control,
anchor: config.anchor,
skinId: hostChromeActiveSkinId.value,
align: config.align,
onModeChange: change => { slotMode.value = change.mode; },
});
}
function syncPopover() {
syncRecoveryPopover(recoveryPopover.value, recoveryVisible.value);
}
@@ -169,7 +236,11 @@ function onKeydown(event: KeyboardEvent) {
if (isRecoveryShortcut(event)) {
event.preventDefault();
shortcutOpen.value = true;
void nextTick(() => { syncPopover(); recoveryPopover.value?.querySelector("button")?.focus(); });
void nextTick(() => {
syncPopover();
slotBinding?.revalidate();
recoveryPopover.value?.querySelector("button")?.focus();
});
}
}
@@ -178,7 +249,18 @@ function refreshLauncherReachability() {
launcherReachable.value = isHostPartVisuallyReachable("host.plugins.launcher");
}
watch([recoveryVisible, recoveryPopover], () => { void nextTick(syncPopover); }, { flush: "post" });
watch([recoveryVisible, recoveryPopover], () => {
void nextTick(() => {
syncPopover();
slotBinding?.revalidate();
});
}, { flush: "post" });
watch([slotConfig, hostChromeActiveSkinId], () => {
void nextTick(() => {
syncPopover();
syncSlotBinding();
});
}, { flush: "post" });
watch(thirdPartyAllowed, allowed => { if (!allowed) shortcutOpen.value = false; });
onMounted(() => {
@@ -186,10 +268,15 @@ onMounted(() => {
reachabilityTimer = setInterval(refreshLauncherReachability, 1000);
window.addEventListener("resize", refreshLauncherReachability);
window.addEventListener("keydown", onKeydown, true);
void nextTick(syncPopover);
void nextTick(() => {
syncPopover();
syncSlotBinding();
});
});
onBeforeUnmount(() => {
if (reachabilityTimer) clearInterval(reachabilityTimer);
if (compactHoldTimer) clearTimeout(compactHoldTimer);
slotBinding?.dispose();
window.removeEventListener("resize", refreshLauncherReachability);
window.removeEventListener("keydown", onKeydown, true);
layer?.dispose();
@@ -1,6 +1,12 @@
import assert from "node:assert/strict";
import test from "node:test";
import { isBundledSkin } from "./host-chrome.js";
import { parseSkinChromeDocument } from "../../../src/shared/skin-chrome.js";
import {
activeHostSlot,
isBundledSkin,
setHostChromeActiveSkin,
setSkinHostChromeParts,
} from "./host-chrome.js";
import { isHostPartReachable, shouldShowRecovery, type HostPartVisibilitySnapshot } from "./host-chrome-visibility.js";
const snapshot = (overrides: Partial<HostPartVisibilitySnapshot> = {}): HostPartVisibilitySnapshot => ({
@@ -78,3 +84,28 @@ test("the recovery entry is persistent for third-party skins only", () => {
assert.equal(shouldShowRecovery({ ...base, thirdPartyAllowed: false, thirdPartySkinActive: true }), false);
assert.equal(shouldShowRecovery({ ...base, thirdPartyAllowed: false, launcherHiddenByConfig: true }), false);
});
test("slot declarations are honoured for third-party skins only", () => {
const document = parseSkinChromeDocument(JSON.stringify({
schemaVersion: 1,
slots: { "host.recovery": { anchor: "voice.audio-dock" } },
}));
setSkinHostChromeParts(document);
setHostChromeActiveSkin("community.demo");
assert.deepEqual(activeHostSlot("host.recovery"), {
anchor: "voice.audio-dock",
align: "end",
compact: false,
});
// A part that is not slottable cannot be selected through chrome.json.
assert.equal(activeHostSlot("host.permission"), undefined);
// Bundled/official skins keep the default floating placement.
for (const id of [undefined, "builtin.light", "builtin.dark", "community.illusia-voice", "community.kaak-voice"]) {
setHostChromeActiveSkin(id);
assert.equal(activeHostSlot("host.recovery"), undefined, String(id));
}
setSkinHostChromeParts(undefined);
setHostChromeActiveSkin(undefined);
});
+15 -2
View File
@@ -1,6 +1,6 @@
import { computed, readonly, ref, shallowRef } from "vue";
import { DECORATIVE_PART_IDS } from "../../../src/shared/skin-visual-parts.js";
import { resolveHiddenHostParts, type SkinChromeDocument } from "../../../src/shared/skin-chrome.js";
import { DECORATIVE_PART_IDS, HOST_SLOTTABLE_PART_IDS } from "../../../src/shared/skin-visual-parts.js";
import { resolveHiddenHostParts, type SkinChromeDocument, type SkinChromeSlot } from "../../../src/shared/skin-chrome.js";
import { isHostPartReachable } from "./host-chrome-visibility.js";
/**
@@ -30,6 +30,9 @@ export function setHostChromeActiveSkin(id: string | undefined): void {
activeSkin.value = id;
}
/** Read-only view for host chrome that reacts to skin activations. */
export const hostChromeActiveSkinId = readonly(activeSkin);
export function isBundledSkin(id: string | undefined): boolean {
return !id || id.startsWith("builtin.") || BUNDLED_SKIN_IDS.has(id);
}
@@ -60,6 +63,16 @@ export function isHostPartHidden(part: string): boolean {
return hiddenParts.value.has(part);
}
/**
* Slot declarations are honoured only for an active third-party skin. Bundled
* skins, and any skin without a matching `chrome.json#slots` entry, keep the
* default floating placement.
*/
export function activeHostSlot(part: string): SkinChromeSlot | undefined {
if (!HOST_SLOTTABLE_PART_IDS.has(part) || !isThirdPartySkinActive()) return undefined;
return skinChrome.value?.slots?.[part];
}
/**
* Read the live DOM to find out whether a host part can actually be used.
* Declarative hiding (`v-if`) removes the element entirely; CSS-only hiding
@@ -0,0 +1,243 @@
import assert from "node:assert/strict";
import test from "node:test";
import {
INITIAL_SLOT_MODE_STATE,
RECOVERY_MIN_ACCENT_CONTRAST,
RECOVERY_MIN_FONT_SIZE,
RECOVERY_MIN_FOREGROUND_CONTRAST,
RECOVERY_MIN_HIT_TARGET,
RECOVERY_SIZE_METRICS,
computeSlotPlacement,
contrastRatio,
evaluateSlottedControl,
nextSlotModeState,
parseCssColor,
relativeLuminance,
resolveRecoveryTokens,
type SlotAnchorSnapshot,
} from "./host-slot-placement.js";
import type { HostPartRect } from "./host-chrome-visibility.js";
const rect = (left: number, top: number, width: number, height: number): HostPartRect => ({
left,
top,
width,
height,
right: left + width,
bottom: top + height,
});
const anchor = (overrides: Partial<SlotAnchorSnapshot> = {}): SlotAnchorSnapshot => ({
rect: rect(100, 100, 200, 40),
display: "flex",
visibility: "visible",
opacity: "1",
...overrides,
});
const baseEvaluation = {
anchor: anchor(),
controlRect: rect(180, 105, 120, 30),
viewport: { width: 800, height: 600 },
minHitTarget: RECOVERY_MIN_HIT_TARGET,
hitTestOk: true,
};
test("slot placement aligns inside the anchor and centres vertically", () => {
const control = { width: 120, height: 30 };
assert.deepEqual(computeSlotPlacement(anchor().rect, control, baseEvaluation.viewport, "start"), { left: 100, top: 105 });
assert.deepEqual(computeSlotPlacement(anchor().rect, control, baseEvaluation.viewport, "center"), { left: 140, top: 105 });
assert.deepEqual(computeSlotPlacement(anchor().rect, control, baseEvaluation.viewport), { left: 180, top: 105 });
assert.deepEqual(computeSlotPlacement(anchor().rect, control, baseEvaluation.viewport, "end"), { left: 180, top: 105 });
});
test("slot placement clamps the control fully inside the viewport", () => {
const viewport = { width: 800, height: 600 };
const control = { width: 120, height: 30 };
// Anchor off the left/top edge: the control stops at the safe inset.
assert.deepEqual(computeSlotPlacement(rect(-500, -200, 100, 40), control, viewport, "start"), { left: 4, top: 4 });
// Anchor off the right/bottom edge: the control stops before the edge.
assert.deepEqual(computeSlotPlacement(rect(900, 700, 100, 40), control, viewport, "end"), { left: 676, top: 566 });
// The anchor may be smaller than the control; the control overflows the
// anchor but still stays inside the viewport.
const tiny = computeSlotPlacement(rect(400, 300, 20, 10), control, viewport, "center");
assert.ok(tiny.left >= 4 && tiny.left + control.width <= viewport.width - 4);
assert.ok(tiny.top >= 4 && tiny.top + control.height <= viewport.height - 4);
});
test("a usable slotted control passes every check", () => {
assert.deepEqual(evaluateSlottedControl(baseEvaluation), { ok: true });
});
test("missing, hidden, tiny and off-screen anchors fall back", () => {
assert.deepEqual(evaluateSlottedControl({ ...baseEvaluation, anchor: null }), { ok: false, reason: "anchor-missing" });
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ display: "none" }) }),
{ ok: false, reason: "anchor-hidden" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ visibility: "hidden" }) }),
{ ok: false, reason: "anchor-hidden" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ opacity: "0" }) }),
{ ok: false, reason: "anchor-hidden" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ rect: rect(100, 100, 4, 4) }) }),
{ ok: false, reason: "anchor-tiny" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ rect: rect(-500, -500, 100, 40) }) }),
{ ok: false, reason: "anchor-offscreen" },
);
// Only a small fraction of the anchor is on screen.
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ rect: rect(790, 100, 100, 40) }) }),
{ ok: false, reason: "anchor-offscreen" },
);
// A clipped anchor is unusable even when its box looks fine.
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, anchor: anchor({ clipPath: "inset(50%)" }) }),
{ ok: false, reason: "anchor-offscreen" },
);
});
test("an anchor that is at least half visible can host the control", () => {
assert.deepEqual(
evaluateSlottedControl({
...baseEvaluation,
anchor: anchor({ rect: rect(750, 100, 100, 40) }),
controlRect: rect(700, 100, 60, 30),
}),
{ ok: true },
);
});
test("control size, position and hit test are enforced", () => {
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, controlRect: rect(180, 105, 0, 0) }),
{ ok: false, reason: "control-hidden" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, controlRect: rect(180, 105, 20, 20) }),
{ ok: false, reason: "control-too-small" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, controlRect: rect(760, 105, 120, 30) }),
{ ok: false, reason: "control-offscreen" },
);
assert.deepEqual(
evaluateSlottedControl({ ...baseEvaluation, hitTestOk: false }),
{ ok: false, reason: "hit-test" },
);
});
test("CSS colours parse to RGB and ignore alpha for contrast", () => {
assert.deepEqual(parseCssColor("#1d4f9c"), { r: 29, g: 79, b: 156 });
assert.deepEqual(parseCssColor("#fff"), { r: 255, g: 255, b: 255 });
assert.deepEqual(parseCssColor("#ffff"), { r: 255, g: 255, b: 255 });
assert.deepEqual(parseCssColor("rgb(29, 79, 156)"), { r: 29, g: 79, b: 156 });
assert.deepEqual(parseCssColor("rgb(29 79 156 / 50%)"), { r: 29, g: 79, b: 156 });
assert.deepEqual(parseCssColor("rgba(255, 0, 0, .4)"), { r: 255, g: 0, b: 0 });
assert.deepEqual(parseCssColor("rgb(300, -5, 0)"), { r: 255, g: 0, b: 0 });
for (const invalid of ["", "red", "hsl(10, 20%, 30%)", "color(srgb 1 0 0)", "#12345", "rgb(1, 2)"]) {
assert.equal(parseCssColor(invalid), null, invalid);
}
});
test("WCAG contrast follows the relative-luminance formula", () => {
assert.equal(relativeLuminance({ r: 0, g: 0, b: 0 }), 0);
assert.equal(relativeLuminance({ r: 255, g: 255, b: 255 }), 1);
assert.equal(contrastRatio({ r: 0, g: 0, b: 0 }, { r: 255, g: 255, b: 255 }), 21);
assert.equal(contrastRatio({ r: 12, g: 34, b: 56 }, { r: 12, g: 34, b: 56 }), 1);
});
test("skin tokens default to the host palette", () => {
const tokens = resolveRecoveryTokens();
assert.deepEqual(tokens, {
accent: "rgb(180, 83, 9)",
background: "rgb(255, 255, 255)",
foreground: "rgb(49, 82, 79)",
radius: 999,
font: "ui-sans-serif, system-ui, sans-serif",
size: "small",
hitTarget: 28,
fontSize: 11,
});
});
test("a readable skin palette is kept and translucent backgrounds become opaque", () => {
const tokens = resolveRecoveryTokens({
accent: "#47dcc4",
background: "rgba(9, 19, 23, 0.4)",
foreground: "#edf8f5",
});
assert.equal(tokens.accent, "rgb(71, 220, 196)");
assert.equal(tokens.background, "rgb(9, 19, 23)");
assert.equal(tokens.foreground, "rgb(237, 248, 245)");
});
test("an unreadable colour group falls back as a whole", () => {
const lowForeground = resolveRecoveryTokens({ background: "#777777", foreground: "#888888", accent: "#47dcc4" });
assert.equal(lowForeground.background, "rgb(255, 255, 255)");
assert.equal(lowForeground.foreground, "rgb(49, 82, 79)");
assert.equal(lowForeground.accent, "rgb(180, 83, 9)");
const lowAccent = resolveRecoveryTokens({ background: "#ffffff", foreground: "#31524f", accent: "#eeeeee" });
assert.equal(lowAccent.accent, "rgb(180, 83, 9)");
assert.ok(contrastRatio(parseCssColor(lowAccent.accent)!, parseCssColor(lowAccent.background)!) >= RECOVERY_MIN_ACCENT_CONTRAST);
assert.ok(contrastRatio(parseCssColor(lowAccent.foreground)!, parseCssColor(lowAccent.background)!) >= RECOVERY_MIN_FOREGROUND_CONTRAST);
});
test("radius, font and size tokens are sanitized and host minimums win", () => {
assert.equal(resolveRecoveryTokens({ radius: "12px" }).radius, 12);
assert.equal(resolveRecoveryTokens({ radius: "2000px" }).radius, 999);
assert.equal(resolveRecoveryTokens({ radius: "-5px" }).radius, 0);
assert.equal(resolveRecoveryTokens({ radius: "1em" }).radius, 999);
assert.equal(resolveRecoveryTokens({ radius: "12" }).radius, 999);
assert.equal(
resolveRecoveryTokens({ font: 'Tahoma, "Segoe UI", sans-serif' }).font,
'Tahoma, "Segoe UI", sans-serif',
);
for (const font of ["url(https://example.invalid/a.woff2)", "Tahoma; color: red", "x".repeat(121)]) {
assert.equal(resolveRecoveryTokens({ font }).font, "ui-sans-serif, system-ui, sans-serif", font);
}
assert.equal(resolveRecoveryTokens({ size: "medium" }).hitTarget, 32);
assert.equal(resolveRecoveryTokens({ size: "medium" }).fontSize, 12);
assert.equal(resolveRecoveryTokens({ size: "large" }).hitTarget, 36);
assert.equal(resolveRecoveryTokens({ size: "large" }).fontSize, 13);
assert.equal(resolveRecoveryTokens({ size: "huge" }).size, "small");
for (const metrics of Object.values(RECOVERY_SIZE_METRICS)) {
assert.ok(metrics.hitTarget >= RECOVERY_MIN_HIT_TARGET);
assert.ok(metrics.fontSize >= RECOVERY_MIN_FONT_SIZE);
}
});
test("the mode gate does not flap between the anchor and the fallback", () => {
let state = INITIAL_SLOT_MODE_STATE;
// The first valid check slots immediately, even under the anti-flap rule.
state = nextSlotModeState(state, true);
assert.deepEqual(state, { slotted: true, validStreak: 0, hasSlotted: true });
// Any failure returns to the floating position at once.
state = nextSlotModeState(state, false);
assert.deepEqual(state, { slotted: false, validStreak: 0, hasSlotted: true });
// A single valid sample is not enough to re-slot.
state = nextSlotModeState(state, true);
assert.equal(state.slotted, false);
assert.equal(state.validStreak, 1);
// A failure resets the streak.
state = nextSlotModeState(state, false);
assert.equal(state.validStreak, 0);
state = nextSlotModeState(state, true);
assert.equal(state.slotted, false);
state = nextSlotModeState(state, true);
assert.equal(state.slotted, true);
assert.equal(state.hasSlotted, true);
});
+683
View File
@@ -0,0 +1,683 @@
/**
* Runtime placement for trusted host controls that a skin may fold into its
* own layout.
*
* A skin cannot own, style or hide these controls: the control still lives in
* the host's closed shadow root and top layer. `chrome.json#slots` only names
* an existing `data-ws-part` anchor inside `.ws-skin-root`; this module reads
* that anchor, positions the host control over it, and falls back to the
* default corner whenever the anchor stops being a safe place to click.
*
* `isHostPartReachable` stays the single visibility predicate for host parts;
* the extra checks here cover only slot-specific geometry (anchor size and
* on-screen ratio, control hit target, top-layer hit test).
*/
import type { SkinChromeSlotAlign } from "../../../src/shared/skin-chrome.js";
import type { HostOwnedLayer } from "./host-owned-layer.js";
import { isHostPartReachable, type HostPartRect } from "./host-chrome-visibility.js";
export interface SlotViewport {
width: number;
height: number;
}
/** Small inset so the capsule never touches the viewport edge. */
export const HOST_SLOT_VIEWPORT_INSET = 4;
/** An anchor narrower or shorter than this cannot host a usable control. */
export const HOST_SLOT_MIN_ANCHOR_SIZE = 8;
/** At least half of the anchor must be on screen. */
export const HOST_SLOT_MIN_VISIBLE_RATIO = 0.5;
/** Re-slot only after the anchor stays usable for consecutive checks. */
export const HOST_SLOT_REQUIRED_VALID_CHECKS = 2;
export const HOST_SLOT_SLOTTED_INTERVAL_MS = 400;
export const HOST_SLOT_IDLE_INTERVAL_MS = 1000;
/**
* Place the control inside the anchor rect, vertically centred and aligned on
* one horizontal edge. The result is clamped so the whole control stays inside
* the viewport; when the anchor is smaller than the control the control may
* overflow the anchor, but never the viewport.
*/
export function computeSlotPlacement(
anchor: HostPartRect,
control: { width: number; height: number },
viewport: SlotViewport,
align: SkinChromeSlotAlign = "end",
): { left: number; top: number } {
const width = Math.max(0, control.width);
const height = Math.max(0, control.height);
const inset = HOST_SLOT_VIEWPORT_INSET;
const desiredLeft = align === "start"
? anchor.left
: align === "center"
? anchor.left + (anchor.width - width) / 2
: anchor.right - width;
const desiredTop = anchor.top + (anchor.height - height) / 2;
const maxLeft = Math.max(inset, viewport.width - inset - width);
const maxTop = Math.max(inset, viewport.height - inset - height);
return {
left: Math.min(Math.max(inset, desiredLeft), maxLeft),
top: Math.min(Math.max(inset, desiredTop), maxTop),
};
}
export type SlotFailureReason =
| "anchor-missing"
| "anchor-hidden"
| "anchor-tiny"
| "anchor-offscreen"
| "control-hidden"
| "control-too-small"
| "control-offscreen"
| "hit-test";
export interface SlotAnchorSnapshot {
rect: HostPartRect;
display: string;
visibility: string;
opacity: string;
clipPath?: string;
clip?: string;
}
export interface SlotEvaluationInput {
anchor: SlotAnchorSnapshot | null | undefined;
controlRect: HostPartRect;
viewport: SlotViewport;
/** Host-enforced minimum hit target for the current size tier. */
minHitTarget: number;
/** `document.elementFromPoint` at the control centre is the host layer. */
hitTestOk: boolean;
}
export type SlotEvaluation = { ok: true } | { ok: false; reason: SlotFailureReason };
function visibleAreaRatio(rect: HostPartRect, viewport: SlotViewport): number {
const width = Math.min(rect.right, viewport.width) - Math.max(rect.left, 0);
const height = Math.min(rect.bottom, viewport.height) - Math.max(rect.top, 0);
const area = rect.width * rect.height;
if (!(area > 0) || !(width > 0) || !(height > 0)) return 0;
return (width * height) / area;
}
function fullyInsideViewport(rect: HostPartRect, viewport: SlotViewport): boolean {
const epsilon = 0.5;
return rect.left >= -epsilon
&& rect.top >= -epsilon
&& rect.right <= viewport.width + epsilon
&& rect.bottom <= viewport.height + epsilon;
}
export function evaluateSlottedControl(input: SlotEvaluationInput): SlotEvaluation {
const { anchor, controlRect, viewport, minHitTarget, hitTestOk } = input;
if (!anchor) return { ok: false, reason: "anchor-missing" };
if (anchor.display === "none" || anchor.visibility === "hidden" || anchor.visibility === "collapse") {
return { ok: false, reason: "anchor-hidden" };
}
const opacity = Number.parseFloat(anchor.opacity);
if (Number.isFinite(opacity) && opacity <= 0.01) return { ok: false, reason: "anchor-hidden" };
if (anchor.rect.width < HOST_SLOT_MIN_ANCHOR_SIZE || anchor.rect.height < HOST_SLOT_MIN_ANCHOR_SIZE) {
return { ok: false, reason: "anchor-tiny" };
}
// The anchor itself does not have to accept pointer events: the control is
// hit-tested separately, above it.
if (!isHostPartReachable({
display: anchor.display,
visibility: anchor.visibility,
opacity: anchor.opacity,
pointerEvents: "auto",
clipPath: anchor.clipPath,
clip: anchor.clip,
rect: anchor.rect,
viewport,
})) {
return { ok: false, reason: "anchor-offscreen" };
}
if (visibleAreaRatio(anchor.rect, viewport) < HOST_SLOT_MIN_VISIBLE_RATIO) {
return { ok: false, reason: "anchor-offscreen" };
}
if (!(controlRect.width > 0) || !(controlRect.height > 0)) return { ok: false, reason: "control-hidden" };
if (controlRect.width < minHitTarget || controlRect.height < minHitTarget) {
return { ok: false, reason: "control-too-small" };
}
if (!fullyInsideViewport(controlRect, viewport)) return { ok: false, reason: "control-offscreen" };
if (!hitTestOk) return { ok: false, reason: "hit-test" };
return { ok: true };
}
export interface SlotModeState {
slotted: boolean;
/** Consecutive valid checks since the last fallback. */
validStreak: number;
/** Once true, re-slotting needs the anti-flap streak. */
hasSlotted: boolean;
}
export const INITIAL_SLOT_MODE_STATE: SlotModeState = { slotted: false, validStreak: 0, hasSlotted: false };
/**
* The first valid check slots immediately (no initial flash); after any
* fallback, the anchor must stay valid for `requiredValidChecks` checks so a
* flickering CSS animation cannot make the capsule jump every frame.
*/
export function nextSlotModeState(
current: SlotModeState,
ok: boolean,
requiredValidChecks = HOST_SLOT_REQUIRED_VALID_CHECKS,
): SlotModeState {
if (!ok) {
return { slotted: false, validStreak: 0, hasSlotted: current.hasSlotted };
}
if (current.slotted) return { ...current, validStreak: 0 };
const validStreak = current.validStreak + 1;
const slotted = !current.hasSlotted || validStreak >= Math.max(1, requiredValidChecks);
return {
slotted,
validStreak: slotted ? 0 : validStreak,
hasSlotted: current.hasSlotted || slotted,
};
}
/* ------------------------------------------------------------------ *
* Style tokens
* ------------------------------------------------------------------ */
export type RecoverySizeTier = "small" | "medium" | "large";
export interface RecoverySizeMetrics {
/** Host-enforced minimum button hit target in CSS pixels. */
readonly hitTarget: number;
/** Host-enforced minimum font size in CSS pixels. */
readonly fontSize: number;
}
export const RECOVERY_MIN_HIT_TARGET = 28;
export const RECOVERY_MIN_FONT_SIZE = 11;
export const RECOVERY_MIN_FOREGROUND_CONTRAST = 4.5;
export const RECOVERY_MIN_ACCENT_CONTRAST = 3;
/** Host-controlled metrics; a skin can choose a tier, never a smaller size. */
export const RECOVERY_SIZE_METRICS: Readonly<Record<RecoverySizeTier, RecoverySizeMetrics>> = {
small: { hitTarget: RECOVERY_MIN_HIT_TARGET, fontSize: RECOVERY_MIN_FONT_SIZE },
medium: { hitTarget: 32, fontSize: 12 },
large: { hitTarget: 36, fontSize: 13 },
};
export interface RecoveryRawTokens {
/** Computed-style colour values; unresolved CSS values are rejected. */
accent?: string;
background?: string;
foreground?: string;
radius?: string;
font?: string;
size?: string;
}
export interface RecoveryTokens {
accent: string;
background: string;
foreground: string;
radius: number;
font: string;
size: RecoverySizeTier;
hitTarget: number;
fontSize: number;
}
export interface RgbColor {
r: number;
g: number;
b: number;
}
/** CSS custom properties a skin declares on the anchor or an ancestor. */
export const RECOVERY_SKIN_TOKEN_PROPERTIES = {
accent: "--skin-recovery-accent",
background: "--skin-recovery-background",
foreground: "--skin-recovery-foreground",
radius: "--skin-recovery-radius",
font: "--skin-recovery-font",
size: "--skin-recovery-size",
} as const;
/** Host-owned variables set inside the closed shadow root. */
export const RECOVERY_APPLIED_VARIABLES = {
accent: "--ws-recovery-accent",
background: "--ws-recovery-background",
foreground: "--ws-recovery-foreground",
radius: "--ws-recovery-radius",
font: "--ws-recovery-font",
hitTarget: "--ws-recovery-hit",
fontSize: "--ws-recovery-font-size",
} as const;
const DEFAULT_ACCENT: RgbColor = { r: 180, g: 83, b: 9 };
const DEFAULT_BACKGROUND: RgbColor = { r: 255, g: 255, b: 255 };
const DEFAULT_FOREGROUND: RgbColor = { r: 49, g: 82, b: 79 };
const DEFAULT_RADIUS = 999;
const DEFAULT_FONT = "ui-sans-serif, system-ui, sans-serif";
const MAX_RADIUS = 999;
const MAX_FONT_LENGTH = 120;
const FONT_FAMILY_PATTERN = /^[A-Za-z0-9 ,'"-]{1,120}$/;
/** Parse `#rgb`, `#rgba`, `#rrggbb`, `#rrggbbaa`, `rgb()` and `rgba()`. */
export function parseCssColor(value: string | undefined | null): RgbColor | null {
if (!value) return null;
const text = value.trim().toLowerCase();
const hex = /^#([0-9a-f]{3,8})$/.exec(text);
if (hex) {
const digits = hex[1];
if (digits.length === 3 || digits.length === 4) {
return {
r: Number.parseInt(digits[0] + digits[0], 16),
g: Number.parseInt(digits[1] + digits[1], 16),
b: Number.parseInt(digits[2] + digits[2], 16),
};
}
if (digits.length === 6 || digits.length === 8) {
return {
r: Number.parseInt(digits.slice(0, 2), 16),
g: Number.parseInt(digits.slice(2, 4), 16),
b: Number.parseInt(digits.slice(4, 6), 16),
};
}
return null;
}
const fn = /^rgba?\(([^()]*)\)$/.exec(text);
if (!fn) return null;
const parts = fn[1].replace(/\//g, " ").split(/[\s,]+/).filter(Boolean);
if (parts.length < 3 || parts.length > 4) return null;
const channels: number[] = [];
for (const part of parts.slice(0, 3)) {
const percent = part.endsWith("%");
const parsed = Number.parseFloat(part);
if (!Number.isFinite(parsed)) return null;
const channel = percent ? parsed * 2.55 : parsed;
channels.push(Math.round(Math.min(255, Math.max(0, channel))));
}
return { r: channels[0], g: channels[1], b: channels[2] };
}
export function formatRgb(color: RgbColor): string {
return `rgb(${color.r}, ${color.g}, ${color.b})`;
}
/** WCAG 2.x relative luminance, 0 (black) to 1 (white). */
export function relativeLuminance(color: RgbColor): number {
const channel = (value: number): number => {
const normalized = Math.min(255, Math.max(0, value)) / 255;
return normalized <= 0.03928 ? normalized / 12.92 : ((normalized + 0.055) / 1.055) ** 2.4;
};
return 0.2126 * channel(color.r) + 0.7152 * channel(color.g) + 0.0722 * channel(color.b);
}
export function contrastRatio(first: RgbColor, second: RgbColor): number {
const lighter = Math.max(relativeLuminance(first), relativeLuminance(second));
const darker = Math.min(relativeLuminance(first), relativeLuminance(second));
return (lighter + 0.05) / (darker + 0.05);
}
export function isRecoverySizeTier(value: unknown): value is RecoverySizeTier {
return value === "small" || value === "medium" || value === "large";
}
function sanitizeRadius(value: string | undefined): number {
if (!value) return DEFAULT_RADIUS;
const match = /^(-?\d+(?:\.\d+)?)px$/i.exec(value.trim());
if (!match) return DEFAULT_RADIUS;
const radius = Number.parseFloat(match[1]);
return Number.isFinite(radius) ? Math.min(MAX_RADIUS, Math.max(0, radius)) : DEFAULT_RADIUS;
}
function sanitizeFont(value: string | undefined): string {
if (!value) return DEFAULT_FONT;
const font = value.trim().replace(/\s+/g, " ");
if (!font || font.length > MAX_FONT_LENGTH || !FONT_FAMILY_PATTERN.test(font) || !/[A-Za-z0-9]/.test(font)) {
return DEFAULT_FONT;
}
return font;
}
/**
* Sanitize skin-provided tokens. Colours are all-or-nothing: a readable
* foreground/background pair and a visible accent keep the skin palette;
* otherwise the whole colour group reverts to the host palette so a skin can
* never make the recovery action unreadable.
*/
export function resolveRecoveryTokens(raw: RecoveryRawTokens = {}): RecoveryTokens {
const accent = parseCssColor(raw.accent) ?? DEFAULT_ACCENT;
const background = parseCssColor(raw.background) ?? DEFAULT_BACKGROUND;
const foreground = parseCssColor(raw.foreground) ?? DEFAULT_FOREGROUND;
const readable = contrastRatio(foreground, background) >= RECOVERY_MIN_FOREGROUND_CONTRAST
&& contrastRatio(accent, background) >= RECOVERY_MIN_ACCENT_CONTRAST;
const palette = readable ? { accent, background, foreground } : {
accent: DEFAULT_ACCENT,
background: DEFAULT_BACKGROUND,
foreground: DEFAULT_FOREGROUND,
};
const sizeValue = raw.size?.trim();
const size = isRecoverySizeTier(sizeValue) ? sizeValue : "small";
const metrics = RECOVERY_SIZE_METRICS[size];
return {
accent: formatRgb(palette.accent),
background: formatRgb(palette.background),
foreground: formatRgb(palette.foreground),
radius: sanitizeRadius(raw.radius),
font: sanitizeFont(raw.font),
size,
hitTarget: metrics.hitTarget,
fontSize: metrics.fontSize,
};
}
/** Apply (or clear) host-owned variables on the control inside the shadow root. */
export function applyRecoveryTokens(control: HTMLElement, tokens: RecoveryTokens | null): void {
const style = control.style;
if (!tokens) {
for (const property of Object.values(RECOVERY_APPLIED_VARIABLES)) style.removeProperty(property);
return;
}
style.setProperty(RECOVERY_APPLIED_VARIABLES.accent, tokens.accent);
style.setProperty(RECOVERY_APPLIED_VARIABLES.background, tokens.background);
style.setProperty(RECOVERY_APPLIED_VARIABLES.foreground, tokens.foreground);
style.setProperty(RECOVERY_APPLIED_VARIABLES.radius, `${tokens.radius}px`);
style.setProperty(RECOVERY_APPLIED_VARIABLES.font, tokens.font);
style.setProperty(RECOVERY_APPLIED_VARIABLES.hitTarget, `${tokens.hitTarget}px`);
style.setProperty(RECOVERY_APPLIED_VARIABLES.fontSize, `${tokens.fontSize}px`);
}
/* ------------------------------------------------------------------ *
* DOM binding
* ------------------------------------------------------------------ */
function rectOf(rect: DOMRect | HostPartRect): HostPartRect {
return {
width: rect.width,
height: rect.height,
left: rect.left,
top: rect.top,
right: rect.right,
bottom: rect.bottom,
};
}
function isVisibleAnchor(element: HTMLElement): boolean {
const view = element.ownerDocument.defaultView;
if (!view) return false;
const style = view.getComputedStyle(element);
return isHostPartReachable({
display: style.display,
visibility: style.visibility,
opacity: style.opacity,
pointerEvents: "auto",
clipPath: style.clipPath,
clip: style.clip,
rect: rectOf(element.getBoundingClientRect()),
viewport: { width: view.innerWidth, height: view.innerHeight },
});
}
/**
* Resolve the first visible `[data-ws-part="<anchor>"]` inside the active
* skin's DOM. Plugin and surface hosts render inside `.ws-skin-root` too
* (runtime plugins may Teleport to a `data-ws-skin-mount` slot, but every
* mount slot is part of the skin root), so a skin cannot point the recovery
* control at host chrome outside its own subtree.
*/
export function resolveSkinAnchor(
anchor: string,
doc: Document = document,
skinId?: string,
): HTMLElement | null {
const selector = `[data-ws-part="${anchor}"]`;
for (const root of doc.querySelectorAll(".ws-skin-root")) {
if (skinId && root.getAttribute("data-ws-skin") !== skinId) continue;
for (const element of root.querySelectorAll<HTMLElement>(selector)) {
if (isVisibleAnchor(element)) return element;
}
}
return null;
}
/** The active skin's root element; the token source while no anchor is usable. */
export function findSkinRoot(skinId: string | undefined, doc: Document = document): Element | null {
for (const root of doc.querySelectorAll(".ws-skin-root")) {
if (!skinId || root.getAttribute("data-ws-skin") === skinId) return root;
}
return null;
}
export interface HostSlotModeChange {
mode: "slotted" | "floating";
reason?: SlotFailureReason;
}
export interface HostSlotBindingOptions {
layer: HostOwnedLayer;
control: HTMLElement;
anchor: string;
/** Restricts anchor resolution and token fallback to this active skin. */
skinId?: string;
align?: SkinChromeSlotAlign;
resolveAnchor?: (anchor: string) => HTMLElement | null;
onModeChange?: (change: HostSlotModeChange) => void;
}
export interface HostSlotBinding {
revalidate(): void;
dispose(): void;
}
const NOOP_BINDING: HostSlotBinding = {
revalidate: () => undefined,
dispose: () => undefined,
};
/**
* Position the controlled host element over its anchor, re-check it on layout
* changes and return it to the default floating position on any failure.
* Tokens are read from the anchor's computed style and applied inside the
* closed shadow root; the skin never writes to the control itself.
*/
export function bindHostSlot(options: HostSlotBindingOptions): HostSlotBinding {
const { layer, control, anchor: anchorName, onModeChange } = options;
const align = options.align ?? "end";
const doc = control.ownerDocument;
const view = doc.defaultView;
if (!view) return NOOP_BINDING;
const win = view;
const resolveAnchor = options.resolveAnchor
?? ((name: string) => resolveSkinAnchor(name, doc, options.skinId));
let state = INITIAL_SLOT_MODE_STATE;
let lastReason: SlotFailureReason | undefined;
let currentAnchor: HTMLElement | null = null;
let observer: ResizeObserver | null = null;
let probe: HTMLElement | null = null;
let frame: number | undefined;
let timer: ReturnType<typeof setTimeout> | undefined;
let disposed = false;
const requestFrame = (callback: FrameRequestCallback): number => win.requestAnimationFrame(callback);
const cancelFrame = (handle: number): void => win.cancelAnimationFrame(handle);
function ensureProbe(): HTMLElement | null {
if (probe) return probe;
probe = doc.createElement("span");
probe.setAttribute("aria-hidden", "true");
probe.style.cssText = "position:absolute;left:-9999px;top:0;width:0;height:0;visibility:hidden;pointer-events:none;";
layer.shadow.append(probe);
return probe;
}
/** Resolve a CSS colour through the browser, inside the closed shadow root. */
function probeColor(raw: string | undefined): string | undefined {
if (!raw) return undefined;
const element = ensureProbe();
if (!element) return undefined;
element.style.color = "rgb(1, 2, 3)";
element.style.color = raw;
const computed = win.getComputedStyle(element).color;
return computed === "rgb(1, 2, 3)" ? undefined : computed;
}
function readTokens(source: Element | null): RecoveryTokens {
if (!source) return resolveRecoveryTokens();
const style = win.getComputedStyle(source);
const read = (property: string): string | undefined => style.getPropertyValue(property).trim() || undefined;
return resolveRecoveryTokens({
accent: probeColor(read(RECOVERY_SKIN_TOKEN_PROPERTIES.accent)),
background: probeColor(read(RECOVERY_SKIN_TOKEN_PROPERTIES.background)),
foreground: probeColor(read(RECOVERY_SKIN_TOKEN_PROPERTIES.foreground)),
radius: read(RECOVERY_SKIN_TOKEN_PROPERTIES.radius),
font: read(RECOVERY_SKIN_TOKEN_PROPERTIES.font),
size: read(RECOVERY_SKIN_TOKEN_PROPERTIES.size),
});
}
function clearPlacement(): void {
control.style.removeProperty("position");
control.style.removeProperty("left");
control.style.removeProperty("top");
control.style.removeProperty("right");
control.style.removeProperty("bottom");
}
function applyPlacement(left: number, top: number): void {
control.style.position = "fixed";
control.style.left = `${left}px`;
control.style.top = `${top}px`;
control.style.right = "auto";
control.style.bottom = "auto";
}
function hitTestAt(left: number, top: number, size: { width: number; height: number }): boolean {
if (typeof doc.elementFromPoint !== "function") return true;
// A hit inside the closed shadow root retargets to the layer host, which is
// exactly what `elementFromPoint` returns for the top-layer capsule.
const hit = doc.elementFromPoint(left + size.width / 2, top + size.height / 2);
return hit === layer.host;
}
function observe(anchorElement: HTMLElement | null): void {
if (currentAnchor === anchorElement) return;
currentAnchor = anchorElement;
if (!observer && typeof ResizeObserver !== "undefined") {
observer = new ResizeObserver(() => schedule());
}
if (!observer) return;
observer.disconnect();
observer.observe(control);
if (anchorElement) observer.observe(anchorElement);
}
function evaluate(): void {
if (disposed) return;
const anchorElement = resolveAnchor(anchorName);
observe(anchorElement);
// The fallback keeps the skin palette: read tokens from the anchor, or
// from the skin root when the anchor is missing or hidden.
const tokenSource = anchorElement
?? (options.skinId ? findSkinRoot(options.skinId, doc) : null);
const tokens = readTokens(tokenSource);
applyRecoveryTokens(control, tokenSource ? tokens : null);
const controlBox = control.getBoundingClientRect();
const viewport: SlotViewport = { width: win.innerWidth, height: win.innerHeight };
let anchorSnapshot: SlotAnchorSnapshot | null = null;
if (anchorElement) {
const style = win.getComputedStyle(anchorElement);
anchorSnapshot = {
rect: rectOf(anchorElement.getBoundingClientRect()),
display: style.display,
visibility: style.visibility,
opacity: style.opacity,
clipPath: style.clipPath,
clip: style.clip,
};
}
const canPlace = Boolean(anchorSnapshot) && controlBox.width > 0 && controlBox.height > 0;
const placement = canPlace && anchorSnapshot
? computeSlotPlacement(anchorSnapshot.rect, controlBox, viewport, align)
: null;
const intendedRect: HostPartRect = placement
? {
width: controlBox.width,
height: controlBox.height,
left: placement.left,
top: placement.top,
right: placement.left + controlBox.width,
bottom: placement.top + controlBox.height,
}
: rectOf(controlBox);
// Move first, then hit-test: the capsule has to actually occupy the
// intended point before `elementFromPoint` can see it. The move and a
// failed revert happen in one task, so nothing paints in between.
if (placement) applyPlacement(placement.left, placement.top);
const evaluation = evaluateSlottedControl({
anchor: anchorSnapshot,
controlRect: intendedRect,
viewport,
minHitTarget: tokens?.hitTarget ?? RECOVERY_SIZE_METRICS.small.hitTarget,
hitTestOk: placement
? hitTestAt(placement.left, placement.top, { width: controlBox.width, height: controlBox.height })
: false,
});
const next = nextSlotModeState(state, evaluation.ok);
const changed = next.slotted !== state.slotted;
state = next;
if (state.slotted && placement) applyPlacement(placement.left, placement.top);
else clearPlacement();
const reason = evaluation.ok ? undefined : evaluation.reason;
const mode = state.slotted ? "slotted" : "floating";
if (changed || (mode === "floating" && reason !== lastReason)) {
lastReason = reason;
onModeChange?.({ mode, ...(reason ? { reason } : {}) });
}
}
function schedule(): void {
if (disposed || frame !== undefined) return;
frame = requestFrame(() => {
frame = undefined;
evaluate();
});
}
function scheduleTick(): void {
if (disposed) return;
timer = setTimeout(() => {
if (disposed) return;
evaluate();
scheduleTick();
}, state.slotted ? HOST_SLOT_SLOTTED_INTERVAL_MS : HOST_SLOT_IDLE_INTERVAL_MS);
}
win.addEventListener("resize", schedule, { passive: true });
win.addEventListener("scroll", schedule, { capture: true, passive: true });
evaluate();
scheduleTick();
return {
revalidate: evaluate,
dispose() {
disposed = true;
observer?.disconnect();
observer = null;
if (frame !== undefined) cancelFrame(frame);
if (timer) clearTimeout(timer);
win.removeEventListener("resize", schedule);
win.removeEventListener("scroll", schedule, { capture: true });
clearPlacement();
applyRecoveryTokens(control, null);
probe?.remove();
probe = null;
},
};
}