Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
47 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,52 @@ | ||
import React, { Fragment } from 'react'; | ||
import { AddonPanel } from '@storybook/components'; | ||
import { useParameter } from '@storybook/api'; | ||
import { useParameter, useStorybookState } from '@storybook/api'; | ||
import { addons, types } from '@storybook/addons'; | ||
import { styled } from '@storybook/theming'; | ||
|
||
const getUrl = (input) => { | ||
return typeof input === 'string' ? input : input.url; | ||
}; | ||
|
||
const Iframe = styled.iframe({ | ||
width: '100%', | ||
height: '100%', | ||
border: '0 none', | ||
}); | ||
const Img = styled.img({ | ||
width: '100%', | ||
height: '100%', | ||
border: '0 none', | ||
objectFit: 'contain', | ||
}); | ||
|
||
const Asset = ({ url }) => { | ||
if (!url) { | ||
return null; | ||
} | ||
if (url.match(/\.(png|gif|jpeg|tiff|svg|anpg|webp)/)) { | ||
// do image viewer | ||
return <Img alt="" src={url} />; | ||
} | ||
|
||
return <Iframe title={url} src={url} />; | ||
}; | ||
|
||
const Content = () => { | ||
const results = useParameter('assets', []); // story's parameter being retrieved here | ||
// story's parameter being retrieved here | ||
const results = useParameter('assets', []); | ||
// the id of story retrieved from Storybook global state | ||
const { storyId } = useStorybookState(); | ||
|
||
if (results.length === 0) { | ||
return null; | ||
} | ||
|
||
const url = getUrl(results[0]).replace('{id}', storyId); | ||
|
||
return ( | ||
<Fragment> | ||
{results.length ? ( | ||
<ol> | ||
{results.map((i) => ( | ||
<li>{i}</li> | ||
))} | ||
</ol> | ||
) : null} | ||
<Asset url={url} /> | ||
</Fragment> | ||
); | ||
}; | ||
|
||
addons.register('my/design-addon', () => { | ||
addons.add('design-addon/panel', { | ||
title: 'assets', | ||
type: types.PANEL, | ||
render: ({ active, key }) => ( | ||
<AddonPanel active={active} key={key}> | ||
<Content /> | ||
</AddonPanel> | ||
), | ||
}); | ||
}); |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
This file contains 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