Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use nanojsx #42

Open
satyarohith opened this issue Oct 17, 2021 · 1 comment
Open

Use nanojsx #42

satyarohith opened this issue Oct 17, 2021 · 1 comment

Comments

@satyarohith
Copy link
Owner

It seems to be helpful to build single-page apps quickly. I like how simple this example is.

import { h, ssr, tw } from "https://crux.land/[email protected]";

const Hello = (props) => (
  <div class={tw`bg-white flex h-screen`}>
    <h1 class={tw`text-5xl text-gray-600 m-auto mt-20`}>
      Hello {props.name}!
    </h1>
  </div>
);

addEventListener("fetch", (event) => {
  const url = new URL(event.request.url);
  const name = url.searchParams.get("name") ?? "world";
  event.respondWith(ssr(() => <Hello name={name} />));
});
@hastebrot
Copy link

hastebrot commented Mar 30, 2022

[email protected] was not updated for a while. Latest dependencies are now

Based on the example and the ssr dependency I've written some code a few weeks ago. Maybe it is somehow useful.

web.tsx:

// deno run --watch --no-clear-screen --allow-net=0.0.0.0:8000 web.tsx

/// <reference no-default-lib="true"/>
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

/** @jsx h */
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { h, ssr } from "./webssr.ts";
import { sheet, stringify, tw } from "./webtw.ts";

const Hello = (props: { name: string }) => (
  <div class={tw`bg-white flex h-screen`}>
    <h1 class={tw`text-indigo-500 p-5 text-xl font-semibold`}>
      Hello {props.name}!
    </h1>
  </div>
);

if (import.meta.main) {
  console.log("Listening on http://localhost:8000");
  await serve((req) => {
    const url = new URL(req.url);
    const name = url.searchParams.get("name") ?? "world";
    return new Response(
      ssr(
        () => h(<Hello name={name} />),
        () => stringify(sheet.target),
      ),
      { headers: { "content-type": "text/html" } },
    );
  });
}

webssr.ts:

import {
  h,
  Helmet,
  renderSSR,
} from "https://deno.land/x/[email protected]/mod.ts";

export function ssr(render: CallableFunction, renderStyles: CallableFunction) {
  const app = renderSSR(render());
  const styles = renderStyles();
  const { body, head, footer } = Helmet.SSR(app);

  return `
    <html>
      <head>
        ${head}
        <style>${styles}</style>
      </head>
      <body>
        ${body}
        ${footer}
      </body>
    </html>
  `;
}

export { h };

webtw.ts:

import {
  stringify,
  twind,
  virtual,
} from "https://cdn.skypack.dev/[email protected]";
import presetTailwind from "https://cdn.skypack.dev/@twind/[email protected]";

export const sheet = virtual();

export const tw = twind({
  presets: [
    presetTailwind({ disablePreflight: false }),
  ],
}, sheet);

export { stringify };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants