Skip to content

Commit

Permalink
feat(docz): add groups feature
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 18, 2018
1 parent 48a0484 commit 9652b30
Show file tree
Hide file tree
Showing 25 changed files with 299 additions and 108 deletions.
6 changes: 0 additions & 6 deletions examples/basic/src/Button.doc.jsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import React, { Fragment } from 'react'
import { doc } from 'docz'

import { Alert } from './Alert'
import { components } from './'

doc('Alert')
.group(components)
.description('This component is used to show alerts')
.section('Basic usage', () => <Alert>Some message</Alert>)
.section('Using different kinds', () => (
Expand Down
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/basic/src/components/Button.doc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import { doc } from 'docz'

import { Button } from './Button'
import { components } from './'

doc('Button')
.group(components)
.section(() => <Button>Click me</Button>)
File renamed without changes.
6 changes: 6 additions & 0 deletions examples/basic/src/components/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { group } from 'docz'

export { Alert } from './Alert'
export { Button } from './Button'

export const components = group('Components')
6 changes: 6 additions & 0 deletions examples/basic/src/design.doc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react'
import { doc } from 'docz'

doc('Design System')
.order(0)
.section(() => <div>Now we will talk about design systems!</div>)
4 changes: 4 additions & 0 deletions examples/basic/src/hocs/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { group } from 'docz'
export { withProps } from './withProps'

export const hocs = group('hocs')
8 changes: 8 additions & 0 deletions examples/basic/src/hocs/withProps.doc.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import React from 'react'
import { doc } from 'docz'

import { hocs } from './'

doc('withProps')
.group(hocs)
.section(() => <div>This section talk about withProps hoc</div>)
5 changes: 5 additions & 0 deletions examples/basic/src/hocs/withProps.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import React from 'react'

export const withProps = (mapProps = p => p) => WrappedComponent => props => (
<WrappedComponent {...props} {...mapProps(props)} />
)
2 changes: 0 additions & 2 deletions examples/basic/src/index.jsx

This file was deleted.

44 changes: 0 additions & 44 deletions packages/docz-theme-default/src/components/List.jsx

This file was deleted.

85 changes: 85 additions & 0 deletions packages/docz-theme-default/src/components/Menu.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React from 'react'
import { NavLink as BaseLink } from 'react-router-dom'
import styled, { css } from 'react-emotion'

import { Links } from 'docz'

const BORDER_COLOR = '#ced6e0'
const LINK_COLOR = '#2f3542'
const PURPLE = '#5352ed'

const Sidebar = styled('div')`
padding: 20px 0;
width: 250px;
height: 100vh;
border-right: 1px solid ${BORDER_COLOR};
`

const List = styled('ul')`
list-style: none;
padding: 5px 15px;
margin: 0;
& ~ & {
margin-top: 10px;
}
`

const GroupTitle = styled('div')`
font-size: 12px;
text-transform: uppercase;
color: #b2bec3;
`

const LinkStyled = styled(BaseLink)`
display: block;
padding: 4px 0;
font-size: 14px;
font-weight: 700;
&,
&:visited {
color: ${LINK_COLOR};
}
&.active {
color: ${PURPLE};
}
`

const Link = props => {
const isActive = (match, location) => match && match.url === location.pathname
return <LinkStyled isActive={isActive} {...props} />
}

export const Menu = () => (
<Links>
{({ groups, docs }) => (
<Sidebar>
<List>
{docs.filter(doc => !doc.group).map(doc => (
<li key={doc.id}>
<Link to={doc.route}>{doc.name}</Link>
</li>
))}
</List>
{groups.map(group => (
<List key={group.id}>
<li>
<GroupTitle>{group.name}</GroupTitle>
<List>
{docs
.filter(doc => doc.group && doc.group.id === group.id)
.map(doc => (
<li key={doc.id}>
<Link to={doc.route}>{doc.name}</Link>
</li>
))}
</List>
</li>
</List>
))}
</Sidebar>
)}
</Links>
)
2 changes: 1 addition & 1 deletion packages/docz-theme-default/src/components/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Preview } from 'docz'
const Container = styled('div')`
width: 960px;
max-width: 960px;
padding: 50px 20px;
padding: 50px;
margin: 0 auto;
`

Expand Down
6 changes: 3 additions & 3 deletions packages/docz-theme-default/src/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { createTheme } from 'docz'
import { injectGlobal } from 'emotion'
import { Helmet } from 'react-helmet'

import { List } from './components/List'
import { View } from './components/View'
import { Main } from './components/Main'
import { Menu } from './components/Menu'
import { View } from './components/View'

export const Theme = createTheme(() => (
<React.Fragment>
Expand All @@ -18,7 +18,7 @@ export const Theme = createTheme(() => (
/>
</Helmet>
<Main>
<List />
<Menu />
<View />
</Main>
</React.Fragment>
Expand Down
4 changes: 3 additions & 1 deletion packages/docz-theme-default/src/styles.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { css, injectGlobal } from 'emotion'

const BACKGROUND = '#white'
const BACKGROUND = 'white'
const TEXT_COLOR = '#2f3542'
const LINK_COLOR = '#5352ed'

Expand All @@ -24,6 +24,8 @@ injectGlobal`
}
body {
margin: 0;
padding: 0;
font-family: 'Noto Sans', sans-serif;
font-size: 16px;
line-height: 1.5;
Expand Down
38 changes: 33 additions & 5 deletions packages/docz/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,39 @@ export interface Section {
render: () => ReactNode
}

export interface GroupConstructorArgs {
name: string
}

export class Group {
public constructor({ name }: GroupConstructorArgs)

public route(value: string): Group
public order(num: number): Group
public toObject(): GroupObj

public name: string
}

export interface GroupObj {
id: string
name: string
order: number
route: string
}

export interface DocConstructorArgs {
name: string
}

export class Doc {
public constructor({ name }: DocConstructorArgs)

public order(num: number): Doc
public group(group: Group): Doc
public route(value: string): Doc
public description(value: string): Doc
public section(...args: any[]): Doc
public route(value: string): Doc
public order(num: number): Doc
public toObject(): DocObj

public name: string
Expand All @@ -25,6 +47,7 @@ export class Doc {
export interface DocObj {
readonly id: string
readonly name: string
readonly group: GroupObj | null
readonly sections: Section[]
readonly description: string | null
readonly route: string
Expand All @@ -43,8 +66,13 @@ export interface PreviewProps {

export const Preview: SFC<PreviewProps>

export interface DocsProps {
children: (docs: DocObj[]) => ReactNode
export interface LinksObj {
groups: GroupObj[]
docs: DocObj[]
}

export interface LinksProps {
children: ({ groups, docs }: LinksObj) => ReactNode
}

export const Docs: SFC<DocsProps>
export const Links: SFC<LinksProps>
28 changes: 20 additions & 8 deletions packages/docz/src/Doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
import { ulid } from 'ulid'
import kebabcase from 'lodash.kebabcase'

import { isFn } from './utils/helpers'
import { Section, DocConstructorArgs, DocObj } from '../'
import { isFn, safeUrl } from './utils/helpers'
import { Section, DocConstructorArgs, DocObj, Group, GroupObj } from '../'
import { docsContainer } from './DocsContainer'

class Doc {
private _id: string
private _name: string
private _description: string | null
private _group: GroupObj | null
private _sections: Section[]
private _route: string
private _order: number
Expand All @@ -18,6 +19,7 @@ class Doc {
this._id = ulid()
this._name = name
this._description = null
this._group = null
this._sections = []
this._route = `/${kebabcase(name)}`
this._order = 0
Expand All @@ -29,6 +31,20 @@ class Doc {
* setters
*/

public group(group: Group): Doc {
const groupObj = group.toObject()

this._group = groupObj
this._route = groupObj.route + this._route

return this
}

public order(num: number): Doc {
this._order = num
return this
}

public description(value: string): Doc {
this._description = value
return this
Expand All @@ -48,12 +64,7 @@ class Doc {
}

public route(path: string): Doc {
this._route = encodeURI(path.replace(/\s/g, ''))
return this
}

public order(num: number): Doc {
this._order = num
this._route = safeUrl(path)
return this
}

Expand All @@ -68,6 +79,7 @@ class Doc {
public toObject(): DocObj {
return {
description: this._description,
group: this._group,
id: this._id,
name: this._name,
order: this._order,
Expand Down
Loading

0 comments on commit 9652b30

Please sign in to comment.