Skip to content

Commit c051ff3

Browse files
Adding static backdrop behaviour to modal when using ionic theme
1 parent ab61c12 commit c051ff3

File tree

5 files changed

+63
-22
lines changed

5 files changed

+63
-22
lines changed

core/src/components/modal/animations/sheet.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createAnimation } from '@utils/animation/animation';
22

33
import type { ModalAnimationOptions } from '../modal-interface';
4-
import { getBackdropValueForSheet } from '../utils';
4+
import { getBackdropValueForSheet, staticBackdropOpacity } from '../utils';
55

66
export const createSheetEnterAnimation = (opts: ModalAnimationOptions) => {
77
const { currentBreakpoint, backdropBreakpoint } = opts;
@@ -12,7 +12,13 @@ export const createSheetEnterAnimation = (opts: ModalAnimationOptions) => {
1212
* current breakpoint, then the backdrop should be fading in.
1313
*/
1414
const shouldShowBackdrop = backdropBreakpoint === undefined || backdropBreakpoint < currentBreakpoint!;
15-
const initialBackdrop = shouldShowBackdrop ? `calc(var(--backdrop-opacity) * ${currentBreakpoint!})` : '0';
15+
16+
const initialBackdrop =
17+
opts.theme === 'ionic'
18+
? staticBackdropOpacity
19+
: shouldShowBackdrop
20+
? `calc(var(--backdrop-opacity) * ${currentBreakpoint!})`
21+
: '0';
1622

1723
const backdropAnimation = createAnimation('backdropAnimation').fromTo('opacity', 0, initialBackdrop);
1824

@@ -40,10 +46,11 @@ export const createSheetLeaveAnimation = (opts: ModalAnimationOptions) => {
4046
* is defined, so we need to account for that offset by figuring out
4147
* what the current backdrop value should be.
4248
*/
43-
const backdropValue = `calc(var(--backdrop-opacity) * ${getBackdropValueForSheet(
44-
currentBreakpoint!,
45-
backdropBreakpoint!
46-
)})`;
49+
const backdropValue =
50+
opts.theme === 'ionic'
51+
? staticBackdropOpacity
52+
: `calc(var(--backdrop-opacity) * ${getBackdropValueForSheet(currentBreakpoint!, backdropBreakpoint!)})`;
53+
4754
const defaultBackdrop = [
4855
{ offset: 0, opacity: backdropValue },
4956
{ offset: 1, opacity: 0 },

core/src/components/modal/gestures/sheet.ts

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import { getIonTheme } from '@global/ionic-global';
12
import { isIonContent, findClosestIonContent } from '@utils/content';
23
import { createGesture } from '@utils/gesture';
34
import { clamp, raf, getElementRoot } from '@utils/helpers';
45
import { FOCUS_TRAP_DISABLE_CLASS } from '@utils/overlays';
56

67
import type { Animation } from '../../../interface';
78
import type { GestureDetail } from '../../../utils/gesture';
8-
import { getBackdropValueForSheet } from '../utils';
9+
import { getBackdropValueForSheet, staticBackdropOpacity } from '../utils';
910

1011
import { calculateSpringStep, handleCanDismiss } from './utils';
1112

@@ -53,6 +54,7 @@ export const createSheetGesture = (
5354
onDismiss: () => void,
5455
onBreakpointChange: (breakpoint: number) => void
5556
) => {
57+
const theme = getIonTheme(baseEl);
5658
// Defaults for the sheet swipe animation
5759
const defaultBackdrop = [
5860
{ offset: 0, opacity: 'var(--backdrop-opacity)' },
@@ -65,12 +67,18 @@ export const createSheetGesture = (
6567
{ offset: 1, opacity: 0 },
6668
];
6769

70+
const ionicThemeBackdrop = [
71+
{ offset: 0, opacity: staticBackdropOpacity },
72+
{ offset: 1, opacity: staticBackdropOpacity },
73+
];
74+
6875
const SheetDefaults = {
6976
WRAPPER_KEYFRAMES: [
7077
{ offset: 0, transform: 'translateY(0%)' },
7178
{ offset: 1, transform: 'translateY(100%)' },
7279
],
73-
BACKDROP_KEYFRAMES: backdropBreakpoint !== 0 ? customBackdrop : defaultBackdrop,
80+
BACKDROP_KEYFRAMES:
81+
theme === 'ionic' ? ionicThemeBackdrop : backdropBreakpoint !== 0 ? customBackdrop : defaultBackdrop,
7482
};
7583

7684
const contentEl = baseEl.querySelector('ion-content');
@@ -309,19 +317,35 @@ export const createSheetGesture = (
309317
{ offset: 1, transform: `translateY(${(1 - snapToBreakpoint) * 100}%)` },
310318
]);
311319

312-
backdropAnimation.keyframes([
313-
{
314-
offset: 0,
315-
opacity: `calc(var(--backdrop-opacity) * ${getBackdropValueForSheet(
316-
1 - breakpointOffset,
317-
backdropBreakpoint
318-
)})`,
319-
},
320-
{
321-
offset: 1,
322-
opacity: `calc(var(--backdrop-opacity) * ${getBackdropValueForSheet(snapToBreakpoint, backdropBreakpoint)})`,
323-
},
324-
]);
320+
backdropAnimation.keyframes(
321+
theme === 'ionic'
322+
? [
323+
{
324+
offset: 0,
325+
opacity: staticBackdropOpacity,
326+
},
327+
{
328+
offset: 1,
329+
opacity: staticBackdropOpacity,
330+
},
331+
]
332+
: [
333+
{
334+
offset: 0,
335+
opacity: `calc(var(--backdrop-opacity) * ${getBackdropValueForSheet(
336+
1 - breakpointOffset,
337+
backdropBreakpoint
338+
)})`,
339+
},
340+
{
341+
offset: 1,
342+
opacity: `calc(var(--backdrop-opacity) * ${getBackdropValueForSheet(
343+
snapToBreakpoint,
344+
backdropBreakpoint
345+
)})`,
346+
},
347+
]
348+
);
325349

326350
animation.progressStep(0);
327351
}

core/src/components/modal/modal-interface.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OverlayOptions } from '@utils/overlays-interface';
22

3-
import type { ComponentProps, ComponentRef, FrameworkDelegate } from '../../interface';
3+
import type { ComponentProps, ComponentRef, FrameworkDelegate, Theme } from '../../interface';
44

55
export interface ModalOptions<T extends ComponentRef = ComponentRef> extends OverlayOptions {
66
component: T;
@@ -27,6 +27,7 @@ export interface ModalAnimationOptions {
2727
presentingEl?: HTMLElement;
2828
currentBreakpoint?: number;
2929
backdropBreakpoint?: number;
30+
theme: Theme;
3031
}
3132

3233
export interface ModalBreakpointChangeEventDetail {

core/src/components/modal/modal.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type {
3131
FrameworkDelegate,
3232
Gesture,
3333
OverlayInterface,
34+
Theme,
3435
} from '../../interface';
3536
import { KEYBOARD_DID_OPEN } from '../../utils/keyboard/keyboard';
3637
import type { OverlayEventDetail } from '../../utils/overlays-interface';
@@ -573,6 +574,7 @@ export class Modal implements ComponentInterface, OverlayInterface {
573574
presentingEl: presentingElement,
574575
currentBreakpoint: this.initialBreakpoint,
575576
backdropBreakpoint: this.backdropBreakpoint,
577+
theme: getIonTheme(this),
576578
});
577579

578580
/* tslint:disable-next-line */
@@ -789,6 +791,7 @@ export class Modal implements ComponentInterface, OverlayInterface {
789791
presentingEl: presentingElement,
790792
currentBreakpoint: this.currentBreakpoint ?? this.initialBreakpoint,
791793
backdropBreakpoint: this.backdropBreakpoint,
794+
theme: getIonTheme(this),
792795
}
793796
);
794797

@@ -1048,6 +1051,10 @@ interface ModalOverlayOptions {
10481051
* to fade in when using a sheet modal.
10491052
*/
10501053
backdropBreakpoint: number;
1054+
/**
1055+
* The theme used by the sheet modal
1056+
*/
1057+
theme: Theme;
10511058
}
10521059

10531060
type ModalPresentOptions = ModalOverlayOptions;

core/src/components/modal/utils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { win } from '@utils/browser';
22
import { StatusBar, Style } from '@utils/native/status-bar';
33

4+
export const staticBackdropOpacity = 0.7;
5+
46
/**
57
* Use y = mx + b to
68
* figure out the backdrop value

0 commit comments

Comments
 (0)