ps2.live-old/app/styletron.ts
2023-02-01 19:17:41 -05:00

33 lines
930 B
TypeScript

import { Client, Server } from "styletron-engine-atomic"; // or "styletron-engine-monolithic"
/**
* The Styletron engine to use on the current runtime.
*/
export const styletron =
typeof document === "undefined"
? new Server()
: new Client({
hydrate: getHydrateClass(),
});
export function isStyletronClient(engine: typeof styletron): engine is Client {
return styletron instanceof Client;
}
export function isStyletronServer(engine: typeof styletron): engine is Server {
return styletron instanceof Server;
}
export function collectStyles(): string {
if (!isStyletronServer(styletron)) {
throw new Error("Can only collect styles during server-side rendering.");
}
return styletron.getStylesheetsHtml();
}
function getHydrateClass(): HTMLCollectionOf<HTMLStyleElement> {
return document.getElementsByClassName(
"_styletron_hydrate_"
) as HTMLCollectionOf<HTMLStyleElement>;
}