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/icon #5

Merged
merged 5 commits into from
Apr 23, 2022
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
21 changes: 21 additions & 0 deletions .storybook/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,27 @@ module.exports = {
'@emotion/babel-plugin',
]

// ref: https://stackoverflow.com/a/61706308/11681543
const fileLoaderRule = config.module.rules.find(rule => rule.test && rule.test.test('.svg'));
fileLoaderRule.exclude = /\.svg$/;

config.module.rules.push({
test: /\.svg$/,
enforce: "pre",
use: [{
// because storybook support webpack 5, we can't use latest version of @svgr/webpack. so using @svgr/webpack@5.5.0
loader: "@svgr/webpack",
options: {
// ref: https://stackoverflow.com/a/71127131/11681543
svgoConfig: {
plugins: [
{removeViewBox: false},
],
},
},
}],
});

return config
},
}
44 changes: 44 additions & 0 deletions components/icon/icon.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {ComponentStory, ComponentMeta} from "@storybook/react";
import React from "react";

import {Icon, IconName} from "./icon";

const iconNames: IconName[] = ["arrow-back", "arrow-right", "check-circle", "circle", "close-circle", "close", "eye", "search"]

export default {
title: "atoms/Icon",
component: Icon,
argTypes: {
name: {
options: iconNames,
control: {type: "radio"},
},
},
args: {
name: "close",
},
} as ComponentMeta<typeof Icon>;

const Template: ComponentStory<typeof Icon> = (args) => <Icon {...args} />;

export const Close = Template.bind({});
Close.args = {
name: "close",
};

export const AllIcons: ComponentStory<typeof Icon> = (props) => {
return (
<div css={{
display: "flex",
flexDirection: "row",
flexWrap: "wrap",
}}>
{iconNames.map(name=>(
<div key={name} css={{padding: "4px"}}>
<Icon {...props} name={name}/>
</div>
))}
</div>
)
}

49 changes: 49 additions & 0 deletions components/icon/icon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import {useTheme} from "@emotion/react"
import React, {ReactElement, SVGProps} from "react"

import ArrowBack from "../../public/icons/arrow-back.svg"
import ArrowRight from "../../public/icons/arrow-right.svg"
import CheckCircle from "../../public/icons/check-circle.svg"
import Circle from "../../public/icons/circle.svg"
import CloseCircle from "../../public/icons/close-circle.svg"
import Close from "../../public/icons/close.svg"
import Eye from "../../public/icons/eye.svg"
import Search from "../../public/icons/search.svg"

const nameIconMap = {
"arrow-back": ArrowBack,
"arrow-right": ArrowRight,
"check-circle": CheckCircle,
"circle": Circle,
"close-circle": CloseCircle,
"close": Close,
"eye": Eye,
"search": Search,
}

export type IconName = keyof typeof nameIconMap

export type IconProps = {
name: IconName
color?: string
size?: number
}

export const Icon = ({
color,
size = 24,
name,
}: IconProps) => {
const theme = useTheme()

const Icon = nameIconMap[name] as (props: SVGProps<SVGElement>) => ReactElement

return (
<span css={{
color: color ? color : theme.colors["grey-90"],
fontSize: 0,
}}>
<Icon width={size} height={size}/>
</span>
)
}
1 change: 1 addition & 0 deletions components/icon/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./icon"
23 changes: 23 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
reactStrictMode: true,
webpack(config) {
// ref: https://stackoverflow.com/a/61706308/11681543
const fileLoaderRule = config.module.rules.find(rule => rule.test && rule.test.test(".svg"));
fileLoaderRule.exclude = /\.svg$/;

config.module.rules.push({
test: /\.svg$/,
enforce: "pre",
use: [{
loader: "@svgr/webpack",
options: {
// ref: https://stackoverflow.com/a/71127131/11681543
svgoConfig: {
plugins: [
{removeViewBox: false},
],
},
},
}],
});

return config;
}
}

module.exports = nextConfig
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"fontfaceobserver": "^2.1.0",
"next": "12.1.4",
"react": "18.0.0",
"react-dom": "18.0.0"
"react-dom": "18.0.0",
"react-svg": "^14.1.18"
},
"devDependencies": {
"@babel/core": "^7.17.9",
Expand All @@ -29,6 +30,7 @@
"@storybook/addon-links": "^6.4.22",
"@storybook/react": "^6.4.22",
"@storybook/testing-library": "^0.0.9",
"@svgr/webpack": "5.5.0",
"@types/node": "17.0.23",
"@types/react": "18.0.1",
"@types/react-dom": "18.0.0",
Expand Down
3 changes: 3 additions & 0 deletions public/icons/arrow-back.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/icons/arrow-right.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/icons/check-circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/icons/circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions public/icons/close-circle.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/icons/close.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions public/icons/eye.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions public/icons/search.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"isolatedModules": true,
"jsx": "preserve",
"incremental": true,
"baseUrl": "./"
"baseUrl": "./",
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
Expand Down
Loading