Skip to content

Commit 8ad8237

Browse files
feat: supports v6 custom close button (#1190)
1 parent e7fd98c commit 8ad8237

File tree

4 files changed

+63
-18
lines changed

4 files changed

+63
-18
lines changed

src/components/modal/v2/lib/postMessage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const POSTMESSENGER_EVENT_TYPES = {
88
const POSTMESSENGER_ACK_PAYLOAD = {
99
ok: 'true'
1010
};
11-
11+
// these constants should maintain parity with MESSAGE_MODAL_EVENT_NAMES in core-web-sdk
1212
export const POSTMESSENGER_EVENT_NAMES = {
1313
CALCULATE: 'paypal-messages-modal-calculate',
1414
CLOSE: 'paypal-messages-modal-close',

src/components/modal/v2/lib/zoid-polyfill.js

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { sendEvent, createPostMessengerEvent, POSTMESSENGER_EVENT_NAMES } from '
66

77
const IOS_INTERFACE_NAME = 'paypalMessageModalCallbackHandler';
88
const ANDROID_INTERFACE_NAME = 'paypalMessageModalCallbackHandler';
9-
// these constants should maintain parity with MESSAGE_MODAL_EVENT_NAMES in core-web-sdk
109

1110
function updateProps(newProps, propListeners) {
1211
Array.from(propListeners.values()).forEach(listener => {
@@ -15,20 +14,43 @@ function updateProps(newProps, propListeners) {
1514
Object.assign(window.xprops, newProps);
1615
}
1716

18-
export function handleBrowserEvents(trustedOrigin, propListeners, updatedPropsEvent) {
17+
export function handlePropsUpdateEvent(propListeners, updatedPropsEvent) {
1918
const {
20-
origin: eventOrigin,
21-
data: { eventName, id, eventPayload: newProps }
19+
data: { eventPayload: newProps }
2220
} = updatedPropsEvent;
23-
24-
if (eventOrigin === trustedOrigin && eventName === 'PROPS_UPDATE' && newProps && typeof newProps === 'object') {
25-
// send event ack with original event id so PostMessenger will stop reposting event
26-
sendEvent(createPostMessengerEvent('ack', id), trustedOrigin);
21+
if (newProps && typeof newProps === 'object') {
2722
const validProps = validateProps(newProps);
2823
updateProps(validProps, propListeners);
2924
}
3025
}
3126

27+
export function logModalClose(linkName) {
28+
logger.track({
29+
index: '1',
30+
et: 'CLICK',
31+
event_type: 'modal_close',
32+
page_view_link_name: linkName
33+
});
34+
}
35+
36+
export function handleBrowserEvents(clientOrigin, propListeners, event) {
37+
const {
38+
origin: eventOrigin,
39+
data: { eventName, id }
40+
} = event;
41+
if (eventOrigin !== clientOrigin) {
42+
return;
43+
}
44+
if (eventName === 'PROPS_UPDATE') {
45+
handlePropsUpdateEvent(propListeners, event);
46+
}
47+
if (eventName === 'MODAL_CLOSED') {
48+
logModalClose(event.data.eventPayload.linkName);
49+
}
50+
// send event ack with original event id so PostMessenger will stop reposting event
51+
sendEvent(createPostMessengerEvent('ack', id), clientOrigin);
52+
}
53+
3254
const getAccount = (merchantId, clientId, payerId) => {
3355
if (merchantId) {
3456
return merchantId;
@@ -144,12 +166,7 @@ const setupBrowser = props => {
144166
createPostMessengerEvent('message', POSTMESSENGER_EVENT_NAMES.CLOSE, eventPayload),
145167
trustedOrigin
146168
);
147-
logger.track({
148-
index: '1',
149-
et: 'CLICK',
150-
event_type: 'modal_close',
151-
page_view_link_name: linkName
152-
});
169+
logModalClose(linkName);
153170
},
154171
// Overridable defaults
155172
integrationType: __MESSAGES__.__TARGET__,

src/components/modal/v2/parts/Container.jsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ const Container = ({ children }) => {
4545
useEffect(() => {
4646
if (transitionState === 'CLOSED') {
4747
contentWrapperRef.current.scrollTop = 0;
48-
} else if (transitionState === 'OPEN') {
49-
window.focus();
5048
}
5149
}, [transitionState]);
5250

tests/unit/spec/src/components/modal/v2/lib/zoid-polyfill.test.js

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ describe('zoidPollyfill', () => {
448448
expect(addEventListenerSpy).toHaveBeenCalledTimes(1);
449449
expect(addEventListenerSpy).toHaveBeenCalledWith('message', expect.any(Function), false);
450450
});
451-
test('handleBrowserEvents updates props when values are valid', () => {
451+
test('handleBrowserEvents handles PROPS_UPDATE and updates props when values are valid', () => {
452452
// jest doesn't support calling postMessage, so we cannot use the event listener above
453453
// instead we will manually verify that handleBrowserEvents works as intended
454454
const clientOrigin = 'http://example.com';
@@ -479,6 +479,36 @@ describe('zoidPollyfill', () => {
479479
})
480480
);
481481
});
482+
test('handleBrowserEvents handles MODAL_CLOSE and logs close method', () => {
483+
// jest doesn't support calling postMessage, so we cannot use the event listener above
484+
// instead we will manually verify that handleBrowserEvents works as intended
485+
const clientOrigin = 'http://example.com';
486+
487+
const newPropsEvent = {
488+
origin: clientOrigin,
489+
data: {
490+
eventName: 'MODAL_CLOSED',
491+
eventPayload: {
492+
linkName: 'Custom Close Button'
493+
}
494+
}
495+
};
496+
497+
const propListeners = new Set();
498+
const onPropsCallback = jest.fn();
499+
propListeners.add(onPropsCallback);
500+
handleBrowserEvents(clientOrigin, propListeners, newPropsEvent);
501+
502+
expect(logger.track).toHaveBeenCalledTimes(1);
503+
expect(logger.track).toHaveBeenCalledWith(
504+
expect.objectContaining({
505+
index: '1',
506+
et: 'CLICK',
507+
event_type: 'modal_close',
508+
page_view_link_name: 'Custom Close Button'
509+
})
510+
);
511+
});
482512
test('handleBrowserEvents handles unrelated events with no data', () => {
483513
const unrelatedEvent = {
484514
data: {}

0 commit comments

Comments
 (0)