1
1
import { QRL , QwikMouseEvent } from '@builder.io/qwik' ;
2
2
import { FocusTrap , createFocusTrap } from 'focus-trap' ;
3
3
4
+ export type WidthState = {
5
+ width : number | null ;
6
+ } ;
7
+
4
8
/**
5
9
* Traps the focus of the given Modal
6
10
* @returns FocusTrap
@@ -29,23 +33,22 @@ export function deactivateFocusTrap(focusTrap: FocusTrap | null) {
29
33
30
34
/**
31
35
* Shows the given Modal.
32
- * Applies CSS-Class to animate the modal-showing.
36
+ * Applies a CSS-Class to animate the modal-showing.
33
37
* Calls the given callback that is executed after the Modal has been opened.
34
38
*/
35
39
export async function showModal ( modal : HTMLDialogElement , onShow$ ?: QRL < ( ) => void > ) {
36
40
modal . showModal ( ) ;
37
- opening ( modal ) ;
41
+ supportShowAnimation ( modal ) ;
38
42
await onShow$ ?.( ) ;
39
43
}
40
44
41
45
/**
42
46
* Closes the given Modal.
43
- * Applies CSS-Class to animate the Modal-closing.
47
+ * Applies a CSS-Class to animate the Modal-closing.
44
48
* Calls the given callback that is executed after the Modal has been closed.
45
49
*/
46
50
export async function closeModal ( modal : HTMLDialogElement , onClose$ ?: QRL < ( ) => void > ) {
47
- modal . close ( ) ;
48
- modal . classList . remove ( 'modal-showing' ) ;
51
+ supportClosingAnimation ( modal , ( ) => modal . close ( ) ) ;
49
52
await onClose$ ?.( ) ;
50
53
}
51
54
@@ -78,10 +81,6 @@ export function lockScroll() {
78
81
window . document . body . style . overflow = 'hidden' ;
79
82
}
80
83
81
- export type WidthState = {
82
- width : number | null ;
83
- } ;
84
-
85
84
/**
86
85
* Unlocks scrolling of the document.
87
86
* Adjusts padding of the given scrollbar.
@@ -96,10 +95,12 @@ export function unlockScroll(scrollbar: WidthState) {
96
95
}
97
96
98
97
/**
98
+ * When the Modal is opened we are disabling scrolling.
99
+ * This means the scrollbar will vanish.
100
+ * The scrollbar has a width and causes the Modal to reposition.
99
101
*
100
- * Adjusts scrollbar padding
101
- * TODO: Why???
102
- *
102
+ * That's why we take the scrollbar-width into account so that the
103
+ * Modal does not jump to the right.
103
104
*/
104
105
export function adjustScrollbar ( scrollbar : WidthState , modal : HTMLDialogElement ) {
105
106
if ( scrollbar . width === null ) {
@@ -121,7 +122,7 @@ export function overrideNativeDialogEscapeBehaviorWith(continuation: () => void)
121
122
}
122
123
123
124
/**
124
- * When the Modal is closed we are enabling scrolling again .
125
+ * When the Modal is closed we are enabling scrolling.
125
126
* This means the scrollbar will reappear in the browser.
126
127
* The scrollbar has a width and causes the Modal to reposition.
127
128
*
@@ -141,27 +142,34 @@ export function keepModalInPlaceWhileScrollbarReappears(
141
142
}
142
143
}
143
144
145
+ /*
146
+ * Adds CSS-Class to support modal-opening-animation
147
+ */
148
+ export function supportShowAnimation ( modal : HTMLDialogElement ) {
149
+ modal . classList . add ( 'modal-showing' ) ;
150
+ }
151
+
144
152
/*
145
153
* Listens for animation/transition events in order to
146
154
* remove Animation-CSS-Classes after animation/transition ended.
147
155
*/
148
- export function closing ( modal : HTMLDialogElement , onClose$ ?: QRL < ( ) => void > ) {
149
- if ( ! modal ) {
150
- return ;
151
- }
152
-
156
+ export function supportClosingAnimation (
157
+ modal : HTMLDialogElement ,
158
+ afterAnimate : ( ) => void ,
159
+ ) {
160
+ modal . classList . remove ( 'modal-showing' ) ;
153
161
modal . classList . add ( 'modal-closing' ) ;
154
162
155
163
const { animationDuration, transitionDuration } = getComputedStyle ( modal ) ;
156
164
157
165
const runAnimationEnd = ( ) => {
158
166
modal . classList . remove ( 'modal-closing' ) ;
159
- closeModal ( modal , onClose$ ) ;
167
+ afterAnimate ( ) ;
160
168
} ;
161
169
162
170
const runTransitionEnd = ( ) => {
163
171
modal . classList . remove ( 'modal-closing' ) ;
164
- closeModal ( modal , onClose$ ) ;
172
+ afterAnimate ( ) ;
165
173
} ;
166
174
167
175
if ( animationDuration !== '0s' ) {
@@ -170,15 +178,6 @@ export function closing(modal: HTMLDialogElement, onClose$?: QRL<() => void>) {
170
178
modal . addEventListener ( 'transitionend' , runTransitionEnd , { once : true } ) ;
171
179
} else {
172
180
modal . classList . remove ( 'modal-closing' ) ;
173
- closeModal ( modal , onClose$ ) ;
181
+ afterAnimate ( ) ;
174
182
}
175
183
}
176
-
177
- /*
178
- * Adds CSS-Class to support modal-opening-animation
179
- */
180
- export function opening ( modal : HTMLDialogElement ) {
181
- if ( ! modal ) return ;
182
-
183
- modal . classList . add ( 'modal-showing' ) ;
184
- }
0 commit comments