Skip to content

Commit 475927a

Browse files
committed
Add icon packages to v2 (#598)
1 parent fac833f commit 475927a

File tree

164 files changed

+1122
-90
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

164 files changed

+1122
-90
lines changed

.changeset/gold-shrimps-peel.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@obosbbl/grunnmuren-icons-react': major
3+
'@obosbbl/grunnmuren-icons-svg': major
4+
---
5+
6+
v2 icon packages

.prettierignore

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ pnpm-lock.yaml
22
renovate.json
33
.changeset/
44
dist/
5-
packages/icons/icons.tsx
6-
packages/icons-react/icons.tsx
5+
packages/icons-react/src/index.tsx

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"scripts": {
99
"build": "pnpm --filter './packages/**' build",
10+
"build:netlify": "pnpm --filter @obosbbl/grunnmuren-icons-react build && pnpm build:storybook",
1011
"build:storybook": "storybook build",
1112
"ci:publish": "pnpm build && changeset publish",
1213
"ci:version": "changeset version",

packages/icons-react/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# The React icons are written to folder before bundling
2+
src/

packages/icons-react/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# @obosbbl/grunnmuren-icons-react
2+
3+
Grunnmuren's icon set as React components.
4+
5+
To reduce the package installation size, this
6+
7+
If you want the raw SVG files for the icons, see [`@obosbbl/grunnmuren-icons-svg`](../@obosbbl/grunnmuren-icons-svg) instead.
8+
9+
## Install
10+
11+
```sh
12+
npm install @obosbbl/grunnmuren-icons-react
13+
14+
pnpm add @obosbbl/grunnmuren-icons-react
15+
```
16+
17+
## Usage
18+
19+
```jsx
20+
import { House } from '@obosbbl/grunnmuren-icons-react';
21+
22+
export function Page() {
23+
return <House />;
24+
}
25+
```
26+
27+
## Accessibility
28+
29+
The SVG markup has `role="img"` set to indicate to screen readers that the element should be interpreted as an image.
30+
31+
Since icons mostly are used as a visual decoration, they will also render with `aria-hidden="true"` by default, unless you specify an accessible label using `aria-label`.
32+
33+
```jsx
34+
<House />
35+
// <svg role="img" aria-hidden="true">...</svg>
36+
37+
<House aria-label="Hus" />
38+
// <svg role="img" aria-label="Hus">...</svg>
39+
```

packages/icons-react/package.json

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "@obosbbl/grunnmuren-icons-react",
3+
"version": "1.0.0",
4+
"description": "Grunnmuren's icon set as React components",
5+
"repository": {
6+
"url": "https://github.com/code-obos/grunnmuren"
7+
},
8+
"license": "MIT",
9+
"sideEffects": false,
10+
"type": "module",
11+
"exports": {
12+
"types": "./dist/index.d.ts",
13+
"default": "./dist/index.mjs"
14+
},
15+
"main": "dist/index.mjs",
16+
"module": "dist/index.mjs",
17+
"types": "./dist/index.d.ts",
18+
"files": [
19+
"dist"
20+
],
21+
"scripts": {
22+
"build": "pnpm run build:make-react && pnpm run build:unbuild",
23+
"build:make-react": "node ./scripts/make-react-icons.mjs",
24+
"build:unbuild": "unbuild"
25+
},
26+
"devDependencies": {
27+
"@obosbbl/grunnmuren-icons-svg": "workspace:*",
28+
"@svgr/core": "8.1.0",
29+
"@svgr/plugin-jsx": "8.1.0",
30+
"@svgr/plugin-svgo": "8.1.0",
31+
"fs-extra": "11.1.1"
32+
},
33+
"peerDependencies": {
34+
"react": "^18"
35+
},
36+
"unbuild": {
37+
"rollup": {
38+
"esbuild": {
39+
"jsx": "automatic"
40+
}
41+
}
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#!/usr/bin/env node
2+
import { transform } from '@svgr/core';
3+
import path from 'path';
4+
import fs from 'fs-extra';
5+
import { __dirname, listSvgs } from './utils.mjs';
6+
7+
// Path where SVGs are located
8+
const SVG_PATH = path.join(
9+
__dirname,
10+
'../node_modules/@obosbbl/grunnmuren-icons-svg/src',
11+
);
12+
13+
// The path where we write the SVGs as React components.
14+
// The file is temporary, for pre bundling.
15+
const REACT_FILE = path.join(__dirname, '../src/index.tsx');
16+
17+
const files = listSvgs(SVG_PATH);
18+
19+
// Create the React file
20+
fs.outputFileSync(REACT_FILE, '');
21+
fs.appendFile(REACT_FILE, 'import type { SVGProps } from "react";\n');
22+
23+
files.forEach(async (file) => {
24+
const jsx = await toReact(file);
25+
fs.appendFile(REACT_FILE, jsx + '\n');
26+
});
27+
28+
// Inline SVGs doesn't need the namespace
29+
// This is the only svgo optimization we perform, as the SVGs are already optimized
30+
const svgoConfig = { plugins: ['removeXMLNS'] };
31+
32+
// We write all the icons to a single file, so we modify the default template to remove all other stuff
33+
// Default template: https://github.com/gregberge/svgr/blob/main/packages/babel-plugin-transform-svg-component/src/defaultTemplate.ts
34+
function reactTemplate(variables, { tpl }) {
35+
return tpl`
36+
export const ${variables.componentName} = (${variables.props}) => (
37+
${variables.jsx}
38+
);
39+
`;
40+
}
41+
42+
async function toReact(svgFilePath) {
43+
const svg = await fs.readFile(svgFilePath, 'utf-8');
44+
45+
const componentName = path.parse(svgFilePath).name;
46+
47+
const jsx = await transform(
48+
svg,
49+
{
50+
plugins: ['@svgr/plugin-svgo', '@svgr/plugin-jsx'],
51+
// Setting this to false, as it changes the height/width of the svg
52+
icon: false,
53+
svgo: true,
54+
typescript: true,
55+
svgProps: {
56+
role: 'img',
57+
// If an aria-label is not specified, the icon is automatically hidden from screen readers
58+
'aria-hidden': '{props["aria-label"] == null}',
59+
},
60+
svgoConfig,
61+
template: reactTemplate,
62+
},
63+
{ componentName },
64+
);
65+
66+
return jsx;
67+
}
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import fs from 'fs-extra';
2+
import path from 'path';
3+
import { fileURLToPath } from 'url';
4+
5+
export const __dirname = path.dirname(fileURLToPath(import.meta.url));
6+
7+
// Assumes the folder only contains SVGs. Poor man's glob
8+
export function listSvgs(dirPath) {
9+
const files = fs.readdirSync(dirPath);
10+
11+
return files.map((file) => path.join(dirPath, file));
12+
}

packages/icons-react/tsconfig.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../../tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "dist"
5+
}
6+
}

packages/icons-svg/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# The raw folder is where the downloaded icons go
2+
/raw
3+
.FIGMA_TOKEN

packages/icons-svg/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# @obosbbl/grunnmuren-icons-svg
2+
3+
Grunnmuren's icon set as raw SVG files.
4+
5+
If you are using React, you may want to use [`@obosbbl/grunnmuren-icons-react`](../@obosbbl/grunnmuren-icons-react) instead.
6+
7+
## Install
8+
9+
```sh
10+
npm install @obosbbl/grunnmuren-icons-svg
11+
12+
pnpm add @obosbbl/grunnmuren-icons-react
13+
```
14+
15+
## Usage
16+
17+
```jsx
18+
// SVG
19+
import House from '@obosbbl/grunnmuren-icons-svg/src/House.svg';
20+
```
21+
22+
## Updating the icons
23+
24+
To update the icons, run the _update_ and _build_ scripts. The icons should never be edited manually, as the source of truth is in [Figma](https://www.figma.com/file/XRHRRytz9DqrDkWpE4IKVB/OBOS-DS?node-id=2192%3A33204).
25+
26+
Running the `update` script downloads all the icons as SVG files and perform some basic optimizations on them. The icons are checked into the [src folder](./src).
27+
28+
```sh
29+
pnpm run update
30+
```
31+
32+
### Figma access token
33+
34+
If you are running the import script for the first time, it will prompt your for a [Figma access token](https://www.figma.com/developers/api#access-tokens). The token is is required to access Figma's API. It can be generated on your Figma account settings page.
35+
36+
The import script may store the token to a local file, so you won't have to supply the token again on subsequent runs.
37+
38+
If the script fails with authentication issues, you could try to create a new access token and delete the local file `.FIGMA_TOKEN` before running the script again.

packages/icons-svg/package.json

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "@obosbbl/grunnmuren-icons-svg",
3+
"version": "1.0.0",
4+
"description": "Grunnmuren's icon set as raw SVG files",
5+
"repository": {
6+
"url": "https://github.com/code-obos/grunnmuren"
7+
},
8+
"license": "MIT",
9+
"type": "module",
10+
"exports": {
11+
".": "./src"
12+
},
13+
"files": [
14+
"src"
15+
],
16+
"scripts": {
17+
"update": "pnpm run update:download && pnpm run update:optimize",
18+
"update:download": "node ./scripts/figma-import.mjs",
19+
"update:optimize": "node ./scripts/optimize.mjs"
20+
},
21+
"devDependencies": {
22+
"fs-extra": "11.1.1",
23+
"ora": "7.0.1",
24+
"picocolors": "1.0.0",
25+
"prompts": "2.4.2",
26+
"svgo": "3.0.2"
27+
}
28+
}

0 commit comments

Comments
 (0)