npm i @gluestack-ui/themed @gluestack-style/react @gluestack/ui-next-adapter react-native-web react-native-svg@13.4.0
Step 1.5: Default Theme (Optional)
gluestack-ui is intentionally designed as an unstyled library, providing you with the flexibility to style your components as you prefer. However, if you want to use the default theme, you can install @gluestack-ui/config as well.
yarnadd @gluestack-ui/config@latest
OR
npm i @gluestack-ui/config@latest
Step 2: Server-side rendering (SSR)
It's also recommended to set up your server-side rendering (SSR) correctly. To do this, you will need to use the flush() function exported by the @gluestack-ui/themed.
Next.js App Routers (which supports React Server Components)
For Next.js App Routers we will create a new registry.tsx file in the root of your project and use the flush function from @gluestack-ui/themed:
We also need to add className="gs" to the <html> element in the layout.tsx file.
We also need to wrap children with StyledJsxRegistry in the layout.tsx file.
If dark mode needs to be rendered from the server side, you also need to add className="gs gs-dark" to the html element in the layout.tsx file. (optional)
The code in layout.tsx file at this point of time will look like this:
import"./globals.css"
import{Inter}from"next/font/google"
importStyledJsxRegistryfrom"./registry"
const inter =Inter({subsets:["latin"]})
exportconst metadata ={
title:"Create Next App",
description:"Generated by create next app",
}
exportdefaultfunctionRootLayout({
children,
}:{
children:React.ReactNode
}){
return(
<htmllang="en"className="gs">
<bodyclassName={inter.className}>
<StyledJsxRegistry>{children}</StyledJsxRegistry>
</body>
</html>
)
}
Next.js Page Routers
To prevent flickering, the gs class should be attached, which increases the specificity of any inline styles that are being used.
If dark mode needs to be rendered from the server side, you also need to add className="gs gs-dark" to the Html element in the _document.tsx file. (optional)
Step 3: Setup @gluestack/ui-next-adapter
To use gluestack-ui components with server-side rendering, you need to configure your project to transpile the modules correctly. The easiest way to do this is by using the withGluestackUI Next.js adapter. This adapter adds the necessary configuration to your project to ensure that your gluestack-ui components are transpiled correctly for server-side rendering.
This component returns a GluestackUIProvider component which wraps the children with the theme from @gluestack-ui/config file.
After creating providers.tsx file, we need to wrap the exported GluestackUIProvider component around the children in layout.tsx file. The code in layout.tsx file at this point of time will look like this:
import"./globals.css"
import{Inter}from"next/font/google"
import{Providers}from"./providers"
importStyledJsxRegistryfrom"./registry"
const inter =Inter({subsets:["latin"]})
exportconst metadata ={
title:"Create Next App",
description:"Generated by create next app",
}
exportdefaultfunctionRootLayout({
children,
}:{
children:React.ReactNode
}){
return(
<htmllang="en"className="gs gs-light">
<bodyclassName={inter.className}>
<Providers>
<StyledJsxRegistry>{children}</StyledJsxRegistry>
</Providers>
</body>
</html>
)
}
Next.js Page Routers
For Next.js Page Router just add GluestackUIProvider to the root of your app and update _app.tsx as follows:
This ensures that all components are wrapped with <GluestackUIProvider />, and the theme from @gluestack-ui/config is applied.
This guide should help you get started with gluestack-ui in your expo project. If you run into any issues or have further questions, please refer to the official documentation or community forums.