This repository has been archived by the owner on Jan 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(docz-theme-default): add line numbers on Playground
- Loading branch information
1 parent
bd4b27f
commit 204f1bb
Showing
8 changed files
with
187 additions
and
81 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,13 +1,25 @@ | ||
const svg = require('rollup-plugin-svg') | ||
const pkg = require('./package.json') | ||
|
||
const inline = [ | ||
'facepaint', | ||
'match-sorter', | ||
'react-breakpoints', | ||
'react-feather', | ||
'react-powerplug', | ||
'webfontloader', | ||
] | ||
|
||
const external = Object.keys(pkg.dependencies) | ||
.filter(dep => inline.indexOf(dep) === -1) | ||
.concat([ | ||
'polished/lib/colors/rgba', | ||
'react-syntax-highlighter/prism', | ||
'react-feather/dist/icons/search', | ||
'react-feather/dist/icons/chevron-down', | ||
]) | ||
|
||
module.exports = { | ||
external: [ | ||
'docz', | ||
'react', | ||
'react-dom', | ||
'react-router', | ||
'react-router-dom', | ||
'prop-types', | ||
], | ||
plugins: [svg()], | ||
external, | ||
} |
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 |
---|---|---|
@@ -1,50 +1,77 @@ | ||
import 'prismjs' | ||
import 'prismjs/components/prism-jsx' | ||
|
||
import React, { PureComponent } from 'react' | ||
import prism from 'prismjs' | ||
import * as React from 'react' | ||
import { SFC, Fragment } from 'react' | ||
import { ThemeConfig } from 'docz' | ||
import rgba from 'polished/lib/color/rgba' | ||
import styled, { cx } from 'react-emotion' | ||
import SyntaxHighlighter from 'react-syntax-highlighter/prism-light' | ||
|
||
const PreStyled = styled('pre')` | ||
overflow-y: hidden; | ||
border: 1px solid ${p => p.theme.colors.border}; | ||
margin: 2em 0; | ||
border-radius: 5px; | ||
background: ${p => p.theme.colors.preBg}; | ||
const PrismTheme = styled('pre')` | ||
${p => p.theme.prismTheme}; | ||
${p => p.theme.mq(p.theme.styles.pre)}; | ||
padding: 30px; | ||
margin: 0; | ||
flex: 1; | ||
` | ||
|
||
interface PreProps { | ||
className: string | ||
children: any | ||
const getLanguage = (children: any) => { | ||
if (typeof children === 'string') return 'language-jsx' | ||
return children.props.props.className | ||
} | ||
|
||
export class Pre extends PureComponent<PreProps> { | ||
private el: any | ||
const getCode = (content: any): SFC => ({ children }) => { | ||
const className = cx('react-prism', getLanguage(content)) | ||
return <PrismTheme className={className}>{children}</PrismTheme> | ||
} | ||
|
||
public render(): JSX.Element { | ||
const { children } = this.props | ||
const childrenProps = children.props.props | ||
const childrenClassName = childrenProps && childrenProps.className | ||
const className = cx('react-prism', this.props.className, childrenClassName) | ||
const Wrapper = styled('div')` | ||
display: flex; | ||
position: relative; | ||
overflow-y: hidden; | ||
border: 1px solid ${p => p.theme.colors.border}; | ||
border-radius: 5px; | ||
background: ${p => p.theme.colors.preBg}; | ||
${p => p.theme.mq(p.theme.styles.pre)}; | ||
return ( | ||
<PreStyled innerRef={ref => (this.el = ref)} className={className}> | ||
<code>{children.props.children}</code> | ||
</PreStyled> | ||
) | ||
.react-syntax-highlighter-line-number { | ||
display: block; | ||
padding: 0 15px; | ||
opacity: 0.3; | ||
text-align: right; | ||
} | ||
` | ||
|
||
public componentDidMount(): void { | ||
this.highlightCode() | ||
} | ||
const Nullable: SFC = ({ children }) => <Fragment>{children}</Fragment> | ||
|
||
public componentDidUpdate(): void { | ||
this.highlightCode() | ||
} | ||
const linesStyle = (colors: any) => ({ | ||
padding: '30px 0', | ||
borderRight: `1px solid ${colors.border}`, | ||
background: rgba(colors.background, 0.5), | ||
left: 0, | ||
}) | ||
|
||
private highlightCode(): void { | ||
prism.highlightElement(this.el) | ||
} | ||
interface PreProps { | ||
children: any | ||
className?: string | ||
} | ||
|
||
export const Pre: SFC<PreProps> = ({ children, className }) => ( | ||
<ThemeConfig> | ||
{config => ( | ||
<Wrapper className={className}> | ||
<SyntaxHighlighter | ||
language="javascript" | ||
showLineNumbers | ||
useInlineStyles={false} | ||
lineProps={{ class: 'helo' }} | ||
lineNumberContainerStyle={linesStyle(config.colors)} | ||
PreTag={Nullable} | ||
CodeTag={getCode(children)} | ||
> | ||
{children && typeof children !== 'string' | ||
? children.props.children | ||
: children} | ||
</SyntaxHighlighter> | ||
</Wrapper> | ||
)} | ||
</ThemeConfig> | ||
) |
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
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
Oops, something went wrong.