Icon
Icon (아이콘)
Icon은 의미를 담은 벡터 그래픽이에요. 아이콘을 사용하기 전에 @ant-design/icons-vue 패키지를 설치해야 해요:
npm install --save @ant-design/icons-vue
출처: 문서
본문
API
Common Icon (공통 아이콘)
| 속성 | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| rotate | n 도만큼 회전 (IE9에서는 동작하지 않음) | number | - | |
| spin | 애니메이션으로 아이콘 회전 | boolean | false | |
| style | fontSize, color 같은 아이콘의 스타일 속성 |
CSSProperties | - | |
| twoToneColor | 두 톤(two-tone) 아이콘만 지원해요. 기본 색상을 지정해요. | string (hex color) | - |
아이콘은 세 가지 다른 테마가 있어요. 아이콘 컴포넌트 이름은 아이콘 이름에 테마 이름이 붙은 형태예요.
import { StarOutlined, StarFilled, StarTwoTone } from '@ant-design/icons-vue';
<star-outlined />
<star-filled />
<star-two-tone two-tone-color="#eb2f96" />
Custom Icon (커스텀 아이콘)
| 속성 | 설명 | 타입 | 기본값 | 버전 |
|---|---|---|---|---|
| component | 루트 노드에 사용되는 컴포넌트 | ComponentType<CustomIconComponentProps> | - | |
| rotate | 회전 각도 (IE9에서는 동작하지 않음) | number | - | |
| spin | 애니메이션으로 아이콘 회전 | boolean | false | |
| style | fontSize, color 같은 아이콘의 스타일 속성 |
CSSProperties | - |
SVG 아이콘에 대해 (About SVG icons)
우리는 버전 1.2.0에서 폰트 아이콘을 대체하는 SVG 아이콘을 도입했어요. 다음과 같은 장점이 있어요:
- 아이콘을 완전히 오프라인으로 사용할 수 있어 CDN 호스팅 폰트 아이콘 파일이 필요 없어요. (다운로드 중 빈 사각형이 더 이상 나타나지 않고, 아이콘 폰트 파일을 로컬에 배포할 필요도 없어요!)
- 저해상도 화면에서 훨씬 더 정확하게 표시돼요.
- 아이콘 색을 선택할 수 있어요.
- 컴포넌트에 더 많은 prop을 제공해서 스타일을 덮어쓰는 방식으로 내장 아이콘을 바꿀 필요가 없어요.
SVG 아이콘에 대한 더 많은 논의는 #10353에서 볼 수 있어요.
⚠️ 1.2.0에서 모든 SVG 아이콘이 import되면서 번들 크기가 커지기 때문에, 우리는 개발자가 필요할 때만 아이콘을 import할 수 있는 새 API를 제공할 예정이에요. 업데이트는 #12011에서 추적할 수 있어요.
기다리는 동안은 커뮤니티의 webpack plugin을 사용해서 아이콘 파일을 청크로 나눌 수 있어요.
theme, component, twoToneColor 속성은 1.2.0에서 추가됐어요. 모범 사례는 모든 <Icon /> 컴포넌트에 theme 속성을 전달하는 거예요.
<template>
<message-outlined :style="{fontSize: '16px', color: '#08c'}" />
</template>
<script>
import { MessageOutlined } from '@ant-design/icons-vue';
import { defineComponent } from 'vue';
export default defineComponent({
components: {
MessageOutlined,
},
});
</script>
모든 아이콘은 <svg>로 렌더링돼요. style과 class를 설정해서 아이콘의 크기와 색을 조절할 수도 있어요.
TwoTone 색 설정 (Set TwoTone Color)
두 톤 아이콘을 사용할 때 정적 메서드 getTwoToneColor()와 setTwoToneColor(colorString)를 사용해 기본 색을 지정할 수 있어요.
import { getTwoToneColor, setTwoToneColor } from '@ant-design/icons';
setTwoToneColor('#eb2f96');
getTwoToneColor(); // #eb2f96
커스텀 폰트 아이콘 (Custom Font Icon)
우리는 iconfont.cn에 배포된 사용자 아이콘을 편리하게 사용할 수 있는 createFromIconfontCN 함수를 추가했어요.
이 메서드는 iconfont.cn 전용이에요.
import { createFromIconfontCN } from '@ant-design/icons-vue';
import { defineComponent } from 'vue';
const MyIcon = createFromIconfontCN({
scriptUrl: '//at.alicdn.com/t/font_8d5l8fzk5b87iudi.js', // generated by iconfont.cn
});
export default defineComponent({
setup() {
return () => <MyIcon type="icon-dianzan" />;
},
});
이것은 본질적으로 SVG 스프라이트를 사용하는 컴포넌트를 만들어요.
다음 옵션을 사용할 수 있어요:
| 속성 | 설명 | 타입 | 기본값 |
|---|---|---|---|
| extraCommonProps | 컴포넌트에 추가 속성 정의 | { class, attrs, props, on, style } |
{} |
| scriptUrl | iconfont.cn 프로젝트에서 생성된 URL | string | - |
scriptUrl 속성은 SVG 스프라이트 심볼을 import하기 위해 설정해야 해요.
scriptUrl을 만드는 방법은 iconfont.cn documents를 참고하세요.
커스텀 SVG 아이콘 (Custom SVG Icon)
vue cli 3
vue cli 3과 vue-svg-loader를 사용하면 SVG 아이콘을 vue 컴포넌트로 import할 수 있어요. vue-svg-loader의 options는 참고.
// vue.config.js
module.exports = {
chainWebpack: config => {
const svgRule = config.module.rule('svg');
svgRule.uses.clear();
svgRule.use('vue-svg-loader').loader('vue-svg-loader');
},
};
import { defineComponent } from 'vue';
import Icon from '@ant-design/icons-vue';
import MessageSvg from 'path/to/message.svg'; // path to your '*.svg' file.
export default defineComponent({
setup() {
return () => <Icon type={MessageSvg} />;
},
});
Rsbuild
Rsbuild는 차세대 빌드 도구로, 공식 사이트는 https://rsbuild.dev/ 입니다.
vue-svg-loader.js 파일을 직접 만들어 SVG를 커스터마이즈하고 다듬은 뒤, rsbuild.config.ts에서 설정할 수 있어요.
// vue-svg-loader.js
/* eslint-disable */
const { optimize } = require('svgo');
const { version } = require('vue');
const semverMajor = require('semver/functions/major');
module.exports = async function (svg) {
const callback = this.async();
try {
({ data: svg } = await optimize(svg, {
path: this.resourcePath,
js2svg: {
indent: 2,
pretty: true,
},
plugins: [
'convertStyleToAttrs',
'removeDoctype',
'removeXMLProcInst',
'removeComments',
'removeMetadata',
'removeTitle',
'removeDesc',
'removeStyleElement',
'removeXMLNS',
'removeXMLProcInst',
],
}));
} catch (error) {
callback(error);
return;
}
if (semverMajor(version) === 2) {
svg = svg.replace('<svg', '<svg v-on="$listeners"');
}
callback(null, `<template>${svg}</template>`);
};
// rsbuild.config.ts
/* eslint-disable */
import { defineConfig } from '@rsbuild/core';
import { pluginVue } from '@rsbuild/plugin-vue';
export default defineConfig({
tools: {
bundlerChain(chain, { CHAIN_ID }) {
chain.module.rule(CHAIN_ID.RULE.SVG).exclude.add(/\.svg$/);
},
rspack: {
module: {
rules: [
{
test: /\.svg$/,
use: ['vue-loader', 'vue-svg-loader'],
},
],
},
resolveLoader: {
alias: {
'vue-svg-loader': require('path').join(__dirname, './vue-svg-loader.js'),
},
},
},
},
});
컴포넌트에 다음 속성을 사용할 수 있어요:
| 속성 | 설명 | 타입 | 기본값 |
|---|---|---|---|
| class | svg 요소의 계산된 클래스 이름 |
string | - |
| fill | svg 요소를 칠할 색 정의 |
string | 'currentColor' |
| height | svg 요소의 높이 |
string | number | '1em' |
| style | svg 요소의 계산된 스타일 |
CSSProperties | - |
| width | svg 요소의 너비 |
string | number | '1em' |
더 알아보기 (Learn more)
- Button 컴포넌트 — 버튼 아이콘 조합
- @ant-design/icons-vue — 아이콘 패키지
- Ant Design Vue 시작하기 — 프로젝트 설정