Skip to content

Commit

Permalink
Merge pull request #4 from react18-tools/fix/localStorage-not-defined
Browse files Browse the repository at this point in the history
Fix/localStorage-not-defined
  • Loading branch information
mayank1513 committed Jun 9, 2024
2 parents 102a783 + 9605fe4 commit 2fc6de4
Show file tree
Hide file tree
Showing 22 changed files with 452 additions and 12 deletions.
6 changes: 3 additions & 3 deletions .tkb
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,15 @@
"zE0aqgk59cyZ95s5TkECd": {
"id": "zE0aqgk59cyZ95s5TkECd",
"description": "Update doc comments",
"columnId": "column-todo"
"columnId": "column-done"
}
},
"columns": [
{
"id": "column-todo",
"title": "To do",
"tasksIds": [
"W_uPAM5Vm86aJ8yEEMKHh",
"zE0aqgk59cyZ95s5TkECd"
"W_uPAM5Vm86aJ8yEEMKHh"
]
},
{
Expand All @@ -150,6 +149,7 @@
"id": "column-done",
"title": "Done",
"tasksIds": [
"zE0aqgk59cyZ95s5TkECd",
"O1qAByMoxhHOR-a_guL91",
"FDFqCQvLm2mUlnTJ3Gna5",
"RX4J5v4y5IOe_ucf8pMRT",
Expand Down
4 changes: 2 additions & 2 deletions examples/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "next build",
"clean": "rm -rf .next",
"dev": "next dev -p 3002",
"dev": "next dev -p 3000",
"lint": "next lint",
"typecheck": "tsc --noEmit",
"start": "next start",
Expand All @@ -30,4 +30,4 @@
"@types/react-dom": "^18.3.0",
"typescript": "^5.4.5"
}
}
}
8 changes: 8 additions & 0 deletions examples/tailwind/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/** @type {import("eslint").Linter.Config} */
module.exports = {
extends: ["@repo/eslint-config/next.js"],
parser: "@typescript-eslint/parser",
parserOptions: {
project: true,
},
};
24 changes: 24 additions & 0 deletions examples/tailwind/app/_components/card.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
interface CardProps {
href: string;
title: string;
text: string;
}

/** Card component */
export default function Card({ href, title, text }: CardProps) {
return (
<a
href={href}
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30"
target="_blank"
rel="noopener noreferrer">
<h2 className={"mb-3 text-2xl font-semibold"}>
{`${title} `}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={"m-0 max-w-[30ch] text-sm opacity-50"}>{text}</p>
</a>
);
}
Binary file added examples/tailwind/app/favicon.ico
Binary file not shown.
24 changes: 24 additions & 0 deletions examples/tailwind/app/globals.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

@import "nextjs-darkmode/css";

:root {
--foreground-rgb: 0, 0, 0;
--background-start-rgb: 214, 219, 220;
--background-end-rgb: 255, 255, 255;
}

.dark,
.cs-dark {
--foreground-rgb: 255, 255, 255;
--background-start-rgb: 0, 0, 0;
--background-end-rgb: 0, 0, 0;
}

body {
color: rgb(var(--foreground-rgb));
background: linear-gradient(to bottom, transparent, rgb(var(--background-end-rgb)))
rgb(var(--background-start-rgb));
}
25 changes: 25 additions & 0 deletions examples/tailwind/app/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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"] });

export const metadata: Metadata = {
title: "Tailwind CSS Example",
description: "Generated by Mayank <https://github.com/mayank1513>",
};

/** Root layout */
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<body className={inter.className}>
<Core t="background .3s" />
<ServerTarget />
{children}
</body>
</html>
);
}
63 changes: 63 additions & 0 deletions examples/tailwind/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { StarMe } from "@mayank1513/fork-me";
import { Switch } from "nextjs-darkmode/switch";
import Card from "./_components/card";

// Home page
export default function Home() {
return (
// skipcq: JS-0415
<main className="flex min-h-screen flex-col items-center justify-between p-24">
<div className="z-10 max-w-5xl w-full items-center justify-between font-mono text-sm lg:flex">
<p className="fixed left-0 top-0 flex w-full justify-center border-b border-gray-300 bg-gradient-to-b from-zinc-200 pb-6 pt-8 backdrop-blur-2xl dark:border-neutral-800 dark:bg-zinc-800/30 dark:from-inherit lg:static lg:w-auto lg:rounded-xl lg:border lg:bg-gray-200 lg:p-4 lg:dark:bg-zinc-800/30">
nextjs-darkmode Tailwind CSS example
</p>
<div className="fixed bottom-0 left-0 flex h-48 w-full items-end justify-center bg-gradient-to-t from-white via-white dark:from-black dark:via-black lg:static lg:h-auto lg:w-auto lg:bg-none">
<a
className="pointer-events-none flex place-items-center gap-2 p-8 lg:pointer-events-auto lg:p-0"
href="https://vercel.com?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
target="_blank"
rel="noopener noreferrer">
<a href="https://mayank-chaudhari.vercel.app" rel="noopener noreferrer" target="_blank">
By Mayank
</a>
</a>
</div>
</div>

<div className="relative flex place-items-center before:absolute before:h-[300px] before:w-[480px] before:-translate-x-1/2 before:rounded-full before:bg-gradient-radial before:from-white before:to-transparent before:blur-2xl before:content-[''] after:absolute after:-z-20 after:h-[180px] after:w-[240px] after:translate-x-1/3 after:bg-gradient-conic after:from-sky-200 after:via-blue-200 after:blur-2xl after:content-[''] before:dark:bg-gradient-to-br before:dark:from-transparent before:dark:to-blue-700 before:dark:opacity-10 after:dark:from-sky-900 after:dark:via-[#0141ff] after:dark:opacity-40 before:lg:h-[360px] z-[-1]" />

<Switch />

<div className="mb-32 grid text-center lg:max-w-5xl lg:w-full lg:mb-0 lg:grid-cols-4 lg:text-left">
<Card
href="https://react18-tools.github.io/nextjs-darkmode/"
title="Docs"
text="Find in-depth information about nextjs-darkmode features and API."
/>
<Card
href="https://github.com/react18-tools/nextjs-darkmode"
title="Examples"
text="Learn through more examples on official GitHub repo."
/>
<StarMe
gitHubUrl="https://github.com/react18-tools/nextjs-darkmode"
className="group rounded-lg border border-transparent px-5 py-4 transition-colors hover:border-gray-300 hover:bg-gray-100 hover:dark:border-neutral-700 hover:dark:bg-neutral-800/30">
<h2 className={"mb-3 text-2xl font-semibold"}>
Star Me{" "}
<span className="inline-block transition-transform group-hover:translate-x-1 motion-reduce:transform-none">
-&gt;
</span>
</h2>
<p className={"m-0 max-w-[30ch] text-sm opacity-50"}>
Explore and star official <code>nextjs-darkmode</code> repo.
</p>
</StarMe>
<Card
href="https://vercel.com/new?utm_source=create-next-app&utm_medium=appdir-template&utm_campaign=create-next-app"
title="Deploy"
text="Instantly deploy your Next.js site to a shareable URL with Vercel."
/>
</div>
</main>
);
}
5 changes: 5 additions & 0 deletions examples/tailwind/next-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/// <reference types="next" />
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/basic-features/typescript for more information.
3 changes: 3 additions & 0 deletions examples/tailwind/next.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
reactStrictMode: true,
};
37 changes: 37 additions & 0 deletions examples/tailwind/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "@example/tailwind",
"version": "0.0.0",
"private": true,
"scripts": {
"build": "next build",
"clean": "rm -rf .next",
"dev": "next dev -p 3002",
"lint": "next lint",
"typecheck": "tsc --noEmit",
"start": "next start",
"postinstall": "next telemetry disable"
},
"dependencies": {
"@mayank1513/fork-me": "^2.1.2",
"@repo/logger": "workspace:*",
"@repo/shared": "workspace:*",
"next": "^14.2.3",
"nextjs-darkmode": "workspace:*",
"nextjs-themes": "^3.1.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react18-loaders": "^1.1.1"
},
"devDependencies": {
"@next/eslint-plugin-next": "^14.2.3",
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^20.14.2",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"autoprefixer": "^10.4.19",
"postcss": "^8.4.38",
"tailwindcss": "^3.4.4",
"typescript": "^5.4.5"
}
}
6 changes: 6 additions & 0 deletions examples/tailwind/postcss.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
plugins: {
tailwindcss: {},
autoprefixer: {},
},
};
20 changes: 20 additions & 0 deletions examples/tailwind/tailwind.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Config } from "tailwindcss";

const config: Config = {
content: [
"./pages/**/*.{js,ts,jsx,tsx,mdx}",
"./components/**/*.{js,ts,jsx,tsx,mdx}",
"./app/**/*.{js,ts,jsx,tsx,mdx}",
],
theme: {
extend: {
backgroundImage: {
"gradient-radial": "radial-gradient(var(--tw-gradient-stops))",
"gradient-conic": "conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))",
},
},
},
plugins: [],
darkMode: "class",
};
export default config;
13 changes: 13 additions & 0 deletions examples/tailwind/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"exclude": ["node_modules"],
"extends": "@repo/typescript-config/nextjs.json",
"compilerOptions": {
"outDir": "dist",
"plugins": [
{
"name": "next"
}
]
},
"include": ["src", "next.config.js", "next-env.d.ts", ".next/types/**/*.ts"]
}
13 changes: 13 additions & 0 deletions examples/tailwind/turbo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": [
"//"
],
"tasks": {
"build": {
"outputs": [
".next/**",
"!.next/cache/**"
]
}
}
}
6 changes: 6 additions & 0 deletions lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# nextjs-darkmode

## 0.0.2

### Patch Changes

- Fix: fix localStorage not found (non-blocking) error

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nextjs-darkmode",
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
"private": false,
"version": "0.0.1",
"version": "0.0.2",
"description": "Unleash the Power of React Server Components! Use dark/light mode on your site with confidence, without losing any advantages of React Server Components",
"license": "MPL-2.0",
"main": "./dist/index.js",
Expand Down
5 changes: 2 additions & 3 deletions lib/src/client/core/core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ColorSchemePreference, Store, useStore } from "../../utils";
import { useEffect } from "react";

const useEffectMinify = useEffect;
const localStorageMinify = localStorage;
export interface CoreProps {
/** force apply CSS transition property to all the elements during theme switching. E.g., `all .3s` */
t?: string;
Expand Down Expand Up @@ -50,7 +49,7 @@ export const Core = ({ t }: CoreProps) => {

setThemeState(state => ({
...state,
m: (localStorageMinify?.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
m: (localStorage.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
}));
/** Sync the tabs */
const storageListener = (e: StorageEvent): void => {
Expand Down Expand Up @@ -80,7 +79,7 @@ export const Core = ({ t }: CoreProps) => {
});
restoreTransitions();
// System mode is decided by current system state and need not be stored in localStorage
localStorageMinify?.setItem(COOKIE_KEY, mode);
localStorage.setItem(COOKIE_KEY, mode);
if (serverTargetEl)
documentMinify.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=31536000;SameSite=Strict;`;
}, [resolvedMode, systemMode, mode, t]);
Expand Down
5 changes: 3 additions & 2 deletions lib/src/client/switch/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ export const Switch = ({ tag: Tag = "button", size = 24, skipSystem, ...props }:
/** toggle mode */
const handleModeSwitch = () => {
let index = modes.indexOf(state.m);
if (skipSystem && index === modes.length - 1) index = 0;
const n = modes.length;
if (skipSystem && index === n - 1) index = 0;
setState({
...state,
m: modes[(index + 1) % modes.length],
m: modes[(index + 1) % n],
});
};
const className = [props.className, styles["switch"]].filter(Boolean).join(" ");
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @repo/shared

## 0.0.2

### Patch Changes

- Updated dependencies
- [email protected]

## 0.0.1

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@repo/shared",
"version": "0.0.1",
"version": "0.0.2",
"private": true,
"sideEffects": false,
"main": "./dist/index.js",
Expand Down
Loading

0 comments on commit 2fc6de4

Please sign in to comment.