Skip to content

Commit 71198e3

Browse files
authored
[MOB-5930]: Fix some safari dismissal bugs (#197)
* temp * fix exit and background button * update tests * lets see * another * unit tests fixed * revert some dev changes * remove elements on trigger close * update utils test --------- Co-authored-by: mitch prewitt <[email protected]>
1 parent 616a134 commit 71198e3

File tree

5 files changed

+33
-15
lines changed

5 files changed

+33
-15
lines changed

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@
9797
},
9898
"lint-staged": {
9999
"*.{ts,tsx,js}": [
100-
"eslint",
101-
"prettier --write"
100+
"eslint --fix",
101+
"prettier --write",
102+
"eslint"
102103
]
103104
}
104-
}
105+
}

src/inapp/inapp.ts

+17-10
Original file line numberDiff line numberDiff line change
@@ -407,9 +407,11 @@ export function getInAppMessages(
407407
The overlay doesn't handle this because the overlay only surrounds the iframe,
408408
not the in-app message. So imagine an in-app looking like this:
409409
*/
410-
if (activeIframe.contentWindow?.document) {
411-
const absoluteDismissButton =
412-
activeIframe.contentWindow.document.createElement('button');
410+
if (activeIframe?.contentWindow?.document) {
411+
const closeXButtonId = 'close-x';
412+
const absoluteDismissButton = document.createElement('button');
413+
const absoluteDismissId = 'absolute-dismiss';
414+
absoluteDismissButton.setAttribute('id', absoluteDismissId);
413415
absoluteDismissButton.style.cssText = `
414416
background: none;
415417
color: inherit;
@@ -441,11 +443,15 @@ export function getInAppMessages(
441443
);
442444
}
443445
global.removeEventListener('resize', throttledResize);
446+
const closeXButtonElement =
447+
document.getElementById(closeXButtonId);
448+
const absoluteDismissButtonElement =
449+
document.getElementById(absoluteDismissId);
450+
closeXButtonElement?.remove();
451+
absoluteDismissButtonElement?.remove();
444452
};
445453
absoluteDismissButton.addEventListener('click', triggerClose);
446-
activeIframe.contentWindow.document.body.appendChild(
447-
absoluteDismissButton
448-
);
454+
document.body.appendChild(absoluteDismissButton);
449455

450456
/**
451457
* Here we paint an optional close button if the user provided configuration
@@ -456,8 +462,9 @@ export function getInAppMessages(
456462
* button will not be able to dismiss the message (Safari blocks JS from running
457463
* on bound event handlers)
458464
*/
459-
if (payload.closeButton && !isSafari) {
460-
const newButton = generateCloseButton(
465+
if (payload.closeButton) {
466+
const closeXButton = generateCloseButton(
467+
closeXButtonId,
461468
document,
462469
payload.closeButton?.position,
463470
payload.closeButton?.color,
@@ -466,8 +473,8 @@ export function getInAppMessages(
466473
payload.closeButton.topOffset,
467474
payload.closeButton.sideOffset
468475
);
469-
newButton.addEventListener('click', triggerClose);
470-
activeIframe.contentWindow.document.body.appendChild(newButton);
476+
closeXButton.addEventListener('click', triggerClose);
477+
document.body.appendChild(closeXButton);
471478
}
472479
}
473480

src/inapp/tests/inapp.test.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ describe('getInAppMessages', () => {
325325

326326
expect(frame?.tagName).toBe('IFRAME');
327327

328-
const element = frame?.contentWindow?.document.body.querySelector(
328+
const element = document.body.querySelector(
329329
'[data-qa-custom-close-button]'
330330
);
331331

@@ -385,7 +385,9 @@ describe('getInAppMessages', () => {
385385
pauseMessageStream();
386386
jest.advanceTimersByTime(32000);
387387

388-
expect(document.body.innerHTML).toBe('');
388+
expect(document.body.innerHTML).toBe(
389+
'<button id="absolute-dismiss" style="background: none; padding: 0px; cursor: unset; outline: inherit; height: 100vh; width: 100vw; position: fixed; top: 0px; left: 0px; z-index: -1;" tabindex="-1"></button>'
390+
);
389391
});
390392

391393
it('should paint next message to DOM if resumed', async () => {

src/inapp/tests/utils.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,9 @@ describe('Utils', () => {
895895

896896
describe('Close Button', () => {
897897
it('should paint the close button with the correct properties', () => {
898+
const buttonId = 'some-test-id';
898899
const button = generateCloseButton(
900+
buttonId,
899901
document,
900902
'top-left',
901903
'blue',
@@ -905,6 +907,7 @@ describe('Utils', () => {
905907
'20px'
906908
);
907909

910+
expect(button.id).toEqual(buttonId);
908911
expect(button.style.width).toBe('20px');
909912
expect(button.style.height).toBe('20px');
910913
expect(button.style.fontSize).toBe('20px');
@@ -916,6 +919,7 @@ describe('Utils', () => {
916919
expect(button.innerHTML).toBe('✕');
917920

918921
const rightButton = generateCloseButton(
922+
buttonId,
919923
document,
920924
'top-right',
921925
'blue',
@@ -925,6 +929,7 @@ describe('Utils', () => {
925929
'20px'
926930
);
927931

932+
expect(button.id).toEqual(buttonId);
928933
expect(rightButton.style.width).toBe('20px');
929934
expect(rightButton.style.height).toBe('20px');
930935
expect(rightButton.style.fontSize).toBe('20px');

src/inapp/utils.ts

+3
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export const addNewMessagesToCache = async (
236236
};
237237

238238
export const generateCloseButton = (
239+
id: string,
239240
doc: Document,
240241
position?: 'top-right' | 'top-left',
241242
color?: string,
@@ -263,6 +264,7 @@ export const generateCloseButton = (
263264
height: ${parsedSize};
264265
font-size: ${parsedSize};
265266
color: ${color};
267+
z-index: 1000000;
266268
`;
267269
const button = doc.createElement('button');
268270
button.style.cssText =
@@ -290,6 +292,7 @@ export const generateCloseButton = (
290292
*/
291293
button.ariaLabel = 'Close modal button';
292294
button.setAttribute('data-qa-custom-close-button', 'true');
295+
button.setAttribute('id', id);
293296
return button;
294297
};
295298

0 commit comments

Comments
 (0)