GithubHelp home page GithubHelp logo

theemattoliver / gatsby-starter-ssr-dark-mode-storybook Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 1.2 MB

Getting SSR dark mode, Themeprovider, and Storybook to work together.

Home Page: gatsby-starter-ssr-dark-mode-storybook.vercel.app

License: BSD Zero Clause License

JavaScript 96.37% TypeScript 3.63%
ssr storybook themes dark-mode design

gatsby-starter-ssr-dark-mode-storybook's Introduction

Minimal repro

Getting SSR dark mode, ThemeProvider, and Storybook to work together.

๐Ÿ‘‰ site with ssr "dark mode" (color mode) here: https://gatsby-starter-ssr-dark-mode-storybook.vercel.app/

๐Ÿ‘‰ storybook using site's ThemeProvider here (illustrates the defect): https://sb-gatsby-starter-ssr-dark-mode-storybook.vercel.app/?path=/story/components-testcomponent--test-component

To run locally

  1. fork this repo and clone a local copy

  2. install dependencies using package manager of your choice

yarn

// or

npm install
  1. Run Gatsby:
yarn start
  1. Run Storybook:
yarn storybook

What we want to happen:

Josh Comeau's Gatsby SSR color-mode implementation lets us use CSS variables to implement theming and inject those variables into the <head> at compile time. Is it possible to achieve the same thing in Storybook? E.g.:

โ€ข When Gatsby SSR generates HTML at compile-time, inject a <script> tag before all of our content (which in the case of Storybook, I assume would be Storybook Preview, which is the place components related to stories are rendered. You can read about how this works here.

โ€ข In that script tag, work out what the user's color preferences are

โ€ข Update the CSS variables using JavaScript

In Storybook, we want to import our ThemeProvider component in preview.js; we want to pass our colorMode value into the Storybook iFrame and access SSR-generated CSS variables that have been injected into the <head> at compile time.

Our CSS variables are stored in constants.js. We want to inject those into the <head> of the Storybook instance so that our <ThemeProvider> component can access them, based on the colorMode passed into the <ThemeProvider> each story is wrapped in.

What we don't want to do:

It's possible to take a styled-system-like approach, by making our <ThemeProvider> accept a theme object that we can import as a CommonJS module using @preval. We can pass that theme object in to Storybook stories or anywhere else. That all involves a bunch of extra stuff like existential getter functions that look up styles in dot-delimited objects, based on the theme.

That's fine, but it would be nice to get to take advantage of CSS variables instead.

Like this:

Gatsby SSR

What is currently happening:

Nothing, really. We're using our <ThemeProvider> component in preview.js, but right now, its logic is almost entirely focused on doing all its work on the server.

Storybook dark mode decorator

Note that the "correct" theme value is being passed into the iFrame'd <Story>, but nothing is happening, as expected, because the iFrame has no notion of the contextValue that the <ThemeProvider> creates in gatsby-ssr.js.

This feels like it gets into webpack-config world?

Seems like there's ongoing discussion on this here.

gatsby-starter-ssr-dark-mode-storybook's People

Contributors

theemattoliver avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.