-
-
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 options to the props component (#823)
- Loading branch information
1 parent
4d96db6
commit 9edaac8
Showing
6 changed files
with
257 additions
and
10 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
148 changes: 148 additions & 0 deletions
148
core/docz-theme-default/src/components/ui/Props/PropsTable.tsx
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,148 @@ | ||
import * as React from 'react' | ||
import { useMemo } from 'react' | ||
import { PropsComponentProps, useComponents } from 'docz' | ||
import styled from 'styled-components' | ||
|
||
import { get } from '@utils/theme' | ||
|
||
const breakpoint = '600px' | ||
|
||
const Container = styled.div` | ||
border: 1px solid ${get('colors.border')}; | ||
border-radius: 4px; | ||
overflow: hidden; | ||
background: ${get('colors.propsBg')}; | ||
color: ${get('colors.propsText')}; | ||
` | ||
|
||
const Line = styled.div` | ||
padding: 8px 0; | ||
@media (min-width: ${breakpoint}) { | ||
padding: O; | ||
} | ||
& + & { | ||
border-top: 1px solid ${get('colors.border')}; | ||
} | ||
` | ||
|
||
|
||
const Column = styled.div` | ||
min-width: 0; | ||
padding: 2px 15px; | ||
word-wrap: break-word; | ||
@media (min-width: ${breakpoint}) { | ||
padding: 8px 15px; | ||
} | ||
` | ||
|
||
const ColumnName = styled(Column)` | ||
@media (min-width: ${breakpoint}) { | ||
flex-basis: 25%; | ||
} | ||
` | ||
|
||
const ColumnType = styled(Column)` | ||
@media (min-width: ${breakpoint}) { | ||
flex-basis: 50%; | ||
} | ||
` | ||
|
||
const ColumnValue = styled(Column)` | ||
@media (min-width: ${breakpoint}) { | ||
flex-basis: 25%; | ||
} | ||
` | ||
|
||
const Content = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
font-family: ${get('fonts.mono')}; | ||
@media (min-width: ${breakpoint}) { | ||
flex-wrap: nowrap; | ||
flex-direction: row; | ||
} | ||
` | ||
|
||
const PropName = styled.span` | ||
color: ${get('colors.primary')}; | ||
font-weight: bold; | ||
` | ||
|
||
const PropType = styled.span` | ||
font-size: 0.9em; | ||
` | ||
|
||
const PropDefaultValue = styled.span` | ||
font-size: 0.9em; | ||
` | ||
|
||
const PropRequired = styled.span` | ||
font-size: 0.8em; | ||
opacity: 0.8; | ||
` | ||
|
||
export const PropsTable: React.SFC<PropsComponentProps> = ({ | ||
props, | ||
getPropType, | ||
}) => { | ||
const entries = Object.entries(props) | ||
const components = useComponents() | ||
const Paragraph = useMemo( | ||
() => styled(components.P || 'p')` | ||
margin: 0; | ||
font-size: 16px; | ||
color: ${get('colors.blockquoteColor')}; | ||
padding: 0 15px 8px 15px; | ||
`, | ||
[] | ||
) | ||
|
||
return ( | ||
<Container> | ||
{entries.map(([key, prop]) => { | ||
if (!prop.type && !prop.flowType) return null | ||
return ( | ||
<Line key={key}> | ||
<Content> | ||
<ColumnName> | ||
<PropName> | ||
{key} | ||
</PropName> | ||
</ColumnName> | ||
<ColumnType> | ||
<PropType> | ||
{getPropType(prop)} | ||
</PropType> | ||
</ColumnType> | ||
<ColumnValue> | ||
{prop.defaultValue && ( | ||
<PropDefaultValue> | ||
{prop.defaultValue.value === "''" ? ( | ||
<em>= [Empty String]</em> | ||
) : ( | ||
<em>= {prop.defaultValue.value.replace(/\'/g, '"')}</em> | ||
)} | ||
</PropDefaultValue> | ||
)} | ||
{prop.required && ( | ||
<PropRequired> | ||
<strong>required</strong> | ||
</PropRequired> | ||
)} | ||
</ColumnValue> | ||
</Content> | ||
{prop.description && ( | ||
<Paragraph> | ||
{prop.description} | ||
</Paragraph> | ||
)} | ||
</Line> | ||
) | ||
})} | ||
</Container> | ||
) | ||
} |
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,76 @@ | ||
import * as React from 'react' | ||
import { useState, useMemo } from 'react' | ||
import { PropsComponentProps, useComponents } from 'docz' | ||
import styled from 'styled-components' | ||
import { PropsRaw } from './PropsRaw' | ||
import { PropsTable } from './PropsTable' | ||
|
||
const Container = styled.div` | ||
margin: 20px 0; | ||
` | ||
|
||
export const Props: React.SFC<PropsComponentProps> = ({ | ||
title, | ||
isRaw, | ||
isToggle, | ||
...props | ||
}) => { | ||
const [isOpen, setIsOpen] = useState(true); | ||
|
||
const components = useComponents() | ||
const Title = useMemo( | ||
() => styled(components.H3 || 'h3')` | ||
padding: 8px 0; | ||
position: relative; | ||
${!isRaw ? 'margin-bottom: 0;' : ''} | ||
${!isOpen || isRaw | ||
? 'border-bottom: 1px solid rgba(0, 0, 0, 0.1);' | ||
: ''} | ||
${isToggle ? ` | ||
cursor: pointer; | ||
padding-right: 40px; | ||
&::after { | ||
content: ''; | ||
position: absolute; | ||
top: 50%; | ||
right: 16px; | ||
transform: translateY(-50%) ${isOpen ? 'rotate(-135deg)' : 'rotate(45deg)'}; | ||
${!isOpen ? 'margin-top: -2px;' : ''} | ||
width: 8px; | ||
height: 8px; | ||
border-bottom: 2px solid; | ||
border-right: 2px solid; | ||
} | ||
`: ''} | ||
`, | ||
[isOpen] | ||
) | ||
|
||
const titleProps = isToggle ? { | ||
onClick: () => setIsOpen(open => !open), | ||
onkeydown: () => setIsOpen(open => !open), | ||
role: 'button', | ||
tabindex: 0, | ||
} : {} | ||
|
||
return ( | ||
<Container> | ||
{(!!title || isToggle) && ( | ||
<Title {...titleProps}> | ||
{title || 'Component props'} | ||
</Title> | ||
)} | ||
{isOpen && ( | ||
<div> | ||
{isRaw ? ( | ||
<PropsRaw {...props} /> | ||
): ( | ||
<PropsTable {...props} /> | ||
)} | ||
</div> | ||
)} | ||
</Container> | ||
) | ||
} |
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