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

Feature: allow custom classnames on DarkModeProvider and noflash.js #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,36 @@ Copy the `noflash.min.js` from `node_modules/@rbnd/react-dark-mode/src/noflash.m
</html>
```

### 5. Set custom styles

It's possible to override the default classnames, so other libraries can be used that handle a different dark-mode classname convention.

```jsx
import { DarkModeProvider } from "@rbnd/react-dark-mode";

const App = () => {
// ...

return (
<DarkModeProvider darkModeClass="dark-theme" lightModeClass="light-theme">
{/* Your other components */}
</DarkModeProvider>
);
};
```

Also the classnames used in `noflash.min.js` can be overridden, by using the following data-attrs on html element.

```html
<html data-dark-mode-classname="dark-theme" data-light-mode-classname="light-theme">
<head>
<!-- ... -->
<script src="/noflash.min.js"></script>
</head>
<body></body>
</html>
```

---

Created by [rbnd.studio](https://rbnd.studio/). Check out [Atmos](https://atmos.style/) our tool for creating professional color palettes, hey it's free!
18 changes: 10 additions & 8 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,29 @@ const DarkModeContext = createContext<IDarkModeContext>({
});

type Props = {
darkModeClass?: string;
lightModeClass?: string;
children?: ReactNode;
};

const preferDarkQuery = "(prefers-color-scheme: dark)";
const mediaQueryEventTarget =
typeof window !== "undefined" ? window.matchMedia(preferDarkQuery) : null;
export const DarkModeProvider = ({ children }: Props) => {
export const DarkModeProvider = ({ children, darkModeClass, lightModeClass }: Props) => {
const [mode, setMode] = useLocalStorage<Mode>("scheme-mode", "system");

useEffect(() => {
const listener = (e: MediaQueryListEvent) => {
if (mode === "system") setClassOnDocument(e.matches);
if (mode === "system") setClassOnDocument(e.matches, darkModeClass, lightModeClass);
};
mediaQueryEventTarget?.addEventListener("change", listener);
return () => mediaQueryEventTarget?.removeEventListener("change", listener);
}, [mode]);

useEffect(() => {
if (mode === "dark") return setClassOnDocument(true);
if (mode === "light") return setClassOnDocument(false);
setClassOnDocument(window.matchMedia(preferDarkQuery).matches);
if (mode === "dark") return setClassOnDocument(true, darkModeClass, lightModeClass);
if (mode === "light") return setClassOnDocument(false, darkModeClass, lightModeClass);
setClassOnDocument(window.matchMedia(preferDarkQuery).matches, darkModeClass, lightModeClass);
}, [mode]);

return (
Expand All @@ -44,9 +46,9 @@ export const DarkModeProvider = ({ children }: Props) => {
);
};

const setClassOnDocument = (darkTheme: boolean) => {
const classNameDark = "dark-mode";
const classNameLight = "light-mode";
const setClassOnDocument = (darkTheme: boolean, darkModeClass?: string, lightModeClass?: string) => {
const classNameDark = darkModeClass || "dark-mode";
const classNameLight = lightModeClass || "light-mode";
const element =
typeof window !== "undefined" ? document.querySelector("html") : null;
if (!element) return;
Expand Down
6 changes: 4 additions & 2 deletions src/noflash.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
(function () {
var classNameDark = "dark-mode";
var classNameLight = "light-mode";
var html = document.documentElement;
var classNameDark = html.dataset.darkModeClassname || "dark-mode";
var classNameLight = html.dataset.lightModeClassname || "light-mode";

function setClassOnDocument(darkMode) {
document.querySelector("html").classList.add(darkMode ? classNameDark : classNameLight);
document.querySelector("html").classList.remove(darkMode ? classNameLight : classNameDark);
Expand Down
2 changes: 1 addition & 1 deletion src/noflash.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.