Skip to content

Commit bc48c30

Browse files
authored
feat: adding support for changing overflow button at any time (or any changes to the overflow button slot) #5
2 parents 0c48c74 + 3f12ac3 commit bc48c30

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

src/component/vcf-toolbar-layout.ts

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ export class VcfToolbarLayout extends ResizeMixin(
237237

238238
protected _overflowContainer!: HTMLElement;
239239
protected _overflowButton!: HTMLElement;
240+
protected _popover!: Popover;
240241
private __resizeObserver!: ResizeObserver;
241242
private __updateTimeout: NodeJS.Timeout | null = null;
242243

@@ -274,20 +275,16 @@ export class VcfToolbarLayout extends ResizeMixin(
274275
}
275276

276277
// get the overflow button (or create it)
277-
this._overflowButton = this.querySelector('[slot="overflow-button"]') as HTMLElement;
278-
if (!this._overflowButton) {
279-
this._overflowButton = this._createDefaultOverflowButton();
280-
this.appendChild(this._overflowButton);
281-
}
278+
this._overflowButton = this._findOrCreateOverflowButton();
282279

283280
// setup overflow container
284281
this._overflowContainer = document.createElement('vaadin-vertical-layout');
285282
this._overflowContainer.classList.add('overflow-container');
286283

287284
// setup the popover
288-
let popover: Popover = this.shadowRoot.querySelector('vaadin-popover') as Popover;
289-
popover.target = this._overflowButton;
290-
popover.renderer = (root: Element) => {
285+
this._popover = this.shadowRoot.querySelector('vaadin-popover') as Popover;
286+
this._popover.target = this._overflowButton;
287+
this._popover.renderer = (root: Element) => {
291288
// Ensure content is only added once
292289
if (!root.firstChild) {
293290
root.appendChild(this._overflowContainer!);
@@ -299,6 +296,18 @@ export class VcfToolbarLayout extends ResizeMixin(
299296
setTimeout(() => this._updateOverflowingItems(), 0);
300297
}
301298

299+
protected _findOrCreateOverflowButton() {
300+
let button = this.querySelector('[slot="overflow-button"]') as HTMLElement;
301+
302+
// create default overflow button if not found
303+
if (!button) {
304+
button = this._createDefaultOverflowButton();
305+
this.appendChild(button);
306+
}
307+
308+
return button;
309+
}
310+
302311
protected _createDefaultOverflowButton() {
303312
let button = document.createElement('vaadin-button') as Button;
304313
button.setAttribute('slot', 'overflow-button');
@@ -313,7 +322,9 @@ export class VcfToolbarLayout extends ResizeMixin(
313322
return html`
314323
<slot></slot>
315324
<slot name="menu"></slot>
316-
<slot name="overflow-button"></slot>
325+
<slot name="overflow-button"
326+
@slotchange="${this._onOverflowButtonSlotChange}">
327+
</slot>
317328
318329
<vaadin-popover
319330
part="popover"
@@ -326,6 +337,22 @@ export class VcfToolbarLayout extends ResizeMixin(
326337
`;
327338
}
328339

340+
/**
341+
* Fired when the overflow button slot changes. This is likely because
342+
* a new overflow button was added or the existing one was removed.
343+
* @param e
344+
*/
345+
protected _onOverflowButtonSlotChange(e: Event) {
346+
// update our reference to new overflow button
347+
this._overflowButton = this._findOrCreateOverflowButton();
348+
349+
// update the popover target to the new overflow button
350+
this._popover.target = this._overflowButton;
351+
352+
// depending on the state of the overflow items, we may need to the new button to immediately be visible
353+
this._updateOverflowButtonState();
354+
}
355+
329356
/**
330357
* Request an update to the overflow items. This method debounces the update.
331358
*/

0 commit comments

Comments
 (0)