Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

docs(prettier): add prettier support #568

Merged
merged 2 commits into from
Dec 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"$schema": "http://json.schemastore.org/prettierrc",
"htmlWhitespaceSensitivity": "ignore",
"printWidth": 100,
"tabWidth": 2,
"semi": false,
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Features
- `Ref` components uses `forwardRef` API by default @layershifter ([#491](https://github.com/stardust-ui/react/pull/491))

### Documentation
- Add `prettier` support throughout the docs @levithomason ([#568](https://github.com/stardust-ui/react/pull/568))

<!--------------------------------[ v0.14.0 ]------------------------------- -->
## [v0.14.0](https://github.com/stardust-ui/react/tree/v0.14.0) (2018-12-05)
[Compare changes](https://github.com/stardust-ui/react/compare/v0.13.3...v0.14.0)
Expand Down
84 changes: 50 additions & 34 deletions docs/src/components/CodeSnippet.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
import * as _ from 'lodash'
import * as React from 'react'

import formatCode from '../utils/formatCode'
import Editor, { EDITOR_BACKGROUND_COLOR } from './Editor'

export interface CodeSnippetProps {
fitted?: boolean
label?: string
mode?: 'jsx' | 'html' | 'sh'
value: string
style?: React.CSSProperties
}

const CodeSnippet = ({ label, value, mode = 'jsx', style, ...rest }: CodeSnippetProps) => (
<div
style={{
position: 'relative',
padding: '1rem',
marginBottom: '2rem',
background: EDITOR_BACKGROUND_COLOR,
...style,
}}
>
const formatters = {
sh: (val: string = ''): string => val.replace(/^/g, '$ '),
html: (val: string = ''): string => formatCode(val, 'html'),
jsx: (val: string = ''): string => formatCode(val, 'babylon'),
}

const CodeSnippet = ({ fitted, label, value, mode = 'jsx', ...rest }: CodeSnippetProps) => {
const format = formatters[mode]
const formattedValue = format(value)
// remove eof line break, they are not helpful for snippets
.replace(/\n$/, '')

return (
<div
style={{
position: 'absolute',
padding: '0.2rem 0.35rem',
top: '1rem',
right: '1rem',
lineHeight: 1,
color: '#899',
fontFamily: 'monospace',
fontSize: '0.8rem',
border: '1px solid #566',
zIndex: 100,
position: 'relative',
padding: '1rem',
marginBottom: fitted ? 0 : '2rem',
background: EDITOR_BACKGROUND_COLOR,
...rest.style,
}}
>
{label || mode}
<div
style={{
position: 'absolute',
padding: '0.2rem 0.35rem',
top: '1rem',
right: '1rem',
lineHeight: 1,
color: '#899',
fontFamily: 'monospace',
fontSize: '0.8rem',
border: '1px solid #566',
zIndex: 100,
}}
>
{label || mode}
</div>
<Editor
highlightActiveLine={false}
highlightGutterLine={false}
mode={mode}
readOnly
showGutter={false}
showCursor={false}
value={formattedValue}
{...rest}
/>
</div>
<Editor
id={btoa(value)}
highlightActiveLine={false}
highlightGutterLine={false}
mode={mode}
readOnly
showGutter={false}
showCursor={false}
value={mode === 'sh' ? value.replace(/^/g, '$ ') : value}
{...rest}
/>
</div>
)
)
}
export default CodeSnippet
Loading