Skip to content

Commit e8ce2a9

Browse files
committed
feat: add new <pl-icon> component
1 parent ea95de9 commit e8ce2a9

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { LitElement, unsafeCSS, css, svg, customElement } from 'lit-element';
2+
3+
// temp solution until https://github.com/Polymer/lit-html/pull/1000 ships
4+
import { unsafeSVG } from './unsafe-svg.ts';
5+
import styles from './pl-icon.scss?external';
6+
const icons = {};
7+
8+
// automatically pull in every SVG icon file in from the icons folder
9+
const svgIcons = require.context('../../../icons', true, /\.svg$/);
10+
svgIcons.keys().forEach(iconName => {
11+
const name = iconName.replace('./', '');
12+
const icon = import(`../../../icons/${name}`);
13+
14+
icon.then(Icon => {
15+
icons[Icon.default.id] = Icon.default;
16+
});
17+
});
18+
19+
// This decorator defines the element.
20+
@customElement('pl-icon')
21+
class Icon extends LitElement {
22+
static get properties() {
23+
return {
24+
name: {
25+
attribute: true,
26+
type: String,
27+
},
28+
size: {
29+
attribute: true,
30+
type: String,
31+
},
32+
};
33+
}
34+
35+
createRenderRoot() {
36+
return this;
37+
}
38+
39+
connectedCallback() {
40+
super.connectedCallback && super.connectedCallback();
41+
styles.use();
42+
}
43+
44+
disconnectedCallback() {
45+
super.disconnectedCallback && super.disconnectedCallback();
46+
styles.unuse();
47+
}
48+
49+
// Render element DOM by returning a `lit-html` template.
50+
render() {
51+
return svg`
52+
<svg
53+
class="c-icon c-icon--${this.name} c-icon--${this.size || 'auto'}"
54+
viewBox="${
55+
icons[this.name] && icons[this.name].viewBox
56+
? icons[this.name].viewBox
57+
: '0 0 20 20'
58+
}"
59+
>
60+
${unsafeSVG(
61+
`<use xlink:href="#${
62+
icons[this.name] && icons[this.name].id ? icons[this.name].id : ''
63+
}"></use>`
64+
)}
65+
</svg>
66+
`;
67+
}
68+
}
69+
70+
export { Icon };
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
pl-icon {
2+
display: block;
3+
width: 1em;
4+
height: 1em;
5+
}
6+
7+
// @todo: build out additional icon sizes
8+
.c-icon {
9+
display: block;
10+
width: 1em;
11+
height: 1em;
12+
}

0 commit comments

Comments
 (0)