Home >Web Front-end >JS Tutorial >Apply CSS in Next.js with StayedCSS
Since the launch of the Next.js App Router, developers have embraced a paradigm that separates server and client components. While efficient, it introduces new challenges.
Next.js often uses React styling libraries, as it’s based on React. However, many of these libraries are optimized for CSR (Client-Side Rendering), while the App Router defaults to SSR (Server-Side Rendering). This mismatch can lead to inconsistent styles between SSR and CSR.
Dark mode is a common feature for better UX, but SSR typically renders HTML in light mode. The client applies dark mode later, causing screen flickering that disrupts the user experience.
I’ve faced these challenges myself during development. Despite the strengths of Next.js, styling problems kept getting in the way. Here's how I addressed them.
StayedCSS: The CSS library for Next.js App Router
StayedCSS - beta version
StayedCSS is a static CSS library designed for Next.js App Router, offering full support for both server and client components. With a simple syntax similar to basic CSS, it enables efficient styling and aims to be the next-generation CSS library for the Next.js App Router.
Currently in its beta version, StayedCSS is rapidly improving in optimization and stability. It will continue to evolve to deliver a better styling experience for your projects.
Installation
npm install stayedcss <span># or</span> yarn…
To address these issues, I created StayedCSS. This library supports both server and client environments, offering flicker-free dark mode and seamless compatibility with the Next.js App Router.
I hope StayedCSS helps other developers solve these challenges, and I’m excited to share it with the community!
npm install stayedcss <span># or</span> yarn
Define styles using the st function, which generates static CSS files and unique class names. Each style object is linked to a componentId, ensuring easy differentiation. Server components can now leverage CSS-in-JS-like syntax while benefiting from static CSS optimized for SSR.
import { st } from "stayedcss"; const styles = st({ componentId: "components/example", container: { backgroundColor: "white", padding: "20px", }, }); export default function Example() { return ( <div className={styles.container}> <h1>Hello, StayedCSS!</h1> </div> ); }
For client components, use the stClient function to define styles. Similar to server components, it generates static CSS files and unique class names based on a componentId. Both server and client components share the same simple styling approach.
'use client' import { stClient } from "stayedcss/client"; export default function ExampleClient() { return ( <div className={style.container}> <div className={style.title}>title</div> </div> ); } const style = stClient({ componentId: "components/example-client", container: { marginBottom: 60, }, title:{ fontSize: 27, }, });
StayedCSS resolves FOUC issues in SSR by using cookies to apply dark mode without delays or flickering. Define light and dark mode styles with matching componentId for smooth transitions.
import { st, stDark } from "stayedcss"; export default function DarkModeExample() { return ( <div className={style.container}> <h1>Hello world!</h1> </div> ); } const style = st({ componentId: "component/darkmode-example", container: { backgroundColor: "white", }, }); stDark({ componentId: "component/darkmode-example", container: { backgroundColor: "black", }, });
StayedCSS makes media queries easy with keywords like mobile, tablet, and desktop. Declare responsive styles directly in the style object for clean and adaptive designs across screen sizes.
const style = st({ componentId: "components/docs/media-query", container: { display: "flex", justifyContent: "center", padding: "20px", backgroundColor: "lightgray", }, "@mobile": { container: { backgroundColor: "pink", padding: "10px", }, }, "@tablet": { container: { backgroundColor: "lightblue", padding: "15px", }, }, "@desktop": { container: { backgroundColor: "lightgreen", width: "50%", }, }, });
Supported features include pseudo-classes :hover, pseudo-elements ::before, and combinators ~. Write familiar CSS syntax, and StayedCSS optimizes it into static files, enabling high-performance styling across server and client components.
For detailed examples, visit the Docs page.
StayedCSS is here to simplify styling in the Next.js App Router environment. From server-client compatibility to advanced CSS features, it’s designed for a better styling experience. Try it today and share your thoughts — I’d love to hear your feedback! ?
The above is the detailed content of Apply CSS in Next.js with StayedCSS. For more information, please follow other related articles on the PHP Chinese website!