Git Product home page Git Product logo

Comments (6)

natasha08n avatar natasha08n commented on May 20, 2024 1

I managed to make theme changeable in toolbar in storybook

in preview.js

export const globalTypes = {
  theme: {
    name: 'Theme',
    description: 'Global theme for components',
    defaultValue: 'light',
    toolbar: {
      items: ['light', 'dark'],
      showName: true,
      dynamicTitle: true,
    },
  },
};

and also I added decorator in the same file

export const decorators = [withStyle];

And here is withStyle.tsx file

import { FC } from 'react';
import { ColorThemeProvider } from 'modules/styleSystem';

export function withStyle(Story: FC, context: any) {
  return (
    <ColorThemeProvider forcedTheme={context.globals.theme}>
        <Story />
    </ColorThemeProvider>
  );
}

And this is my ColorThemeProvider

import { PropsWithChildren } from 'react';
import { ThemeProvider as NextThemeProvider } from 'next-themes';

export const ColorThemeProvider = ({
  forcedTheme,
  ...props
}: PropsWithChildren<{ forcedTheme?: 'light' | 'dark' }>) => {
  return (
    <NextThemeProvider themes={['light', 'dark']} {...(forcedTheme ? { forcedTheme } : {})}>
      {props.children}
    </NextThemeProvider>
  );
};

Please let me know if it works for you guys

Screen.Recording.2022-09-02.at.20.28.25.mov

from next-themes.

talohana avatar talohana commented on May 20, 2024

I am also trying to integrate next-themes with storybook, using globalTypes and forcedTheme also renders light mode only

export const decorators = [
  (Story, { globals }) => {
    return (
      <ThemeProvider attribute="class" forcedTheme={globals.theme}>
        <Story />
      </ThemeProvider>
    );
  },
];

export const globalTypes = {
  theme: {
    name: 'Theme',
    defaultValue: 'system',
    toolbar: {
      icon: 'circlehollow',
      items: ['light', 'dark', 'system'],
      showName: true,
    },
  },
};

EDIT:

It does change the theme, but doesn't apply the class attribute, setTheme is calling to changeTheme with updateDom set to false, which in return does not update the class attribute.

from next-themes.

trm217 avatar trm217 commented on May 20, 2024

Storybook is not implemented with Next.js. Since the purpose of this library is a clean implementation of theming in Next.js, I doubt storybook support will be a thing. (Considering CRA and Gatsby are out of scope as well)

from next-themes.

pacocoursey avatar pacocoursey commented on May 20, 2024

In v0.2 we're not using any Next.js specific code anymore, so I'll think about making the library more generic to support Storybook or other frameworks. Definitely low priority though โ€“ย using next-themes with Next.js will always be the focus.

from next-themes.

fede-rodes avatar fede-rodes commented on May 20, 2024

This worked for me:

// ThemeChanger.tsx
import React from "react";
import { useTheme } from "next-themes";
import { useEffect } from "react";

const ThemeChanger = ({ theme }: { theme: "light" | "dark" }) => {
    const { setTheme } = useTheme();

    useEffect(() => {
        setTheme(theme);
    }, [setTheme, theme]);

    return null;
};

export default React.memo(ThemeChanger);

// preview.js
import React from 'react';
import { ThemeProvider } from "next-themes";
import ThemeChanger from './ThemeChanger';

export const decorators = [
    (Story, { globals }) => (
        <ThemeProvider>
            <ThemeChanger
                theme={
                    globals?.backgrounds?.value == null ||
                    globals.backgrounds.value === "#F8F8F8"
                        ? "light"
                        : "dark"
                }
            />
            <Story />
        </ThemeProvider>
    ),
];

export const parameters = {
    actions: { argTypesRegex: "^on[A-Z].*" },
    controls: {
        matchers: {
            color: /(background|color)$/i,
            date: /Date$/,
        },
    },
};

from next-themes.

olaj avatar olaj commented on May 20, 2024

I used a combination of the above.

// ThemeChanger.tsx
import React from "react";
import { useTheme } from "next-themes";
import { useEffect } from "react";

const ThemeChanger = ({ theme }: { theme: "modern" | "dark" | "classic" }) => {
  const { setTheme } = useTheme();

  useEffect(() => {
    console.log(theme, "theme");
    setTheme(theme);
  }, [setTheme, theme]);

  return null;
};

export default React.memo(ThemeChanger);
//preview.js
import "../public/static/global-styles.scss";
import { ThemeProvider } from "next-themes";
import ThemeChanger from "./ThemeChanger";

export const globalTypes = {
  theme: {
    name: "Theme",
    description: "Global theme for components",
    defaultValue: "modern",
    toolbar: {
      items: ["modern", "dark", "classic"],
      showName: true,
      dynamicTitle: true,
    },
  },
};

export const decorators = [
  (Story, { globals }) => (
    <ThemeProvider>
      <ThemeChanger theme={globals.theme ? globals.theme : "modern"} />
      <Story />
    </ThemeProvider>
  ),
];

export const parameters = {
  actions: { argTypesRegex: "^on[A-Z].*" },
  controls: {
    matchers: {
      color: /(background|color)$/i,
      date: /Date$/,
    },
  },
};

The example from @fede-rodes didn't include a "toolbar switcher" and i grabbed that from @natasha08n example and made some minor changes. Works for me!

from next-themes.

Related Issues (20)

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.