Skip to content

Commit

Permalink
feat(toast): add ability to use toast inline (#26215)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandaejohnston committed Nov 7, 2022
1 parent 92b763a commit 003de44
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 26 deletions.
6 changes: 6 additions & 0 deletions core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1353,20 +1353,26 @@ ion-toast,prop,enterAnimation,((baseEl: any, opts?: any) => Animation) | undefin
ion-toast,prop,header,string | undefined,undefined,false,false
ion-toast,prop,htmlAttributes,undefined | { [key: string]: any; },undefined,false,false
ion-toast,prop,icon,string | undefined,undefined,false,false
ion-toast,prop,isOpen,boolean,false,false,false
ion-toast,prop,keyboardClose,boolean,false,false,false
ion-toast,prop,leaveAnimation,((baseEl: any, opts?: any) => Animation) | undefined,undefined,false,false
ion-toast,prop,message,IonicSafeString | string | undefined,undefined,false,false
ion-toast,prop,mode,"ios" | "md",undefined,false,false
ion-toast,prop,position,"bottom" | "middle" | "top",'bottom',false,false
ion-toast,prop,translucent,boolean,false,false,false
ion-toast,prop,trigger,string | undefined,undefined,false,false
ion-toast,method,dismiss,dismiss(data?: any, role?: string) => Promise<boolean>
ion-toast,method,onDidDismiss,onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>
ion-toast,method,onWillDismiss,onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>
ion-toast,method,present,present() => Promise<void>
ion-toast,event,didDismiss,OverlayEventDetail<any>,true
ion-toast,event,didPresent,void,true
ion-toast,event,ionToastDidDismiss,OverlayEventDetail<any>,true
ion-toast,event,ionToastDidPresent,void,true
ion-toast,event,ionToastWillDismiss,OverlayEventDetail<any>,true
ion-toast,event,ionToastWillPresent,void,true
ion-toast,event,willDismiss,OverlayEventDetail<any>,true
ion-toast,event,willPresent,void,true
ion-toast,css-prop,--background
ion-toast,css-prop,--border-color
ion-toast,css-prop,--border-radius
Expand Down
36 changes: 36 additions & 0 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2849,6 +2849,7 @@ export namespace Components {
* Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.
*/
"cssClass"?: string | string[];
"delegate"?: FrameworkDelegate;
/**
* Dismiss the toast overlay after it has been presented.
* @param data Any data to emit in the dismiss events.
Expand All @@ -2863,6 +2864,7 @@ export namespace Components {
* Animation to use when the toast is presented.
*/
"enterAnimation"?: AnimationBuilder;
"hasController": boolean;
/**
* Header to be shown in the toast.
*/
Expand All @@ -2875,6 +2877,10 @@ export namespace Components {
* The name of the icon to display, or the path to a valid SVG file. See `ion-icon`. https://ionic.io/ionicons
*/
"icon"?: string;
/**
* If `true`, the toast will open. If `false`, the toast will close. Use this if you need finer grained control over presentation, otherwise just use the toastController or the `trigger` property. Note: `isOpen` will not automatically be set back to `false` when the toast dismisses. You will need to do that in your code.
*/
"isOpen": boolean;
/**
* If `true`, the keyboard will be automatically dismissed when the overlay is presented.
*/
Expand Down Expand Up @@ -2912,6 +2918,10 @@ export namespace Components {
* If `true`, the toast will be translucent. Only applies when the mode is `"ios"` and the device supports [`backdrop-filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter#Browser_compatibility).
*/
"translucent": boolean;
/**
* An ID corresponding to the trigger element that causes the toast to open when clicked.
*/
"trigger": string | undefined;
}
interface IonToggle {
/**
Expand Down Expand Up @@ -6727,6 +6737,7 @@ declare namespace LocalJSX {
* Additional classes to apply for custom CSS. If multiple classes are provided they should be separated by spaces.
*/
"cssClass"?: string | string[];
"delegate"?: FrameworkDelegate;
/**
* How many milliseconds to wait before hiding the toast. By default, it will show until `dismiss()` is called.
*/
Expand All @@ -6735,6 +6746,7 @@ declare namespace LocalJSX {
* Animation to use when the toast is presented.
*/
"enterAnimation"?: AnimationBuilder;
"hasController"?: boolean;
/**
* Header to be shown in the toast.
*/
Expand All @@ -6747,6 +6759,10 @@ declare namespace LocalJSX {
* The name of the icon to display, or the path to a valid SVG file. See `ion-icon`. https://ionic.io/ionicons
*/
"icon"?: string;
/**
* If `true`, the toast will open. If `false`, the toast will close. Use this if you need finer grained control over presentation, otherwise just use the toastController or the `trigger` property. Note: `isOpen` will not automatically be set back to `false` when the toast dismisses. You will need to do that in your code.
*/
"isOpen"?: boolean;
/**
* If `true`, the keyboard will be automatically dismissed when the overlay is presented.
*/
Expand All @@ -6763,6 +6779,14 @@ declare namespace LocalJSX {
* The mode determines which platform styles to use.
*/
"mode"?: "ios" | "md";
/**
* Emitted after the toast has dismissed. Shorthand for ionToastDidDismiss.
*/
"onDidDismiss"?: (event: IonToastCustomEvent<OverlayEventDetail>) => void;
/**
* Emitted after the toast has presented. Shorthand for ionToastWillDismiss.
*/
"onDidPresent"?: (event: IonToastCustomEvent<void>) => void;
/**
* Emitted after the toast has dismissed.
*/
Expand All @@ -6779,6 +6803,14 @@ declare namespace LocalJSX {
* Emitted before the toast has presented.
*/
"onIonToastWillPresent"?: (event: IonToastCustomEvent<void>) => void;
/**
* Emitted before the toast has dismissed. Shorthand for ionToastWillDismiss.
*/
"onWillDismiss"?: (event: IonToastCustomEvent<OverlayEventDetail>) => void;
/**
* Emitted before the toast has presented. Shorthand for ionToastWillPresent.
*/
"onWillPresent"?: (event: IonToastCustomEvent<void>) => void;
"overlayIndex": number;
/**
* The position of the toast on the screen.
Expand All @@ -6788,6 +6820,10 @@ declare namespace LocalJSX {
* If `true`, the toast will be translucent. Only applies when the mode is `"ios"` and the device supports [`backdrop-filter`](https://developer.mozilla.org/en-US/docs/Web/CSS/backdrop-filter#Browser_compatibility).
*/
"translucent"?: boolean;
/**
* An ID corresponding to the trigger element that causes the toast to open when clicked.
*/
"trigger"?: string | undefined;
}
interface IonToggle {
/**
Expand Down
50 changes: 50 additions & 0 deletions core/src/components/toast/test/isOpen/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title>Toast - 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>
</head>

<body>
<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Toast - isOpen</ion-title>
</ion-toolbar>
</ion-header>

<ion-content class="ion-padding">
<ion-button id="default" onclick="openToast()">Open Toast, 2s Duration</ion-button>
<ion-button id="timeout" onclick="openToast(500)">Open Toast, Close Manually After 500ms</ion-button>

<ion-toast message="Hello World" duration="2000"></ion-toast>
</ion-content>
</ion-app>

<script>
const toast = document.querySelector('ion-toast');

const openToast = (timeout) => {
toast.isOpen = true;

if (timeout) {
setTimeout(() => {
toast.isOpen = false;
}, timeout);
}
};

toast.addEventListener('ionToastDidDismiss', () => {
toast.isOpen = false;
});
</script>
</body>
</html>
30 changes: 30 additions & 0 deletions core/src/components/toast/test/isOpen/toast.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test } from '@utils/test/playwright';

test.describe('toast: 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/toast/test/isOpen');
});

test('should open the toast', async ({ page }) => {
const ionToastDidPresent = await page.spyOnEvent('ionToastDidPresent');
await page.click('#default');

await ionToastDidPresent.next();
await page.waitForSelector('ion-toast', { state: 'visible' });
});

test('should open the toast then close after a timeout', async ({ page }) => {
const ionToastDidPresent = await page.spyOnEvent('ionToastDidPresent');
const ionToastDidDismiss = await page.spyOnEvent('ionToastDidDismiss');
await page.click('#timeout');

await ionToastDidPresent.next();
await page.waitForSelector('ion-toast', { state: 'visible' });

await ionToastDidDismiss.next();

await page.waitForSelector('ion-toast', { state: 'hidden' });
});
});
43 changes: 43 additions & 0 deletions core/src/components/toast/test/trigger/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="UTF-8" />
<title>Toast - 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>
</head>

<body>
<ion-app>
<ion-header>
<ion-toolbar>
<ion-title>Toast - Trigger</ion-title>
</ion-toolbar>
</ion-header>

<ion-content class="ion-padding">
<ion-button id="default">Open Toast, 2s Duration</ion-button>
<ion-button id="timeout">Open Toast, Close Manually After 500ms</ion-button>

<ion-toast id="default-toast" trigger="default" message="Hello World" duration="2000"></ion-toast>
<ion-toast id="timeout-toast" trigger="timeout" message="Hello World" duration="2000"></ion-toast>
</ion-content>
</ion-app>

<script>
const timeoutToast = document.querySelector('#timeout-toast');

timeoutToast.addEventListener('didPresent', () => {
setTimeout(() => {
timeoutToast.dismiss();
}, 500);
});
</script>
</body>
</html>
31 changes: 31 additions & 0 deletions core/src/components/toast/test/trigger/toast.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test } from '@utils/test/playwright';

test.describe('toast: trigger', () => {
test.beforeEach(async ({ page, skip }) => {
skip.rtl('trigger does not behave differently in RTL');
skip.mode('md');
await page.goto('/src/components/toast/test/trigger');
});

test('should open the toast', async ({ page }) => {
const ionToastDidPresent = await page.spyOnEvent('ionToastDidPresent');
await page.click('#default');

await ionToastDidPresent.next();
await page.waitForSelector('#default-toast', { state: 'visible' });
});

test('should present a previously presented toast', async ({ page }) => {
const ionToastDidPresent = await page.spyOnEvent('ionToastDidPresent');
const ionToastDidDismiss = await page.spyOnEvent('ionToastDidDismiss');

await page.click('#timeout');

await ionToastDidDismiss.next();

await page.click('#timeout');

await ionToastDidPresent.next();
await page.waitForSelector('#timeout-toast', { state: 'visible' });
});
});

0 comments on commit 003de44

Please sign in to comment.