theme-tool.ts 588 B

123456789101112131415161718192021
  1. import { createElement } from "react"
  2. import { useGlobals } from "storybook/manager-api"
  3. import { ToggleButton } from "storybook/internal/components"
  4. export function ThemeTool() {
  5. const [globals, updateGlobals] = useGlobals()
  6. const mode = globals.theme === "dark" ? "dark" : "light"
  7. const toggle = () => {
  8. const next = mode === "dark" ? "light" : "dark"
  9. updateGlobals({ theme: next })
  10. }
  11. return createElement(
  12. ToggleButton,
  13. {
  14. title: "Toggle theme",
  15. active: mode === "dark",
  16. onClick: toggle,
  17. },
  18. mode === "dark" ? "Dark" : "Light",
  19. )
  20. }