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

How to setup twind-next with new "app" folder from next.js 13? #32

Open
juniorbotelho opened this issue Dec 3, 2022 · 7 comments
Open

Comments

@juniorbotelho
Copy link

How to setup twind-next with new "app" folder from next.js 13?

@sastan
Copy link
Contributor

sastan commented Dec 8, 2022

Still working on an updated example.

@JacobWeisenburger
Copy link

any progress on this?

@vicary
Copy link

vicary commented Apr 7, 2023

Is there any news? We rely heavily on the shim approach, and would love to see this happening.

@spigelli
Copy link

Any updates on this?

@lveillard
Copy link

lveillard commented Jul 19, 2023

Hello @sastan!

Could you maybe share some overall ideas so we can try to implement it ourselves and potentially open a PR? I tried different approaches with no success. If you have something in mind or protocode it would help a lot 🚀

@lveillard
Copy link

For those willing just to try it, a hideous way to do so which only works in client components is this one:

///useTwind.ts

import {useState, useEffect} from 'react';
import { defineConfig } from '@twind/core';
import presetAutoprefix from '@twind/preset-autoprefix';
import presetTailwind from '@twind/preset-tailwind';
import {setup} from '@twind/core';

export const config = defineConfig({
  presets: [presetAutoprefix(), presetTailwind()],
});


export const useTwind = () => {
  const [isLoaded, setIsLoaded] = useState(null);

  useEffect(() => {
    const asyncFn = async () => {
      setup(config);
      setIsLoaded(true);
    };
    asyncFn();
  }, []);

  return isLoaded;
};

And then in your client components:

'use client';
import { useState, useEffect, lazy } from 'react';
import { useTwind } from '../../hooks/useTwind';


export const Counter = () => {
  const [value, setValue] = useState(0);
  const isLoaded = useTwind();


  if (!isLoaded) {
    return <div>Loading...</div>;
  }

  return (
    <div className='m-10'>
          <h1 className={`mt-2`}>Count: {value}</h1>
          <button onClick={() => setValue((prev) => prev + 1)}>
            Increment
          </button>
    </div>
  );
};

Btw I saw a considerable performance drop with v1 (Yes, I'm running the setup onMount so obviously it has a bigger impact in this ugly code, but I wonder what made the setup slower).

@lveillard
Copy link

lveillard commented Jul 20, 2023

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

6 participants