Skip to content

Commit

Permalink
feat(docs): create get started icons documentation (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciomutte authored Jul 25, 2023
1 parent f9ff461 commit 436b9f7
Show file tree
Hide file tree
Showing 17 changed files with 265 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ packages/vue/src/components
packages/**/coverage
packages/**/components.d.ts
packages/tokens/index.ts
packages/icons/index.ts
packages/core/react
packages/core/vue
packages/core/core.css
Expand Down
11 changes: 11 additions & 0 deletions apps/docs/.storybook/preview-body.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<style>
.icon-gallery > summary {
border-bottom: 1px solid #0002;
cursor: pointer;
font-family: 'Nunito Sans', sans-serif;
font-size: 18px;
font-weight: 600;
margin-bottom: 16px;
user-select: none;
}
</style>
24 changes: 24 additions & 0 deletions apps/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,29 @@
"scripts": {
"start": "storybook dev -p 6006",
"build": "storybook build"
},
"nx": {
"targets": {
"build": {
"dependsOn": [
{
"projects": [
"@juntossomosmais/atomium-icons"
],
"target": "generate:types"
}
]
},
"start": {
"dependsOn": [
{
"projects": [
"@juntossomosmais/atomium-icons"
],
"target": "generate:types"
}
]
}
}
}
}
39 changes: 39 additions & 0 deletions apps/docs/stories/1_4_Icons.stories.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Meta, Title, IconGallery, IconItem } from '@storybook/blocks'

import { iconsList } from '../utils/icons-list'

<Meta title="Getting Started/Icons" />

# Icons

The icons are a collection of icons that can be used in your components. It is based on the [Material Design Icons](https://materialdesignicons.com/) library but we can also add our own custom icons.

- [Architecture decisions](#architecture-decisions)
- [Using icons](/docs/components-icon--docs)
- [How add a custom icon](https://github.com/juntossomosmais/atomium/tree/main/packages/icons#adding-a-new-icon)

## List of icons

See the list of all available icons:

<details class="icon-gallery">
<summary>Custom Icons</summary>
<IconGallery>
{iconsList?.custom?.map((icon) => (
<IconItem name={icon}>
<atom-icon icon={icon}></atom-icon>
</IconItem>
))}
</IconGallery>
</details>

<details class="icon-gallery">
<summary>Material Design Icons</summary>
<IconGallery>
{iconsList?.mdi?.map((icon) => (
<IconItem name={icon}>
<atom-icon icon={icon}></atom-icon>
</IconItem>
))}
</IconGallery>
</details>
2 changes: 2 additions & 0 deletions apps/docs/utils/icons-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// @note: this export statement is placed here because Nx does not track imports inside .mdx files used for stories.
export * from '@juntossomosmais/atomium-icons'
20 changes: 8 additions & 12 deletions nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,26 @@
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": [
"build",
"lint",
"test"
]
"cacheableOperations": ["build", "lint", "test"]
}
}
},
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
},
"pluginsConfig": {
"@nrwl/js": {
"analyzeSourceFiles": true
}
},
"namedInputs": {
"default": [
"{projectRoot}/**/*",
"sharedGlobals"
],
"default": ["{projectRoot}/**/*", "sharedGlobals"],
"sharedGlobals": [
"{workspaceRoot}/tsconfig.base.json",
"{workspaceRoot}/nx.json"
],
"production": [
"default"
]
"production": ["default"]
}
}
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@
"utils/*"
],
"scripts": {
"core:build": "npm run icons:generate-types && npx nx build @juntossomosmais/atomium && npm run react:build",
"core:build": "npx nx build @juntossomosmais/atomium && npm run react:build",
"docs:build": "npx nx build @atomium/docs && npx nx build @atomium/docs-react",
"docs-react:start": "npx nx run @atomium/docs-react:start",
"lint:pre-commit": "eslint --resolve-plugins-relative-to . -c ./.eslintrc.json $(git diff --cached --name-only --diff-filter=AM | grep -E \"(.js$|.ts$)\") --cache --cache-location node_modules/.cache/eslint",
"lint:styles": "stylelint '**/*.scss'",
"icons:build": "npx nx build @juntossomosmais/atomium-icons",
"icons:generate-types": "cd packages/icons && npm run generate:types",
"postinstall": "npx nx build @juntossomosmais/atomium:prebuild && npm run icons:build && npm run core:build && npm run tokens:build",
"pre-commit": "lint-staged",
"prepare": "husky install",
Expand Down
24 changes: 24 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,29 @@
"generate": "stencil generate",
"prebuild": "npx mkdirp react && npx mkdirp vue && cd > core.css",
"publish-library": "npm publish --access public"
},
"nx": {
"targets": {
"build": {
"dependsOn": [
{
"projects": [
"@juntossomosmais/atomium-icons"
],
"target": "generate:types"
}
]
},
"start": {
"dependsOn": [
{
"projects": [
"@juntossomosmais/atomium-icons"
],
"target": "generate:types"
}
]
}
}
}
}
12 changes: 12 additions & 0 deletions packages/icons/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
testEnvironment: 'node',
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
testMatch: ['**/*.spec.ts'],
moduleFileExtensions: ['ts', 'js'],
collectCoverage: true,
coverageReporters: ['lcov'],
coveragePathIgnorePatterns: ['/node_modules/'],
collectCoverageFrom: ['scripts/*.ts', '!scripts/*.spec.ts'],
}
25 changes: 21 additions & 4 deletions packages/icons/package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,31 @@
{
"name": "@juntossomosmais/atomium-icons",
"private": true,
"type": "module",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"files": [
"dist/"
],
"scripts": {
"build": "tsc && node dist/scripts/build.js && npm run generate:types",
"generate:types": "node dist/scripts/generate-types.js && npm run copy",
"copy": "npx copyfiles --flat ./dist/icons.d.ts ../core/src"
"compile": "tsc",
"build": "npx nx exec -- node dist/scripts/build.js",
"generate:types": "npx nx exec -- node dist/scripts/generate-types.js && npm run compile && npm run copy",
"copy": "npx copyfiles --flat ./dist/icons.d.ts ../core/src",
"test:ci": "jest --coverage --no-cache --updateSnapshot --ci",
"test": "jest --maxWorkers=50%"
},
"nx": {
"targets": {
"build": {
"dependsOn": [
"compile"
]
},
"generate:types": {
"dependsOn": [
"compile"
]
}
}
}
}
4 changes: 0 additions & 4 deletions packages/icons/project.json

This file was deleted.

1 change: 1 addition & 0 deletions packages/icons/scripts/__mocks__/svg/custom/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions packages/icons/scripts/__mocks__/svg/mdi/icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 6 additions & 1 deletion packages/icons/scripts/generate-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { readSvgDirectories, writeTypeDefinitionFile } from './helpers.js'
import {
readSvgDirectories,
writeListFile,
writeTypeDefinitionFile,
} from './helpers.js'

function generateTypes() {
console.log(`Generating types...`)
const files = readSvgDirectories()
writeListFile(files)
writeTypeDefinitionFile(files)
}

Expand Down
84 changes: 84 additions & 0 deletions packages/icons/scripts/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import fs from 'fs'
import path from 'path'
import {
readSvgDirectories,
writeListFile,
writeSvgFile,
writeTypeDefinitionFile,
} from './helpers'

const MOCK_SVG_DIR = path.resolve(__dirname, './__mocks__/svg')

describe('readSvgDirectories', () => {
it('should return an array of all SVG files in the specified directory', () => {
const svgFiles = readSvgDirectories(MOCK_SVG_DIR)
expect(svgFiles).toHaveLength(2)
expect(svgFiles).toContain(path.join(MOCK_SVG_DIR, 'custom/icon.svg'))
expect(svgFiles).toContain(path.join(MOCK_SVG_DIR, 'mdi/icon.svg'))
})
})

describe('writeSvgFile', () => {
it('should mock the fs.writeFileSync function', () => {
jest.mock('fs')
const mockWriteFileSync = jest.fn()
fs.writeFileSync = mockWriteFileSync

const fileName = 'test.svg'
const content = '<svg viewBox="0 0 100 100"></svg>'
const outputDirectory = './dist'

writeSvgFile(fileName, content, outputDirectory)

expect(mockWriteFileSync).toHaveBeenCalledWith(
path.join(outputDirectory, fileName),
content,
'utf-8'
)
})
})

describe('writeTypeDefinitionFile', () => {
it('should write the type definition file for the specified SVG files', () => {
jest.mock('fs')
const mockWriteFileSync = jest.fn()
fs.writeFileSync = mockWriteFileSync

const svgFiles = ['./svg/custom/customIcon.svg', './svg/mdi/mdiIcon.svg']
const outputPath = './'
const output = `export type IconProps = ${[
`'customIcon'`,
`'mdiIcon'`,
].join('\n | ')};`

writeTypeDefinitionFile(svgFiles, outputPath)

expect(mockWriteFileSync).toHaveBeenCalledWith('../icons.d.ts', output)
})
})

describe('writeListFile', () => {
it('should mock the fs.writeFileSync function', () => {
jest.mock('fs')
const mockWriteFileSync = jest.fn()
fs.writeFileSync = mockWriteFileSync

const svgFiles = ['./svg/custom/icon.svg', './svg/mdi/icon.svg']
const outputPath = './'
const list = {
custom: ['icon'],
mdi: ['icon'],
}
const output = [
'// THIS FILE IS GENERATED AUTOMATICALLY DO NOT EDIT MANUALLY\n\n',
`export const iconsList = ${JSON.stringify(list, null, 2)}`,
]

writeListFile(svgFiles, outputPath)

expect(mockWriteFileSync).toHaveBeenCalledWith(
path.join(outputPath, '../..', 'index.ts'),
output.join('')
)
})
})
36 changes: 29 additions & 7 deletions packages/icons/scripts/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'

export const getCurrentDirPath = (currentUrl = import.meta.url) =>
path.dirname(fileURLToPath(currentUrl))

const ICONS_DIR = path.resolve(getCurrentDirPath(), '../../svg')
const DIST_DIR = path.resolve(getCurrentDirPath(), '../svg')
const CURRENT_DIR = __dirname
const ICONS_DIR = path.resolve(CURRENT_DIR, '../../svg')
const DIST_DIR = path.resolve(CURRENT_DIR, '../svg')

export const readSvg = (path: string) => fs.readFileSync(path, 'utf-8')

Expand Down Expand Up @@ -52,9 +49,34 @@ export const writeSvgFile = (

export const writeTypeDefinitionFile = (
svgFiles: string[],
outputPath = getCurrentDirPath()
outputPath = CURRENT_DIR
) => {
const types = svgFiles.map((file) => `'${path.basename(file, '.svg')}'`)
const typeDef = `export type IconProps = ${types.join('\n | ')};`
fs.writeFileSync(path.join(outputPath, '..', 'icons.d.ts'), typeDef)
}

export const writeListFile = (svgFiles: string[], outputPath = CURRENT_DIR) => {
const list = svgFiles.reduce(
(acc, file) => {
const category = path.basename(path.dirname(file))
const name = path.basename(file, '.svg')

if (!acc[category]) {
acc[category] = []
}

acc[category].push(name)

return acc
},
{} as Record<'custom' | 'mdi', string[]>
)

const output = [
'// THIS FILE IS GENERATED AUTOMATICALLY DO NOT EDIT MANUALLY\n\n',
`export const iconsList = ${JSON.stringify(list, null, 2)}`,
]

fs.writeFileSync(path.join(outputPath, '../..', 'index.ts'), output.join(''))
}
2 changes: 1 addition & 1 deletion packages/icons/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"module": "CommonJS",
"moduleResolution": "node",
"target": "ES6",
"outDir": "./dist",
Expand Down

0 comments on commit 436b9f7

Please sign in to comment.