fix(menu-toggle): use ESM menuController (#19225)

This commit is contained in:
Manu MA
2019-08-29 15:19:01 +02:00
committed by GitHub
parent 9751f145e1
commit 4c30878fc7
5 changed files with 27 additions and 61 deletions

View File

@ -4,8 +4,9 @@ import { config } from '../../global/config';
import { getIonMode } from '../../global/ionic-global'; import { getIonMode } from '../../global/ionic-global';
import { Color } from '../../interface'; import { Color } from '../../interface';
import { ButtonInterface } from '../../utils/element-interface'; import { ButtonInterface } from '../../utils/element-interface';
import { menuController } from '../../utils/menu-controller';
import { createColorClasses } from '../../utils/theme'; import { createColorClasses } from '../../utils/theme';
import { toggleMenu, updateVisibility } from '../menu-toggle/menu-toggle-util'; import { updateVisibility } from '../menu-toggle/menu-toggle-util';
@Component({ @Component({
tag: 'ion-menu-button', tag: 'ion-menu-button',
@ -46,22 +47,18 @@ export class MenuButton implements ComponentInterface, ButtonInterface {
*/ */
@Prop() type: 'submit' | 'reset' | 'button' = 'button'; @Prop() type: 'submit' | 'reset' | 'button' = 'button';
async componentDidLoad() { componentDidLoad() {
await this.setVisibility(); this.visibilityChanged();
} }
@Listen('ionMenuChange', { target: 'body' }) @Listen('ionMenuChange', { target: 'body' })
@Listen('ionSplitPaneVisible', { target: 'body' }) @Listen('ionSplitPaneVisible', { target: 'body' })
async visibilityChanged() { async visibilityChanged() {
await this.setVisibility();
}
private setVisibility = async () => {
this.visible = await updateVisibility(this.menu); this.visible = await updateVisibility(this.menu);
} }
private onClick = async () => { private onClick = async () => {
await toggleMenu(this.menu); return menuController.toggle(this.menu);
} }
render() { render() {
@ -93,7 +90,7 @@ export class MenuButton implements ComponentInterface, ButtonInterface {
> >
<button <button
{...attrs} {...attrs}
disabled={this.disabled} disabled={disabled}
class="button-native" class="button-native"
> >
<slot> <slot>

View File

@ -1,32 +1,7 @@
import { menuController } from '../../utils/menu-controller';
// Get the menu controller element
export const getMenuController = (doc: Document): Promise<HTMLIonMenuControllerElement | undefined> => {
const menuControllerElement = doc.querySelector('ion-menu-controller');
if (!menuControllerElement) {
return Promise.resolve(undefined);
}
return menuControllerElement.componentOnReady();
};
// Given a menu, toggle it
export const toggleMenu = async (menu: string | undefined) => {
const menuCtrl = await getMenuController(document);
if (menuCtrl) {
const menuEl = await menuCtrl.get(menu);
if (menuEl) {
menuCtrl.toggle(menu);
}
}
};
// Given a menu, return whether or not the menu toggle should be visible // Given a menu, return whether or not the menu toggle should be visible
export const updateVisibility = async (menu: string | undefined) => { export const updateVisibility = async (menu: string | undefined) => {
const menuCtrl = await getMenuController(document); const menuEl = await menuController.get(menu);
if (menuCtrl) { return !!(menuEl && await menuEl.isActive());
const menuEl = await menuCtrl.get(menu);
if (menuEl && await menuEl.isActive()) {
return true;
}
}
return false;
}; };

View File

@ -1,8 +1,9 @@
import { Component, ComponentInterface, Host, Listen, Prop, State, h } from '@stencil/core'; import { Component, ComponentInterface, Host, Listen, Prop, State, h } from '@stencil/core';
import { getIonMode } from '../../global/ionic-global'; import { getIonMode } from '../../global/ionic-global';
import { menuController } from '../../utils/menu-controller';
import { toggleMenu, updateVisibility } from './menu-toggle-util'; import { updateVisibility } from './menu-toggle-util';
@Component({ @Component({
tag: 'ion-menu-toggle', tag: 'ion-menu-toggle',
@ -31,22 +32,18 @@ export class MenuToggle implements ComponentInterface {
*/ */
@Prop() autoHide = true; @Prop() autoHide = true;
async componentDidLoad() { connectedCallback() {
await this.setVisibility(); this.visibilityChanged();
} }
@Listen('ionMenuChange', { target: 'body' }) @Listen('ionMenuChange', { target: 'body' })
@Listen('ionSplitPaneVisible', { target: 'body' }) @Listen('ionSplitPaneVisible', { target: 'body' })
async visibilityChanged() { async visibilityChanged() {
await this.setVisibility();
}
private setVisibility = async () => {
this.visible = await updateVisibility(this.menu); this.visible = await updateVisibility(this.menu);
} }
private onClick = async () => { private onClick = () => {
await toggleMenu(this.menu); return menuController.toggle(this.menu);
} }
render() { render() {

View File

@ -91,21 +91,18 @@
<script> <script>
async function openDefault() { function openDefault() {
const menuButton = document.querySelector('ion-menu-toggle'); const menuButton = document.querySelector('ion-menu-toggle');
await menuButton.componentOnReady();
menuButton.menu = null; menuButton.menu = null;
} }
async function openStart() { function openStart() {
const menuButton = document.querySelector('ion-menu-toggle'); const menuButton = document.querySelector('ion-menu-toggle');
await menuButton.componentOnReady();
menuButton.menu = 'start'; menuButton.menu = 'start';
} }
async function openEnd() { function openEnd() {
const menuButton = document.querySelector('ion-menu-toggle'); const menuButton = document.querySelector('ion-menu-toggle');
await menuButton.componentOnReady();
menuButton.menu = 'end'; menuButton.menu = 'end';
} }
</script> </script>

View File

@ -93,21 +93,21 @@
} }
</style> </style>
<script type="module"> <script>
const menuCtrl = document.querySelector('ion-menu-controller'); const menuCtrl = document.querySelector('ion-menu-controller');
function openFirst() { async function openFirst() {
menuCtrl.enable(true, 'first'); await menuCtrl.enable(true, 'first');
menuCtrl.open('first'); await menuCtrl.open('first');
} }
function openEnd() { async function openEnd() {
menuCtrl.open('end'); awaitmenuCtrl.open('end');
} }
function openCustom() { async function openCustom() {
menuCtrl.enable(true, 'custom'); await menuCtrl.enable(true, 'custom');
menuCtrl.open('custom'); await menuCtrl.open('custom');
} }
</script> </script>
</body> </body>