feat(loading): use loading overlay inline (#26153)

This commit is contained in:
Sean Perkins
2022-10-24 16:49:17 -04:00
committed by GitHub
parent d1fb7b039b
commit 34ca337b8a
13 changed files with 536 additions and 15 deletions

View File

@ -1,16 +1,26 @@
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Component, Element, Event, Host, Method, Prop, h } from '@stencil/core';
import { Watch, Component, Element, Event, Host, Method, Prop, h } from '@stencil/core';
import { config } from '../../global/config';
import { getIonMode } from '../../global/ionic-global';
import type {
AnimationBuilder,
FrameworkDelegate,
LoadingAttributes,
OverlayEventDetail,
OverlayInterface,
SpinnerTypes,
} from '../../interface';
import { BACKDROP, dismiss, eventMethod, prepareOverlay, present } from '../../utils/overlays';
import { raf } from '../../utils/helpers';
import {
BACKDROP,
dismiss,
eventMethod,
prepareOverlay,
present,
createDelegateController,
createTriggerController,
} from '../../utils/overlays';
import type { IonicSafeString } from '../../utils/sanitization';
import { sanitizeDOMString } from '../../utils/sanitization';
import { getClassMap } from '../../utils/theme';
@ -32,7 +42,10 @@ import { mdLeaveAnimation } from './animations/md.leave';
scoped: true,
})
export class Loading implements ComponentInterface, OverlayInterface {
private readonly delegateController = createDelegateController(this);
private readonly triggerController = createTriggerController();
private durationTimeout: any;
private currentTransition?: Promise<any>;
presented = false;
lastFocus?: HTMLElement;
@ -42,6 +55,12 @@ export class Loading implements ComponentInterface, OverlayInterface {
/** @internal */
@Prop() overlayIndex!: number;
/** @internal */
@Prop() delegate?: FrameworkDelegate;
/** @internal */
@Prop() hasController = false;
/**
* If `true`, the keyboard will be automatically dismissed when the overlay is presented.
*/
@ -105,6 +124,36 @@ export class Loading implements ComponentInterface, OverlayInterface {
*/
@Prop() htmlAttributes?: LoadingAttributes;
/**
* If `true`, the loading indicator will open. If `false`, the loading indicator will close.
* Use this if you need finer grained control over presentation, otherwise
* just use the loadingController or the `trigger` property.
* Note: `isOpen` will not automatically be set back to `false` when
* the loading indicator dismisses. You will need to do that in your code.
*/
@Prop() isOpen = false;
@Watch('isOpen')
onIsOpenChange(newValue: boolean, oldValue: boolean) {
if (newValue === true && oldValue === false) {
this.present();
} else if (newValue === false && oldValue === true) {
this.dismiss();
}
}
/**
* An ID corresponding to the trigger element that
* causes the loading indicator to open when clicked.
*/
@Prop() trigger: string | undefined;
@Watch('trigger')
triggerChanged() {
const { trigger, el, triggerController } = this;
if (trigger) {
triggerController.addClickListener(el, trigger);
}
}
/**
* Emitted after the loading has presented.
*/
@ -125,8 +174,33 @@ export class Loading implements ComponentInterface, OverlayInterface {
*/
@Event({ eventName: 'ionLoadingDidDismiss' }) didDismiss!: EventEmitter<OverlayEventDetail>;
/**
* Emitted after the loading indicator has presented.
* Shorthand for ionLoadingWillDismiss.
*/
@Event({ eventName: 'didPresent' }) didPresentShorthand!: EventEmitter<void>;
/**
* Emitted before the loading indicator has presented.
* Shorthand for ionLoadingWillPresent.
*/
@Event({ eventName: 'willPresent' }) willPresentShorthand!: EventEmitter<void>;
/**
* Emitted before the loading indicator has dismissed.
* Shorthand for ionLoadingWillDismiss.
*/
@Event({ eventName: 'willDismiss' }) willDismissShorthand!: EventEmitter<OverlayEventDetail>;
/**
* Emitted after the loading indicator has dismissed.
* Shorthand for ionLoadingDidDismiss.
*/
@Event({ eventName: 'didDismiss' }) didDismissShorthand!: EventEmitter<OverlayEventDetail>;
connectedCallback() {
prepareOverlay(this.el);
this.triggerChanged();
}
componentWillLoad() {
@ -136,16 +210,48 @@ export class Loading implements ComponentInterface, OverlayInterface {
}
}
componentDidLoad() {
/**
* If loading indicator was rendered with isOpen="true"
* then we should open loading indicator immediately.
*/
if (this.isOpen === true) {
raf(() => this.present());
}
}
disconnectedCallback() {
this.triggerController.removeClickListener();
}
/**
* Present the loading overlay after it has been created.
*/
@Method()
async present(): Promise<void> {
await present(this, 'loadingEnter', iosEnterAnimation, mdEnterAnimation, undefined);
/**
* When using an inline loading indicator
* and dismissing a loading indicator it is possible to
* quickly present the loading indicator while it is
* dismissing. We need to await any current
* transition to allow the dismiss to finish
* before presenting again.
*/
if (this.currentTransition !== undefined) {
await this.currentTransition;
}
await this.delegateController.attachViewToDom();
this.currentTransition = present(this, 'loadingEnter', iosEnterAnimation, mdEnterAnimation);
await this.currentTransition;
if (this.duration > 0) {
this.durationTimeout = setTimeout(() => this.dismiss(), this.duration + 10);
}
this.currentTransition = undefined;
}
/**
@ -158,11 +264,19 @@ export class Loading implements ComponentInterface, OverlayInterface {
* Some examples include: ``"cancel"`, `"destructive"`, "selected"`, and `"backdrop"`.
*/
@Method()
dismiss(data?: any, role?: string): Promise<boolean> {
async dismiss(data?: any, role?: string): Promise<boolean> {
if (this.durationTimeout) {
clearTimeout(this.durationTimeout);
}
return dismiss(this, data, role, 'loadingLeave', iosLeaveAnimation, mdLeaveAnimation);
this.currentTransition = dismiss(this, data, role, 'loadingLeave', iosLeaveAnimation, mdLeaveAnimation);
const dismissed = await this.currentTransition;
if (dismissed) {
this.delegateController.removeViewFromDom();
}
return dismissed;
}
/**

View File

@ -0,0 +1,80 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title>Loading - isOpen</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
<script src="../../../../../scripts/testing/scripts.js"></script>
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
<style>
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-row-gap: 20px;
grid-column-gap: 20px;
}
.grid-item {
margin: 0 auto;
}
h2 {
font-size: 12px;
font-weight: normal;
color: #6f7378;
margin-top: 10px;
margin-left: 5px;
}
</style>
</head>
<body>
<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Loading - isOpen</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<div class="grid">
<div class="grid-item">
<h2>Default</h2>
<ion-button id="default" onclick="openLoading()">Open Loading</ion-button>
</div>
<div class="grid-item">
<h2>Open, then close after 500ms</h2>
<ion-button id="timeout" onclick="openLoading(500)">Open Loading</ion-button>
</div>
</div>
<ion-loading message="Hello world"></ion-loading>
</ion-content>
</ion-app>
<script>
const loading = document.querySelector('ion-loading');
const openLoading = (timeout) => {
loading.isOpen = true;
if (timeout) {
setTimeout(() => {
loading.isOpen = false;
}, timeout);
}
};
loading.addEventListener('ionLoadingDidDismiss', () => {
loading.isOpen = false;
});
</script>
</body>
</html>

View File

@ -0,0 +1,30 @@
import { test } from '@utils/test/playwright';
test.describe('loading: isOpen', () => {
test.beforeEach(async ({ page, skip }) => {
skip.rtl('isOpen does not behave differently in RTL');
skip.mode('md', 'isOpen does not behave differently in MD');
await page.goto('/src/components/loading/test/isOpen');
});
test('should open the loading indicator', async ({ page }) => {
const ionLoadingDidPresent = await page.spyOnEvent('ionLoadingDidPresent');
await page.click('#default');
await ionLoadingDidPresent.next();
await page.waitForSelector('ion-loading', { state: 'visible' });
});
test('should open the loading indicator then close after a timeout', async ({ page }) => {
const ionLoadingDidPresent = await page.spyOnEvent('ionLoadingDidPresent');
const ionLoadingDidDismiss = await page.spyOnEvent('ionLoadingDidDismiss');
await page.click('#timeout');
await ionLoadingDidPresent.next();
await page.waitForSelector('ion-loading', { state: 'visible' });
await ionLoadingDidDismiss.next();
await page.waitForSelector('ion-loading', { state: 'hidden' });
});
});

View File

@ -0,0 +1,71 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title>Loading - trigger</title>
<meta
name="viewport"
content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"
/>
<link href="../../../../../css/ionic.bundle.css" rel="stylesheet" />
<link href="../../../../../scripts/testing/styles.css" rel="stylesheet" />
<script src="../../../../../scripts/testing/scripts.js"></script>
<script type="module" src="../../../../../dist/ionic/ionic.esm.js"></script>
<style>
.grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-row-gap: 20px;
grid-column-gap: 20px;
}
.grid-item {
margin: 0 auto;
}
h2 {
font-size: 12px;
font-weight: normal;
color: #6f7378;
margin-top: 10px;
margin-left: 5px;
}
</style>
</head>
<body>
<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Loading - trigger</ion-title>
</ion-toolbar>
</ion-header>
<ion-content class="ion-padding">
<div class="grid">
<div class="grid-item">
<h2>Default</h2>
<ion-button id="default">Open Loading</ion-button>
</div>
<div class="grid-item">
<h2>Open, then close after 500ms</h2>
<ion-button id="timeout">Open Loading</ion-button>
</div>
</div>
<ion-loading id="default-loading" trigger="default" message="Hello world"></ion-loading>
<ion-loading id="timeout-loading" trigger="timeout" message="Hello world"></ion-loading>
</ion-content>
</ion-app>
<script>
const loadingTimeout = document.getElementById('timeout-loading');
loadingTimeout.addEventListener('didPresent', () => {
setTimeout(() => {
loadingTimeout.dismiss();
}, 500);
});
</script>
</body>
</html>

View File

@ -0,0 +1,31 @@
import { test } from '@utils/test/playwright';
test.describe('loading: trigger', () => {
test.beforeEach(async ({ page, skip }) => {
skip.rtl('trigger does not behave differently in RTL');
skip.mode('md');
await page.goto('/src/components/loading/test/trigger');
});
test('should open the loading indicator', async ({ page }) => {
const ionLoadingDidPresent = await page.spyOnEvent('ionLoadingDidPresent');
await page.click('#default');
await ionLoadingDidPresent.next();
await page.waitForSelector('#default-loading', { state: 'visible' });
});
test('should present a previously presented loading indicator', async ({ page }) => {
const ionLoadingDidPresent = await page.spyOnEvent('ionLoadingDidPresent');
const ionLoadingDidDismiss = await page.spyOnEvent('ionLoadingDidDismiss');
await page.click('#timeout');
await ionLoadingDidDismiss.next();
await page.click('#timeout');
await ionLoadingDidPresent.next();
await page.waitForSelector('#timeout-loading', { state: 'visible' });
});
});