Skip to content

Commit

Permalink
feat(action-sheet): use action sheet overlay inline (#26172)
Browse files Browse the repository at this point in the history
  • Loading branch information
sean-perkins committed Nov 2, 2022
1 parent 84990ce commit 92b763a
Show file tree
Hide file tree
Showing 12 changed files with 409 additions and 39 deletions.
6 changes: 6 additions & 0 deletions core/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ ion-action-sheet,prop,cssClass,string | string[] | undefined,undefined,false,fal
ion-action-sheet,prop,enterAnimation,((baseEl: any, opts?: any) => Animation) | undefined,undefined,false,false
ion-action-sheet,prop,header,string | undefined,undefined,false,false
ion-action-sheet,prop,htmlAttributes,undefined | { [key: string]: any; },undefined,false,false
ion-action-sheet,prop,isOpen,boolean,false,false,false
ion-action-sheet,prop,keyboardClose,boolean,true,false,false
ion-action-sheet,prop,leaveAnimation,((baseEl: any, opts?: any) => Animation) | undefined,undefined,false,false
ion-action-sheet,prop,mode,"ios" | "md",undefined,false,false
ion-action-sheet,prop,subHeader,string | undefined,undefined,false,false
ion-action-sheet,prop,translucent,boolean,false,false,false
ion-action-sheet,prop,trigger,string | undefined,undefined,false,false
ion-action-sheet,method,dismiss,dismiss(data?: any, role?: string) => Promise<boolean>
ion-action-sheet,method,onDidDismiss,onDidDismiss<T = any>() => Promise<OverlayEventDetail<T>>
ion-action-sheet,method,onWillDismiss,onWillDismiss<T = any>() => Promise<OverlayEventDetail<T>>
ion-action-sheet,method,present,present() => Promise<void>
ion-action-sheet,event,didDismiss,OverlayEventDetail<any>,true
ion-action-sheet,event,didPresent,void,true
ion-action-sheet,event,ionActionSheetDidDismiss,OverlayEventDetail<any>,true
ion-action-sheet,event,ionActionSheetDidPresent,void,true
ion-action-sheet,event,ionActionSheetWillDismiss,OverlayEventDetail<any>,true
ion-action-sheet,event,ionActionSheetWillPresent,void,true
ion-action-sheet,event,willDismiss,OverlayEventDetail<any>,true
ion-action-sheet,event,willPresent,void,true
ion-action-sheet,css-prop,--backdrop-opacity
ion-action-sheet,css-prop,--background
ion-action-sheet,css-prop,--button-background
Expand Down
44 changes: 40 additions & 4 deletions core/src/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,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 action sheet overlay after it has been presented.
* @param data Any data to emit in the dismiss events.
Expand All @@ -102,6 +103,7 @@ export namespace Components {
* Animation to use when the action sheet is presented.
*/
"enterAnimation"?: AnimationBuilder;
"hasController": boolean;
/**
* Title for the action sheet.
*/
Expand All @@ -110,6 +112,10 @@ export namespace Components {
* Additional attributes to pass to the action sheet.
*/
"htmlAttributes"?: { [key: string]: any };
/**
* If `true`, the action sheet will open. If `false`, the action sheet will close. Use this if you need finer grained control over presentation, otherwise just use the actionSheetController or the `trigger` property. Note: `isOpen` will not automatically be set back to `false` when the action sheet 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 @@ -143,6 +149,10 @@ export namespace Components {
* If `true`, the action sheet 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 action sheet to open when clicked.
*/
"trigger": string | undefined;
}
interface IonAlert {
/**
Expand Down Expand Up @@ -3851,10 +3861,12 @@ 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;
/**
* Animation to use when the action sheet is presented.
*/
"enterAnimation"?: AnimationBuilder;
"hasController"?: boolean;
/**
* Title for the action sheet.
*/
Expand All @@ -3863,6 +3875,10 @@ declare namespace LocalJSX {
* Additional attributes to pass to the action sheet.
*/
"htmlAttributes"?: { [key: string]: any };
/**
* If `true`, the action sheet will open. If `false`, the action sheet will close. Use this if you need finer grained control over presentation, otherwise just use the actionSheetController or the `trigger` property. Note: `isOpen` will not automatically be set back to `false` when the action sheet 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 @@ -3876,21 +3892,37 @@ declare namespace LocalJSX {
*/
"mode"?: "ios" | "md";
/**
* Emitted after the alert has dismissed.
* Emitted after the action sheet has dismissed. Shorthand for ionActionSheetDidDismiss.
*/
"onDidDismiss"?: (event: IonActionSheetCustomEvent<OverlayEventDetail>) => void;
/**
* Emitted after the action sheet has presented. Shorthand for ionActionSheetWillDismiss.
*/
"onDidPresent"?: (event: IonActionSheetCustomEvent<void>) => void;
/**
* Emitted after the action sheet has dismissed.
*/
"onIonActionSheetDidDismiss"?: (event: IonActionSheetCustomEvent<OverlayEventDetail>) => void;
/**
* Emitted after the alert has presented.
* Emitted after the action sheet has presented.
*/
"onIonActionSheetDidPresent"?: (event: IonActionSheetCustomEvent<void>) => void;
/**
* Emitted before the alert has dismissed.
* Emitted before the action sheet has dismissed.
*/
"onIonActionSheetWillDismiss"?: (event: IonActionSheetCustomEvent<OverlayEventDetail>) => void;
/**
* Emitted before the alert has presented.
* Emitted before the action sheet has presented.
*/
"onIonActionSheetWillPresent"?: (event: IonActionSheetCustomEvent<void>) => void;
/**
* Emitted before the action sheet has dismissed. Shorthand for ionActionSheetWillDismiss.
*/
"onWillDismiss"?: (event: IonActionSheetCustomEvent<OverlayEventDetail>) => void;
/**
* Emitted before the action sheet has presented. Shorthand for ionActionSheetWillPresent.
*/
"onWillPresent"?: (event: IonActionSheetCustomEvent<void>) => void;
"overlayIndex": number;
/**
* Subtitle for the action sheet.
Expand All @@ -3900,6 +3932,10 @@ declare namespace LocalJSX {
* If `true`, the action sheet 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 action sheet to open when clicked.
*/
"trigger"?: string | undefined;
}
interface IonAlert {
/**
Expand Down
134 changes: 118 additions & 16 deletions core/src/components/action-sheet/action-sheet.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import type { ComponentInterface, EventEmitter } from '@stencil/core';
import { Component, Element, Event, Host, Method, Prop, h, readTask } from '@stencil/core';
import { Watch, Component, Element, Event, Host, Method, Prop, h, readTask } from '@stencil/core';

import { getIonMode } from '../../global/ionic-global';
import type {
ActionSheetButton,
AnimationBuilder,
CssClassMap,
FrameworkDelegate,
OverlayEventDetail,
OverlayInterface,
} from '../../interface';
import type { Gesture } from '../../utils/gesture';
import { createButtonActiveGesture } from '../../utils/gesture/button-active';
import { BACKDROP, dismiss, eventMethod, isCancel, prepareOverlay, present, safeCall } from '../../utils/overlays';
import {
BACKDROP,
createDelegateController,
createTriggerController,
dismiss,
eventMethod,
isCancel,
prepareOverlay,
present,
safeCall,
} from '../../utils/overlays';
import { getClassMap } from '../../utils/theme';

import { iosEnterAnimation } from './animations/ios.enter';
Expand All @@ -31,18 +42,28 @@ import { mdLeaveAnimation } from './animations/md.leave';
scoped: true,
})
export class ActionSheet implements ComponentInterface, OverlayInterface {
presented = false;
lastFocus?: HTMLElement;
animation?: any;
private readonly delegateController = createDelegateController(this);
private readonly triggerController = createTriggerController();
private currentTransition?: Promise<any>;
private wrapperEl?: HTMLElement;
private groupEl?: HTMLElement;
private gesture?: Gesture;

presented = false;
lastFocus?: HTMLElement;
animation?: any;

@Element() el!: HTMLIonActionSheetElement;

/** @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.
*/
Expand Down Expand Up @@ -102,35 +123,103 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
@Prop() htmlAttributes?: { [key: string]: any };

/**
* Emitted after the alert has presented.
* If `true`, the action sheet will open. If `false`, the action sheet will close.
* Use this if you need finer grained control over presentation, otherwise
* just use the actionSheetController or the `trigger` property.
* Note: `isOpen` will not automatically be set back to `false` when
* the action sheet 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 action sheet 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 action sheet has presented.
*/
@Event({ eventName: 'ionActionSheetDidPresent' }) didPresent!: EventEmitter<void>;

/**
* Emitted before the alert has presented.
* Emitted before the action sheet has presented.
*/
@Event({ eventName: 'ionActionSheetWillPresent' }) willPresent!: EventEmitter<void>;

/**
* Emitted before the alert has dismissed.
* Emitted before the action sheet has dismissed.
*/
@Event({ eventName: 'ionActionSheetWillDismiss' }) willDismiss!: EventEmitter<OverlayEventDetail>;

/**
* Emitted after the alert has dismissed.
* Emitted after the action sheet has dismissed.
*/
@Event({ eventName: 'ionActionSheetDidDismiss' }) didDismiss!: EventEmitter<OverlayEventDetail>;

/**
* Emitted after the action sheet has presented.
* Shorthand for ionActionSheetWillDismiss.
*/
@Event({ eventName: 'didPresent' }) didPresentShorthand!: EventEmitter<void>;

/**
* Emitted before the action sheet has presented.
* Shorthand for ionActionSheetWillPresent.
*/
@Event({ eventName: 'willPresent' }) willPresentShorthand!: EventEmitter<void>;

/**
* Emitted before the action sheet has dismissed.
* Shorthand for ionActionSheetWillDismiss.
*/
@Event({ eventName: 'willDismiss' }) willDismissShorthand!: EventEmitter<OverlayEventDetail>;

/**
* Emitted after the action sheet has dismissed.
* Shorthand for ionActionSheetDidDismiss.
*/
@Event({ eventName: 'didDismiss' }) didDismissShorthand!: EventEmitter<OverlayEventDetail>;

/**
* Present the action sheet overlay after it has been created.
*/
@Method()
present(): Promise<void> {
return present(this, 'actionSheetEnter', iosEnterAnimation, mdEnterAnimation);
}
async present(): Promise<void> {
/**
* When using an inline action sheet
* and dismissing a action sheet it is possible to
* quickly present the action sheet 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;
}

connectedCallback() {
prepareOverlay(this.el);
await this.delegateController.attachViewToDom();

this.currentTransition = present(this, 'actionSheetEnter', iosEnterAnimation, mdEnterAnimation);

await this.currentTransition;

this.currentTransition = undefined;
}

/**
Expand All @@ -143,8 +232,15 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
* Some examples include: ``"cancel"`, `"destructive"`, "selected"`, and `"backdrop"`.
*/
@Method()
dismiss(data?: any, role?: string): Promise<boolean> {
return dismiss(this, data, role, 'actionSheetLeave', iosLeaveAnimation, mdLeaveAnimation);
async dismiss(data?: any, role?: string): Promise<boolean> {
this.currentTransition = dismiss(this, data, role, 'actionSheetLeave', iosLeaveAnimation, mdLeaveAnimation);
const dismissed = await this.currentTransition;

if (dismissed) {
this.delegateController.removeViewFromDom();
}

return dismissed;
}

/**
Expand Down Expand Up @@ -207,11 +303,17 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
}
};

connectedCallback() {
prepareOverlay(this.el);
this.triggerChanged();
}

disconnectedCallback() {
if (this.gesture) {
this.gesture.destroy();
this.gesture = undefined;
}
this.triggerController.removeClickListener();
}

componentDidLoad() {
Expand Down
30 changes: 30 additions & 0 deletions core/src/components/action-sheet/test/isOpen/action-sheet.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test } from '@utils/test/playwright';

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

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

await ionActionSheetDidPresent.next();
await page.waitForSelector('ion-action-sheet', { state: 'visible' });
});

test('should open the action sheet then close after a timeout', async ({ page }) => {
const ionActionSheetDidPresent = await page.spyOnEvent('ionActionSheetDidPresent');
const ionActionSheetDidDismiss = await page.spyOnEvent('ionActionSheetDidDismiss');
await page.click('#timeout');

await ionActionSheetDidPresent.next();
await page.waitForSelector('ion-action-sheet', { state: 'visible' });

await ionActionSheetDidDismiss.next();

await page.waitForSelector('ion-action-sheet', { state: 'hidden' });
});
});

0 comments on commit 92b763a

Please sign in to comment.