Static GPT Tailwind generator for Next.js.
Under development.
Use GPT-4 to generate Tailwind styles from natural language descriptions.
Generated styles are saved to a JSON cache and injected into the application at build-time: No production fetch requests, no FOUC.
Play with the useSpell() hook in a pre-configured Next.js project on CodeSandbox.
IMPORTANT:You must set the getStaticProps export in order to load
your styles from the JSON cache at build-time.
After following the Setup instructions below, you can generate styles using
the useSpell() hook, like so:
// src/pages/index.tsximport{useSpell}from"@spellcraft/ui/client";import{withStylesCache}from"@spellcraft/ui/server";exportdefaultfunctionHome(){conststyles=useSpell("purple text in small font");return(<main><spanclassName={styles}>
hello world
</span></main>);}exportconstgetStaticProps=withStylesCache();Note: In production, the hook will not cause a re-render: It is only used to fetch new styles and add them to the cache in dev mode.
First, ensure you create a .env file with your OpenAI API key:
# .env
OPENAI_API_KEY="YOUR-KEY-HERE"You'll need to configure the following pages to use UI Spells:
pages/_document.tsxpages/_app.tsxpages/api/spellcraft.ts
And for every page you use UI Spells, you'll need to ensure getStaticProps
exists and is wrapped:
exportconstgetStaticProps=withStylesCache();We'll go over how to set this up below.
This adds support for SSR-compatible dynamic Tailwind with @twind/next.
// pages/_document.tsximport{withDocument}from"@spellcraft/ui/document";exportdefaultwithDocument();This wraps your app with a cache provider and utilities from @twind/next.
// pages/app.tsximport{StrictMode}from"react";import{typeAppProps}from"next/app";import{withApp}from"@spellcraft/ui/client";exportconstMyApp=({ Component, pageProps }: AppProps)=>{return(<StrictMode><Component{...pageProps}/></StrictMode>);};exportdefaultwithApp(MyApp);This endpoint is used only in development mode to update your styles cache.
When you publish to production, this endpoint will not run, and the styles will be loaded from the static JSON file at build-time only.
// pages/api/spellcraft.tsimport{StylesAPI}from"@spellcraft/ui/server";exportdefaultStylesAPI;That's it!
With pages/_document.tsx, pages/_app.tsx, and pages/api/spellcraft.ts
configured, you're ready to use the useSpell() hook.