feat: add custom render function support (#30)

This commit is contained in:
Luca Casonato
2021-06-16 19:36:12 +02:00
committed by GitHub
parent 1346105a79
commit 940f34d811
11 changed files with 236 additions and 39 deletions
+11
View File
@@ -0,0 +1,11 @@
import { h, tw } from "../deps.ts";
export function RoundedButton(props: h.JSX.HTMLAttributes<HTMLButtonElement>) {
return (
<button
{...props}
class={tw
`p-3 border border-transparent rounded-full text-white bg-indigo(600 hover:700) focus:(outline-none ring(2 offset-2 indigo-500)) disabled:(bg-indigo-200 cursor-default)`}
/>
);
}
+41
View File
@@ -0,0 +1,41 @@
import { h, tw } from "../deps.ts";
export function IconMinus() {
return (
<svg
class={tw`h-6 w-6`}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
aria-hidden="true"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M20 12H4"
/>
</svg>
);
}
export function IconPlus() {
return (
<svg
class={tw`h-6 w-6`}
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
stroke="currentColor"
aria-hidden="true"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M12 6v6m0 0v6m0-6h6m-6 0H6"
/>
</svg>
);
}
+6
View File
@@ -1 +1,7 @@
export * from "../runtime.ts";
import { IS_BROWSER } from "../runtime.ts";
import { setup, tw } from "https://esm.sh/twind";
export { setup, tw };
if (IS_BROWSER) {
setup({});
}
+32
View File
@@ -0,0 +1,32 @@
// This module adds twind support.
import {
getStyleTagProperties,
virtualSheet,
} from "https://esm.sh/twind/sheets";
import { h, setup } from "../deps.ts";
import { RenderContext, RenderFn } from "../server_deps.ts";
const sheet = virtualSheet();
const initial = sheet.reset();
setup({ sheet });
export function render(ctx: RenderContext, render: RenderFn) {
const snapshot = ctx.state.get("twindSnapshot") as unknown[] | null;
sheet.reset(snapshot || initial);
render();
const newSnapshot = sheet.reset(initial);
ctx.state.set("twindSnapshot", newSnapshot);
}
export function postRender(ctx: RenderContext) {
const snapshot = ctx.state.get("twindSnapshot") as unknown[] | null;
if (snapshot !== null) {
sheet.reset(snapshot);
const { id, textContent } = getStyleTagProperties(sheet);
ctx.head.push(
h("style", { id, dangerouslySetInnerHTML: { __html: textContent } }),
);
}
sheet.reset(initial);
}
+26 -18
View File
@@ -1,33 +1,41 @@
import { h, IS_BROWSER, useState } from "../deps.ts";
import { RoundedButton } from "../components/Button.tsx";
import { IconMinus, IconPlus } from "../components/Icons.tsx";
import { h, IS_BROWSER, tw, useState } from "../deps.ts";
export default function Home() {
return (
<div>
<p>
<div class={tw`max-w-screen-md px-4 mx-auto my-8`}>
<p class={tw`font-bold`}>
Welcome to `fresh`. Try update this message in the ./pages/index.tsx
file, and refresh.
</p>
<Counter />
<p>{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}</p>
<p class={tw`mt-4`}>
{IS_BROWSER ? "Viewing browser render." : "Viewing JIT render."}
</p>
</div>
);
}
function Counter() {
const [count, setCount] = useState(0);
return <div>
<p>{count}</p>
<button
onClick={() => setCount(count - 1)}
disabled={!IS_BROWSER}
>
-1
</button>
<button
onClick={() => setCount(count + 1)}
disabled={!IS_BROWSER}
>
+1
</button>
return <div class={tw`mt-4 border rounded p-4 text-center`}>
<p class={tw`text-lg font-medium`}>Current count: {count}</p>
<div class={tw`flex gap-2 mt-4 justify-center`}>
<RoundedButton
type="button"
onClick={() => setCount(count - 1)}
disabled={!IS_BROWSER}
>
<IconMinus />
</RoundedButton>
<RoundedButton
type="button"
onClick={() => setCount(count + 1)}
disabled={!IS_BROWSER}
>
<IconPlus />
</RoundedButton>
</div>
</div>;
}
+8 -6
View File
@@ -3,16 +3,18 @@
// To update this file, run `fresh routes`.
import * as $0 from "./pages/api/joke.ts";
import * as $1 from "./pages/x/[module].tsx";
import * as $2 from "./pages/[name].tsx";
import * as $3 from "./pages/index.tsx";
import * as $1 from "./pages/_render.ts";
import * as $2 from "./pages/x/[module].tsx";
import * as $3 from "./pages/[name].tsx";
import * as $4 from "./pages/index.tsx";
const routes = {
pages: {
"./pages/api/joke.ts": $0,
"./pages/x/[module].tsx": $1,
"./pages/[name].tsx": $2,
"./pages/index.tsx": $3,
"./pages/_render.ts": $1,
"./pages/x/[module].tsx": $2,
"./pages/[name].tsx": $3,
"./pages/index.tsx": $4,
},
baseUrl: import.meta.url,
};
+1
View File
@@ -0,0 +1 @@
export * from "../server.ts";