Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate react-native icon components that use react-native-svg #87

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,22 @@ yarn add @lifeomic/chromicons
import { Apple } from '@lifeomic/chromicons';
```

**With React-Native (react-native-svg)**

1. Install Dependencies

```bash
yarn add @lifeomic/chromicons react-native-svg
```

2. Import the Icon Components

```jsx
import { Apple } from '@lifeomic/chromicons/src/lined/react-native';

<Apple stroke="red" />;
```

### Web App 💻

You can view and copy all of the SVGs to your clipboard via the [web app](https://lifeomic.github.io/chromicons.com/). It also provides a link to download all raw SVGs.
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
"analyze": "size-limit --why",
"build": "tsdx build",
"clean": "rm -rf src/lined && rm -rf dist/",
"generate:build-output": "yarn generate:react-components && yarn generate:index-file",
"generate:build-output": "yarn generate:react-components && yarn generate:react-native-components && yarn generate:index-file",
"generate:index-file": "echo \"export * from './lined'\" > src/index.tsx",
"generate:react-components": "svgr --template svg-template.js src/raw/lined --out-dir src/lined --typescript",
"generate:react-native-components": "svgr --template svg-rn-template.js src/raw/lined --out-dir src/lined/react-native --typescript --native",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome, svgr is great! Glad you figured this out!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
Expand All @@ -46,7 +47,13 @@
"typescript": "^4.1.2"
},
"peerDependencies": {
"react": ">=16"
"react": ">=16",
"react-native-svg": "12.x"
},
"peerDependenciesMeta": {
"react-native-svg": {
"optional": true
}
},
"prettier": {
"printWidth": 80,
Expand Down
32 changes: 32 additions & 0 deletions svg-rn-template.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* This template allows us to modify the generated React Native component template.
*/

function template(
{ template },
_,
{ imports, componentName, props, jsx, exports }
) {
jsx.openingElement.attributes = [
...jsx.openingElement.attributes,
{
type: 'JSXAttribute',
name: { type: 'JSXIdentifier', name: 'viewBox' },
value: { type: 'StringLiteral', value: '0 0 24 24' },
},
// For our generated React components, we think this makes sense
// to do by default: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-hidden_attribute
{
type: 'JSXAttribute',
name: { type: 'JSXIdentifier', name: 'importantForAccessibility' },
value: { type: 'StringLiteral', value: 'no-hide-descendants' },
},
];
return template.ast`
${imports}
const ${componentName} = (${props}) => ${jsx}
${exports}
`;
}

module.exports = template;