Skip to content

Commit

Permalink
feat(docz): add module declarations for typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
pedronauck committed Apr 15, 2018
1 parent f8e0383 commit ae597af
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 63 deletions.
2 changes: 1 addition & 1 deletion packages/playgrodd/.librc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"external": ["react", "react-dom", "react-router-dom", "history"]
"external": ["react-router-dom", "history"]
}
54 changes: 54 additions & 0 deletions packages/playgrodd/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { SFC, ReactNode, ComponentType } from 'react'

declare module 'playgrodd' {
export interface Section {
id: string
title?: string
render: () => ReactNode
}

export interface DocArgs {
name: string
}

export interface DocConstructorArgs {
name: string
}

export class Doc {
public id: string
public name: string
public docDescription: string | null
public sections: Section[]
public route: string

public constructor({ name }: DocConstructorArgs)

public description(value: string): Doc
public section(...args: any[]): Doc
}

export interface DocMap {
[key: string]: Doc
}

export type Docs = Doc[]

/**
* Components
*/

export function createTheme(WrappedComponent: ComponentType): ComponentType

export interface PreviewProps {
children: (doc: Doc) => ReactNode
}

export const Preview: SFC<PreviewProps>

export interface DocsProps {
children: (docs: Doc[]) => ReactNode
}

export const Docs: SFC<DocsProps>
}
7 changes: 5 additions & 2 deletions packages/playgrodd/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
},
"scripts": {
"dev": "libundler watch --ts",
"build": "libundler build --ts",
"build:prod": "yarn run build --compress --sourcemap",
"build": "libundler build --ts --compress --sourcemap",
"fix": "run-s fix:*",
"fix:prettier": "prettier \"src/**/*.{ts,tsx}\" --write",
"fix:tslint": "tslint --fix --project ."
Expand All @@ -27,6 +26,10 @@
"unstated": "^1.1.0",
"yargs": "^11.0.0"
},
"peerDependencies": {
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"devDependencies": {
"@types/babel-traverse": "^6.25.3",
"@types/babylon": "^6.16.2",
Expand Down
12 changes: 12 additions & 0 deletions packages/playgrodd/src/components/Docs.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react'
import { SFC } from 'react'
import { Subscribe } from 'unstated'
import { DocsProps } from 'playgrodd'

import { DocsContainer } from '../container'

export const Docs: SFC<DocsProps> = ({ children }) => (
<Subscribe to={[DocsContainer]}>
{({ state }) => children(Object.values(state.docs))}
</Subscribe>
)
25 changes: 19 additions & 6 deletions packages/playgrodd/src/components/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
import * as React from 'react'
import { SFC } from 'react'
import { Subscribe } from 'unstated'
import { Route } from 'react-router-dom'
import { PreviewProps, Doc, Docs } from 'playgrodd'

import { DocumentsContainer } from '../container'
import { IDoc } from '../documents'
import { DocsContainer } from '../container'

export const Preview: React.SFC = () => (
<Subscribe to={[DocumentsContainer]}>
export const Preview: SFC<PreviewProps> = ({ children }) => (
<Subscribe to={[DocsContainer]}>
{({ state }) => {
const documents: IDoc[] = Object.values(state.documents)
return documents.length > 0 && documents.map(doc => doc.getName())
const docs: Docs = Object.values(state.docs)

return (
docs.length > 0 &&
docs.map((doc: Doc) => (
<Route
exact
key={doc.id}
path={doc.route}
render={() => children(doc)}
/>
))
)
}}
</Subscribe>
)
2 changes: 1 addition & 1 deletion packages/playgrodd/src/components/create-theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { container } from '../container'

export const history: History = createBrowserHistory()

export interface ICreateTheme {
interface ICreateTheme {
(WrappedComponent: React.ComponentType): React.ComponentType
}

Expand Down
24 changes: 9 additions & 15 deletions packages/playgrodd/src/container/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import { Container } from 'unstated'
import { IDoc, IDocMap } from '../documents'
import { Doc, DocMap } from 'playgrodd'

export interface DocumentState {
documents: IDocMap | undefined
interface DocsState {
docs: DocMap | undefined
}

export class DocumentsContainer extends Container<DocumentState> {
export class DocsContainer extends Container<DocsState> {
constructor() {
super()
this.state = {
documents: {},
}
this.state = { docs: {} }
}

public addDoc(doc: IDoc) {
public addDoc(doc: Doc) {
this.setState({
documents: Object.assign({}, this.state.documents, {
[`${doc.getName()}`]: doc,
docs: Object.assign({}, this.state.docs, {
[`${doc.name}`]: doc,
}),
})
}

public getDocuments(): IDocMap | undefined {
return this.state.documents
}
}

export const container = new DocumentsContainer()
export const container = new DocsContainer()
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
import { ulid } from 'ulid'
import { Section, DocConstructorArgs } from 'playgrodd'

import { container } from '../container'

const isFn = (value: any): boolean => typeof value === 'function'

export interface IRenderMethod {
(): JSX.Element
}

export interface ISection {
id: string
render: IRenderMethod
title?: string
}

export interface IDocArgs {
name: string
}

export interface IDoc {
description(value: string): Doc
section(...args: any[]): Doc
getName(): string
getDescription(): string | null
getSections(): ISection[]
}

export interface IDocMap {
[key: string]: IDoc
}

export class Doc implements IDoc {
export class Doc {
private _id: string
private _name: string
private _description: string | null
private _sections: ISection[]
private _sections: Section[]

constructor({ name }: IDocArgs) {
constructor({ name }: DocConstructorArgs) {
this._id = ulid()
this._name = name
this._sections = []
this._description = null
Expand All @@ -45,14 +23,14 @@ export class Doc implements IDoc {

// setters

public description(value: string) {
public description(value: string): Doc {
this._description = value
return this
}

public section(...args: any[]) {
public section(...args: any[]): Doc {
const [title, renderMethod] = args
const render: IRenderMethod = isFn(title) ? title : renderMethod
const render = isFn(title) ? title : renderMethod

this._sections.push({
render,
Expand All @@ -65,17 +43,25 @@ export class Doc implements IDoc {

// getters

public getName() {
public get id(): string {
return this._id
}

public get name(): string {
return this._name
}

public getDescription() {
public get docDescription(): string | null {
return this._description
}

public getSections() {
public get sections(): Section[] {
return this._sections
}

public get route(): string {
return `/${this._name}`
}
}

export const doc = (name: string): Doc => new Doc({ name })
3 changes: 2 additions & 1 deletion packages/playgrodd/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { Docs } from './components/Docs'
export { Preview } from './components/Preview'
export { createTheme } from './components/create-theme'
export { doc } from './documents'
export { doc } from './docs'
1 change: 0 additions & 1 deletion packages/playgrodd/src/types.d.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/playgrodd/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"compilerOptions": {
"outDir": "dist/main",
"rootDir": "src",
"declaration": false,
"types": ["node"],
"typeRoots": ["node_modules/@types", "src/types"]
"typeRoots": ["node_modules/@types", "index.d.ts"]
},
"include": ["src/**/*"],
"exclude": ["node_modules/**"]
Expand Down

0 comments on commit ae597af

Please sign in to comment.