From e5021187e96b78b53203bd95d08d6818aea47d17 Mon Sep 17 00:00:00 2001
From: "Thomas F. K. Jorna" <thomasfkjorna@gmail.com>
Date: Wed, 14 Jul 2021 15:10:31 +0200
Subject: New Ignite 7.0.6 app

---
 storybook/toggle-storybook.tsx | 51 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 storybook/toggle-storybook.tsx

(limited to 'storybook/toggle-storybook.tsx')

diff --git a/storybook/toggle-storybook.tsx b/storybook/toggle-storybook.tsx
new file mode 100644
index 0000000..394ecf4
--- /dev/null
+++ b/storybook/toggle-storybook.tsx
@@ -0,0 +1,51 @@
+import React, { useState, useEffect } from "react"
+import { DevSettings } from "react-native"
+import { loadString, saveString } from "../app/utils/storage"
+
+/**
+ * Toggle Storybook mode, in __DEV__ mode only.
+ *
+ * In non-__DEV__ mode, or when Storybook isn't toggled on,
+ * renders its children.
+ *
+ * The mode flag is persisted in async storage, which means it
+ * persists across reloads/restarts - this is handy when developing
+ * new components in Storybook.
+ */
+export function ToggleStorybook(props) {
+  const [showStorybook, setShowStorybook] = useState(false)
+  const [StorybookUIRoot, setStorybookUIRoot] = useState(null)
+
+  useEffect(() => {
+    if (__DEV__ && DevSettings) {
+      // Load the setting from storage if it's there
+      loadString("devStorybook").then((storedSetting) => {
+        // Set the initial value
+        setShowStorybook(storedSetting === "on")
+
+        // Add our toggle command to the menu
+        DevSettings.addMenuItem("Toggle Storybook", () => {
+          setShowStorybook((show) => {
+            // On toggle, flip the current value
+            show = !show
+
+            // Write it back to storage
+            saveString("devStorybook", show ? "on" : "off")
+
+            // Return it to change the local state
+            return show
+          })
+        })
+
+        // Load the storybook UI once
+        setStorybookUIRoot(() => require("./storybook").StorybookUIRoot)
+      })
+    }
+  }, [])
+
+  if (showStorybook) {
+    return StorybookUIRoot ? <StorybookUIRoot /> : null
+  } else {
+    return props.children
+  }
+}
-- 
cgit v1.2.3

