0.1.5 • Published 1 month ago

@-ft/mode-next v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

mode-next - Theme mode manager wrapper for Next

Next wrapper of dark/light theme mode manager for web

Usage

  • Generate /public/mode.js using @-ft/mode-codegen package.
{
  "variableName": "__theme_mode",
  "persist": {
    "type": "cookie",
    "key": "THEME",
  }
}
  • Load /mode.js in src/app/layout.tsx, apply default cookie-stored theme mode to class name/ssrInitialMode and suppress hydration warning
import { ModeContextProvider } from '@-ft/mode-next';

export default function RootLayout({ children }: PropsWithChildren) {
  return (
    <html
      lang="en"
      className={cookies().get('THEME')?.value === "dark" ? "dark" : undefined}
      suppressHydrationWarning
    >
      <head>
        <script src="/mode.js" />
      </head>
      <body>
        <ModeContextProvider
          variableName="__theme_mode"
          ssrInitialMode={theme?.value ?? "system"}
        >
          {children}
        </ModeContextProvider>
      </body>
    </html>
  );
}
  • Then you can use ModeContext in your app.
'use client';

import { ModeContext } from '@-ft/mode-next';

function ModeBar() {
  const { mode, setMode } = useContext(ModeContext);

  return (
    <Bar>
      Current theme mode is {mode}.
      <Select
        onChange={useCallback((mode) => setMode(mode), [setMode])}
      >
        {['system', 'light', 'dark']}
      </Select>
    </Bar>
  )
}
0.1.4

1 month ago

0.1.3

1 month ago

0.1.5

1 month ago

0.1.2

7 months ago

0.1.1

8 months ago

0.1.0

8 months ago

0.0.5

8 months ago

0.0.4

8 months ago

0.0.3

8 months ago

0.0.2

8 months ago

0.0.1

8 months ago