-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoolbars.js
More file actions
112 lines (101 loc) · 3.63 KB
/
toolbars.js
File metadata and controls
112 lines (101 loc) · 3.63 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
/**
* MD3E Components | Toolbar component.
* Developed by bocajthomas
* Version: 0.0.1
*
* The Toolbar component displays a floating, rounded toolbar with Material Design icons.
*
* @example
* <!-- Usage in HTML -->
* <md-toolbar></md-toolbar>
*
* @fires toolbar-click - Dispatched when any button inside the toolbar is clicked.
*
* @slot - Allows for custom buttons or other elements to be placed inside the toolbar.
*
* @csspart md-toolbar - The container of the toolbar.
* @csspart toolbar-button - The individual button elements.
*/
class MdToolbar 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: block;
font-family: 'Roboto', sans-serif;
}
.toolbar {
position: fixed;
bottom: 2rem;
left: 50%;
transform: translateX(-50%);
display: flex;
align-items: center;
justify-content: space-between;
background-color: #242c33;
border-radius: 2rem;
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.4);
padding: 0.5rem;
width: 20rem;
gap: 0.25rem;
}
.toolbar-button {
display: flex;
align-items: center;
justify-content: center;
padding: 0.75rem;
border-radius: 50px;
color: white;
transition: background-color 150ms ease-in-out;
cursor: pointer;
border: none;
background-color: transparent;
}
.toolbar-button:hover {
background-color: #3e4c5a;
}
</style>
<div class="toolbar" part="md-toolbar">
<slot></slot>
</div>
`;
shadow.innerHTML = newLocal;
this._toolbar = shadow.querySelector('.toolbar');
}
connectedCallback() {
this._toolbar.addEventListener('click', this._handleButtonClick.bind(this));
if (this.innerHTML.trim() === '') {
this._createDefaultButtons();
}
}
disconnectedCallback() {
this._toolbar.removeEventListener('click', this._handleButtonClick.bind(this));
}
_createDefaultButtons() {
const icons = ['undo', 'format_bold', 'format_italic', 'format_underline', 'edit', 'more_vert'];
icons.forEach(iconName => {
const button = document.createElement('button');
button.className = 'toolbar-button';
button.setAttribute('part', 'toolbar-button');
button.innerHTML = `<span class="material-icons-round">${iconName}</span>`;
button.dataset.icon = iconName;
this._toolbar.appendChild(button);
});
}
_handleButtonClick(event) {
const button = event.target.closest('.toolbar-button');
if (button) {
this.dispatchEvent(new CustomEvent('toolbar-click', {
bubbles: true,
composed: true,
detail: {
icon: button.dataset.icon
}
}));
}
}
}
customElements.define('md-toolbar', MdToolbar);