A Gatsby plugin for styletron with built-in server-side rendering support
npx @tessl/cli install tessl/npm-gatsby-plugin-styletron@8.15.0Gatsby Plugin Styletron is a Gatsby plugin that integrates Styletron CSS-in-JS library with built-in server-side rendering support. It provides seamless setup for using Styletron styled components in Gatsby applications with proper style hydration and SSR support.
npm install gatsby-plugin-styletron styletron-react styletron-engine-atomicThis is a Gatsby plugin that does not export APIs for direct import. It provides functionality through Gatsby's plugin system.
Configure the plugin in your gatsby-config.js:
module.exports = {
plugins: [
{
resolve: `gatsby-plugin-styletron`,
options: {
// Optional CSS class prefix for Styletron
prefix: "_",
// Optional debug mode, defaults to true in development, false in production
debug: false,
},
},
],
}Once configured, use Styletron components in your React components:
import React from "react";
import { styled, useStyletron } from "styletron-react";
// Statically styled component
const RedAnchor = styled("a", { color: "red" });
// Dynamically styled component
const BigAnchor = styled("a", ({ $size }) => ({ fontSize: `${$size}px` }));
const IndexPage = () => {
// Hook-based API
const [css] = useStyletron();
return (
<div>
<RedAnchor>Red Anchor</RedAnchor>
<BigAnchor $size={64}>Big Anchor</BigAnchor>
<p className={css({ color: "blue" })}>blue text</p>
</div>
);
};The plugin integrates Styletron into Gatsby through two main components:
Configuration options for customizing Styletron behavior in Gatsby applications.
// Plugin options interface
interface PluginOptions {
/** CSS class prefix for Styletron styles (optional) */
prefix?: string;
/** Enable debug mode in development (optional, defaults to true in development, false in production) */
debug?: boolean;
}Wraps the root React element with Styletron Provider for client-side hydration.
/**
* Gatsby Browser API - wraps root element with Styletron Provider for client hydration
* @param element - The root React element from Gatsby
* @param prefix - CSS class prefix for Styletron styles (optional)
* @param debug - Enable debug mode in development (optional, defaults to true in development, false in production)
* @returns React element wrapped with Styletron Provider
*/
export function wrapRootElement({ element }, { prefix, debug }): React.ReactElement;Wraps the root React element with Styletron Provider for server-side rendering.
/**
* Gatsby SSR API - wraps root element with Styletron Provider for server rendering
* @param element - The root React element from Gatsby
* @param options - Plugin configuration options containing prefix and debug settings
* @returns React element wrapped with Styletron Provider
*/
export function wrapRootElement({ element }, options): React.ReactElement;Injects generated Styletron stylesheets into the HTML head during server-side rendering.
/**
* Gatsby SSR API - injects generated CSS into HTML head during server rendering
* @param setHeadComponents - Gatsby function to set HTML head components
* @returns void
*/
export function onRenderBody({ setHeadComponents }): void;@babel/runtime: ^7.20.13gatsby: ^5.0.0-nextreact: ^18.0.0 || ^0.0.0styletron-engine-atomic: ^1.4.8styletron-react: ^5.2.7 || ^6.0.0This plugin integrates with Gatsby's plugin architecture through:
wrapRootElement hook for client-side setupwrapRootElement and onRenderBody hooks for server-side renderingstyletron-engine-atomic for CSS generationstyletron-react Provider and DebugEngineThe plugin ensures that: