Pretty Basic (I think) React / Tailwind / Next question
Hi everyone, I have tailwind UI and I'm trying to implement a theming system similar to : https://www.skies.dev/tailwind-themes
- I'm sure something I'm missing is basic react functionality but I've not been able to find the right answer and I'm sorry to just brain dump here but I've kind of reached an impasse (and don't have a lot of friends familiar enough with React to help...)
- His basic tutorial works great, however I'm trying to expand on that.
- The Basics, I'm using React, Next.JS (App Directory), Tailwind UI
- - I've defined my theme colors in global.css and their associated variables
- - In layout.tsx I declare the themes (as colors) and define color as setColor
- - page.tsx calls the component
<Sidebar />
which is not really just a sidebar but also a navbar (from TailwindUI)
- - Inside Sidebar I have the following menu drop down to select the themes
- - I tried this with both a links and the RadioGroup (since it was what skies used in his demo) but part of the problem is that color and setColor are not defined, I can redeclare them but I feel like that doesn't work since it's not mutating the original property
- - My GOAL here isn't JUST to get this working but to also understand why it's either NOT working or what I'm overlooking, I've been googling this for about a week and been given a couple of suggestions of entirely different ways to handle this but I feel like I'm missing a basic thing here... maybe not... and though I want to get this done, it's not an urgent item but I also want to learnHow to Create Themes with Tailwind CSS and React
Use CSS variables to leverage client-side theming with Tailwind CSS and React.js.
5 Replies
@drewhack Best thing to do to get others to help you would be to post your repo. It's hard reading chunks of code without knowing the full context (i.e. what does your layout.tsx/page.tsx/ and sidebar component look like).
For example, if your Sidebar button is calling
setColor
, then setColor
needs to be defined as a prop in the component.
So const Sidebar = (prop) => {}
and your RadioGroup onChange handler would call prop.setColor
.
You also have setColor on the Layout, instead of the page.tsx. But there's no easy way to call setColor on the Layout without doing more complicated stuff. So for simplicity, I would do the setColor stuff on page.tsx, get that working, and then maybe look at the Layout stuff later. The tutorial doesn't actually mention using layout / app router, and instead stores the preference on local storage. Maybe try and get that workingGitHub
GitHub - drewhack/testnextapp
Contribute to drewhack/testnextapp development by creating an account on GitHub.
Yeah I was able to get what he was doing working in the base example but once I started trying to adapt it is where things went awry
I’m still thinking with a PHP brain
Okay. So.... if you want "child" components to influence something that is overarching, the best thing to do is to use React Context. Might need to watch a tutorial or two. But basically your layout.tsx would have something like:
<ThemeProvider>
{children}
<ThemeProvider/>
Usually most people would create a contexts
folder, with a file called ThemeProvider.tsx
and in there you would use React.createContext
, create a component ThemeContext
and export const useTheme = React.useContext(ThemeContext)
. (I can't really dictate out exactly what that would look like right at this second, but there's tons of tutorials). And then in your component, you would call const { currentColor, setColor } = useTheme()
or something that you've created.Awesome thank you! Honestly I was so stuck on this I think. That’s a great jumping off point to be honest