-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathicon-button.tsx
120 lines (103 loc) · 3.24 KB
/
icon-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { MDCRipple } from '@limetech/mdc-ripple';
import { Component, Element, h, Method, Prop } from '@stencil/core';
import { IconSize } from '@limetech/lime-elements';
/**
* @exampleComponent limel-example-icon-button
* @exampleComponent limel-example-icon-button-toggle-state
* @exampleComponent limel-example-icon-button-elevated
*/
@Component({
tag: 'limel-icon-button',
shadow: true,
styleUrl: 'icon-button.scss',
})
export class IconButton {
public constructor() {
this.removeFocusedStyleOnClick = this.removeFocusedStyleOnClick.bind(
this
);
}
/**
* The icon to display.
*/
@Prop({ reflect: true })
public icon: string;
/**
* Set to `true` to give the button our standard "elevated" look, lifting
* it off the flat layout.
*/
@Prop({ reflect: true })
public elevated = false;
/**
* The text to show to screenreaders and other assistive tech.
*/
@Prop({ reflect: true })
public label: string;
/**
* Set to `true` to disable the button.
*/
@Prop({ reflect: true })
public disabled = false;
@Element()
private host: HTMLLimelIconButtonElement;
private mdcIconButtonRipple;
/**
* If the button is hidden or inside another element that is animating
* while the button is instantiated, the hover-highlight may become
* misaligned. If so, calling this method will make the button re-layout
* the highlight.
*/
@Method()
public async relayout() {
if (this.mdcIconButtonRipple) {
this.mdcIconButtonRipple.layout();
}
}
private removeFocusedStyleOnClick() {
const mdcButton = this.host.shadowRoot.querySelector(
'.mdc-icon-button'
);
mdcButton.classList.remove('mdc-ripple-upgraded--background-focused');
}
public connectedCallback() {
this.initialize();
}
public componentDidLoad() {
this.initialize();
}
private initialize() {
const element = this.host.shadowRoot.querySelector('.mdc-icon-button');
if (!element) {
return;
}
this.mdcIconButtonRipple = new MDCRipple(element);
this.mdcIconButtonRipple.unbounded = true;
this.host.addEventListener('click', this.removeFocusedStyleOnClick);
}
public disconnectedCallback() {
this.mdcIconButtonRipple?.destroy();
this.host.removeEventListener('click', this.removeFocusedStyleOnClick);
}
public render() {
const buttonAttributes: { tabindex?: string } = {};
if (this.host.hasAttribute('tabindex')) {
buttonAttributes.tabindex = this.host.getAttribute('tabindex');
}
const iconAttributes: { badge?: boolean; size?: IconSize } = {};
if (this.elevated) {
iconAttributes.badge = true;
iconAttributes.size = 'small';
}
return (
<button
class="mdc-icon-button"
disabled={this.disabled}
aria-label={this.label}
title={this.label}
{...buttonAttributes}
>
<limel-icon name={this.icon} {...iconAttributes} />
</button>
);
}
}