Skip to content

Commit a85b7b6

Browse files
committed
Create Icon component
1 parent 4c6b7ad commit a85b7b6

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

index.html

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,7 @@
99
margin: 0;
1010
}
1111

12-
.icon {
13-
display: inline-block;
14-
line-height: 0;
15-
}
1612

17-
.icon > svg {
18-
stroke: currentColor;
19-
}
2013
</style>
2114
</head>
2215
<body>

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
"babel-loader": "^6.2.10",
1010
"babel-preset-es2015": "^6.22.0",
1111
"css-loader": "^0.26.1",
12+
"stylus": "^0.54.5",
13+
"stylus-loader": "^2.4.0",
1214
"vue": "^2.1.10",
1315
"vue-loader": "^10.1.2",
1416
"vue-template-compiler": "^2.1.10",

src/App.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
<template>
2-
<div>Hello World</div>
2+
<div><icon name="square" size="48"></icon></div>
33
</template>
44

55
<script>
66
import {mapState} from 'vuex';
7+
import Icon from './Icon';
78
89
export default {
910
name: 'App',

src/Icon.vue

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<template>
2+
<div class="icon"></div>
3+
</template>
4+
5+
<script>
6+
export default {
7+
name: 'Icon',
8+
props: {
9+
name: {
10+
type: String,
11+
required: true
12+
},
13+
size: {
14+
type: String,
15+
default: '24'
16+
}
17+
},
18+
mounted() {
19+
fetch(`./icons/${this.name}.svg`)
20+
.then(response => {
21+
if (response.ok) {
22+
return response.text();
23+
}
24+
throw new Error(`Cannot find ${this.name}.svg`);
25+
})
26+
.then(svgText => {
27+
const svgDocument = new DOMParser().parseFromString(svgText, 'image/svg+xml');
28+
const svgIcon = svgDocument.querySelector('svg').cloneNode(true);
29+
30+
svgIcon.setAttribute('width', this.size);
31+
svgIcon.setAttribute('height', this.size);
32+
33+
this.$el.appendChild(svgIcon);
34+
})
35+
.catch(error => {
36+
console.error(error);
37+
});
38+
}
39+
}
40+
</script>
41+
42+
<style lang="stylus">
43+
.icon
44+
display inline-block
45+
line-height 0
46+
47+
.icon > svg
48+
stroke currentColor
49+
</style>

0 commit comments

Comments
 (0)