Skip to content

Commit

Permalink
Merge pull request #11 from react18-tools/fix/use-script
Browse files Browse the repository at this point in the history
Fix/use script
  • Loading branch information
mayank1513 committed Jun 15, 2024
2 parents b9c1839 + dcb1ca9 commit f0ac35d
Show file tree
Hide file tree
Showing 31 changed files with 101 additions and 481 deletions.
5 changes: 5 additions & 0 deletions .changeset/seven-parents-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-darkmode": major
---

Inject script to set mode on page load.
5 changes: 5 additions & 0 deletions .changeset/shaggy-mirrors-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"nextjs-darkmode": patch
---

Reuse code from script
47 changes: 6 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ The `nextjs-themes` library was initially created to achieve a similar functiona

- ✅ Manipulate theme via the `useMode` hook

- ✅ No cookies when not using the corresponding `ServerTarget`

- ✅ Comprehensive documentation with [Typedoc](https://react18-tools.github.io/nextjs-darkmode)

Feel free to [request new features](https://github.com/react18-tools/nextjs-darkmode/issues/new?assignees=&labels=&projects=&template=feature_request.md&title=), [discuss](https://github.com/react18-tools/nextjs-darkmode/discussions), or [report bugs](https://github.com/react18-tools/nextjs-darkmode/issues/new?assignees=&labels=&projects=&template=bug_report.md&title=).
Expand Down Expand Up @@ -108,7 +106,7 @@ $ yarn add nextjs-darkmode-lite

> Please explore `examples` and `packages/shared-ui` for more working examples. (updates coming soon...)
### SPA (e.g., Vite, CRA) and Next.js pages directory (No server components)
### SPA (e.g., Vite, CRA) and Next.js pages directory

Modify `_app` to add dark mode support:

Expand All @@ -135,21 +133,19 @@ export default MyApp;

> For `vite` or any other build tool, find a similar root component, e.g., `<App />` in `CRA` and `vite`.
### With Next.js `app` router (Server Components)
### With Next.js `app` router

Update `app/layout.jsx` to add `Core` and `ServerTarget` to avoid flash of un-themed content:
Update `app/layout.jsx` to add `Core` component.

```tsx
// app/layout.jsx
import { Core } from "nextjs-darkmode"; // for better tree-shaking
import { ServerTarget } from "nextjs-darkmode/server";

export default function Layout({ children }) {
return (
<html lang="en">
<head />
<body>
<ServerTarget />
<Core />
{children}
</body>
Expand All @@ -158,26 +154,6 @@ export default function Layout({ children }) {
}
```

If you prefer SSR over SSG for your entire app, you can wrap entire app with `ServerTarget` as follows:

```tsx
// app/layout.jsx
import { Core } from "nextjs-darkmode"; // for better tree-shaking
import { ServerTarget } from "nextjs-darkmode/server";

export default function Layout({ children }) {
return (
<ServerTarget tag="html" lang="en">
<head />
<body>
<Core />
{children}
</body>
</ServerTarget>
);
}
```

### Switch

An elegant color switch to toggle color schemes:
Expand All @@ -188,7 +164,7 @@ An elegant color switch to toggle color schemes:

### HTML & CSS

Fully support dark mode, including system preference with `prefers-color-scheme`. The dark/light mode is synced between tabs and modifies the `className` and data-attributes on the `html` or ServerTarget element:
Fully support dark mode, including system preference with `prefers-color-scheme`. The dark/light mode is synced between tabs and modifies the `className` and data-attributes on the `html` elemnt.

```css
:root {
Expand All @@ -206,19 +182,10 @@ Fully support dark mode, including system preference with `prefers-color-scheme`
[data-rm="dark"] {...}
```

When using `ServerTarget`, use the CSS general sibling combinator (~):

```css
.selector,
.selector *,
.selector ~ *,
.selector ~ * * {
--th-variable: value;
}
```

#### Using the data-attributes

data-attributes are very helpful when you want to customize styles in a CSS module file (`styles.module.css`)

`data-rm` -> Resolved Mode

`data-m` -> User's preference
Expand Down Expand Up @@ -352,8 +319,6 @@ Now you can use dark-mode specific classes:
<h1 className="text-black dark:text-white">
```

> When you use Tailwind, make sure you replace `html` in `app/layout` to `<ServerTarget tag="html"...` to avoid FOUC.

## Performance

`nextjs-darkmode` is designed to be fully tree-shakable, including only the code you use. For instance, if you only use the `useMode` hook, the rest of the library's code will be removed during the build process.
Expand Down
2 changes: 0 additions & 2 deletions examples/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import "./styles.css";
import "react18-loaders/dist/index.css";
import { ServerTarget } from "nextjs-darkmode/server";
import { Core } from "nextjs-darkmode";
import { Layout } from "@repo/shared/dist/server";
import { GlobalLoader, Header } from "@repo/shared";
Expand All @@ -13,7 +12,6 @@ export default function RootLayout({ children }: { children: React.ReactNode }):
return (
<html lang="en">
<body className={inter.className}>
<ServerTarget />
<Core t="all .5s" />
<Layout>
<Header />
Expand Down
8 changes: 0 additions & 8 deletions examples/tailwind-ssg/.eslintrc.js

This file was deleted.

24 changes: 0 additions & 24 deletions examples/tailwind-ssg/app/_components/card.tsx

This file was deleted.

Binary file removed examples/tailwind-ssg/app/favicon.ico
Binary file not shown.
5 changes: 0 additions & 5 deletions examples/tailwind-ssg/app/globals.css

This file was deleted.

23 changes: 0 additions & 23 deletions examples/tailwind-ssg/app/layout.tsx

This file was deleted.

61 changes: 0 additions & 61 deletions examples/tailwind-ssg/app/page.tsx

This file was deleted.

6 changes: 0 additions & 6 deletions examples/tailwind-ssg/cache/config.json

This file was deleted.

5 changes: 0 additions & 5 deletions examples/tailwind-ssg/next-env.d.ts

This file was deleted.

3 changes: 0 additions & 3 deletions examples/tailwind-ssg/next.config.js

This file was deleted.

37 changes: 0 additions & 37 deletions examples/tailwind-ssg/package.json

This file was deleted.

6 changes: 0 additions & 6 deletions examples/tailwind-ssg/postcss.config.js

This file was deleted.

16 changes: 0 additions & 16 deletions examples/tailwind-ssg/tailwind.config.ts

This file was deleted.

13 changes: 0 additions & 13 deletions examples/tailwind-ssg/tsconfig.json

This file was deleted.

13 changes: 0 additions & 13 deletions examples/tailwind-ssg/turbo.json

This file was deleted.

5 changes: 2 additions & 3 deletions examples/tailwind/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Metadata } from "next";
import { Inter } from "next/font/google";
import "./globals.css";
import { Core } from "nextjs-darkmode";
import { ServerTarget } from "nextjs-darkmode/server";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -14,11 +13,11 @@ export const metadata: Metadata = {
/** Root layout */
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<ServerTarget tag="html" lang="en">
<html lang="en">
<body className={inter.className}>
<Core t="background .3s" />
{children}
</body>
</ServerTarget>
</html>
);
}
Loading

0 comments on commit f0ac35d

Please sign in to comment.