-
-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add TypeScript declarations to
@svgr/core
(#555)
- Loading branch information
Showing
7 changed files
with
233 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
export { default as File. } from './File.' | ||
export { default as File } from './File' |
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,96 @@ | ||
export interface TemplateOptions extends SvgrOpts {} | ||
|
||
export interface TemplateData { | ||
imports?: string[] | ||
interfaces?: string[] | ||
componentName?: string | ||
props?: string[] | ||
jsx?: string | ||
exports?: string[] | ||
} | ||
|
||
export type TemplateFunc = ( | ||
templateOptions: { template: unknown }, | ||
opts: TemplateOptions, | ||
data: TemplateData, | ||
) => string | ||
|
||
export interface SvgrOpts { | ||
/** Specify a custom config file. */ | ||
configFile?: string | ||
/** Replace SVG width and height with 1em to make SVG inherit text size. */ | ||
icon?: boolean | ||
/** Custom extension for generated files (default "js"). */ | ||
ext?: string | ||
/** Modify all SVG nodes with uppercase and use react-native-svg template. */ | ||
native?: boolean | { expo: boolean } | ||
/** Generate .tsx files with TypeScript bindings. */ | ||
typescript?: boolean | ||
/** Keep width and height attributes from the root SVG tag. */ | ||
dimensions?: boolean | ||
/** Forward all properties on the React component to the SVG tag. */ | ||
expandProps?: 'start' | 'end' | false | ||
/** Use Prettier to format JavaScript code output. */ | ||
prettier?: boolean | ||
/** Specify prettier config. */ | ||
prettierConfig?: Record<string, unknown> | ||
/** Use SVGO to optimize SVG code before transforming into a React component. Default: true. */ | ||
svgo?: boolean | ||
/** Specify SVGO config. https://gist.github.com/pladaria/69321af86ce165c2c1fc1c718b098dd0 */ | ||
svgoConfig?: Record<string, unknown> | ||
/** Forward the ref to the root SVG tag if true. */ | ||
ref?: boolean | ||
/** Wrap the exported component in React.memo if true. */ | ||
memo?: boolean | ||
/** | ||
* Replace an attribute value by another. Intended for changing an Icon | ||
* color to currentColor to inherit from text color. | ||
* | ||
* Specify dynamic property using curly braces: { '#000': "{props.color}" } | ||
*/ | ||
replaceAttrValues?: Record<string, string> | ||
/** | ||
* Add props to the SVG tag. | ||
* | ||
* Specify dynamic property using curly braces: { focusable: "{true}" }. | ||
* Particularly useful with a custom template. | ||
*/ | ||
svgProps?: Record<string, string> | ||
/** | ||
* Add title tag via title property. If titleProp is set to true and no | ||
* title is provided (title={undefined}) at render time, this will fallback | ||
* to an existing title element in the svg if it exists. | ||
*/ | ||
titleProp?: boolean | ||
/** | ||
* Specify a template file (CLI) or a template function (API) to use. | ||
* For an example of template, see the default one. | ||
* https://github.com/gregberge/svgr/blob/main/packages/babel-plugin-transform-svg-component/src/index.js | ||
*/ | ||
template?: TemplateFunc | ||
/** Output files into a directory. */ | ||
outDir?: string | ||
/** | ||
* Specify a template function (API) to change default index.js output | ||
* (when --out-dir is used). | ||
* | ||
* https://github.com/gregberge/svgr/blob/main/packages/cli/src/dirCommand.js#L39 | ||
*/ | ||
indexTemplate?: (filePaths: string[]) => string | ||
/** When used with --out-dir, it ignores already existing files. */ | ||
ignoreExisting?: boolean | ||
/** | ||
* Specify a custom filename case. Possible values: pascal for PascalCase, | ||
* kebab for kebab-case or camel for camelCase. | ||
*/ | ||
filenameCase?: 'kebab' | 'camel' | 'pascal' | ||
} | ||
|
||
type ConvertT = { | ||
(svgCode: string, opts?: SvgrOpts, state?: TemplateData): Promise<void> | ||
sync: (svgCode: string, opts?: SvgrOpts, state?: TemplateData) => void | ||
} | ||
|
||
declare const convert: ConvertT | ||
|
||
export default convert |
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,10 @@ | ||
import svgr from '.' | ||
|
||
// THROWS Argument of type 'null' is not assignable to parameter of type 'string'. | ||
svgr(null) | ||
|
||
// THROWS Argument of type 'undefined' is not assignable to parameter of type 'string'. | ||
svgr(undefined) | ||
|
||
// THROWS Expected 1-3 arguments, but got 0. | ||
svgr() |
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 svgr from '.' | ||
|
||
const svgCode = ` | ||
<svg xmlns="http://www.w3.org/2000/svg" | ||
xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<rect x="10" y="10" height="100" width="100" | ||
style="stroke:#ff0000; fill: #0000ff"/> | ||
</svg> | ||
` | ||
|
||
svgr(svgCode) | ||
svgr(svgCode, { icon: false }, { componentName: 'MyComponent' }) | ||
svgr(svgCode, { icon: false, memo: true }) | ||
svgr(svgCode, undefined, { componentName: 'MyComponent' }) | ||
svgr.sync(svgCode, { replaceAttrValues: { '#000': '{props.color}' } }) |
Oops, something went wrong.
681303a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: