Created
November 22, 2022 15:19
-
-
Save jgornick/0286bc0fcb98d517d518d24234abd688 to your computer and use it in GitHub Desktop.
React Native Storybook UI Wrapper - Support Deep Linking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { getStorybookUI } = require('@storybook/react-native') | |
const React = require('react') | |
const { useEffect, useState } = require('react') | |
const { Linking } = require('react-native') | |
const { URL } = require('react-native-url-polyfill') | |
export const StorybookUIWrapper = () => { | |
const [config, setConfig] = useState() | |
const [url, setUrl] = useState() | |
useEffect(() => { | |
Linking.addEventListener('url', ({ url }) => { | |
setUrl(url) | |
}) | |
Linking.getInitialURL().then((url) => { | |
if (url === null) { | |
return setConfig({}) | |
} | |
setUrl(url) | |
}) | |
}, []) | |
useEffect(() => { | |
if (url === undefined) { | |
return | |
} | |
if (url === null) { | |
return setConfig({}) | |
} | |
const parsedURL = new URL(url) | |
if (parsedURL.hostname === 'story' && parsedURL.protocol === 'storybook:') { | |
const initialSelection = parsedURL.searchParams.get('id') | |
setConfig({ | |
initialSelection, | |
isUIHidden: true, | |
}) | |
} else { | |
setConfig({}) | |
} | |
}, [url]) | |
if (config === undefined) { | |
return null | |
} | |
const StorybookUIRoot = getStorybookUI(config) | |
return <StorybookUIRoot /> | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment