Skip to content

Commit 7c209a6

Browse files
committed
feat: use code previewer
1 parent 84e719b commit 7c209a6

File tree

6 files changed

+14
-102
lines changed

6 files changed

+14
-102
lines changed

examples/.storybook/config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { configure } from '@storybook/react'
2-
import { setOptions } from '@storybook/addon-options'
2+
import { withOptions } from '@storybook/addon-options'
33

4-
setOptions({
4+
withOptions({
55
name: 'One React',
66
url: 'https://github.com/one-react/sample',
77
goFullScreen: false,

examples/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
"babel-loader": "^8.0.2",
2323
"css-loader": "^1.0.0",
2424
"node-sass": "^4.7.2",
25+
"or-code-previewer": "^1.0.0",
26+
"raw-loader": "^0.5.1",
2527
"react-docgen-typescript-loader": "^3.0.0",
2628
"sass-loader": "^7.1.0",
2729
"style-loader": "^0.23.0",

examples/stories/styles.scss

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,3 @@
22
margin-left: 20px;
33
margin-bottom: 20px;
44
}
5-
6-
// styles for code previewer
7-
.code-previewer {
8-
position: relative;
9-
10-
&-title {
11-
margin-bottom: 0;
12-
padding-bottom: 5px;
13-
border-bottom: 1px solid rgb(238, 238, 238);
14-
}
15-
16-
.copy-btn {
17-
position: absolute;
18-
top: 75px;
19-
right: 10px;
20-
background-color: rgb(255, 255, 255);
21-
cursor: pointer;
22-
font-size: 13px;
23-
align-self: flex-start;
24-
flex-shrink: 0;
25-
overflow: hidden;
26-
border-width: 1px;
27-
border-style: solid;
28-
border-color: rgb(238, 238, 238);
29-
border-image: initial;
30-
border-radius: 3px;
31-
padding: 3px 10px;
32-
33-
&-text {
34-
height: 16px;
35-
transform: translateY(-100%) translateY(-6px);
36-
transition: transform .2s ease;
37-
}
38-
39-
&-copied {
40-
transform: translateY(0);
41-
}
42-
}
43-
}

examples/stories/util.tsx

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,7 @@
1-
import clx from 'classnames'
2-
import React, { PureComponent } from 'react'
3-
import CopyToClipboard from 'react-copy-to-clipboard'
4-
import Highlight from 'react-highlight'
1+
import React from 'react'
52

6-
import 'highlight.js/styles/atom-one-light.css'
3+
import CodePreviewer from 'or-code-previewer'
74

8-
class CopyBtn extends PureComponent<
9-
{
10-
source: string
11-
},
12-
{
13-
copied: boolean
14-
}
15-
> {
16-
timeoutId: NodeJS.Timer
17-
18-
state = {
19-
copied: false
20-
}
21-
22-
render() {
23-
const { copied } = this.state
24-
return (
25-
<CopyToClipboard text={this.props.source} onCopy={this.handleCopy}>
26-
<button className="copy-btn">
27-
<div className={clx('copy-btn-text', { 'copy-btn-copied': copied })}>
28-
<div style={{ marginBottom: 6 }}>Copied!</div>
29-
<div>Copy</div>
30-
</div>
31-
</button>
32-
</CopyToClipboard>
33-
)
34-
}
35-
36-
handleCopy = () => {
37-
if (this.timeoutId) {
38-
clearTimeout(this.timeoutId)
39-
}
40-
41-
this.setState(
42-
{
43-
copied: true
44-
},
45-
() => {
46-
this.timeoutId = setTimeout(() => {
47-
this.setState({ copied: false })
48-
}, 2000)
49-
}
50-
)
51-
}
52-
}
53-
54-
export function previewCode(source: string, lang: string = 'tsx'): any {
55-
return (
56-
<div className="code-previewer">
57-
<h1 className="code-previewer-title">Story Source</h1>
58-
<CopyBtn source={source} />
59-
<Highlight className={lang}>{source}</Highlight>
60-
</div>
61-
)
5+
export function previewCode(code: string) {
6+
return <CodePreviewer title="Story Source" lang="tsx" code={code} />
627
}

examples/types/declarations.d.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import * as React from 'react'
66
import { Options } from '@storybook/addon-info'
77

88
declare module '@storybook/react' {
9+
interface ExtOptions extends Options {
10+
text?: string | React.ReactNode
11+
}
912
export interface Story {
1013
readonly kind: string
1114
add(
1215
storyName: string,
1316
callback: RenderFunction,
14-
options?: { info: Options & { text?: string | React.ReactNode } }
17+
options?: { info: ExtOptions }
1518
): this
1619
addDecorator(decorator: StoryDecorator): this
1720
}

tslint.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"no-implicit-dependencies": false,
1313
"jsdoc-format": false,
1414
"interface-name": false,
15-
"member-access": [true, "no-public"]
15+
"member-access": [true, "no-public"],
16+
"max-classes-per-file": false
1617
}
1718
}

0 commit comments

Comments
 (0)