feat: twind plugin (#513)
This adds a first-party twind plugin to use twind for styling.
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import { virtualSheet } from "twind/sheets";
|
||||
import { Plugin } from "../server.ts";
|
||||
|
||||
import { Options, setup, STYLE_ELEMENT_ID } from "./twind/shared.ts";
|
||||
export type { Options };
|
||||
|
||||
export default function twind(options: Options): Plugin {
|
||||
const sheet = virtualSheet();
|
||||
setup(options, sheet);
|
||||
return {
|
||||
name: "twind",
|
||||
entrypoints: { "main": new URL("./twind/main.ts", import.meta.url).href },
|
||||
render(ctx) {
|
||||
sheet.reset(undefined);
|
||||
const res = ctx.render();
|
||||
const cssText = [...sheet.target].join("\n");
|
||||
const snapshot = sheet.reset();
|
||||
const scripts = [];
|
||||
if (res.requiresHydration) {
|
||||
const precedences = snapshot[1];
|
||||
const mappings: (string | [string, string])[] = [];
|
||||
for (
|
||||
const [key, value] of (snapshot[3] as Map<string, string>).entries()
|
||||
) {
|
||||
if (key === value) {
|
||||
mappings.push(key);
|
||||
} else {
|
||||
mappings.push([key, value]);
|
||||
}
|
||||
}
|
||||
const state = [options, precedences, mappings];
|
||||
scripts.push({ entrypoint: "main", state });
|
||||
}
|
||||
return {
|
||||
scripts,
|
||||
styles: [{ cssText, id: STYLE_ELEMENT_ID }],
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { Sheet } from "twind";
|
||||
import { Options, setup, STYLE_ELEMENT_ID } from "./shared.ts";
|
||||
|
||||
type State = [Options, string[], [string, string][]];
|
||||
|
||||
export default function hydrate(state: State) {
|
||||
const el = document.getElementById(STYLE_ELEMENT_ID) as HTMLStyleElement;
|
||||
const rules = new Set(el.innerText.split("\n"));
|
||||
const precedences = state[1];
|
||||
const mappings = new Map(state[2]
|
||||
.map((v) => typeof v === "string" ? [v, v] : v));
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const sheetState: any[] = [precedences, rules, mappings, true];
|
||||
const target = el.sheet!;
|
||||
const sheet: Sheet = {
|
||||
target,
|
||||
insert: (rule, index) => target.insertRule(rule, index),
|
||||
init: (cb) => cb(sheetState.shift()),
|
||||
};
|
||||
setup(state[0], sheet);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { JSX, options as preactOptions, VNode } from "preact";
|
||||
import {
|
||||
Configuration,
|
||||
DarkMode,
|
||||
setup as twSetup,
|
||||
Sheet,
|
||||
ThemeConfiguration,
|
||||
tw,
|
||||
} from "twind";
|
||||
|
||||
export const STYLE_ELEMENT_ID = "__FRSH_TWIND";
|
||||
|
||||
export interface Options {
|
||||
/**
|
||||
* Determines the dark mode strategy (default: `"media"`).
|
||||
*/
|
||||
darkMode?: DarkMode;
|
||||
|
||||
/**
|
||||
* The twind theme to use.
|
||||
*/
|
||||
theme?: ThemeConfiguration;
|
||||
|
||||
/**
|
||||
* ```js
|
||||
* {
|
||||
* ':new-variant': '& .selector',
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
variants?: Record<string, string>;
|
||||
|
||||
/**
|
||||
* Configure wether the class names should be emitted "as is", or if they
|
||||
* should be hashed.
|
||||
*/
|
||||
hash?: boolean;
|
||||
}
|
||||
|
||||
declare module "preact" {
|
||||
namespace JSX {
|
||||
interface DOMAttributes<Target extends EventTarget> {
|
||||
class?: string;
|
||||
className?: string;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function setup(options: Options, sheet: Sheet) {
|
||||
const config: Configuration = {
|
||||
darkMode: options.darkMode,
|
||||
hash: options.hash,
|
||||
mode: "silent",
|
||||
sheet,
|
||||
variants: options.variants,
|
||||
theme: options.theme,
|
||||
};
|
||||
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);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user