-
Notifications
You must be signed in to change notification settings - Fork 831
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from j-f1/add-types
Add TypeScript types to the React integration
- Loading branch information
Showing
7 changed files
with
180 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env node | ||
const fse = require('fs-extra') | ||
const {join, resolve} = require('path') | ||
|
||
const srcDir = resolve(__dirname, '../src/__generated__') | ||
const iconsSrc = join(srcDir, 'icons.d.ts') | ||
const indexSrc = join(srcDir, '../index.d.ts') | ||
|
||
const destDir = resolve(__dirname, '../dist') | ||
const iconsDest = join(destDir, 'icons.d.ts') | ||
const indexDest = join(destDir, 'index.d.ts') | ||
|
||
fse.copy(iconsSrc, iconsDest).catch(die) | ||
fse | ||
.readFile(indexSrc, 'utf8') | ||
.then(content => content.replace(/.\/__generated__\//g, './')) | ||
.then(fse.writeFile.bind(fse, indexDest)) | ||
.catch(die) | ||
|
||
function die(err) { | ||
console.error(err.stack) | ||
process.exit(1) | ||
} |
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,24 @@ | ||
import * as React from 'react' | ||
|
||
import {Icon} from './__generated__/icons' | ||
|
||
type Size = 'small' | 'medium' | 'large' | ||
export interface OcticonProps { | ||
ariaLabel?: string | ||
children?: React.ReactElement<any> | ||
height?: number | ||
icon: Icon | ||
size?: number | Size | ||
verticalAlign?: 'middle' | 'text-bottom' | 'text-top' | 'top' | ||
width?: number | ||
} | ||
|
||
declare const Octicon: React.SFC<OcticonProps> | ||
export default Octicon | ||
|
||
export function createIcon<C extends React.SFC<{}>, W extends number, H extends number>( | ||
component: C, | ||
size: [W, H] | ||
): Icon<W, H> | ||
|
||
export * from './__generated__/icons' |
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 |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import * as React from 'react' | ||
import Octicon, { | ||
OcticonProps, | ||
Beaker, | ||
Zap, | ||
Repo, | ||
Plus, | ||
LogoGithub, | ||
getIconByName, | ||
iconsByName, | ||
createIcon | ||
} from '../src' | ||
|
||
function Icon({boom}: {boom: boolean}): React.ReactNode { | ||
return <Octicon icon={boom ? Zap : Beaker} /> | ||
} | ||
|
||
function OcticonByName({name, ...props}: {name: keyof iconsByName} & OcticonProps): React.ReactNode { | ||
return <Octicon {...props} icon={getIconByName(name)} /> | ||
} | ||
|
||
// Unfortunately, `Object.keys` returns `string[]` unconditionally; | ||
// see https://github.com/Microsoft/TypeScript/pull/13971 & | ||
// https://github.com/Microsoft/TypeScript/issues/12870 for details. | ||
function keys<T>(obj: T): (keyof T)[] { | ||
return Object.keys(obj) as (keyof T)[] | ||
} | ||
|
||
function OcticonsList() { | ||
return ( | ||
<ul> | ||
{keys(iconsByName).map(key => ( | ||
<li key={key}> | ||
<code>{key}</code> | ||
<Octicon icon={iconsByName[key]} /> | ||
</li> | ||
))} | ||
</ul> | ||
) | ||
} | ||
|
||
function VerticalAlign() { | ||
return ( | ||
<h1> | ||
<Octicon icon={Repo} size="large" verticalAlign="middle" /> github/github | ||
<Octicon icon={Plus} ariaLabel="Add new item" /> New | ||
<Octicon icon={LogoGithub} size="large" ariaLabel="GitHub" /> | ||
</h1> | ||
) | ||
} | ||
|
||
const CirclesIcon = createIcon( | ||
() => { | ||
return ( | ||
<React.Fragment> | ||
<circle r={5} cx={5} cy={5} /> | ||
<circle r={5} cx={15} cy={5} /> | ||
<circle r={5} cx={25} cy={5} /> | ||
</React.Fragment> | ||
) | ||
}, | ||
[30, 10] | ||
) | ||
|
||
export function CirclesOcticon(props: OcticonProps) { | ||
return <Octicon {...props} icon={CirclesIcon} /> | ||
} |
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,12 @@ | ||
{ | ||
"$schema": "http://json.schemastore.org/tsconfig", | ||
"compileOnSave": false, | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"noEmit": true, | ||
"noImplicitAny": true, | ||
"jsx": "react", | ||
"lib": ["es2015"] | ||
}, | ||
"files": ["../src/index.d.ts", "./index.tsx"] | ||
} |