Skip to content

Commit 5e7b014

Browse files
committed
feat: add new <pl-button> component to make Button-like styles more reusable
1 parent e8ce2a9 commit 5e7b014

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import { LitElement, html, customElement } from 'lit-element';
2+
import { Slotify } from './slotify';
3+
import styles from './pl-button.scss?external';
4+
5+
// This decorator defines the element.
6+
@customElement('pl-button')
7+
class Icon extends Slotify(LitElement) {
8+
static get properties() {
9+
return {
10+
href: {
11+
attribute: true,
12+
type: String,
13+
},
14+
target: {
15+
attribute: true,
16+
type: String,
17+
},
18+
size: {
19+
attribute: true,
20+
type: String,
21+
},
22+
iconOnly: {
23+
attribute: 'icon-only',
24+
type: Boolean,
25+
reflect: true,
26+
},
27+
};
28+
}
29+
30+
createRenderRoot() {
31+
return this;
32+
}
33+
34+
connectedCallback() {
35+
super.connectedCallback && super.connectedCallback();
36+
styles.use();
37+
}
38+
39+
disconnectedCallback() {
40+
super.disconnectedCallback && super.disconnectedCallback();
41+
styles.unuse();
42+
}
43+
44+
innerTemplate() {
45+
return html`
46+
${this.slotify('before')
47+
? html`
48+
<span class="pl-c-button__icon">${this.slotify('before')}</span>
49+
`
50+
: ''}
51+
${this.slotify('default')
52+
? html`
53+
<span
54+
class="pl-c-button__text ${this.iconOnly ? 'is-vishidden' : ''}"
55+
>${this.slotify('default')}</span
56+
>
57+
`
58+
: ''}
59+
${this.slotify('after')
60+
? html`
61+
<span class="pl-c-button__icon">${this.slotify('after')}</span>
62+
`
63+
: ''}
64+
`;
65+
}
66+
67+
// Render element DOM by returning a `lit-html` template.
68+
render() {
69+
const size = this.size || 'medium';
70+
// const iconOnly = this.iconOnly !== false|| false;
71+
72+
return html`
73+
${this.href
74+
? html`
75+
<a
76+
class="pl-c-button pl-c-button--${size} ${this.iconOnly
77+
? 'pl-c-button--icon-only'
78+
: ''}"
79+
href="${this.href}"
80+
target="${this.target ? this.target : 'self'}"
81+
>
82+
${this.innerTemplate()}
83+
</a>
84+
`
85+
: html`
86+
<button
87+
class="pl-c-button pl-c-button--${size} ${this.iconOnly
88+
? 'pl-c-button--icon-only'
89+
: ''}"
90+
>
91+
${this.innerTemplate()}
92+
</button>
93+
`}
94+
`;
95+
}
96+
}
97+
98+
export { Icon };
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
:host {
2+
width: 100%;
3+
}
4+
5+
pl-button {
6+
width: 100%;
7+
}
8+
9+
.pl-c-button {
10+
color: inherit;
11+
text-decoration: none;
12+
background: transparent;
13+
border: 0;
14+
appearance: none;
15+
display: flex;
16+
align-items: center;
17+
width: 100%;
18+
margin: 0;
19+
flex-direction: row-reverse;
20+
justify-content: flex-end;
21+
cursor: pointer;
22+
position: relative;
23+
min-width: 30px;
24+
25+
&:after {
26+
content: '';
27+
display: block;
28+
position: absolute;
29+
top: 0;
30+
left: 0;
31+
height: 100%;
32+
width: 100%;
33+
pointer-events: none;
34+
opacity: 0;
35+
transition: opacity .1s ease;
36+
background-color: currentColor;
37+
}
38+
39+
// &:focus,
40+
&:hover {
41+
&:after {
42+
opacity: 0.1;
43+
}
44+
}
45+
46+
&:active:hover {
47+
&:after {
48+
opacity: 0.2;
49+
}
50+
}
51+
52+
&:focus {
53+
outline: 1px dotted;
54+
outline-offset: -1px;
55+
56+
&:after {
57+
opacity: 0.1;
58+
}
59+
}
60+
}
61+
62+
.pl-c-button--medium {
63+
padding: 0.65rem 1rem;
64+
65+
&.pl-c-button--icon-only {
66+
padding: 0.65rem;
67+
}
68+
}
69+
70+
.pl-c-button--small {
71+
padding: 0.5rem 1rem;
72+
73+
&.pl-c-button--icon-only {
74+
padding: .5rem;
75+
}
76+
}
77+
78+
// Make sure the text and icon align to the opposite ends
79+
* + .pl-c-button__icon {
80+
margin-right: 0.5rem;
81+
}
82+
83+
.pl-c-button__icon + * {
84+
margin-right: 0.5rem;
85+
}

0 commit comments

Comments
 (0)