Files
fresh/plugins/twind/shared.ts
Luca Casonato 1b3c9f2569 fix(plugin/twind): load configuration as module (#705)
This commit loads the Twind configuration as an actual module rather
than evaluating it on the server and then serializing. This allows usage
of unserializable plugin functions.

Co-authored-by: Elias Sjögreen <eliassjogreen1@gmail.com>
2022-09-07 00:44:46 +02:00

49 lines
1.2 KiB
TypeScript

import { JSX, options as preactOptions, VNode } from "preact";
import { Configuration, setup as twSetup, Sheet, tw } from "twind";
export const STYLE_ELEMENT_ID = "__FRSH_TWIND";
export interface Options extends Omit<Configuration, "mode" | "sheet"> {
/** The import.meta.url of the module defining these options. */
selfURL: string;
}
declare module "preact" {
namespace JSX {
interface DOMAttributes<Target extends EventTarget> {
class?: string;
className?: string;
}
}
}
export function setup(options: Options, sheet: Sheet) {
const config: Configuration = {
...options,
mode: "silent",
sheet,
};
twSetup(config);
const originalHook = preactOptions.vnode;
// deno-lint-ignore no-explicit-any
preactOptions.vnode = (vnode: VNode<JSX.DOMAttributes<any>>) => {
if (typeof vnode.type === "string" && typeof vnode.props === "object") {
const { props } = vnode;
const classes: string[] = [];
if (props.class) {
classes.push(tw(props.class));
props.class = undefined;
}
if (props.className) {
classes.push(tw(props.className));
}
if (classes.length) {
props.class = classes.join(" ");
}
}
originalHook?.(vnode);
};
}