-
Notifications
You must be signed in to change notification settings - Fork 65
/
modus-action-bar.tsx
185 lines (155 loc) · 5.2 KB
/
modus-action-bar.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import {
Component,
Prop,
State,
h, // eslint-disable-line @typescript-eslint/no-unused-vars
Element,
Listen,
Event,
EventEmitter,
} from '@stencil/core';
import { createPopper, Instance } from '@popperjs/core';
export interface ModusActionBarOptions {
id: string;
icon: string;
label: string;
}
@Component({
tag: 'modus-action-bar',
styleUrl: 'modus-action-bar.scss',
shadow: true,
})
export class ModusActionBar {
overflowButtonElement: HTMLElement;
overflowMenuElement: HTMLElement;
popperInstance: Instance;
@Element() el: HTMLElement;
@State() showOverflowMenu = false;
/** (optional) List of actions */
@Prop({ mutable: true }) actions: ModusActionBarOptions[];
/** (optional) The size of the action items. */
@Prop() size: 'small' | 'medium' | 'large' = 'medium';
/** (optional) Total number of icons to show */
@Prop() visibleItemCount = 3;
/** (optional) An event that fires on action item click. */
@Event() actionBarClick: EventEmitter;
componentWillLoad() {
this.processChildren();
}
componentDidRender() {
this.initializePopper();
}
@Listen('focusin', { target: 'document' })
@Listen('mouseup', { target: 'document' })
handleGlobalFocus(event: FocusEvent | MouseEvent) {
if (!(event.target instanceof HTMLElement)) {
return;
}
const isMoreVerticalBtnTarget = (event.target as HTMLElement).localName === 'modus-tree-view-item';
// Check if the focus is moving outside the component and if not clicked on the ellipsis button.
if (!this.el.contains(event.target as Node) && !isMoreVerticalBtnTarget && this.showOverflowMenu) {
this.closeOverflowMenu();
}
}
processChildren() {
const actionItemsChildren = this.el.querySelectorAll('modus-action-item');
if (actionItemsChildren.length > 0) {
const actionItems = Array.from(actionItemsChildren);
this.actions = actionItems.map((item) => ({
id: item.getAttribute('id'),
icon: item.getAttribute('icon'),
label: item.textContent.trim(),
}));
}
}
initializePopper() {
this.popperInstance = createPopper(this.overflowButtonElement, this.overflowMenuElement, {
placement: 'bottom-end',
});
}
handleKeyDown(event: KeyboardEvent, action: ModusActionBarOptions) {
if (event && event.key !== 'Enter' && event.key !== ' ') {
return;
}
this.handleButtonClick(event, action);
}
handleOverflowClick(event: KeyboardEvent | MouseEvent) {
event.preventDefault();
event.stopPropagation();
this.toggleOverflowMenu();
}
handleOverflowKeyDown(event: KeyboardEvent) {
if (event && event.key !== 'Enter' && event.key !== ' ') {
return;
}
this.handleOverflowClick(event);
}
handleButtonClick(event: KeyboardEvent | MouseEvent, action: ModusActionBarOptions) {
event.preventDefault();
event.stopPropagation();
this.actionBarClick.emit({ actionId: action.id });
if (this.showOverflowMenu) {
this.closeOverflowMenu();
}
}
toggleOverflowMenu() {
this.showOverflowMenu = !this.showOverflowMenu;
if (this.showOverflowMenu) {
this.popperInstance.update();
}
}
closeOverflowMenu() {
this.showOverflowMenu = false;
}
render() {
const visibleActions =
this.actions.length > this.visibleItemCount ? this.actions.slice(0, this.visibleItemCount - 1) : this.actions;
const overflowActions =
this.actions.length > this.visibleItemCount ? this.actions.slice(this.visibleItemCount - 1) : null;
return (
<div class="modus-action-bar">
{visibleActions.map((action) => (
<modus-tooltip text={action.label}>
<modus-button
icon-only={action.icon}
buttonStyle="borderless"
color="secondary"
size={this.size}
onClick={(event) => this.handleButtonClick(event, action)}
onKeyDown={(event) => this.handleKeyDown(event, action)}>
{action.label}
</modus-button>
</modus-tooltip>
))}
{this.actions.length > this.visibleItemCount && (
<modus-button
ref={(el) => (this.overflowButtonElement = el)}
icon-only="more_vertical"
buttonStyle="borderless"
color="secondary"
size={this.size}
onClick={(event) => this.handleOverflowClick(event)}
onKeyDown={(event) => this.handleOverflowKeyDown(event)}></modus-button>
)}
{this.showOverflowMenu && (
<div
style={{ width: '200px', display: this.showOverflowMenu ? 'block' : 'none' }}
class="overflow-menu"
ref={(el) => (this.overflowMenuElement = el)}>
<modus-list>
{overflowActions.map((action) => (
<modus-list-item
size={this.size === 'small' ? 'condensed' : 'standard'}
onClick={(event) => this.handleButtonClick(event, action)}
onKeyDown={(event) => this.handleKeyDown(event, action)}
leftIcon={action.icon}>
{action.label}
</modus-list-item>
))}
</modus-list>
</div>
)}
</div>
);
}
}