-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
401 changed files
with
2,861 additions
and
5,498 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,11 +12,14 @@ | |
/.next/ | ||
/out/ | ||
|
||
.source | ||
# production | ||
/build | ||
|
||
# misc | ||
.DS_Store | ||
|
||
.npmrc | ||
.env* | ||
.idea | ||
|
||
|
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,100 @@ | ||
import fetch from 'node-fetch' | ||
import { JSDOM } from 'jsdom' | ||
import fs from 'fs-extra' | ||
import path from 'path' | ||
import { optimize } from 'svgo/lib/svgo' | ||
import { transform } from '@babel/core' | ||
import { | ||
moduleBabelConfig, | ||
allModulesBabelConfig, | ||
replaceAll, | ||
toHumpName, | ||
toComponentName, | ||
makeBasicDefinition, | ||
} from './utils' | ||
import { svgoOptions } from './svgo.config' | ||
|
||
const outputDir = path.join(__dirname, '../', '../', 'src/components/icons') | ||
const sourceFile = path.join(__dirname, '../', '../', '.source') | ||
|
||
export default (async () => { | ||
await fs.remove(outputDir) | ||
const html = await fs.readFile(sourceFile, 'utf8') | ||
const document = new JSDOM(html).window.document | ||
|
||
let exports = `import { SVGAttributes } from 'react'; | ||
export interface IconProps { | ||
size?: string|number, | ||
color?: string, | ||
} | ||
type NativeAttrs = Omit<SVGAttributes<SVGElement>, keyof IconProps> | ||
export type IconPropsNative = IconProps & NativeAttrs | ||
` | ||
let definition = makeBasicDefinition() | ||
|
||
const icons = document.querySelectorAll('.geist-list .icon') | ||
const promises = Array.from(icons).map(async (icon: Element) => { | ||
const name: string = icon.querySelector('.geist-text').textContent | ||
const componentName = toComponentName(name) | ||
const fileName = toHumpName(name) | ||
|
||
const svg = icon.querySelector('svg') | ||
|
||
const { data: optimizedSvgString } = await optimize(svg.outerHTML, svgoOptions) | ||
const styles = parseStyles(svg.getAttribute('style')) | ||
|
||
console.log(styles); | ||
|
||
const component = `'use client'; | ||
import React from 'react'; | ||
import {IconPropsNative} from './' | ||
const ${componentName} = ({ size = 24, color, style, ...props } : IconPropsNative ) => { | ||
return ${parseSvg(optimizedSvgString, styles, svg.getAttribute('style'))}; | ||
} | ||
export default ${componentName};` | ||
|
||
exports += `export { default as ${componentName} } from './${fileName}';\n` | ||
definition += `export const ${componentName}: Icon;\n` | ||
|
||
await fs.outputFile(path.join(outputDir, `${fileName}.tsx`), component) | ||
}) | ||
|
||
await Promise.all(promises) | ||
await fs.outputFile(path.join(outputDir, 'index.ts'), exports) | ||
})() | ||
|
||
const parseSvg = (svg: string, styles: any, styleOv) => { | ||
|
||
svg = svg.replace(styleOv, ''); | ||
svg = svg.replace('style="" ', ''); | ||
/* | ||
color: 'var(--geist-foreground)', | ||
'--geist-fill': 'currentcolor', -> ui-icon-background | ||
'--geist-stroke': 'var(--geist-background)', -> --ui-icon-background | ||
*/ | ||
|
||
svg = replaceAll(svg, 'var(--geist-fill)', 'currentColor'); | ||
svg = replaceAll(svg, '--geist-stroke', '--ui-icon-background'); | ||
|
||
svg = svg.replace(/-([a-z])(?=[a-z\-]*[=\s/>])/g, g => g[1].toUpperCase()) | ||
svg = svg.replace(/<svg([^>]+)>/, `<svg$1 {...props} height={size} width={size} style={{...style, color: color }}>`) | ||
|
||
return svg | ||
} | ||
|
||
const parseStyles = (inlineStyle = '') => { | ||
return (inlineStyle || '').split(';').reduce((styleObject, stylePropertyValue) => { | ||
// extract the style property name and value | ||
let [property, value] = stylePropertyValue | ||
.split(/^([^:]+):/) | ||
.filter((val, i) => i > 0) | ||
.map(item => item.trim().toLowerCase()) | ||
|
||
styleObject[property] = value | ||
return styleObject | ||
}, {}) | ||
|
||
|
||
} |
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,15 @@ | ||
import fetch from 'node-fetch' | ||
import path from 'path' | ||
import fs from 'fs-extra' | ||
const sourceFile = path.join(__dirname, '../', '../', '.source') | ||
|
||
export default (async () => { | ||
try { | ||
const res = await fetch('https://vercel.com/design/icons') | ||
const html = await res.text() | ||
await fs.writeFile(sourceFile, html) | ||
} catch (e) { | ||
console.log(e) | ||
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,27 @@ | ||
import { parserStyle, getSpecifiedColorVar } from './utils' | ||
import type { Config, CustomPlugin } from 'svgo' | ||
import type { XastElement } from 'svgo/lib/types' | ||
|
||
|
||
|
||
export const svgoOptions: Config = { | ||
plugins: [ | ||
{ | ||
name: 'preset-default', | ||
params: { | ||
overrides: { | ||
removeViewBox: false, | ||
cleanupNumericValues: { | ||
floatPrecision: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: 'removeAttrs', | ||
params: { | ||
attrs: ['svg:width', 'svg:height', 'svg:color', 'svg:data-testid', 'svg:class'], | ||
}, | ||
}, | ||
], | ||
} |
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,17 @@ | ||
{ | ||
"compilerOptions": { | ||
"outDir": "../../dist/icons-build", | ||
"sourceMap": false, | ||
"noImplicitAny": false, | ||
"esModuleInterop": true, | ||
"module": "commonjs", | ||
"target": "es2015" | ||
}, | ||
"include": [ | ||
"**/*" | ||
], | ||
"exclude": [ | ||
"node_modules", | ||
"**/*.spec.ts" | ||
] | ||
} |
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,48 @@ | ||
export const moduleBabelConfig = { | ||
presets: ['@babel/preset-env', '@babel/preset-react'], | ||
plugins: [ | ||
['@babel/plugin-proposal-object-rest-spread', { loose: true }], | ||
'@babel/plugin-transform-runtime', | ||
], | ||
minified: true, | ||
} | ||
|
||
export const allModulesBabelConfig = { | ||
presets: ['@babel/preset-env'], | ||
minified: true, | ||
} | ||
|
||
export const replaceAll = (target: string, find: string, replace: string): string => { | ||
return target.split(find).join(replace) | ||
} | ||
|
||
export const toHumpName = (name: string): string => { | ||
return name.replace(/-(.)/g, g => g[1].toUpperCase()) | ||
} | ||
|
||
export const toComponentName = (name: string): string => { | ||
const first = name.slice(0, 1).toUpperCase() | ||
const last = toHumpName(name.slice(1)) | ||
return `${first}${last}` | ||
} | ||
|
||
export const makeBasicDefinition = (): string => { | ||
return `import React from 'react'; | ||
interface Props extends React.SVGProps<SVGElement> { | ||
color?: string; | ||
size?: number | string; | ||
} | ||
type Icon = React.FunctionComponent<Props>;\n` | ||
} | ||
|
||
export const parserStyle = (inlineStyle: string) => { | ||
return inlineStyle | ||
.split(';') | ||
.filter((_, i) => i > 0) | ||
.map(item => item.trim().toLowerCase()) | ||
} | ||
|
||
export const getSpecifiedColorVar = (val: string, ident: string) => { | ||
if (!val) return '' | ||
return val.includes(ident) ? '{color}' : 'var(--geist-icons-background)' | ||
} |
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
Oops, something went wrong.