Giter Club home page Giter Club logo

chakra-ui-flashless's Introduction

Flashless Chakra UI

This library contains all the tools necessary to implement Chakra UI color modes on statically rendered websites without the flash.

Note: This technique sets Chakra's colors based on the system color mode and doesn't currently support toggling the color mode within the site.

night time

The approach is based on a blog post by @joshwcomeau. It goes something like this:

  1. Inject a script at the top of your HTML <body> that checks the system color mode.
  2. Define a bunch of CSS variables for your different UI colors, based on whether the system color mode is light or dark.
  3. Use those variables in your CSS instead of raw colors.

This library simplifies the creation of these light-sensitive color variables, and makes it easy to update your Chakra UI theme to use them.

Installation

Before using this library, you should have installed Chakra UI and its dependencies.

npm install chakra-ui-flashless

Usage

First, wrap your theme overrides in the flashless function. If you use the default theme with no overrides, simply pass flashless() to extendTheme.

import {extendTheme} from '@chakra-ui/react';
import {flashless} from 'chakra-ui-flashless';

// without overrides
const theme = extendTheme(flashless());

// with overrides
const theme = extendTheme(
  flashless({
    styles: {
      global: {
        'pre, :not(pre) > code': {
          fontSize: 'calc(1em / 1.125)',
          borderRadius: 'sm'
        }
      }
    }
  })
);

export default theme;

Next append the FlashlessScript component to the top of the HTML <body> and pass your Chakra theme as a prop. This looks slightly different depending on your static site generator.

import theme from './theme';

<FlashlessScript theme={theme} />

Gatsby

In gatsby-ssr.js, set a FlashlessScript as a pre-body component using the onRenderBody API.

// gatsby-ssr.js
import React from 'react';
import theme from './src/theme';
import {ChakraProvider} from '@chakra-ui/react';
import {FlashlessScript} from 'chakra-ui-flashless';

export const onRenderBody = ({setPreBodyComponents}) => {
  setPreBodyComponents([
    <FlashlessScript key="chakra-ui-flashless" theme={theme} />
  ]);
};

export const wrapRootElement = ({element}) => (
  <ChakraProvider theme={theme}>{element}</ChakraProvider>
);

You should also use wrapRootElement to wrap your app in a ChakraProvider and pass your theme there as well. You could use @chakra-ui/gatsby-plugin to do this, but it also injects ColorModeScript from Chakra UI, which you won't need anymore if you're using this method.

Export it from gatsby-ssr.js and gatsby-browser.js.

// gatsby-browser.js
export {wrapRootElement} from './gatsby-ssr';

Next.js

In Next.js, simply append the FlashlessScript to the <body> using a custom Document.

// pages/_document.js
import Document, {Head, Html, Main, NextScript} from 'next/document';
import React from 'react';
import theme from './theme';
import {FlashlessScript} from 'chakra-ui-flashless';

export default class MyDocument extends Document {
  render() {
    return (
      <Html>
        <body>
          <FlashlessScript theme={theme} />
          <Main />
          <NextScript />
        </body>
      </Html>
    );
  }
}

If you have a Custom App, you may also need to wrap it with the ChakraProvider and your theme.

import { ChakraProvider } from '@chakra-ui/react';
import theme from './theme';

function myApp({ Component, pageProps }) {
  return (
    <ChakraProvider theme={theme}>
      <Component {...pageProps} />
    </ChakraProvider>
  );
}

Custom variables

You can create additional color variables to use in your UI with the customVariables prop. It accepts an object that maps CSS variable names to their light and dark variants using an array with two values.

To represent semitransparent colors, you can define a single variant as yet another array with two values: the color, and its opacity.

{
  '--variable-name': [lightVariant, [darkVariant, 0.5]]
}

You can pass any named color that is defined in your Chakra theme, and easily manipulate their transparency.

<FlashlessScript
  theme={theme}
  customVariables={{
    // define custom color variables
    '--inline-code-bg': ['indigo.50', 'gray.900'],
    '--inline-code-text': [
      'indigo.800',
      ['indigo.200', 0.5] // supply an array for semitransparent colors
    ]
  }}
/>

License

MIT

chakra-ui-flashless's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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.