Skip to content

Commit

Permalink
Merge pull request #3 from react18-tools/fix/doc-comments
Browse files Browse the repository at this point in the history
Fix/doc-comments
  • Loading branch information
mayank1513 committed Jun 9, 2024
2 parents 2cd1fe7 + 767b124 commit d3ba198
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 26 deletions.
8 changes: 7 additions & 1 deletion .tkb
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,20 @@
"id": "W_uPAM5Vm86aJ8yEEMKHh",
"description": "Update examples\n- Add example for tailwind",
"columnId": "column-todo"
},
"zE0aqgk59cyZ95s5TkECd": {
"id": "zE0aqgk59cyZ95s5TkECd",
"description": "Update doc comments",
"columnId": "column-todo"
}
},
"columns": [
{
"id": "column-todo",
"title": "To do",
"tasksIds": [
"W_uPAM5Vm86aJ8yEEMKHh"
"W_uPAM5Vm86aJ8yEEMKHh",
"zE0aqgk59cyZ95s5TkECd"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/nextjs/src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function RootLayout({ children }: { children: React.ReactNode }):
<html lang="en">
<body className={inter.className}>
<ServerTarget />
<Core />
<Core t="all .5s" />
<Layout>
<Header />
{children}
Expand Down
7 changes: 7 additions & 0 deletions lib/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# nextjs-darkmode

## 0.0.1

### Patch Changes

- Update doc comments, improve minification.
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.0",
"version": "0.0.1",
"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
31 changes: 14 additions & 17 deletions lib/src/client/core/core.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
import { COOKIE_KEY, DARK, LIGHT, MAX_AGE, SYSTEM, modes } from "../../constants";
import { COOKIE_KEY, DARK, LIGHT, SYSTEM, modes } from "../../constants";
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;
}

/** Modify transition globally to avoid patched transitions */
const modifyTransition = (documentMinify: Document, themeTransition = "none") => {
const css = documentMinify.createElement("style");
/** split by ';' to prevent CSS injection */
const transition = `transition: ${themeTransition.split(";")[0]} !important;`;
css.appendChild(
documentMinify.createTextNode(
`*{-webkit-${transition}-moz-${transition}-o-${transition}-ms-${transition}${transition}}`,
),
);
documentMinify.head.appendChild(css);
css.textContent = `*{transition:${themeTransition.split(";")[0]} !important;}`;
const head = documentMinify.head;
head.appendChild(css);

return () => {
// Force restyle
getComputedStyle(documentMinify.body);
// Wait for next tick before removing
setTimeout(() => {
documentMinify.head.removeChild(css);
}, 1);
setTimeout(() => head.removeChild(css), 1);
};
};

/**
*
* The Core component wich applies classes and transitions.
* Cookies are set only if corresponding ServerTarget is detected.
*
* @example
* ```tsx
* <Core />
* <Core [t="background-color .3s"]/>
* ```
*
* @source - Source code
Expand All @@ -53,7 +50,7 @@ export const Core = ({ t }: CoreProps) => {

setThemeState(state => ({
...state,
m: (localStorage.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
m: (localStorageMinify?.getItem(COOKIE_KEY) ?? SYSTEM) as ColorSchemePreference,
}));
/** Sync the tabs */
const storageListener = (e: StorageEvent): void => {
Expand Down Expand Up @@ -83,10 +80,10 @@ export const Core = ({ t }: CoreProps) => {
});
restoreTransitions();
// System mode is decided by current system state and need not be stored in localStorage
localStorage.setItem(COOKIE_KEY, mode);
localStorageMinify?.setItem(COOKIE_KEY, mode);
if (serverTargetEl)
documentMinify.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=${MAX_AGE};SameSite=Strict;`;
}, [resolvedMode, systemMode, mode]);
documentMinify.cookie = `${COOKIE_KEY}=${resolvedMode};max-age=31536000;SameSite=Strict;`;
}, [resolvedMode, systemMode, mode, t]);

return null;
};
4 changes: 2 additions & 2 deletions lib/src/client/switch/switch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export interface SwitchProps extends HTMLProps<HTMLElement> {
}

/**
*
* Switch button to quickly toggle user preference.
*
* @example
* ```tsx
* <Switch />
* <Switch [size={20} skipSystem]/>
* ```
*
* @source - Source code
Expand Down
1 change: 0 additions & 1 deletion lib/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { ColorSchemePreference } from "./utils";
export const SYSTEM: ColorSchemePreference = "system";
export const DARK: ColorSchemePreference = "dark";
export const LIGHT: ColorSchemePreference = "light";
export const MAX_AGE = 31536000;

export const COOKIE_KEY = "gx";
export const modes: ColorSchemePreference[] = [SYSTEM, DARK, LIGHT];
4 changes: 2 additions & 2 deletions lib/src/hooks/use-mode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export interface UseModeYeild {
}

/**
*
* use this hook to gain access to mode state and setter.
*
* @example
* ```tsx
* const [] = useMode(options);
* const {resolvedMode, setMode} = useMode(options);
* ```
*
* @source - Source code
Expand Down
3 changes: 3 additions & 0 deletions lib/src/server/server-target/server-target.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { COOKIE_KEY, LIGHT } from "../../constants";

/**
* Server Side target for avoiding flash of un-themed content.
*
* @example
* ```tsx
* <html>
Expand All @@ -13,6 +14,8 @@ import { COOKIE_KEY, LIGHT } from "../../constants";
* </body>
* </html>
* ```
*
* @source - Source code
*/
export const ServerTarget = () => {
const rm = cookies().get(COOKIE_KEY)?.value ?? LIGHT;
Expand Down
8 changes: 8 additions & 0 deletions packages/shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# @repo/shared

## 0.0.1

### Patch Changes

- Updated dependencies
- [email protected]
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.0",
"version": "0.0.1",
"private": true,
"sideEffects": false,
"main": "./dist/index.js",
Expand Down

0 comments on commit d3ba198

Please sign in to comment.