<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Danny Williams on Medium]]></title>
        <description><![CDATA[Stories by Danny Williams on Medium]]></description>
        <link>https://medium.com/@dannyhyunsoowilliams?source=rss-9d1514410f13------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*UDHcL_FySucz0iBQwyK10g.jpeg</url>
            <title>Stories by Danny Williams on Medium</title>
            <link>https://medium.com/@dannyhyunsoowilliams?source=rss-9d1514410f13------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 15:21:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dannyhyunsoowilliams/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Dynamic imports supported in react native]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/dynamic-imports-supported-in-react-native-e839f6481eb2?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/e839f6481eb2</guid>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[expo]]></category>
            <category><![CDATA[expo-router]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Wed, 26 Jul 2023 13:32:35 GMT</pubDate>
            <atom:updated>2023-07-26T13:32:35.497Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/704/1*My5J7rasriUjrw6qr6pUkw.png" /><figcaption>example of require context</figcaption></figure><p>With the release of react native 0.72 and the latest metro changes we now have access to `require.context` which powers expo router and has potential to power other developer tooling or libraries.</p><p>This got me really excited because I can finally solve one of the biggest problems with react native storybook which is that you needed to generate imports for your stories. I wanted to talk a bit about what this new feature is and why I think its really cool.</p><p>Shout out to [<a href="http://twitter.com/Baconbrix">@Baconbrix</a>](<a href="https://twitter.com/Baconbrix">https://twitter.com/Baconbrix</a>) for contributing this feature to metro!</p><h3>What is require.context?</h3><p>From the <a href="https://webpack.js.org/guides/dependency-management/#requirecontext">webpack documentation</a>: <br>&gt; It allows you to pass in a directory to search, a flag indicating whether subdirectories should be searched too, and a regular expression to match files against.</p><p>Historically for react native we’ve never been able to do dynamic imports, neither based on a variable or a regex. However this kind of functionality has been in webpack and other web bundlers for some time. Now that we have access to require.context we can build tools like storybook and expo router without needing to generate imports at build time, we can instead dynamically import them at runtime.</p><p>Heres some examples:</p><pre>require.context(<br>  directory,<br>  (useSubdirectories = true),<br>  (regExp = /^\.\/.*$/),<br>  (mode = &#39;sync&#39;)<br>);</pre><pre>require.context(&#39;../&#39;, true, /\.stories\.js$/);</pre><p>The result of require context is a object like interface (RequireContext) that has two main functions.</p><ul><li>keys: returns and array of keys (ids) which will correspond to a file path<br>- An accessor `(id) =&gt; module` which given one of the keys returns the module</li></ul><p><em>note: the metro implementation of require context doesn’t include the `resolve` and `id` functionalities that webpack does.</em></p><p>For example</p><pre>const req = require.context(&quot;components&quot;, true, /\.stories\.tsx$/);<br><br>req.keys().forEach((filename: string) =&gt; {<br>   const module = req(filename)<br><br>   // you could then access the exported fields<br>   // if I had a default export like<br>   // export default { title: &quot;hello&quot; }<br>   console.log(module.default.title)<br>})</pre><p>For details on the implementation you can see Evan’s PR to metro <a href="https://github.com/facebook/metro/pull/822">here</a>.</p><h3>Why?</h3><p>The main driver for this feature seems to have been Expo router. The router is a file based routing library for react native and it will define your routes implicitly based on the file directory structure and the files you have. If you didn’t have dynamic imports then to implement this you would have to generate a file of static exports/config that the router would read. Thats also the exactly what we’ve been doing in react native storybook 6.5 before require context was introduced.</p><p>Now that we have require context, the router uses a require.context call with a clever regex and the routes can all be figured out at runtime. This also comes with the benefit that fast refresh and other metro features will work since its built in.</p><p>This is really exciting for me because now I can remove lots of code and improve the developer experience of react native storybook.</p><p>I also imagine that this opens the door to other developer tools that can take advantage of dynamic imports in other ways.</p><h3>Mini Storybook reimplementation</h3><p>To further illustrate my point I’ll show how easily you can now implement a simplified version of storybook in react native. Note that I simplified the code snippet slightly by removing imports etc (full repo linked at the end).</p><p>For context a story written using the CSF syntax looks like this:</p><pre>import MyComponent from &quot;./MyComponent&quot; <br><br>export default {<br>  title: &quot;MyCoolComponent&quot;<br>  component: MyComponent,<br>}<br><br>export const MyStory = {<br>  args: {<br>    text: &quot;hello&quot;<br>  }<br>}</pre><p>We’ll be dynamically importing any `.stories.tsx` file in our `components` folder and rendering the component with the props listed in its `args`.</p><p>Heres the code, make sure to look at the comments I added so you can follow whats going on.</p><pre>// get all story files<br>const req = require.context(&quot;components&quot;, true, /\.stories\.tsx$/);<br><br>// we create a map with id: story-title and value: module<br>let storiesMap = new Map&lt;StoryTitle, ModuleExports&gt;();<br><br>// for every module found lets add it to the storiesMap<br>req.keys().forEach((filename: string) =&gt; {<br>  try {<br>    // fileExports contains all exports, including default and named <br>    const fileExports = req(filename) as ModuleExports;<br> <br> // now we set an entry in the map like &#39;title&#39; =&gt; module<br>    storiesMap.set(fileExports.default.title, fileExports);<br>  } catch (e) {}<br>});<br><br><br>let firstModule = &quot;&quot;;<br><br>// get everything from our map as an array so we can access the first element<br>const entries = Array.from(storiesMap.entries());<br><br>if (entries.length) {<br>  const [title, exports] = entries[0];<br>  <br>  // each non &#39;default&#39; export is a story<br>  const namedExports = Object.keys(exports).filter((key) =&gt; key !== &quot;default&quot;);<br>  <br>  // we&#39;ll use title-storyname as an id <br>  firstModule = `${title}-${namedExports[0]}`;<br>}<br><br><br>const Content = () =&gt; {<br>  // we track the current componentId with this<br>  const [currentComponent, setCurrentComponent] = useState&lt;string&gt;(firstModule);<br><br>  const { Component, props } = useMemo(() =&gt; {<br>    if (currentComponent) {<br>      // we split our title-storyname id to get the key we need to access the map<br>      const [title, exportName] = currentComponent.split(&quot;-&quot;);<br>      <br>      // this gives us all the exports<br>      const exports = storiesMap.get(title);<br>      <br>      // get this current export (the story)<br>      const story = exports?.[exportName];<br>      <br>      return {<br>        // in csf the default export has a component field<br>        Component: exports?.default?.component,<br>        <br>        // a story&#39;s args represent our props<br>        props: story?.args,<br>      };<br>    }<br>    <br>    return {<br>      Component: null,<br>      props: {},<br>    };<br>    <br>  }, [currentComponent]);<br><br>  // we use gorhoms bottom sheet to display a list of stories<br>  const storiesBottomSheetModalRef = useRef&lt;BottomSheetModal&gt;(null);<br><br>  return (<br>    &lt;SafeAreaView style={{ flex: 1, padding: 16 }}&gt;<br>      {/* Render the current story with props from the args */}<br>      {Component &amp;&amp; &lt;Component {...props} /&gt;}<br><br>      {/* A button to open our story list */}<br>      &lt;Button<br>        text=&quot;Stories&quot;<br>        onPress={() =&gt; storiesBottomSheetModalRef.current?.present()}<br>        icon={icons.stories}<br>        style={{ flex: 1 }}<br>      /&gt;<br><br>   {/* A bottom sheet holding the list of stories */}<br>      &lt;BottomSheet stackBehavior=&quot;replace&quot; ref={storiesBottomSheetModalRef}&gt;<br>        &lt;StoryList<br>          setStory={setCurrentComponent}<br>          stories={Array.from(storiesMap.entries())}<br>          selectedStory={currentComponent}<br>        /&gt;<br>      &lt;/BottomSheet&gt;<br>    &lt;/SafeAreaView&gt;<br>  );<br>};</pre><p>If you’re curious then heres a repo with the full code <a href="https://github.com/dannyhw/sb-ui-experiment">https://github.com/dannyhw/sb-ui-experiment</a></p><p>Also heres the pr where I’m adding support for require.context to React Native Storybook <a href="https://github.com/storybookjs/react-native/pull/501">https://github.com/storybookjs/react-native/pull/501</a></p><p>So what do you think? What could you build now that dynamic imports are possible?</p><p>Thanks for reading!</p><p>Get in touch with me at: <a href="http://twitter.com/Danny_H_W">@Danny_H_</a>W<br>Check out my work on github: <a href="https://github.com/dannyhw">https://github.com/dannyhw</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e839f6481eb2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React Native and React Native Web Storybook together]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/react-native-and-react-native-web-storybook-together-53a18cf7d711?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/53a18cf7d711</guid>
            <category><![CDATA[react-native-web]]></category>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Tue, 04 Jul 2023 20:12:33 GMT</pubDate>
            <atom:updated>2023-07-05T15:49:41.016Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*QkhYR3-LXtHX_aTr.jpg" /></figure><p>Storybook for react native allows you to develop your components in isolation on their native platforms, but its not the easiest to share with stakeholders or team members. Running your components on the web using react native web can be a nice way to make your components more shareable and you can integrate with lots of web tooling built around storybook for the web. It’s possible to merge the setup for react native storybook together with the react native web storybook.</p><p>Its not been the easiest setup process for new users so I’ve made this quick guide for those with an existing project wanting to integrate @storybook/react-native and @storybook/addon-react-native-web in the same project.</p><p>If you are creating a fresh project it would be easier to start with one of the templates I’ve already created.</p><p>I’ll be starting with a basic expo template generated with yarn create expo-app</p><h3>Ondevice setup</h3><p>Now to add @storybook/react-native I’ll run the following command:<br>npx sb@latest init --type react_native</p><p>Then in my App.js file I’ll make some changes to import the storybook ui from .storybook and export it as the the entry point of the app.</p><pre><br>// App.js<br>import StorybookUI from “./.storybook”;<br><br>export default StorybookUI;</pre><p>Now I’m going to customize metro config to complete the setup</p><p>This command will generate the config file: npx expo customize metro.config.js</p><pre>// metro.config.js<br>const { getDefaultConfig } = require(&quot;expo/metro-config&quot;);<br><br>const config = getDefaultConfig(__dirname);<br><br>config.resolver.resolverMainFields.unshift(&quot;sbmodern&quot;);<br><br>module.exports = config;</pre><p>Add a folder called components and lets add a basic component called Button</p><pre>// components/Button.js<br>import { Text, TouchableOpacity } from &quot;react-native&quot;;<br><br>export const Button = ({ onPress, text }) =&gt; {<br>  return (<br>    &lt;TouchableOpacity<br>      onPress={onPress}<br>      style={{<br>        borderColor: &quot;lightgrey&quot;,<br>        borderWidth: 1,<br>        borderRadius: 2,<br>        paddingVertical: 8,<br>        paddingHorizontal: 32,<br>        alignSelf: &quot;flex-start&quot;,<br>      }}<br>    &gt;<br>      &lt;Text&gt;{text}&lt;/Text&gt;<br>    &lt;/TouchableOpacity&gt;<br>  );<br>};</pre><pre>// components/Button.stories.js<br>import { Button } from &quot;./Button&quot;;<br><br>export default {<br>  title: &quot;Button&quot;,<br>  component: Button,<br>  argTypes: {<br>    onPress: { action: &quot;onPress&quot; },<br>  },<br>};<br><br>export const Base = {<br>  args: { text: &quot;Hello World&quot; },<br>};</pre><p>lets update the main.js config file to look for stories in this folder</p><pre>// .storybook/main.js<br>module.exports = {<br>  stories: [&quot;../components/**/*.stories.?(ts|tsx|js|jsx)&quot;],<br>  addons: [<br>    &quot;@storybook/addon-ondevice-controls&quot;,<br>    &quot;@storybook/addon-ondevice-actions&quot;,<br>  ],<br>};</pre><p>Lets also update the preview.js to add some padding to our stories</p><pre>// .storybook/preview.js<br>import { View } from &quot;react-native&quot;;<br><br>export const parameters = {};<br><br>export const decorators = [<br>  (Story) =&gt; (<br>    &lt;View style={{ padding: 8 }}&gt;<br>      &lt;Story /&gt;<br>    &lt;/View&gt;<br>  ),<br>];</pre><p>Next I’ll run storybook-generate to make sure the storybook.requires.js file has been generated.</p><pre>yarn storybook-generate</pre><p>This file is generated based on your config and handles importing all the necessary story files.</p><p>This concludes the on-device part of the setup. You should be able to run yarn ios or yarn android and see storybook render your button story.</p><figure><img alt="storyboook running on an ios device" src="https://cdn-images-1.medium.com/max/1024/1*Or-IJUBZkrIB4TbRUKXOvg.jpeg" /></figure><h3>Web setup</h3><p>The best react-native-web Storybook experience will come from using @storybook/addon-react-native-web on top of a ReactJS storybook setup. This is a slightly more complex setup because it requires you to have two sets of storybook config and scripts. However those tradeoffs give you the ability to use all the features of web storybook and tooling that comes with it.</p><p>Since React Native Storybook is currently at compatibility with 6.5 we’ll be using that version for both web and on-device.</p><p>First install the dependencies</p><pre>yarn add -D @storybook/addon-react-native-web @storybook/addon-essentials@^6.5 @storybook/builder-webpack5@^6.5 @storybook/manager-webpack5@^6.5 @storybook/react@^6.5 babel-plugin-react-native-web react-native-web @babel/plugin-proposal-export-namespace-from</pre><p>Now add the scripts to package.json</p><pre>&quot;storybook:web&quot;: &quot;start-storybook -p 6006 -c .storybook-web&quot;,<br>&quot;build-storybook&quot;: &quot;build-storybook -c .storybook-web&quot;</pre><p>Create a new folder called .storybook-web and add the files main.js and preview.js</p><pre>// ./storybook-web/main.js<br>module.exports = {<br>  stories: [<br>    &quot;../components/**/*.stories.mdx&quot;,<br>    &quot;../components/**/*.stories.@(js|jsx|ts|tsx)&quot;,<br>  ],<br>  addons: [<br>    &quot;@storybook/addon-essentials&quot;,<br>    &quot;@storybook/addon-react-native-web&quot;,<br>  ],<br>  core: {<br>    builder: &quot;webpack5&quot;,<br>  },<br>  framework: &quot;@storybook/react&quot;,<br>};</pre><pre>// ./storybook-web/preview.js<br>import { View } from &quot;react-native&quot;;<br><br>export const parameters = {<br>  actions: { argTypesRegex: &quot;^on[A-Z].*&quot; },<br>  controls: {<br>    matchers: {<br>      color: /(background|color)$/i,<br>      date: /Date$/,<br>    },<br>  },<br>};<br><br>export const decorators = [<br>  /* by wrapping everything in a view the parent container is always using flexbox and behaves like a react-native-web component would  */<br>  (Story) =&gt; (<br>    &lt;View&gt;<br>      &lt;Story /&gt;<br>    &lt;/View&gt;<br>  ),<br>];</pre><p>Now if you run yarn storybook:web you should see something like this.</p><figure><img alt="storybook running on both web and ios" src="https://cdn-images-1.medium.com/max/1024/1*PSV4wG-5H_DjtbKmihBe3A.jpeg" /></figure><p>Likely this won’t be the end of the configuring and setup you’ll need to do but this should get you most of the way.</p><p>Thanks for reading.</p><p>I’m on twitter at <a href="https://twitter.com/Danny_H_W">@Danny_H_W</a> and github at <a href="https://github.com/dannyhw">dannyhw</a></p><p><em>Originally published at </em><a href="https://dev.to/dannyhw/react-native-and-react-native-web-storybook-together-261g"><em>https://dev.to</em></a><em> on July 4, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=53a18cf7d711" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Storybook for React Native (6.5)]]></title>
            <link>https://medium.com/storybookjs/storybook-for-react-native-6-5-454b6e1e3778?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/454b6e1e3778</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Wed, 29 Mar 2023 14:31:09 GMT</pubDate>
            <atom:updated>2023-03-29T17:49:17.318Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tYgaa9f_F0dKKz2XfKMJ0w.jpeg" /></figure><h4>Perf improvements, support for Component Story Format, controls, and args. Plus new configuration files format to align with Storybook core.</h4><p>React Native Storybook is a specialized version of Storybook that focuses on creating and organizing stories for components made with React Native rather than HTML and CSS. Unlike the core Storybook, React Native Storybook runs stories on a native emulator or on a physical mobile device instead of a browser. Over time, this divergence has led React Native Storybook to lag behind updates made to the browser version.</p><p>I am excited to announce a <strong>new version of React Native Storybook,</strong> now compatible with Storybook 6.5.14 and beyond. This iteration streamlines the codebase, incorporates more core Storybook packages, and offers an improved developer experience.</p><ul><li>📝 Component Story Format support</li><li>🎛️ Controls and Args support (including experimental auto-generated controls)</li><li>📜 Aligned config with core format (main.js and preview.js files)</li><li>⚡ Performance improvements</li><li>🎨 On-device UI improvements</li></ul><h3>Why version 6.5?</h3><p>To make React Native Storybook work with Storybook 6.5, some changes were needed in Storybook itself. As a result, React Native Storybook is only compatible with Storybook 6.5.14 and later versions. This trade-off will make it easier to integrate React Native Storybook with future versions of Storybook, including <a href="https://storybook.js.org/releases/7.0/?ref=storybookblog.ghost.io">Storybook 7.0</a>.</p><p>This release is the result of many weekends and late nights over multiple years. It is great to finally share it with all of you. I would like to thank everyone in the community for their patience, as well as the contributors who have helped make this possible. Additionally, I extend my thanks to the many Storybook maintainers who paired with me to get through the most complicated parts of the update.</p><p>Let’s dive into the updates!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*Y8-YKbQTuEwNg55OAQB7Fg.gif" /></figure><h3>Component Story Format (CSF) support</h3><p>CSF is a simpler and more powerful syntax for Storybook that makes it easier to read, write and maintain stories. It’s based on ES6 modules using named exports to define stories and a default export to define metadata. CSF is now supported by React Native Storybook.</p><p>With CSF, stories are written as plain JavaScript objects. Component props are specified using <a href="https://storybook.js.org/blog/introducing-storybook-args/?ref=storybookblog.ghost.io">“Args”</a>, which is Storybook’s mechanism for setting default values for props and children.</p><p>We offer a <a href="https://storybook.js.org/docs/7.0/react/migration-guide?ref=storybookblog.ghost.io#storiesof-to-csf">codemod</a> to make this migration easier (see the migration guide below).</p><pre>import Button from &#39;./Button&#39;;<br><br>// The default export has metadata for your component story<br>export default {<br>   title: &quot;Button&quot;<br>   component: Button<br>};<br>// An individual story with some default props<br>export const Base = {<br>  args: {<br>    // default value for the &#39;text&#39; property of this component<br>    text: &quot;Hello World&quot;<br>  }<br>};</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mPlBbyFUJ7XyvSj9EG-Aow.gif" /></figure><h3>Use Controls to manipulate props using graphical UI</h3><p>Controls allow you to interact with components’ args without needing to write code. Storybook automatically generates Controls from components’ PropTypes and TypeScript types. Like the old <a href="https://github.com/storybookjs/addon-knobs?ref=storybookblog.ghost.io">Knobs addon</a>, you can use an addon panel to edit arguments in real time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/928/1*_kTtdxQsTrQtZ68JFRiF7w.gif" /></figure><p>By default, Storybook selects input controls (such as text, radio, select, and range) based on the arg’s type. You can choose which type of control to use for each arg by configuring <a href="https://storybook.js.org/docs/react/api/argtypes?ref=storybookblog.ghost.io">argTypes</a>. Check out the Controls <a href="https://storybook.js.org/docs/react/essentials/controls?ref=storybookblog.ghost.io">documentation</a> to learn more.</p><pre>import Button from &quot;./Button&quot;;<br><br>// The default export has metadata for your component story<br>export default {<br>  title: &quot;Button&quot;,<br>  component: Button,<br><br>  // You can be more specfic about the type you want by using argTypes<br>  argTypes: {<br>    variant: {<br>      options: [&quot;primary&quot;, &quot;secondary&quot;],<br>      control: { type: &quot;radio&quot; },<br>    },<br>  },<br>};<br><br>// An individual story with some default props<br>export const Primary = {<br>  args: {<br>    // default value for the &#39;text&#39; property of this component<br>    text: &quot;Hello World&quot;,<br>    variant: &quot;primary&quot;,<br>  },<br>};</pre><h3>Aligned configuration format with Storybook core</h3><p>One of the biggest changes to core Storybook since React Native diverged is the switch to a more declarative configuration format using main.js and preview.js files. To align with core Storybook, React Native Storybook now also uses this format.</p><p>In the main.js file, provide a glob in the stories field and a list of addons. Storybook then handles the rest by generating a file called storybook.requires.js. The preview.js file is where you define global decorators and parameters.</p><p>More on the semantics in the upgrade guide below. Keep reading to see some examples.</p><h3>Get started</h3><p>If you’re starting a new project, run this in a React Native project to automatically generate the config you need.</p><pre>npx sb@next init --type react_native</pre><p>If you prefer, you can also use one of these templates.</p><pre># Expo<br>npx create-expo-app --template expo-template-storybook AwesomeStorybook<br><br># React Native CLI<br>npx react-native init MyApp --template react-native-template-storybook</pre><p>If you’re new to React Native Storybook, check out our hands-on tutorial to learn all the basics: <a href="https://storybook.js.org/tutorials/intro-to-storybook/react-native/en/get-started/">https://storybook.js.org/tutorials/intro-to-storybook/react-native/en/get-started/</a></p><h3>How to upgrade</h3><h3>Step 1: Update your index file</h3><p>Remove the configure call, story imports and addon imports from your index.js file, and reduce the file to this. The most important thing is to import the storybook.requires file.</p><pre>// .storybook/index.js<br>import { getStorybookUI } from &#39;@storybook/react-native&#39;;<br>import &#39;./storybook.requires&#39;;</pre><pre>const StorybookUIRoot = getStorybookUI({<br>  // options go here<br>});</pre><pre>export default StorybookUI</pre><p>Additionally, if your Storybook folder is called storybook, you should change it to .storybook</p><h3>Step 2: Add new configuration files</h3><p>In your .storybook folder, add the main.js and preview.js files.</p><p>In the stories field of your main.js file, update the glob according to your project setup.</p><p>In the addons field, list the addons you want to use. These must be compatible with React Native Storybook. Addons made for React Native are usually prefixed with addon-ondevice</p><pre>// .storybook/main.js<br>module.exports = {<br>  stories: [<br>    &#39;../components/**/*.stories.?(ts|tsx|js|jsx)&#39;,<br>  ],<br>  addons: [<br>    &#39;@storybook/addon-ondevice-controls&#39;.<br>    &#39;@storybook/addon-ondevice-actions&#39;,<br>    &#39;@storybook/addon-ondevice-backgrounds&#39;,<br>  ],<br>};</pre><p>Move any global decorators and parameters you have to preview.js. If you don’t have any, simply export an empty array for decorators and an empty object for parameters.</p><pre>// .storybook/preview.js<br>import { View } from &#39;react-native&#39;;<br><br>export const decorators = [<br>  // Using a decorator to apply padding for every story<br>  (StoryFn) =&gt; (<br>    &lt;View style={{flex:1, padding:8}}&gt;<br>      &lt;StoryFn /&gt;<br>    &lt;/View&gt;<br>  ),<br>];<br><br>export const parameters = {<br>  my_param: &#39;anything&#39;,<br>};</pre><h3>Step 3: Convert your stories to CSF</h3><p>Whilst storiesOf will still work, it is now deprecated and we recommend that you convert your stories to CSF. There is a codemod for your convenience which should automatically migrate your files to CSF (make sure to update the glob to fit your files):</p><pre>npx storybook@next migrate storiesof-to-csf --glob=&quot;src/**/*.stories.js&quot;</pre><p>Let’s look at an example. We’ll use this Button.stories.js file:</p><pre>// Button.stories.js<br>// Before<br>storiesOf(&#39;Button&#39;, module)<br>  .addParameters({ myParam: &quot;anything&quot; })<br>  .addDecorator((Story)=&gt; &lt;Wrapper&gt;{Story}&lt;/Wrapper&gt;)<br>  .add(&#39;primary&#39;, () =&gt; &lt;Button primary label=&quot;Button&quot; /&gt;)<br>  .add(&#39;secondary&#39;, () =&gt; &lt;Button label=&quot;Button&quot; /&gt;);</pre><p>The codemod will convert this to CSF, like so:</p><pre>// Button.stories.js (CSF1)<br>export default {<br>  title: &#39;Button&#39;,<br>  decorators: [(Story) =&gt; &lt;Wrapper&gt;{Story}&lt;/Wrapper&gt;],<br>  parameters: {<br>    myParam: &#39;anything&#39;,<br>  },<br>};<br><br>export const Primary = () =&gt; &lt;Button primary label=&quot;Button&quot; /&gt;;<br>Primary.story = {<br>  name: &#39;primary&#39;,<br>};<br>export const Secondary = () =&gt; &lt;Button label=&quot;Button&quot; /&gt;;<br>Secondary.story = {<br>  name: &#39;secondary&#39;,<br>};</pre><p>CSF is designed to be backwards compatible, which has allowed the Storybook team to make continuous improvements. The latest story format, CSF3, includes several ergonomic improvements. Refer to the <a href="https://storybook.js.org/blog/storybook-csf3-is-here/?ref=storybookblog.ghost.io">release announcement</a> for a full list of features and benefits.</p><p>Lastly, we can manually update the file to CSF3 format, like so:</p><pre>// Button.stories.js (CSF3)<br>export default {<br>  title: &#39;Button&#39;,<br>  decorators: [(Story) =&gt; &lt;Wrapper&gt;{Story}&lt;/Wrapper&gt;],<br>  parameters: {<br>    myParam: &#39;anything&#39;,<br>  },<br>};<br><br>export const Primary = {<br>  args: {<br>    primary: true,<br>    label: &#39;Button&#39;,<br>  },<br>};<br>export const Secondary = {<br>  args: {<br>    label: &#39;Button&#39;,<br>  },<br>};</pre><p>Note, if you’re using the Knobs addon, consider <a href="https://github.com/storybookjs/storybook/tree/next/code/addons/controls?ref=storybookblog.ghost.io#how-do-i-migrate-from-addon-knobs">migrating to Controls</a>.</p><h3>Optional: Server configuration</h3><p>The server is a package called @storybook/react-native-server. It’s an optional companion app that lets you control the React Native Storybook UI on your device via a web UI.</p><p>In this version, configuring the server has slightly changed. To configure the server, you need to add a .storybook_server folder with a main.js file. In this file, you should add the same stories glob that you have in your .storybook/main.js file.</p><pre>module.exports = {<br>  stories: [<br>    &#39;../components/**/*.stories.?(ts|tsx|js|jsx)&#39;,<br>  ],<br>  env: () =&gt; ({}),<br>  addons: [&#39;@storybook/addon-essentials&#39;],<br>};</pre><p>Then, in package.json, you&#39;ll need a new script:</p><pre>&quot;start-server&quot;: &quot;react-native-storybook-server&quot;</pre><p>For more information about this server setup, please read this <a href="https://dev.to/dannyhw/quick-guide-for-storybookreact-native-server-v6-4nl2?ref=storybookblog.ghost.io">blog post</a>.</p><p><strong>Migration questions?<br></strong>For more details on this upgrade, please refer to the <a href="https://github.com/storybookjs/react-native/blob/next-6.0/MIGRATION.md?ref=storybookblog.ghost.io">migration guide</a>. If you encounter any difficulties, you can raise an issue on GitHub or visit the <a href="https://discord.gg/storybook?ref=storybookblog.ghost.io">Storybook discord</a>.</p><h3>What’s next?</h3><p>React Native Storybook 6.5 brings with it some big improvements, but we’re not done yet. Here’s what you can expect from React Native Storybook.</p><p><strong>Stabilize version 6.5:</strong> This is our top priority at the moment. To help us achieve this goal, <a href="https://github.com/storybookjs/react-native/issues/new/choose?ref=storybookblog.ghost.io">please report any bugs and share your feedback with us on the React Native Storybook GitHub</a>.</p><p><strong>Support Storybook 7.0:</strong> With the release of version 6.5, much of the groundwork has already been laid for compatibility with Storybook version 7. Work on this will begin once version 6.5 is stabilized.</p><h3>Future Improvements</h3><p>There are many directions we could take for future updates, and here are some ideas I have for improvements.</p><ul><li>Support story categorization for more organization in the story list sidebar</li><li>Use of <a href="https://facebook.github.io/metro/?ref=storybookblog.ghost.io">metro’s</a> experimental require.context to simplify story imports</li><li>Better testing support, including potentially supporting <a href="https://storybook.js.org/docs/react/writing-tests/interaction-testing?ref=storybookblog.ghost.io">interaction tests</a> with the play function</li><li>Better integration with <a href="https://storybook.js.org/addons/@storybook/addon-react-native-web?ref=storybookblog.ghost.io">@storybook/addon-react-native-web</a></li></ul><p>There’s a lot more that we could do, and we’d love to hear your thoughts on what to focus on next.</p><h3>Final thoughts</h3><p>React Native Storybook is maintained by me, <a href="https://twitter.com/Danny_H_W?ref=storybookblog.ghost.io">Daniel Williams</a>. This update brings a lot of improvements for React Native Storybook and closes the gap to the latest versions of Storybook.</p><p>There is still a lot that can be improved and version 7 is definitely on my radar. If you are interested in this project and would like to help, please reach out to me, and I will be happy to assist you in getting started. Additionally, you can visit the <a href="https://github.com/storybookjs/react-native?ref=storybookblog.ghost.io">GitHub repository</a> to create issues, open pull requests and start discussions.</p><p>You can find me on Twitter (<a href="https://twitter.com/Danny_H_W?ref=storybookblog.ghost.io">@Danny_H_W</a>) or on Github (<a href="https://github.com/dannyhw?ref=storybookblog.ghost.io">dannyhw</a>). If you want to support my work, please consider sponsoring me via <a href="https://github.com/sponsors/dannyhw/?ref=storybookblog.ghost.io">GitHub Sponsors</a>. Thanks for reading, I look forward to your feedback.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=454b6e1e3778" width="1" height="1" alt=""><hr><p><a href="https://medium.com/storybookjs/storybook-for-react-native-6-5-454b6e1e3778">Storybook for React Native (6.5)</a> was originally published in <a href="https://medium.com/storybookjs">Storybook</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use React Native Storybook Server with webpack 5]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/how-to-use-react-native-storybook-server-with-webpack-5-fc7aa88cb9d4?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc7aa88cb9d4</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[guides-and-tutorials]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Sat, 18 Mar 2023 18:09:21 GMT</pubDate>
            <atom:updated>2023-03-18T18:27:22.407Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FTA1askkvWWc%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DTA1askkvWWc&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FTA1askkvWWc%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/74a0dec8dcfc47b37fe57ad94541e3c6/href">https://medium.com/media/74a0dec8dcfc47b37fe57ad94541e3c6/href</a></iframe><p>This is a quick guide for the prerelease version (6.5.0-rc.6) with a stable release coming out soon for React Native Storybook 6.5.</p><p>The React Native Storybook Server is an optional companion app for react native storybook that lets you control the storybook on your mobile device via a web interface.<br> Until recently webpack 5 was not supported in the react native storybook server. Recently that support was added by a contributor <a href="https://github.com/leonardo-fc">@leonardo-fc</a> and is now available in the release candidate version of the server.</p><h3>The Guide</h3><p>First bootstrap an app with react native storybook</p><pre>yarn create expo-app --template expo-template-storybook AwesomeStorybook</pre><p>Go to the directory of the new app</p><pre>cd AwesomeStorybook</pre><p>Add the react native storybook server package and some webpack5 dependencies to your project</p><pre>yarn add -D @storybook/react-native-server@next webpack @storybook/builder-webpack5 @storybook/manager-webpack5</pre><p>Make sure you add all of these packages otherwise it won’t work.</p><p>Add the following script to your package.json file</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;start-server&quot;: &quot;react-native-storybook-server&quot;<br>  }<br>}</pre><p>Create a folder called .storybook_server and add a main.js file with the following content</p><pre>module.exports = {<br>  stories: [<br>    &quot;../components/**/*.stories.mdx&quot;,<br>    &quot;../components/**/*.stories.@(js|jsx|ts|tsx)&quot;,<br>  ],<br>  env: () =&gt; ({}),<br>  core: {<br>    builder: &quot;webpack5&quot;,<br>  },<br>  addons: [&quot;@storybook/addon-essentials&quot;],<br>};</pre><p>By setting the builder to webpack5 we are telling the storybook server to use webpack 5 instead of the default webpack 4.</p><p>In this bootstrapped project the ondevice storybook is configured in the .ondevice folder.</p><p>In the index.tsx file we need to update some options so that websockets are enabled.</p><pre>const StorybookUIRoot = getStorybookUI({<br>  enableWebsockets: true,<br>});</pre><p>Now you can start the server with yarn start-server and open the storybook on your mobile device with yarn storybook.</p><p>Make sure you restart the app on your device so that the server can pick up the stories sent over the web-sockets (sent on app startup).</p><p>At this point you should be running the react native server with webpack 5.</p><p>Thanks for reading. If you have any questions or feedback please tweet or send me a message <a href="https://twitter.com/Danny_H_W">https://twitter.com/Danny_H_W</a>.</p><p><em>Originally published at </em><a href="https://dev.to/dannyhw/how-to-use-react-native-storybook-server-with-webpack-5-2g16"><em>https://dev.to</em></a><em> on March 18, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc7aa88cb9d4" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiple entry points for React Native Storybook]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/multiple-entry-points-for-react-native-storybook-1c9143f841e5?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/1c9143f841e5</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[storybook]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Sat, 04 Mar 2023 16:25:31 GMT</pubDate>
            <atom:updated>2023-03-04T16:25:31.698Z</atom:updated>
            <content:encoded><![CDATA[<h3>Intro</h3><p>React Native storybook is setup in such a way that the decision is left to the user how it should be loaded. This gives you flexibility on how you do things, however I often get asked how to easily switch between running application code and Storybook.</p><h3>An alternative approach</h3><p>For a while now I’ve been thinking about an easy way to do this by using metro directly.</p><p>This will be a quick guide on how to setup a secondary entry point for React Native</p><p>At the end you’ll be able to run yarn storybook-metro when you want Storybook and yarn start when you want to run your app.</p><p>The key to this solution looks like this:</p><pre>react-native start - config ./.ondevice/metro.storybook.js</pre><p>This solution works with React Native cli and there are other ways to do it with expo which will be covered in a different post.</p><h3>The setup</h3><p>I’ll be working from the <a href="https://github.com/dannyhw/react-native-template-storybook">react native storybook template</a> based on the react native typescript template. However your own setup should work too if you adjust it in the same way.</p><p>To use it you can run</p><pre>npx react-native init MyApp --template react-native-template-storybook</pre><p>At the time of writing when you use this template your directory structure will look something like this</p><pre>.ondevice/<br>  Storybook.tsx<br>  main.js<br>  ...<br>components/<br>index<br>App.tsx<br>...</pre><p>The files in .ondevice are related to React Native storybook and currently the App.tsx file imports the default export from .ondevice/Storybook.tsx and renders that as the Application.</p><p>What if you want to write an application here and run storybook separately, what do you do?</p><p>You might just comment out the storybook import when you aren’t using it, or you might add some kind of conditional to load storybook when an environment variable is set.</p><p>There is another way, you can actually use metro to do all the work for you.</p><h3>Creating a second entry point</h3><p>First I’m going to rename .ondevice/Storybook.tsx to .ondevice/index.js and replace the content with this:</p><pre>import { AppRegistry } from &quot;react-native&quot;;<br>import { getStorybookUI } from &quot;@storybook/react-native&quot;;<br>import { name as appName } from &quot;../app.json&quot;;<br>import &quot;./storybook.requires&quot;;<br><br>const StorybookUIRoot = getStorybookUI({});<br><br>AppRegistry.registerComponent(appName, () =&gt; StorybookUIRoot);</pre><p>This will act as the secondary entry point for Storybook and when used registers the Storybook UI as the Application to render just like before.</p><p>Now in App.tsx lets just remove all storybook related code and make it look like this</p><pre>import React from &quot;react&quot;;<br>import { SafeAreaView, Text } from &quot;react-native&quot;;<br>export default () =&gt; {<br>  return (<br>    &lt;SafeAreaView&gt;<br>      &lt;Text&gt;Not Storybook!&lt;/Text&gt;<br>    &lt;/SafeAreaView&gt;<br>  );<br>};</pre><p>This App file gets loaded by index in the root of the project which is the original entry point. This component will be registered as the app when that entry point is used.</p><p>We have two entry-points .ondevice/index.js and index.js. Now we just need a way to swap between them.</p><p>Create a file in the .ondevice/ folder called metro.storybook.js with the following content:</p><pre>const path = require(&quot;path&quot;);<br>module.exports = {<br>  transformer: {<br>    getTransformOptions: async () =&gt; ({<br>      transform: {<br>        experimentalImportSupport: false,<br>        inlineRequires: false,<br>      },<br>    }),<br>  },<br>  resolver: {<br>    resolverMainFields: [&quot;sbmodern&quot;, &quot;react-native&quot;, &quot;browser&quot;, &quot;main&quot;],<br>  },<br>  watchFolders: [path.resolve(__dirname, &quot;..&quot;)],<br>};</pre><p>The important thing here is the watchFolders config so that metro knows to look for files in the root of the project.</p><p>We’re also using the way metro works to our advantage, by default it will look for an index file relative to the projectRoot. Since we didn’t set the root it will assume that the current directory is the project root.</p><p>We’re now mostly done, all thats left is to tell metro which config file to load.</p><h3>The final pieces</h3><p>Now we just setup some scripts in package json to make our life easier</p><p>The template we used gives us these commands</p><pre>&quot;scripts&quot;: {<br> ...<br>    &quot;start&quot;: &quot;react-native start&quot;,<br>    &quot;prestart&quot;: &quot;yarn update-stories&quot;,<br>    ...<br>},</pre><p>The prestart script uses the a prefix to tell yarn to always run this script before the start script.</p><p>Now that we’ve updated our setup we’ll be using start specifically for our application code so we’ll adjust this.</p><p>Let’s add a new script for starting metro from our new storybook config file.</p><pre>&quot;storybook-metro&quot;: &quot;react-native start --config ./.ondevice/metro.storybook.js&quot;,</pre><p>Here we use the — config option to tell metro to use our storybook specific config.</p><p>Now rename the prestart script to look like this:</p><pre>&quot;prestorybook-metro&quot;: &quot;yarn update-stories&quot;,</pre><p>Now the update-stories script only runs when we need it (when running storybook).</p><h3>Running storybook</h3><p>Whenever you want to run storybook you run yarn storybook-metro in one terminal followed by running yarn ios or yarn android in another terminal.</p><p>To go back to your app you can just stop storybook metro and run yarn start instead. This will start metro with config from metro.config.js and all you have to do is reload the app to get the application javascript bundle.</p><p>Note that since only the bundle changes you won’t need to rebuild the entire app.</p><h3>Closing thoughts</h3><p>As you can see it’s possible to separate your storybook code from your application code with just the built in behaviour of metro and React Native.</p><p>A great benefit of this is that you can easily leave storybook code outside of your application code since the entry points are completely separate.</p><p>This setup could be the default for storybook however a solution is still needed for expo.</p><p>In this guide I’m using the React Native Storybook v6 beta which is currently under development however you can follow a similar approach for v5.3 also.</p><h3>Contact me</h3><p>If you want to get in touch you can contact me on twitter or the storybook discord</p><p><a href="https://twitter.com/Danny_H_W">JavaScript is not available.</a></p><ul><li>on <a href="https://github.com/dannyhw">github</a></li></ul><p>Storybook discord: <em>https://discord.gg/sMFvFsG</em></p><p><em>Originally published at </em><a href="https://www.dannyhwilliams.co.uk/multiple-entry-points-for-react-native-storybook"><em>https://www.dannyhwilliams.co.uk</em></a><em>.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1c9143f841e5" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to use React Native Web with Storybook v7 beta]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/how-to-use-react-native-web-with-storybook-v7-beta-ecc6df18fea7?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecc6df18fea7</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[storybook]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Thu, 16 Feb 2023 14:46:14 GMT</pubDate>
            <atom:updated>2023-03-04T16:46:00.596Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*esqQb7pBihq05Qwn.png" /></figure><p>You may have heard that Storybook is getting close to a v7 release. If you’re using addon-react-native-web you might be wondering how to try out the beta. In this quick guide I will show you how to setup a new project and add @storybook/addon-react-native-web Storybook to the project.<br> This enables you to use the web Storybook with your react native components.</p><p>If you already have this setup in an existing project it should be pretty straight forward to get updated. Change the version of @storybook/addon-react-native-web to next and update all your other relevant storybook dependencies to the latest v7 beta version (you can use next here too). Then you should check this <a href="https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#from-version-65x-to-700">migration guide</a> to see if you need to update anything to get your project ready for v7.</p><p>To setup a fresh new project you can follow this guide.</p><p>First I’ll generate a project.</p><pre>yarn create expo-app</pre><p>Next I’ll open up the project and in the root of the project run</p><pre>npx sb@next init --type react</pre><p>After its done add @storybook/addon-react-native-web@next and the dependencies for it.</p><pre>yarn add --dev react-dom react-native-web babel-plugin-react-native-web @storybook/addon-react-native-web@next</pre><p>Add the addon to .storybook/main.js as follows:</p><pre>const config = {<br>  /* existing config */<br>  addons: [/*existing addons,*/ &quot;@storybook/addon-react-native-web&quot;],<br>  /* more config here */<br>};<br>export default config;</pre><p>If you’re familiar with the Storybook config you’ll notice there are some slight differences in the main.js file. One cool new feature is that you can now get benefits of autocomplete even if you aren&#39;t using typescript for your config files. This change comes from the comment above the config object that looks like this</p><pre>/** @type { import(&#39;@storybook/react-webpack5&#39;).StorybookConfig } */<br>const config = {</pre><p>Now that the setup is done, let’s add a React Native component and a story in the stories folder.</p><p>You may want to delete the ReactJS example components. To do that delete everything inside of the stories folder.</p><pre>rm -rf stories/*</pre><p>Then in that folder you can add this example button component.</p><p>Inside <em>`stories/`</em> create <em>`Button.jsx`</em></p><pre>// stories/Button.jsx<br>import React from &quot;react&quot;;<br>import { StyleSheet, Text, TouchableOpacity, View } from &quot;react-native&quot;;<br><br>const styles = StyleSheet.create({<br>  button: {<br>    paddingVertical: 8,<br>    paddingHorizontal: 16,<br>    borderRadius: 4,<br>    alignSelf: &quot;flex-start&quot;,<br>    flexGrow: 0,<br>    backgroundColor: &quot;purple&quot;,<br>  },<br>  buttonText: {<br>    color: &quot;white&quot;,<br>    fontSize: 16,<br>    fontWeight: &quot;bold&quot;,<br>  },<br>  buttonContainer: {<br>    alignItems: &quot;flex-start&quot;,<br>    flex: 1,<br>  },<br>});<br><br>export const Button = ({ text, onPress, color, textColor }) =&gt; (<br>  &lt;View style={styles.buttonContainer}&gt;<br>    &lt;TouchableOpacity<br>      style={[styles.button, !!color &amp;&amp; { backgroundColor: color }]}<br>      onPress={onPress}<br>      activeOpacity={0.8}<br>    &gt;<br>      &lt;Text style={[styles.buttonText, !!textColor &amp;&amp; { color: textColor }]}&gt;<br>        {text}<br>      &lt;/Text&gt;<br>    &lt;/TouchableOpacity&gt;<br>  &lt;/View&gt;<br>);</pre><p>Now add the files Button.stories.jsx in the stories folder with this content:</p><pre>// stories/Button.stories.jsx<br>import { Button } from &quot;./Button&quot;;<br><br>export default {<br>  component: Button,<br>};<br><br>export const Basic = {<br>  args: {<br>    text: &quot;Hello World&quot;,<br>    color: &quot;purple&quot;,<br>  },<br>};</pre><p>One of the really cool updates in V7 is the introduction of CSF3 which you can see above. There is now much less boilerplate needed to write a story!</p><p>At this point all thats left to do is run yarn storybook and get to work building your components!</p><p>Thanks for reading.</p><p>Follow me on twitter <a href="https://twitter.com/Danny_H_W">@Danny_H_W</a></p><p><em>Originally published at </em><a href="https://dev.to/dannyhw/how-to-use-react-native-web-with-storybook-v7-beta-1io7"><em>https://dev.to</em></a><em> on February 16, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecc6df18fea7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to swap between React Native Storybook and your app]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/how-to-swap-between-react-native-storybook-and-your-app-77ebdd924935?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/77ebdd924935</guid>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Mon, 06 Feb 2023 12:14:16 GMT</pubDate>
            <atom:updated>2023-03-04T16:40:29.104Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*STf2BHl3UM9DJ6K3.png" /></figure><p><em>EDIT: I have recently updated this guide to show a few different approaches I will update further if I find better solutions</em></p><p>Storybook in react native is a component that you can slot into your app and isn’t its own separate runtime which you might be used to on the web.</p><p>If you want to run storybook in the same environment as your app we’re going to need a simple way to switch between them.</p><p>To access an environment variable with expo we can follow their documentation <a href="https://docs.expo.dev/guides/environment-variables/">here</a>. If you aren’t using expo I recommend following <a href="https://dev.to/dannyhw/multiple-entry-points-for-react-native-storybook-4dkp">this guide</a> instead.</p><p>Note: This guide assumes you have an expo application setup for storybook (see the <a href="https://github.com/storybookjs/react-native/blob/next-6.0/README.md">github readme</a> for info on how to get setup).</p><h3>Using Expo constants</h3><p>First rename app.json to app.config.js and edit the file so it looks something like this:</p><pre>module.exports = {<br>  name: &quot;my-app&quot;,<br>  slug: &quot;my-app&quot;,<br>  version: &quot;1.0.0&quot;,<br>  orientation: &quot;portrait&quot;,<br>  ...<br>};</pre><p>Now add the extra config option like this</p><pre>module.exports = {<br>  name: &#39;MyApp&#39;,<br>  version: &#39;1.0.0&#39;,<br>  extra: {<br>    storybookEnabled: process.env.STORYBOOK_ENABLED,<br>  },<br>  ...<br>};</pre><p>Edit App.js so that it looks like this:</p><pre>import { StatusBar } from &quot;expo-status-bar&quot;;<br>import { StyleSheet, Text, View } from &quot;react-native&quot;;<br>import Constants from &quot;expo-constants&quot;;<br><br>function App() {<br>  return (<br>    &lt;View style={styles.container}&gt;<br>      &lt;Text&gt;Open up App.js to start working on your app!&lt;/Text&gt;<br>      &lt;StatusBar style=&quot;auto&quot; /&gt;<br>    &lt;/View&gt;<br>  );<br>}<br><br>let AppEntryPoint = App;<br><br>if (Constants.expoConfig.extra.storybookEnabled === &quot;true&quot;) {<br>  AppEntryPoint = require(&quot;./.storybook&quot;).default;<br>}<br><br>const styles = StyleSheet.create({<br>  container: {<br>    flex: 1,<br>    backgroundColor: &quot;#fff&quot;,<br>    alignItems: &quot;center&quot;,<br>    justifyContent: &quot;center&quot;,<br>  },<br>});<br><br>export default AppEntryPoint;</pre><p>Now in package.json we can add a script like this</p><pre>&quot;scripts&quot;: {<br> &quot;storybook&quot;: &quot;STORYBOOK_ENABLED=&#39;true&#39; expo start&quot;,<br>}</pre><p>We’ve added a new command that will run our app with the STORYBOOK_ENABLED flag set to true.</p><p>When you run yarn start you should see the app code and yarn storybook should show you storybook.</p><h3>Using entryPoint (expo go &lt;48 only)</h3><p><em>This solution won’t work in v48 of expo or with expo dev clients.</em></p><p>First we want to rename our app.json to app.config.js and edit the file so it looks something like this:</p><pre>module.exports = {<br>  name: &quot;my-app&quot;,<br>  slug: &quot;my-app&quot;,<br>  version: &quot;1.0.0&quot;,<br>  orientation: &quot;portrait&quot;,<br>  ...<br>};</pre><p>Add the entryPoint config option like this</p><pre>module.exports = {<br>  name: &#39;MyApp&#39;,<br>  version: &#39;1.0.0&#39;,<br>  entryPoint: process.env.STORYBOOK_ENABLED<br>      ? &quot;index.storybook.js&quot;<br>      : &quot;node_modules/expo/AppEntry.js&quot;,<br>  ...<br>};</pre><p>Add a file called index.storybook.js in the root of the project with the following content</p><pre>import { registerRootComponent } from &quot;expo&quot;;<br><br>import Storybook from &quot;./.storybook&quot;;<br><br>// registerRootComponent calls AppRegistry.registerComponent(&#39;main&#39;, () =&gt; App);<br>// It also ensures that whether you load the app in Expo Go or in a native build,<br>// the environment is set up appropriately<br>registerRootComponent(Storybook);</pre><p>Now in package.json we can add a script like this</p><pre>&quot;scripts&quot;: {<br>  &quot;storybook&quot;: &quot;STORYBOOK_ENABLED=&#39;true&#39; expo start&quot;,<br>}</pre><p>Which will run our app with the STORYBOOK_ENABLED flag set to true.</p><p>Now when you run yarn start you should see the app and yarn storybook should show you storybook.</p><p>You can pass the option --ios or --android to have the simulator autmatically open up otherwise press i or a after the command finishes running to open the simulator.</p><h3>Summary</h3><p>As you can see there are multiple potential approaches here and I’ve tried to offer some possible solutions. Each of these have some potential drawbacks but you should assess your projects needs to see what fits for you.</p><p>If you are using Expo Go on version 47 or below the entryPoint might be an interesting option for you but it will be removed in 48. The version using expo constants has the possibility increase your bundle size however will work in expo 48.</p><p>Heres some comments from Evan Bacon about the entryPoint field:</p><h3>Evan Bacon 🥓 on Twitter: &quot;entryPoint only worked in Expo Go, rendering it mostly useless. React Native CLI (used to build production apps) only supports root index.js entry files. / Twitter&quot;</h3><p>entryPoint only worked in Expo Go, rendering it mostly useless. React Native CLI (used to build production apps) only supports root index.js entry files.</p><h3>Evan Bacon 🥓 on Twitter: &quot;Constants is fine. It acts as a stand-in for environment variables which would also work. Ultimately we just need to pull out RN code loading in favor of expo-updates and RN CLI in favor of Expo CLI so you can dynamically change the entry point without a bunch of native changes / Twitter&quot;</h3><p>Constants is fine. It acts as a stand-in for environment variables which would also work.</p><p>Other possible approaches include using env variables in metro config to change the project root or using storybook as a separate app outside of your project. You could also manually alternate the main field of the package.json between two different entry point paths (similar to the entryPoint option).</p><p>As I find better solutions I will add to this post to showcase the best options.</p><p>Thanks for reading.</p><p>You can follow me on twitter here: <a href="https://twitter.com/Danny_H_W">https://twitter.com/Danny_H_W</a></p><p><em>Originally published at </em><a href="https://dev.to/dannyhw/how-to-swap-between-react-native-storybook-and-your-app-p3o"><em>https://dev.to</em></a><em> on February 6, 2023.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=77ebdd924935" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Quick guide for @storybook/react-native-server v6]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/quick-guide-for-storybook-react-native-server-v6-febe75fc5dfa?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/febe75fc5dfa</guid>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Sat, 03 Dec 2022 18:43:10 GMT</pubDate>
            <atom:updated>2023-03-04T16:33:43.441Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*nO8oM1v0hj5jcNI2.png" /></figure><h3>What is react-native-server</h3><p>The package @storybook/react-native-server is used alongside @storybook/react-native to control the current story and adjust controls remotely via a storybook UI on a simple web app.</p><p>This was a popular tool in storybook/react-native v5 and recently we were able to bring it back for v6.</p><p>Note: The web UI will not show a preview of any component and your components will only show on the mobile device.</p><h3>The setup</h3><p>Initialise a project with react-native storybook configured (replace AwesomeStorybook with your own name)</p><pre>expo init --template expo-template-storybook AwesomeStorybook</pre><p>Add the react-native-server package</p><pre>yarn add -D @storybook/react-native-server@next</pre><p>Add a folder called .storybook_server/ in the root of your project and create a main.js file in there with the following content</p><pre>module.exports = {<br>  stories: [&quot;../components/**/*.stories.?(ts|tsx|js|jsx)&quot;],<br>  env: () =&gt; ({}),<br>  addons: [&quot;@storybook/addon-essentials&quot;],<br>};</pre><p>Add a script to start the server in package.json</p><pre>&quot;start-server&quot;: &quot;react-native-storybook-server&quot;</pre><p>Edit <em>.ondevice/Storybook.tsx</em> and adjust the options so that websockets are enabled and optionally disable the ondevice UI.</p><pre>const StorybookUIRoot = getStorybookUI({<br>  enableWebsockets: true,<br>  onDeviceUI: false,<br>});</pre><p>If you disable the ondevice UI you might find it useful to add a top margin so that nothing gets hidden under the status bar. In the same Storybook file you can add that as follows.</p><pre>import { View } from &quot;react-native&quot;;<br>export default () =&gt; (<br>  &lt;View style={{ marginTop: 52, flex: 1 }}&gt;<br>    &lt;StorybookUIRoot /&gt;<br>  &lt;/View&gt;<br>);</pre><p>Start your app and open the ios or android app with ‘i’ or ‘a’ like you might normally with expo</p><pre>yarn start</pre><p>Now you can run the server with the command like</p><pre>yarn start-server</pre><p>Then open the url of the server that it will print out</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*GQ1Axz9qACr17-0dML9zPw.png" /></figure><h3>Some caveats</h3><p>As mentioned the package is companion app for react-native storybook it also relies on the device to send the story list via websockets. Sometimes its useful to refresh the mobile app once the web ui has loaded so that it receives the story list and current story. It then uses websockets to update the current story on the device whenever you navigate to a new story on the web UI.</p><p>You must use storybook 6.5.14 and above along with react-native storybook 6.0.1-beta.11 or newer for this to work due to patches made in those versions.</p><h3>Feedback</h3><p>Thanks for reading and let me know if you try it out</p><p>This release and many of the recent updates have been made possible due to the help of multiple people on the storybook core team.</p><p>Specifically I’d like to thank <a href="https://twitter.com/NorbertdeLangen">Norbert</a>, <a href="https://twitter.com/mshilman">Shillman</a> and <a href="https://twitter.com/tmeasday">Tom</a> who all paired with me multiple times to help me overcome various blockers.</p><p>If you want to ask anything leave a comment or DM me on twitter <a href="https://twitter.com/Danny_H_W">@Danny_H_W</a></p><p><em>Originally published at </em><a href="https://dev.to/dannyhw/quick-guide-for-storybookreact-native-server-v6-4nl2"><em>https://dev.to</em></a><em> on December 3, 2022.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=febe75fc5dfa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Automatic args in React Native Storybook]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/automatic-args-in-react-native-storybook-99dc7c2c6637?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/99dc7c2c6637</guid>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Sun, 20 Nov 2022 21:55:00 GMT</pubDate>
            <atom:updated>2023-03-04T16:28:56.335Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*QNMAmD90cbXi9tn0.png" /></figure><p>This is a quick guide on how to setup automatic args for React Native Storybook V6 (currently in beta).</p><p>If you’ve used the web version of Storybook you’ll know that your types are used to infer the types of your args. This is done using a transform called docgen in the Storybook webpack bundle process.</p><h3>Docgen for React Native?</h3><p>In React Native Storybook you might have noticed that this doesn’t happen by default. That’s because we don’t bundle a separate app like in the web so it’s not possible to insert a transform into the bundling of the app. In order to do this you the user need to add it to your configuration.</p><p>Recently I worked with Michael Shillman one of the core maintainers of Storybook to find a way to make this work for React Native. We came up with something that works for typescript types but requires some setup from user (for the reasons mentioned above).</p><h3>The setup</h3><p>To use docgen in React Native Storybook you will need at least version 6.0.1-beta.10 and you will want to have types for your component props.</p><p>Then you will want to install these packages:</p><pre>yarn add -D @storybook/react @storybook/docs-tools babel-plugin-react-docgen-typescript</pre><p>Then add the docgen plugin to your babel config:</p><pre>plugins: [[&#39;babel-plugin-react-docgen-typescript&#39;, { exclude: &#39;node_modules&#39; }]],</pre><p>Now add the following code to Storybook.tsx</p><pre>import { extractArgTypes } from &quot;@storybook/react/dist/modern/client/docs/extractArgTypes&quot;;<br>import { addArgTypesEnhancer, addParameters } from &quot;@storybook/react-native&quot;;<br>import { enhanceArgTypes } from &quot;@storybook/docs-tools&quot;;<br><br>addArgTypesEnhancer(enhanceArgTypes);<br>addParameters({<br>  docs: {<br>    extractArgTypes,<br>  },<br>});</pre><p>You can also add it to another file and import it from Storybook.tsx.</p><p>Once this is done then you should have Args and ArgTypes without needing to manually specify them.</p><p>Thanks for reading!<br>Please try it out and let me know if it worked for you.</p><p><a href="https://twitter.com/Danny_H_W"><em>https://twitter.com/Danny_H_W</em></a></p><p><a href="https://github.com/dannyhw"><em>https://github.com/dannyhw</em></a></p><p>Shoutout to <a href="https://twitter.com/mshilman">@mshilman</a> for helping make this possible.</p><p><em>Originally published at </em><a href="https://dev.to/dannyhw/automatic-args-in-react-native-storybook-140a"><em>https://dev.to</em></a><em> on November 20, 2022.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=99dc7c2c6637" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Multiple entry points for React Native Storybook]]></title>
            <link>https://medium.com/@dannyhyunsoowilliams/multiple-entry-points-for-react-native-storybook-268f396332c0?source=rss-9d1514410f13------2</link>
            <guid isPermaLink="false">https://medium.com/p/268f396332c0</guid>
            <category><![CDATA[guides-and-tutorials]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[storybook]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[design-systems]]></category>
            <dc:creator><![CDATA[Danny Williams]]></dc:creator>
            <pubDate>Sat, 16 Apr 2022 16:38:47 GMT</pubDate>
            <atom:updated>2022-04-16T16:38:47.682Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*s85bdgqzSH801fAm9eF8jA.png" /></figure><h4>Intro</h4><p>React Native storybook is setup in such a way that the decision is left to the user how it should be loaded. This gives you flexibility on how you do things, however I often get asked how to easily switch between running application code and Storybook.</p><h4>An alternative approach</h4><p>For a while now I’ve been thinking about an easy way to do this by using metro directly.</p><p>This will be a quick guide on how to setup a secondary entry point for React Native</p><p>At the end you’ll be able to run yarn storybook-metro when you want Storybook and yarn start when you want to run your app.</p><p>The key to this solution looks like this:</p><pre>react-native start --config ./.ondevice/metro.storybook.js</pre><p>This solution works with React Native cli I haven’t worked out a solution for expo yet, but maybe someone can help me with that.</p><h4>The setup</h4><p>I’ll be working from the <a href="https://github.com/dannyhw/react-native-template-storybook">react native storybook template</a> based on the react native typescript template. However your own setup should work too if you adjust it in the same way.</p><p>To use it you can run</p><pre>npx react-native init MyApp --template react-native-template-storybook</pre><p>At the time of writing when you use this template your directory structure will look something like this</p><pre>.ondevice/<br>  Storybook.tsx<br>  main.js<br>  ...<br>components/<br>index<br>App.tsx<br>...</pre><p>The files in .ondevice are related to React Native storybook and currently the App.tsx file imports the default export from .ondevice/Storybook.tsx and renders that as the Application.</p><p>What if you want to write an application here and run storybook separately, what do you do?</p><p>You might just comment out the storybook import when you aren’t using it, or you might add some kind of conditional to load storybook when an environment variable is set.</p><p>There is another way, you can actually use metro to do all the work for you.</p><h4>Creating a second entry point</h4><p>First I’m going to rename .ondevice/Storybook.tsx to .ondevice/index.js and replace the content with this:</p><pre>import { AppRegistry } from &quot;react-native&quot;;<br>import { getStorybookUI } from &quot;@storybook/react-native&quot;;<br>import { name as appName } from &quot;../app.json&quot;;<br>import &quot;./storybook.requires&quot;;<br><br>const StorybookUIRoot = getStorybookUI({});<br><br>AppRegistry.registerComponent(appName, () =&gt; StorybookUIRoot);</pre><p>This will act as the secondary entry point for Storybook and when used registers the Storybook UI as the Application to render just like before.</p><p>Now in App.tsx lets just remove all storybook related code and make it look like this</p><pre>import React from &quot;react&quot;;<br>import { SafeAreaView, Text } from &quot;react-native&quot;;<br>export default () =&gt; {<br>  return (<br>    &lt;SafeAreaView&gt;<br>      &lt;Text&gt;Not Storybook!&lt;/Text&gt;<br>    &lt;/SafeAreaView&gt;<br>  );<br>};</pre><p>This App file gets loaded by index in the root of the project which is the original entry point. This component will be registered as the app when that entry point is used.</p><p>We have two entry-points .ondevice/index.js and index.js. Now we just need a way to swap between them.</p><p>Create a file in the .ondevice/ folder called metro.storybook.js with the following content:</p><pre>const path = require(&quot;path&quot;);<br>module.exports = {<br>  transformer: {<br>    getTransformOptions: async () =&gt; ({<br>      transform: {<br>        experimentalImportSupport: false,<br>        inlineRequires: false,<br>      },<br>    }),<br>  },<br>  resolver: {<br>    resolverMainFields: [&quot;sbmodern&quot;, &quot;react-native&quot;, &quot;browser&quot;, &quot;main&quot;],<br>  },<br>  watchFolders: [path.resolve(__dirname, &quot;..&quot;)],<br>};</pre><p>The important thing here is the watchFolders config so that metro knows to look for files in the root of the project.</p><p>We’re also using the way metro works to our advantage, by default it will look for an index file relative to the projectRoot. Since we didn’t set the root it will assume that the current directory is the project root.</p><p>We’re now mostly done, all that&#39;s left is to tell metro which config file to load.</p><h4>The final pieces</h4><p>Now we just set up some scripts in package json to make our life easier</p><p>The template we used gives us these commands</p><pre>&quot;scripts&quot;: {<br>	...<br>    &quot;start&quot;: &quot;react-native start&quot;,<br>    &quot;prestart&quot;: &quot;yarn update-stories&quot;,<br>    ...<br>},</pre><p>The prestart script uses the a prefix to tell yarn to always run this script before the start script.</p><p>Now that we’ve updated our setup we’ll be using start specifically for our application code so we’ll adjust this.</p><p>Let’s add a new script for starting metro from our new storybook config file.</p><pre>&quot;storybook-metro&quot;: &quot;react-native start --config ./.ondevice/metro.storybook.js&quot;,</pre><p>Here we use the — config option to tell metro to use our storybook specific config.</p><p>Now rename the prestart script to look like this:</p><pre>&quot;prestorybook-metro&quot;: &quot;yarn update-stories&quot;,</pre><p>Now the update-stories script only runs when we need it (when running storybook).</p><h4>Running storybook</h4><p>Whenever you want to run storybook you run yarn storybook-metro in one terminal followed by running yarn ios or yarn android in another terminal.</p><p>To go back to your app you can just stop storybook metro and run yarn start instead. This will start metro with config from metro.config.js and all you have to do is reload the app to get the application javascript bundle.</p><p>Note that since only the bundle changes you won’t need to rebuild the entire app.</p><h4>Closing thoughts</h4><p>As you can see it’s possible to separate your storybook code from your application code with just the built in behaviour of metro and React Native.</p><p>A great benefit of this is that you can easily leave storybook code outside of your application code since the entry points are completely separate.</p><p>This setup could be the default for storybook however a solution is still needed for expo.</p><p>In this guide I’m using the React Native Storybook v6 beta which is currently under development however you can follow a similar approach for v5.3 also.</p><h4>Contact me</h4><p>If you want to get in touch you can contact me on twitter or the storybook discord</p><ul><li>Twitter: <a href="https://twitter.com/Danny_H_W">@Danny_H_W</a></li><li>Github: <a href="https://github.com/dannyhw">dannyhw</a></li><li>Storybook discord: <a href="https://discord.gg/sMFvFsG">Storybook</a> (find me in the react-native channel)</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=268f396332c0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>