fix(desktop): improve legacy WebView style compatibility
This commit is contained in:
30 files changed
+1802
-459
No files matched your search
@@ -0,0 +1,69 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { applyLegacyWebViewClass, isLegacyWebView, LEGACY_WEBVIEW_CLASS, missingLegacyWebViewCapabilities } from "@/lib/ui/legacyWebView";
|
||||
|
||||
const supportedFeatures = new Set(["oklch", "color-mix-oklab", "color-mix-oklch", "has-selector", "dynamic-viewport", "min-function"]);
|
||||
|
||||
function mockCssSupports(features: Set<string>) {
|
||||
vi.stubGlobal("CSS", {
|
||||
supports: (propertyOrCondition: string, value?: string) => {
|
||||
const key =
|
||||
value === undefined
|
||||
? propertyOrCondition === "selector(:has(*))"
|
||||
? "has-selector"
|
||||
: propertyOrCondition
|
||||
: propertyOrCondition === "color"
|
||||
? "oklch"
|
||||
: propertyOrCondition === "background-color"
|
||||
? value?.includes("oklab")
|
||||
? "color-mix-oklab"
|
||||
: "color-mix-oklch"
|
||||
: propertyOrCondition === "height"
|
||||
? "dynamic-viewport"
|
||||
: propertyOrCondition === "width"
|
||||
? "min-function"
|
||||
: "";
|
||||
return features.has(key);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("legacy WebView detection", () => {
|
||||
it("accepts a WebView with the CSS capabilities used by Tailwind v4 fallbacks", () => {
|
||||
mockCssSupports(supportedFeatures);
|
||||
|
||||
expect(isLegacyWebView()).toBe(false);
|
||||
expect(missingLegacyWebViewCapabilities()).toEqual([]);
|
||||
});
|
||||
|
||||
it("marks a partial WebView as legacy when any required capability is missing", () => {
|
||||
mockCssSupports(new Set(["oklch"]));
|
||||
|
||||
expect(isLegacyWebView()).toBe(true);
|
||||
expect(missingLegacyWebViewCapabilities()).toEqual(["color-mix-oklab", "color-mix-oklch", "has-selector", "dynamic-viewport", "min-function"]);
|
||||
});
|
||||
|
||||
it("does not treat one color-mix interpolation space as the other", () => {
|
||||
mockCssSupports(new Set(["oklch", "color-mix-oklab", "has-selector", "dynamic-viewport", "min-function"]));
|
||||
|
||||
expect(isLegacyWebView()).toBe(true);
|
||||
expect(missingLegacyWebViewCapabilities()).toEqual(["color-mix-oklch"]);
|
||||
});
|
||||
|
||||
it("adds and removes the root class idempotently", () => {
|
||||
const root = document.createElement("html");
|
||||
mockCssSupports(new Set(["oklch"]));
|
||||
|
||||
expect(applyLegacyWebViewClass(root)).toBe(true);
|
||||
expect(root.classList.contains(LEGACY_WEBVIEW_CLASS)).toBe(true);
|
||||
|
||||
mockCssSupports(supportedFeatures);
|
||||
expect(applyLegacyWebViewClass(root)).toBe(false);
|
||||
expect(root.classList.contains(LEGACY_WEBVIEW_CLASS)).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
export const LEGACY_WEBVIEW_CLASS = "dbx-legacy-webview";
|
||||
|
||||
type CssSupportCheck = {
|
||||
name: string;
|
||||
property?: string;
|
||||
value?: string;
|
||||
condition?: string;
|
||||
};
|
||||
|
||||
const MODERN_CSS_FEATURES: CssSupportCheck[] = [
|
||||
{ name: "oklch", property: "color", value: "oklch(0.5 0.1 180)" },
|
||||
{ name: "color-mix-oklab", property: "background-color", value: "color-mix(in oklab, black 10%, transparent)" },
|
||||
{ name: "color-mix-oklch", property: "background-color", value: "color-mix(in oklch, black 10%, transparent)" },
|
||||
{ name: "has-selector", condition: "selector(:has(*))" },
|
||||
{ name: "dynamic-viewport", property: "height", value: "100dvh" },
|
||||
{ name: "min-function", property: "width", value: "min(100%, 1px)" },
|
||||
];
|
||||
|
||||
function supports(check: CssSupportCheck): boolean {
|
||||
if (typeof CSS === "undefined" || typeof CSS.supports !== "function") return false;
|
||||
|
||||
try {
|
||||
return check.condition ? CSS.supports(check.condition) : CSS.supports(check.property!, check.value!);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
export function missingLegacyWebViewCapabilities(): string[] {
|
||||
return MODERN_CSS_FEATURES.filter((feature) => !supports(feature)).map((feature) => feature.name);
|
||||
}
|
||||
|
||||
export function isLegacyWebView(): boolean {
|
||||
return missingLegacyWebViewCapabilities().length > 0;
|
||||
}
|
||||
|
||||
export function applyLegacyWebViewClass(root: Element = document.documentElement): boolean {
|
||||
const legacy = isLegacyWebView();
|
||||
root.classList.toggle(LEGACY_WEBVIEW_CLASS, legacy);
|
||||
return legacy;
|
||||
}
|
||||
Reference in new issue
Block a user