@-ft/mode-react
TypeScript icon, indicating that this package has built-in type declarations

0.1.3 • Public • Published

mode-react - Theme mode manager wrapper for React

React wrapper of dark/light theme mode manager for web

Usage

  • Generate mode.js using @-ft/mode-codegen package.
{
  "variableName": "__theme_mode"
}
  • Load mode.js BEFORE load react app.
<!DOCTYPE html>
<html lang="en">
  <head>
    <!-- ./mode.js is generated by @-ft/mode-codegen package -->
    <script src="./mode.js"></script>
    <script type="module" src="./index.tsx"></script>
  </head>
</html>
  • Then wrap your app with ModeContextProvider
import { ModeContextProvider, ModeContext } from '@-ft/mode-react';

function Provider({ children }) {
  return (
    <ModeContextProvider variableName="__theme_mode">
      {children}
    </ModeContextProvider>
  );
}

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>
  )
}

Readme

Keywords

none

Package Sidebar

Install

npm i @-ft/mode-react

Weekly Downloads

13

Version

0.1.3

License

MIT

Unpacked Size

2.74 kB

Total Files

4

Last publish

Collaborators

  • mjy9088