Avoid global window access in Soma helpers

active-uix
dev 5 months ago
parent 9a0ef78b62
commit 0021826413

@ -78,7 +78,11 @@ export class AnnounceProvider {
} }
announce = (message: string, priority: AnnouncePriority = 'polite', timeout?: number): void => { announce = (message: string, priority: AnnouncePriority = 'polite', timeout?: number): void => {
if (typeof window === 'undefined') return; try {
this.soma.dom.getDocument(this.opts.ref?.current);
} catch {
return;
}
const clearAfter = timeout ?? this.opts.defaultTimeout.current; const clearAfter = timeout ?? this.opts.defaultTimeout.current;
if (priority === 'assertive') { if (priority === 'assertive') {

@ -1262,10 +1262,18 @@ export class ColorPickerEyeDropperProvider {
}); });
} }
readonly isSupported: boolean = private get eyeDropperCtor(): (new () => EyeDropperLike) | undefined {
typeof window !== 'undefined' && let win: Window;
'EyeDropper' in window && try {
typeof (window as unknown as { EyeDropper: unknown }).EyeDropper === 'function'; win = this.provider.soma.dom.getWindow(this.opts.ref.current);
} catch {
return undefined;
}
const candidate = (win as unknown as { EyeDropper?: unknown }).EyeDropper;
return typeof candidate === 'function' ? (candidate as new () => EyeDropperLike) : undefined;
}
readonly isSupported = $derived.by(() => this.eyeDropperCtor !== undefined);
readonly resolvedLabel = $derived.by( readonly resolvedLabel = $derived.by(
() => this.opts.ariaLabel.current ?? this.provider.langs(COLOR_PICKER_LANGS.EYE_DROPPER) () => this.opts.ariaLabel.current ?? this.provider.langs(COLOR_PICKER_LANGS.EYE_DROPPER)
@ -1274,7 +1282,8 @@ export class ColorPickerEyeDropperProvider {
readonly onclick = async () => { readonly onclick = async () => {
if (!this.isSupported || this.provider.isDisabled || this.provider.isReadonly) return; if (!this.isSupported || this.provider.isDisabled || this.provider.isReadonly) return;
try { try {
const Ctor = (window as unknown as { EyeDropper: new () => EyeDropperLike }).EyeDropper; const Ctor = this.eyeDropperCtor;
if (!Ctor) return;
const picker = new Ctor(); const picker = new Ctor();
const { sRGBHex } = await picker.open(); const { sRGBHex } = await picker.open();
const hsv = parseColor(sRGBHex); const hsv = parseColor(sRGBHex);

@ -1,8 +1,8 @@
// ─── DPR ───────────────────────────────────────────────────────────────────── // ─── DPR ─────────────────────────────────────────────────────────────────────
export function getDPR(element: Element): number { export function getDPR(element: Element): number {
if (typeof window === 'undefined') return 1; const win = element.ownerDocument.defaultView;
const win = element.ownerDocument.defaultView ?? window; if (!win) return 1;
return win.devicePixelRatio || 1; return win.devicePixelRatio || 1;
} }

Loading…
Cancel
Save

Powered by TurnKey Linux.