-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1369 from GSA/source-code-addon
Source code addon
- Loading branch information
Showing
5 changed files
with
123 additions
and
11 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import React, { useState } from 'react'; | ||
import { AddonPanel, Placeholder } from '@storybook/components'; | ||
import { useParameter, addons, types } from '@storybook/api'; | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { themes } from '@storybook/theming'; | ||
import { oneLight } from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
import { CopyToClipboard } from 'react-copy-to-clipboard'; | ||
import './source-code.css'; | ||
|
||
const customOneLight = { ...oneLight }; | ||
customOneLight['pre[class*="language-"]'].background = 'transparent'; | ||
customOneLight['code[class*="language-"]'].background = 'transparent'; | ||
|
||
addons.register('my/panel', () => { | ||
addons.add('my-panel-addon/panel', { | ||
title: 'View Code', | ||
type: types.PANEL, | ||
render: ({ active, key }) => { | ||
const preview = useParameter('preview', null); | ||
const [selectedTab, setSelectedTab] = useState(null); | ||
|
||
const handleTabClick = (tab) => { | ||
setSelectedTab(tab); | ||
}; | ||
|
||
return ( | ||
<AddonPanel key={key} active={active}> | ||
{preview && preview.length > 0 ? ( | ||
<div> | ||
{preview.map((item, index) => ( | ||
<button | ||
key={index} | ||
onClick={() => handleTabClick(item.tab)} | ||
className={item.tab === selectedTab ? "activeTabButton" : "tabButton"} | ||
> | ||
{item.tab} | ||
</button> | ||
))} | ||
{preview.map((item, index) => { | ||
if (item.tab === selectedTab && item.template) { | ||
return ( | ||
<div key={index} className="codeContainer"> | ||
<CopyToClipboard text={item.template.default}> | ||
<button className="tabButton copyButton">Copy Code</button> | ||
</CopyToClipboard> | ||
<SyntaxHighlighter language="typescript" style={customOneLight}> | ||
{item.template.default} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} | ||
return null; | ||
})} | ||
</div> | ||
) : ( | ||
<Placeholder>No preview data available for this story.</Placeholder> | ||
)} | ||
</AddonPanel> | ||
); | ||
}, | ||
}); | ||
}); | ||
|
||
addons.setConfig({ | ||
theme: themes.light, | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
.tabButton { | ||
padding: 10px 15px; | ||
margin: 5px; | ||
border: none; | ||
cursor: pointer; | ||
background-color: #e0e0e0; | ||
} | ||
|
||
.activeTabButton { | ||
padding: 10px 15px; | ||
margin: 5px; | ||
border: none; | ||
cursor: pointer; | ||
background-color: #c0c0c0; | ||
} | ||
|
||
.codeContainer { | ||
position: relative; | ||
} | ||
|
||
.copyButton { | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
margin-right: 2rem; | ||
} | ||
.tabButton:active { | ||
background-color: #c0c0c0; | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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