diff --git a/with-storybook/.gitignore b/with-storybook/.gitignore index 13dc43e9..05647d55 100644 --- a/with-storybook/.gitignore +++ b/with-storybook/.gitignore @@ -1,2 +1,35 @@ -node_modules -/storybook-static +# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files + +# dependencies +node_modules/ + +# Expo +.expo/ +dist/ +web-build/ + +# Native +*.orig.* +*.jks +*.p8 +*.p12 +*.key +*.mobileprovision + +# Metro +.metro-health-check* + +# debug +npm-debug.* +yarn-debug.* +yarn-error.* + +# macOS +.DS_Store +*.pem + +# local env files +.env*.local + +# typescript +*.tsbuildinfo diff --git a/with-storybook/.storybook/addons.js b/with-storybook/.storybook/addons.js deleted file mode 100644 index 6aed412d..00000000 --- a/with-storybook/.storybook/addons.js +++ /dev/null @@ -1,2 +0,0 @@ -import '@storybook/addon-actions/register'; -import '@storybook/addon-links/register'; diff --git a/with-storybook/.storybook/config.js b/with-storybook/.storybook/config.js deleted file mode 100644 index 8452b27e..00000000 --- a/with-storybook/.storybook/config.js +++ /dev/null @@ -1,4 +0,0 @@ -import { configure } from "@storybook/react"; - -// automatically import all files ending in *.stories.js -configure(require.context("../stories", true, /\.stories\.js$/), module); diff --git a/with-storybook/.storybook/main.js b/with-storybook/.storybook/main.js new file mode 100644 index 00000000..b6216824 --- /dev/null +++ b/with-storybook/.storybook/main.js @@ -0,0 +1,59 @@ +import { getConfigForPWA } from "expo-pwa"; +import path from "path"; +import { DefinePlugin } from "webpack"; + +/** @type { import('@storybook/react-webpack5').StorybookConfig } */ +const config = { + stories: [ + "../stories/**/*.mdx", + "../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)", + ], + addons: [ + "@storybook/addon-links", + "@storybook/addon-essentials", + "@storybook/addon-onboarding", + "@storybook/addon-interactions", + { + name: "@storybook/addon-react-native-web", + options: { + modulesToTranspile: [], + projectRoot: "../", + babelPlugins: ["@babel/plugin-proposal-export-namespace-from"], + }, + }, + ], + framework: { + name: "@storybook/react-webpack5", + options: {}, + }, + docs: { + autodocs: "tag", + }, + webpackFinal: (config) => { + config.plugins.push( + // Used for surfacing information related to constants + new DefinePlugin({ + "process.env.APP_MANIFEST": JSON.stringify( + sanitizeConfig(getConfigForPWA(path.join(__dirname, ".."))) + ), + }) + ); + + return config; + }, +}; + +function sanitizeConfig(config) { + delete config._internal; + delete config.ios; + delete config.android; + delete config.plugins; + delete config.hooks; + delete config.facebookScheme; + delete config.facebookAppId; + delete config.facebookDisplayName; + delete config.assetBundlePatterns; + return config; +} + +export default config; diff --git a/with-storybook/.storybook/preview.js b/with-storybook/.storybook/preview.js new file mode 100644 index 00000000..0f07f766 --- /dev/null +++ b/with-storybook/.storybook/preview.js @@ -0,0 +1,14 @@ +/** @type { import('@storybook/react').Preview } */ +const preview = { + parameters: { + actions: { argTypesRegex: "^on[A-Z].*" }, + controls: { + matchers: { + color: /(background|color)$/i, + date: /Date$/, + }, + }, + }, +}; + +export default preview; diff --git a/with-storybook/.storybook/webpack.config.js b/with-storybook/.storybook/webpack.config.js deleted file mode 100644 index 67c2a3ac..00000000 --- a/with-storybook/.storybook/webpack.config.js +++ /dev/null @@ -1,6 +0,0 @@ -const { resolve } = require("path"); -const { withUnimodules } = require("@expo/webpack-config/addons"); - -module.exports = ({ config }) => { - return withUnimodules(config, { projectRoot: resolve(__dirname, "../") }); -}; diff --git a/with-storybook/App.js b/with-storybook/App.js new file mode 100644 index 00000000..09f879b8 --- /dev/null +++ b/with-storybook/App.js @@ -0,0 +1,20 @@ +import { StatusBar } from 'expo-status-bar'; +import { StyleSheet, Text, View } from 'react-native'; + +export default function App() { + return ( + + Open up App.js to start working on your app! + + + ); +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#fff', + alignItems: 'center', + justifyContent: 'center', + }, +}); diff --git a/with-storybook/App.tsx b/with-storybook/App.tsx deleted file mode 100644 index fe43f8ed..00000000 --- a/with-storybook/App.tsx +++ /dev/null @@ -1,13 +0,0 @@ -import { configure, getStorybookUI } from '@storybook/react-native'; - -configure(() => { - // Since require.context doesn't exist in metro bundler world, we have to - // manually import files ending in *.stories.js - require('./stories'); -}, module); - -export default getStorybookUI({ - // Pass AsyncStorage below if you want Storybook to open your - // last visited story after you close and re-open your app - asyncStorage: null -}); diff --git a/with-storybook/README.md b/with-storybook/README.md index 39a3c1a7..40077667 100644 --- a/with-storybook/README.md +++ b/with-storybook/README.md @@ -1,126 +1,26 @@ -# Storybook Example +# Storybook Webpack Example

- - Supports Expo iOS - - Supports Expo Android Supports Expo Web

-expo examples - -You can use Storybook to test and share your component library quickly and easily! This example shows how to use Expo modules with Storybook CLI and Expo CLI. +You can use Storybook to test and share your component library quickly and easily! This example shows how to use Expo libraries with Storybook CLI and Webpack. ## Running with Storybook CLI -![expo web with storybook-cli](https://i.imgur.com/0x0Ecmp.png "expo web with storybook-cli") +> web only / Webpack -> web only +This system uses the [community react-native-web addon](https://github.com/storybookjs/addon-react-native-web/) to configure Storybook's Webpack config to support running React Native for web. -This method runs your Expo components in a Storybook-React environment. This is different to Expo web, but may prove helpful as the Storybook-React community is more robust than the Storybook-React Native community. +This method runs your Expo components in a Storybook-React environment. This is different to Expo CLI's Webpack config. -- Create Expo project `expo init my-project` +- Create Expo project `npx create expo my-project` - You can use any template, we'll use the managed blank TypeScript project for this example. -- `cd` into the project and run `npx -p @storybook/cli sb init --type react` to bootstrap a new React project -- Install the expo webpack config so we can add unimodules support `yarn add -D @expo/webpack-config` -- Create a [custom webpack config](./.storybook/webpack.config.js) `touch .storybook/webpack.config.js` - - ```js - const { resolve } = require("path"); - const { withUnimodules } = require("@expo/webpack-config/addons"); - - module.exports = ({ config }) => { - return withUnimodules(config, { projectRoot: resolve(__dirname, "../") }); - }; - ``` - +- `cd` into the project and run `npx sb init --type react`, and select Webpack 5 to bootstrap a new React project. +- Install the requisite dependencies `npx expo add react-dom react-native-web @storybook/addon-react-native-web expo-pwa` +- The contents of `.storybook/main.js` have been modified to support loading the Expo config for the `expo-constants` libraries. - Run `yarn build-storybook` to try it out! - The example should open to `http://localhost:6006/` -- You may also want to add `storybook-static` to your `.gitignore` - -### 📁 File Structure - -``` -Expo with Storybook CLI -├── stories -│ └── Example.stories.js ➡️ A Storybook page to render -├── .storybook -│ ├── config.js ➡️ The entry point / config for a typical Storybook project. -│ └── webpack.config.js ➡️ The custom Webpack config used to add Expo support to Storybook CLI. -├── assets ➡️ All static assets for your project -├── storybook-static ➡️ Generated Storybook files (should be ignored) -└── babel.config.js ➡️ Babel config (should be using `babel-preset-expo`) -``` - -## Running with Expo CLI - -storybook with expo-cli - -> This method is universal :] - -This project can be used for iOS, Android, and web! You may find that it's better to use it for native only, and to use the "Running with Storybook" method for web. Unlike the Expo + Next.js flow, you can use both web methods at the same time! - -- Create Expo project `expo init my-project` - - You can use any template, we'll use the managed blank TypeScript project for this example. -- `cd` into the project and run `npx -p @storybook/cli sb init --type react` to bootstrap a new React project. -- Install the Storybook React Native package: - - `yarn add -D @storybook/react-native` -- In your `App.tsx` or `App.js` - -```ts -import { configure, getStorybookUI } from "@storybook/react-native"; - -configure(() => { - // Since require.context doesn't exist in metro bundler world, we have to - // manually import files ending in *.stories.js - require("./stories"); -}, module); - -export default getStorybookUI(); -``` - -- Create a file for importing all of the stories ([`stories/index.js`](./stories/index.js)): - - - `touch stories/index.js` - - Import all of your stories in this file. Ex: - - ```js - // stories/index.js - import "./1-Button.stories"; - ``` - -- Register your stories for React Native: - -```diff -// Example.stories.js -+ import { storiesOf } from '@storybook/react-native'; - -export const text = () => ( /_ Example JSX _/ ); - -// Register your story with the `module`, name, and React functional component. - -+ storiesOf('Button', module).add('Text', text); - -``` - -- Now run `yarn start` or `npm run start` to see it in action! - -### 📁 File Structure - -``` -Storybook with Expo CLI -├── stories -│ ├── index.js ➡️ Native story imports -│ └── Example.stories.js ➡️ A Storybook page to render -├── assets ➡️ All static assets for your project -├── App.tsx ➡️ Entry Point for universal Expo apps -├── app.config.js ➡️ Expo config file -└── babel.config.js ➡️ Babel config (should be using `babel-preset-expo`) -``` - -## 📝 Notes -- [Storybook React](https://storybook.js.org/docs/react/get-started/introduction) -- [Storybook React Native](https://storybook.js.org/docs/guides/guide-react-native/) +To learn more, configure the Storybook plugin according to [the official guide](https://github.com/storybookjs/addon-react-native-web/). diff --git a/with-storybook/assets/retro-regular.ttf b/with-storybook/assets/retro-regular.ttf deleted file mode 100755 index 15bb923c..00000000 Binary files a/with-storybook/assets/retro-regular.ttf and /dev/null differ diff --git a/with-storybook/package.json b/with-storybook/package.json index 0d798213..7cf558d0 100644 --- a/with-storybook/package.json +++ b/with-storybook/package.json @@ -1,31 +1,31 @@ { "scripts": { - "start": "expo start", - "web": "start-storybook -p 6006", - "build-storybook": "build-storybook" + "storybook": "storybook dev -p 6006", + "build-storybook": "storybook build" }, "dependencies": { - "expo": "^49.0.3", - "expo-constants": "~14.4.2", - "expo-font": "~11.4.0", - "expo-linear-gradient": "~12.3.0", + "expo": "~49.0.13", + "expo-linear-gradient": "^12.5.0", + "expo-status-bar": "~1.6.0", "react": "18.2.0", - "react-dom": "18.2.0", - "react-native": "0.72.3", + "react-native": "0.72.5", "react-native-web": "~0.19.6" }, "devDependencies": { - "@babel/core": "^7.19.3", - "@expo/webpack-config": "^18.0.1", - "@storybook/addon-actions": "~5.2.1", - "@storybook/addon-links": "~5.2.1", - "@storybook/addons": "~5.2.1", - "@storybook/react": "~5.2.1", - "@storybook/react-native": "~5.3.19", - "@types/react": "~18.2.14", - "@types/react-dom": "~18.0.8", - "@types/react-native": "~0.70.6", - "babel-loader": "^8.0.6", - "typescript": "^5.1.3" - } + "expo-pwa": "^0.0.127", + "@storybook/addon-react-native-web": "^0.0.21", + "@babel/core": "^7.20.0", + "@storybook/addon-essentials": "^7.4.6", + "@storybook/addon-interactions": "^7.4.6", + "@storybook/addon-links": "^7.4.6", + "@storybook/addon-onboarding": "^1.0.8", + "@storybook/blocks": "^7.4.6", + "@storybook/react": "^7.4.6", + "@storybook/react-webpack5": "^7.4.6", + "@storybook/testing-library": "^0.2.2", + "prop-types": "^15.8.1", + "react-dom": "18.2.0", + "storybook": "^7.4.6" + }, + "private": true } diff --git a/with-storybook/stories/0-Welcome.stories.js b/with-storybook/stories/0-Welcome.stories.js deleted file mode 100644 index dda9c77b..00000000 --- a/with-storybook/stories/0-Welcome.stories.js +++ /dev/null @@ -1,17 +0,0 @@ -import React from 'react'; -import { linkTo } from '@storybook/addon-links'; -import { storiesOf } from '@storybook/react-native'; -import { Welcome } from '@storybook/react/demo'; - -export default { - title: 'Welcome', -}; - -export const toStorybook = () => ; - -toStorybook.story = { - name: 'to Storybook', -}; - -// On-Device Register -storiesOf('Welcome', module).add(toStorybook.story.name, toStorybook); diff --git a/with-storybook/stories/1-Button.stories.js b/with-storybook/stories/1-Button.stories.js deleted file mode 100644 index 661a490e..00000000 --- a/with-storybook/stories/1-Button.stories.js +++ /dev/null @@ -1,15 +0,0 @@ -import React from 'react'; -import { action } from '@storybook/addon-actions'; -import { storiesOf } from '@storybook/react-native'; -import { Button } from 'react-native'; - -export default { - title: 'Button', -}; - -export const text = () => ( -