Skip to content

Commit

Permalink
Create mushroom-select-menu-surface.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
rhys-e2 authored Apr 1, 2024
1 parent 9af9411 commit 901f2ae
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions src/shared/form/mushroom-select-menu-surface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {MenuSurfaceBase} from '@material/mwc-menu/mwc-menu-surface-base.js';
import {styles} from '@material/mwc-menu/mwc-menu-surface.css.js';
import {html, css} from 'lit';
import {customElement, query} from 'lit/decorators.js';
import {classMap} from 'lit/directives/class-map.js';
import {styleMap} from 'lit/directives/style-map.js';

/** */
@customElement('mushroom-select-menu-surface')
export class MenuSurface extends MenuSurfaceBase {
static override styles = [
styles,
css`
dialog {
border: 0px;
}
dialog::backdrop {
/* must still be clickable */
opacity: 0;
}
slot {
/* Makes slot clickable */
display: block;
}`
];

@query('dialog') dialogEl!: HTMLDialogElement | null;

override renderSurface() {
const classes = this.getRootClasses();
const styles = this.getRootStyles();

// swap out div for dialog
return html`
<dialog
class=${classMap(classes)}
style="${styleMap(styles)}"
@click=${this.onScrimClick}
@keydown=${this.onKeydown}
@opened=${this.registerBodyClick}
@closed=${this.deregisterBodyClick}>
${this.renderContent()}
</dialog>`;
}

protected onScrimClick(e: Event) {
// This would be false if the contents of the dialog were clicked rather
// than the transparent scrim
if (e.target === this.dialogEl) {
this.open = false;
}
}

override onOpenChanged(isOpen: boolean, wasOpen: boolean) {
super.onOpenChanged(isOpen, wasOpen);
if (this.dialogEl) {
if (isOpen) {
// MUST use showModal over `open` because this is the only thing that
// will trigger the browser's super special hoisting mechanism
this.dialogEl.showModal();
} else {
this.dialogEl.close();
}
}
}
}

declare global {
interface HTMLElementTagNameMap {
'mushroom-select-menu-surface': MenuSurface;
}
}

0 comments on commit 901f2ae

Please sign in to comment.