Install in Next.js project
Installation
Install gluestack-style and it's peer dependencies using npm or yarn.
npm i @gluestack-style/react react-native-web react-native-svg
yarn add @gluestack-style/react react-native-web react-native-svg
Setup Config
To configure gluestack-style, you will need to create a gluestack-style.config.ts file (.js works too) in the root of your project. In this config file, you can define your theme tokens, breakpoints, and any other settings that you want to use throughout your application.
For example, in gluestack-style.config.ts:
import { createConfig } from '@gluestack-style/react';
export const config = createConfig({
bgColor: 'backgroundColor',
sm: '@media (min-width: 480px)',
md: '@media (min-width: 768px)',
shadowColor: '$primary500',
type ConfigType = typeof config;
declare module '@gluestack-style/react' {
interface ICustomConfig extends ConfigType {}
Setup StyledProvider
You need to setup StyledProvider with config at the root of your application. Once your config file is set up, you can import it and pass it as a prop to the StyledProvider component. This will make the configuration options available to all components using the styled function.
Page router setup
If you are using page router, you can wrap your page component with StyledProvider in pages/_app.tsx or pages/_app.jsx file.
import { config } from "@/gluestack-style.config"
import "@/styles/globals.css"
import { StyledProvider } from "@gluestack-style/react"
import type { AppProps } from "next/app"
export default function App({ Component, pageProps }: AppProps) {
<StyledProvider config={config}>
<Component {...pageProps} />
App directory setup
Next.js 13 introduces a new app/ directory / folder structure. By default it uses Server Components. However, gluestack-style only works in client-side components.
To use gluestack-style in server components, you need to convert them into client-side component by adding a 'use client'; at the top of your file.
To use gluestack-style in app directory, you can wrap StyledProvider in your own client Component. For example, in app/provider.tsx:
import { StyledProvider } from "@gluestack-style/react"
import React from "react"
import { config } from "../../config"
const Provider = ({ children }: { children: React.ReactNode }) => {
return <StyledProvider config={config}>{children}</StyledProvider>
You can wrap your root component with StyledProvider in app/layout.tsx.
import Provider from "./provider"
export default function RootLayout({
children: React.ReactNode
<Provider>{children}</Provider>
Note: You need to pass config to StyledProvider otherwise aliases and design tokens will not work.
Setup next.config.js
To use gluestack-style 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-style dependencies transpiled correctly for server-side rendering.
Install the adapter using npm or yarn:
npm i @gluestack/ui-next-adapter
yarn add @gluestack/ui-next-adapter
Then, add the adapter to your next.config.js file:
const { withGluestackUI } = require('@gluestack/ui-next-adapter');
module.exports = withGluestackUI(nextConfig);
Server-side Rendering
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 library and call it in your _document.tsx or _document.jsx file.
To prevent flickering, the gs class should be attached, which increases the specificity of any inline styles that are being used.
import * as React from "react"
import { Html, Head, Main, NextScript } from "next/document"
import { AppRegistry } from "react-native"
import { flush } from "@gluestack-style/react"
<Html lang="en" className="gs">
/* To smooth any scrolling behavior */
-webkit-overflow-scrolling: touch;
/* Allows content to fill the viewport and go beyond the bottom */
Document.getInitialProps = async ({ renderPage }: any) => {
AppRegistry.registerComponent("Main", () => Main)
const { getStyleElement } = AppRegistry.getApplication("Main")
const page = await renderPage()
<style dangerouslySetInnerHTML={{ __html: style }} />,
return { ...page, styles: React.Children.toArray(styles) }
Babel Plugin (Optional)
You can also add the babel plugin to your project if you want to perform all style calculations during build time. The plugin can be installed via npm or yarn and should be added to your babel config.
Installation
Install it as development dependency.
npm i --save-dev @gluestack-style/babel-plugin-styled-resolver
yarn add --dev @gluestack-style/babel-plugin-styled-resolver
Usage
Add it to babel.config.js or .babelrc.
plugins: ["@gluestack-style/babel-plugin-styled-resolver"],
or
"plugins": ["@gluestack-style/babel-plugin-styled-resolver"]
That's it 🎉
You are ready to use gluestack-style!
import React from "react"
import { styled } from "@gluestack-style/react"
import { View } from "react-native"
const Box = styled(View, {