summaryrefslogtreecommitdiff
path: root/storybook/toggle-storybook.web.tsx
blob: f1bd034c42ca2e68814dbaf90277c77a649109c6 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import React, { useState, useEffect } from "react"
import * as QueryString from "query-string"

interface StorybookQueryParams {
  storybook?: boolean
}

export const ToggleStorybook = (props) => {
  const [StorybookUIRoot, setStorybookUIRoot] = useState<any>(null)
  const [queryParams, setQueryParams] = useState<StorybookQueryParams>({})

  useEffect(() => {
    if (__DEV__) {
      // Load the storybook UI once
      setStorybookUIRoot(() => require("./storybook").StorybookUIRoot)
    }
  }, [])

  useEffect(() => {
    if (__DEV__) {
      setQueryParams(QueryString.parse(window.location.search))
    }
  }, [window.location.search])

  if (queryParams?.storybook) {
    return StorybookUIRoot ? <StorybookUIRoot /> : null
  } else {
    return props.children
  }
}