-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit-button.js
More file actions
137 lines (123 loc) · 4.93 KB
/
split-button.js
File metadata and controls
137 lines (123 loc) · 4.93 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* MD3E Components | Split Button component.
* Developed by bocajthomas
* Version: 0.0.1
*
* The SplitButton component displays a primary action button and a secondary menu button.
*
* @example
* <!-- Usage in HTML -->
* <md-split-button></md-split-button>
*
* @fires main-click - Dispatched when the main button is clicked.
* @fires menu-click - Dispatched when the menu button is clicked.
*
* @slot - (Not used) The component does not currently support slots.
*
* @csspart md-split-button - The container of the split button.
* @csspart common-button - The main action button.
* @csspart menu-icon-button - The menu icon button.
*
* @cssprop --md-sys-color-surface - Background color of the button.
* @cssprop --md-sys-color-outline-variant - Border color of the button.
* @cssprop --md-sys-color-surface-variant - Hover background color.
*/
class SplitButton extends HTMLElement {
constructor() {
super();
const shadow = this.attachShadow({ mode: 'open' });
const newLocal = `
<link href="https://fonts.googleapis.com/icon?family=Material+Icons+Round" rel="stylesheet">
<style>
:host {
display: inline-block;
font-family: 'Roboto', sans-serif;
font-size: 14px;
cursor: pointer;
position: relative;
user-select: none;
padding: 0;
}
.md-split-button {
display: flex;
align-items: center;
justify-content: center;
color: white;
cursor: pointer;
gap: 2px;
}
.material-icons-round {
font-size: 20px;
}
.common-button {
align-items: center;
justify-content: center;
display: flex;
padding: 20px;
background-color: rgb(0, 0, 0);
border-bottom-left-radius: 30px;
border-top-left-radius: 30px;
border-bottom-right-radius: 10px;
border-top-right-radius: 10px;
gap: 10px;
}
.menu-icon-button {
align-items: center;
justify-content: center;
display: flex;
padding: 20px;
background-color: rgb(0, 0, 0);
border-bottom-left-radius: 10px;
border-top-left-radius: 10px;
border-bottom-right-radius: 30px;
border-top-right-radius: 30px;
transition: border-bottom-left-radius 0.1s ease-in-out, border-top-left-radius 0.1s ease-in-out;
}
.expanded {
border-bottom-left-radius: 30px;
border-top-left-radius: 30px;
border-bottom-right-radius: 30px;
border-top-right-radius: 30px;
}
.menu-icon-button .material-icons-round {
transition: transform 0.1s ease-in-out;
}
</style>
<div class="md-split-button">
<div class="common-button" tabindex="0">
<span class="material-icons-round">edit</span>
<span class="label">Edit</span>
</div>
<div class="menu-icon-button" tabindex="0" aria-haspopup="true" aria-expanded="false">
<span class="material-icons-round">expand_more</span>
</div>
</div>
`;
shadow.innerHTML = newLocal;
this._mainButton = shadow.querySelector('.common-button');
this._menuButton = shadow.querySelector('.menu-icon-button');
this._menuIcon = shadow.querySelector('.menu-icon-button .material-icons-round');
const updateIcon = () => {
const icon = getComputedStyle(this).getPropertyValue('--md-split-button-icon').trim();
if (icon) {
const iconSpan = this._mainButton.querySelector('.material-icons-round');
if (iconSpan) iconSpan.textContent = icon;
}
};
updateIcon();
const observer = new MutationObserver(() => updateIcon());
observer.observe(this, { attributes: true, attributeFilter: ['style'] });
this._mainButton.addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('main-click', { bubbles: true, composed: true }));
});
this._menuButton.addEventListener('click', () => {
this.dispatchEvent(new CustomEvent('menu-click', {
bubbles: true,
composed: true
}));
this._menuButton.classList.toggle('expanded');
this._menuIcon.style.transform = this._menuButton.classList.contains('expanded') ? 'rotate(180deg)' : 'rotate(0deg)';
});
}
}
customElements.define('md-split-button', SplitButton);