Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dark mode not working! #86

Open
pol-dev-shinroo opened this issue Jan 9, 2023 · 1 comment
Open

Dark mode not working! #86

pol-dev-shinroo opened this issue Jan 9, 2023 · 1 comment

Comments

@pol-dev-shinroo
Copy link

Because dark mode css for tailwind seemed bit messy (like i have to add "dark:" to every dark mode class i want to implement),
i tried to make it cleaner by passing arrays of dark mode classes as props to my styled-components where i use dark function i created to return "dark: classname" for every classnames in the array.

Here are the example:

const dark = (props: string[]): string => {
  console.log(props);
  return props.map((s) => `dark:${s}`).join(" ");
};

export default dark;
import tw from "tailwind-styled-components";

function App() {
  const [theme, setTheme] = useState<string | null>(null);

  useEffect(() => {
    if (theme === "dark") {
      document.documentElement.classList.add("dark");
    } else {
      document.documentElement.classList.remove("dark");
    }
  }, [theme]);

  const handleThemeSwitch = () => {
    setTheme(theme === "dark" ? "light" : "dark");
  };
  return (
    <>
        <MyBtn
          $dark={["bg-stone-200", "text-xs", "text-black", "rounded-3xl"]}
          className="bg-slate-900 p-4 text-white"
          onClick={handleThemeSwitch}
        >
          Dark Mode
        </MyBtn>
      </Grid>
    </>
  );
}

interface MyBtnProps {
  $dark?: Dark;
}

const MyBtn = tw.button<MyBtnProps>`
 ${({ $dark }) => $dark && dark($dark)}
`;

When i checked the dev-tool, i can clearly see that "dark: classname" is being added. However, the dark mode isnt working when the button is clicked.

After hours of bug-finding, i gave up.

@jahirfiquitiva
Copy link

This might be because of tailwind not recognizing the use of the dark class names. Try adding those classes to the tailwind config safelist, so the compilers keeps them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants