Skip to content

Instantly share code, notes, and snippets.

@jgornick
Created November 22, 2022 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgornick/0286bc0fcb98d517d518d24234abd688 to your computer and use it in GitHub Desktop.
Save jgornick/0286bc0fcb98d517d518d24234abd688 to your computer and use it in GitHub Desktop.
React Native Storybook UI Wrapper - Support Deep Linking
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