diff --git a/plugins/twindv1/main.ts b/plugins/twindv1/main.ts index 9b6a1902..1d7f0e99 100644 --- a/plugins/twindv1/main.ts +++ b/plugins/twindv1/main.ts @@ -1,5 +1,17 @@ -import { getSheet, setup, TwindConfig } from "https://esm.sh/@twind/core@1.1.3"; +import { + cssom, + getSheet, + setup, + TwindConfig, +} from "https://esm.sh/@twind/core@1.1.3"; +import { STYLE_ELEMENT_ID } from "./shared.ts"; export default function hydrate(options: TwindConfig) { - setup(options, getSheet()); + const elem = document.getElementById(STYLE_ELEMENT_ID) as HTMLStyleElement; + const sheet = cssom(elem); + + sheet.resume = getSheet().resume.bind(sheet); + document.querySelector('[data-twind="claimed"]')?.remove(); + + setup(options, sheet); } diff --git a/tests/fixture_twind_hydrate/deno.json b/tests/fixture_twind_hydrate/deno.json new file mode 100644 index 00000000..8d9ac336 --- /dev/null +++ b/tests/fixture_twind_hydrate/deno.json @@ -0,0 +1,10 @@ +{ + "tasks": { + "start": "deno run -A --watch=static/,routes/ dev.ts" + }, + "importMap": "./import_map.json", + "compilerOptions": { + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +} diff --git a/tests/fixture_twind_hydrate/dev.ts b/tests/fixture_twind_hydrate/dev.ts new file mode 100755 index 00000000..2d85d6c1 --- /dev/null +++ b/tests/fixture_twind_hydrate/dev.ts @@ -0,0 +1,5 @@ +#!/usr/bin/env -S deno run -A --watch=static/,routes/ + +import dev from "$fresh/dev.ts"; + +await dev(import.meta.url, "./main.ts"); diff --git a/tests/fixture_twind_hydrate/fresh.gen.ts b/tests/fixture_twind_hydrate/fresh.gen.ts new file mode 100644 index 00000000..521f4ee5 --- /dev/null +++ b/tests/fixture_twind_hydrate/fresh.gen.ts @@ -0,0 +1,26 @@ +// DO NOT EDIT. This file is generated by fresh. +// This file SHOULD be checked into source version control. +// This file is automatically updated during development when running `dev.ts`. + +import config from "./deno.json" assert { type: "json" }; +import * as $0 from "./routes/check-duplication.tsx"; +import * as $1 from "./routes/insert-cssrules.tsx"; +import * as $2 from "./routes/static.tsx"; +import * as $$0 from "./islands/CheckDuplication.tsx"; +import * as $$1 from "./islands/InsertCssrules.tsx"; + +const manifest = { + routes: { + "./routes/check-duplication.tsx": $0, + "./routes/insert-cssrules.tsx": $1, + "./routes/static.tsx": $2, + }, + islands: { + "./islands/CheckDuplication.tsx": $$0, + "./islands/InsertCssrules.tsx": $$1, + }, + baseUrl: import.meta.url, + config, +}; + +export default manifest; diff --git a/tests/fixture_twind_hydrate/import_map.json b/tests/fixture_twind_hydrate/import_map.json new file mode 100644 index 00000000..2914af6c --- /dev/null +++ b/tests/fixture_twind_hydrate/import_map.json @@ -0,0 +1,10 @@ +{ + "imports": { + "$fresh/": "../../", + "preact": "https://esm.sh/preact@10.11.0", + "preact/": "https://esm.sh/preact@10.11.0/", + "preact-render-to-string": "https://esm.sh/*preact-render-to-string@5.2.4", + "@preact/signals": "https://esm.sh/*@preact/signals@1.0.3", + "@preact/signals-core": "https://esm.sh/*@preact/signals-core@1.0.1" + } +} diff --git a/tests/fixture_twind_hydrate/islands/CheckDuplication.tsx b/tests/fixture_twind_hydrate/islands/CheckDuplication.tsx new file mode 100644 index 00000000..1c8d5245 --- /dev/null +++ b/tests/fixture_twind_hydrate/islands/CheckDuplication.tsx @@ -0,0 +1,63 @@ +// https://github.com/denoland/fresh/pull/1050 +import { useEffect, useState } from "preact/hooks"; +import { cmpCssRules } from "../utils/utils.ts"; + +/** + * Returns a cssrulelist of styleElement matching the selector. + */ +function getCssrules(selector: string) { + const elem = document.querySelector(selector) as HTMLStyleElement; + return elem?.sheet?.cssRules; +} + +export default function CheckDuplication() { + const [cssRulesFRSHTWIND, setCssRulesFRSHTWIND] = useState< + undefined | CSSRuleList + >(undefined); + const [cssRulesClaimed, setCssRulesClaimed] = useState< + undefined | CSSRuleList + >(undefined); + + // Init + useEffect(() => { + // get