@placetopay/lightbox-sdk 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,21 +4,24 @@ export const mountListener = (callbacks, styles) => {
4
4
  if (!callbacks)
5
5
  return;
6
6
  const listener = (event) => {
7
- var _a, _b;
7
+ var _a, _b, _c;
8
8
  let receivedStyles;
9
9
  switch (event.data.type) {
10
10
  case LightboxAppEvents.CLOSE:
11
- unmountLightbox(listener);
11
+ unmountLightbox(listener, (_a = event.data.payload) !== null && _a !== void 0 ? _a : event.origin);
12
+ break;
13
+ case LightboxAppEvents.CLOSE_OR_REDIRECT:
14
+ unmountLightbox(listener, event.origin);
12
15
  break;
13
16
  case LightboxAppEvents.SEND_STYLES:
14
17
  receivedStyles = event.data.payload;
15
18
  mountStyles(Object.assign(Object.assign(Object.assign({}, receivedStyles), styles), { background: Object.assign(Object.assign({}, receivedStyles === null || receivedStyles === void 0 ? void 0 : receivedStyles.background), styles.background) }));
16
19
  break;
17
20
  case LightboxAppEvents.HIDE_CLOSE_BUTTON:
18
- (_a = document.getElementById(ElementIds.CLOSE_BUTTON_ID)) === null || _a === void 0 ? void 0 : _a.remove();
21
+ (_b = document.getElementById(ElementIds.CLOSE_BUTTON_ID)) === null || _b === void 0 ? void 0 : _b.remove();
19
22
  break;
20
23
  default:
21
- (_b = callbacks[event.data.type]) === null || _b === void 0 ? void 0 : _b.call(callbacks, event.data.payload);
24
+ (_c = callbacks[event.data.type]) === null || _c === void 0 ? void 0 : _c.call(callbacks, event.data.payload);
22
25
  break;
23
26
  }
24
27
  };
@@ -26,7 +29,8 @@ export const mountListener = (callbacks, styles) => {
26
29
  };
27
30
  export const mountLightbox = (url, styles, closeButtonEnabled) => {
28
31
  const wrapper = document.createElement('div');
29
- wrapper.id = ElementIds.WRAPPER_ID;
32
+ wrapper.id = new URL(url).origin;
33
+ wrapper.className = ElementIds.WRAPPER_ID;
30
34
  const iframe = document.createElement('iframe');
31
35
  iframe.src = url;
32
36
  iframe.id = ElementIds.IFRAME_ID;
@@ -37,7 +41,7 @@ export const mountLightbox = (url, styles, closeButtonEnabled) => {
37
41
  const closeButton = document.createElement('button');
38
42
  closeButton.id = ElementIds.CLOSE_BUTTON_ID;
39
43
  closeButton.addEventListener('click', () => {
40
- globalThis.postMessage({ type: LightboxAppEvents.CLOSE }, '*');
44
+ globalThis.postMessage({ type: LightboxAppEvents.CLOSE, payload: new URL(url).origin }, '*');
41
45
  });
42
46
  const closeButtonContent = document.createElement('span');
43
47
  closeButtonContent.classList.add('placetopay-close-button-content');
@@ -62,9 +66,13 @@ const mountStyles = (styles) => {
62
66
  width = Dimensions[styles.dimension.toUpperCase()].width;
63
67
  document.documentElement.style.setProperty(Styles.MAX_WIDTH, `${width.toString()}px`);
64
68
  };
65
- const unmountLightbox = (listener) => {
66
- var _a;
67
- (_a = document.getElementById(ElementIds.WRAPPER_ID)) === null || _a === void 0 ? void 0 : _a.remove();
68
- globalThis.removeEventListener('message', listener);
69
- document.documentElement.style.removeProperty(Styles.BACKGROUND_COLOR);
69
+ const unmountLightbox = (listener, origin) => {
70
+ const element = document.getElementById(origin);
71
+ if (element) {
72
+ element.remove();
73
+ globalThis.removeEventListener('message', listener);
74
+ document.documentElement.style.removeProperty(Styles.BACKGROUND_COLOR);
75
+ }
76
+ else
77
+ throw new Error(`Frame from "${origin}" not found`);
70
78
  };
@@ -0,0 +1,12 @@
1
+ import { InitOptions, LightboxInstance, LightboxStyles } from '../types';
2
+ export declare const LightboxSdk: {
3
+ isInside: () => boolean;
4
+ close: (frame?: string) => void;
5
+ sendStyles: (styles: LightboxStyles) => void;
6
+ hideCloseButton: () => void;
7
+ closeOrRedirect: (url?: string) => void;
8
+ emit: (type: string, payload: unknown) => {
9
+ close: (frame?: string) => void;
10
+ };
11
+ init: (url: string, options?: InitOptions) => LightboxInstance;
12
+ };
@@ -1,6 +1,27 @@
1
+ import { LightboxAppEvents } from '../constants';
1
2
  import { redirectBasedOnDriver } from '../helpers';
2
3
  import { mountLightbox, mountListener } from './assemblers';
3
- export const LightboxClient = {
4
+ export const LightboxSdk = {
5
+ isInside: () => globalThis.location !== globalThis.parent.location,
6
+ close: (frame) => {
7
+ globalThis.parent.postMessage({ type: LightboxAppEvents.CLOSE, payload: frame }, '*');
8
+ },
9
+ sendStyles: (styles) => {
10
+ globalThis.parent.postMessage({ type: LightboxAppEvents.SEND_STYLES, payload: styles }, '*');
11
+ },
12
+ hideCloseButton: () => {
13
+ globalThis.parent.postMessage({ type: LightboxAppEvents.HIDE_CLOSE_BUTTON }, '*');
14
+ },
15
+ closeOrRedirect: (url) => {
16
+ if (!url || LightboxSdk.isInside())
17
+ globalThis.parent.postMessage({ type: LightboxAppEvents.CLOSE_OR_REDIRECT }, '*');
18
+ else
19
+ globalThis.location.href = url;
20
+ },
21
+ emit: (type, payload) => {
22
+ globalThis.parent.postMessage({ type, payload }, '*');
23
+ return { close: LightboxSdk.close };
24
+ },
4
25
  init: (url, options) => {
5
26
  var _a, _b, _c, _d;
6
27
  const lightbox = {
@@ -7,7 +7,8 @@ export declare enum ElementIds {
7
7
  export declare enum LightboxAppEvents {
8
8
  CLOSE = "lightbox:close",
9
9
  SEND_STYLES = "lightbox:sendStyles",
10
- HIDE_CLOSE_BUTTON = "lightbox:hideCloseButton"
10
+ HIDE_CLOSE_BUTTON = "lightbox:hideCloseButton",
11
+ CLOSE_OR_REDIRECT = "lightbox:closeOrRedirect"
11
12
  }
12
13
  export declare enum Styles {
13
14
  BACKGROUND_COLOR = "--placetopay-lightbox-background-color",
package/dist/constants.js CHANGED
@@ -10,6 +10,7 @@ export var LightboxAppEvents;
10
10
  LightboxAppEvents["CLOSE"] = "lightbox:close";
11
11
  LightboxAppEvents["SEND_STYLES"] = "lightbox:sendStyles";
12
12
  LightboxAppEvents["HIDE_CLOSE_BUTTON"] = "lightbox:hideCloseButton";
13
+ LightboxAppEvents["CLOSE_OR_REDIRECT"] = "lightbox:closeOrRedirect";
13
14
  })(LightboxAppEvents || (LightboxAppEvents = {}));
14
15
  export var Styles;
15
16
  (function (Styles) {
@@ -1,6 +1,6 @@
1
1
  @import url('./close_button.css');
2
2
 
3
- #placetopay_lightbox_wrapper {
3
+ .placetopay_lightbox_wrapper {
4
4
  background: var(--placetopay-lightbox-background-color);
5
5
  position: fixed;
6
6
  display: flex;
package/dist/index.d.ts CHANGED
@@ -1,3 +1,2 @@
1
- export { LightboxApp } from './LightboxApp';
2
- export { LightboxClient } from './LightboxClient';
1
+ export { LightboxSdk } from './LightboxSdk';
3
2
  export * from './types';
package/dist/index.js CHANGED
@@ -1,3 +1,2 @@
1
- export { LightboxApp } from './LightboxApp';
2
- export { LightboxClient } from './LightboxClient';
1
+ export { LightboxSdk } from './LightboxSdk';
3
2
  export * from './types';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placetopay/lightbox-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Small javascript library to encapsulate websites in a lightbox with configurable styles and behaviors",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -1,10 +0,0 @@
1
- import { LightboxStyles } from '../types';
2
- export declare const LightboxApp: {
3
- isInside: () => boolean;
4
- sendStyles: (styles: LightboxStyles) => void;
5
- hideCloseButton: () => void;
6
- close: (url?: string) => void;
7
- emit: (type: string, payload: unknown) => {
8
- close: (url?: string) => void;
9
- };
10
- };
@@ -1,20 +0,0 @@
1
- import { LightboxAppEvents } from '../constants';
2
- export const LightboxApp = {
3
- isInside: () => globalThis.location !== globalThis.parent.location,
4
- sendStyles: (styles) => {
5
- globalThis.parent.postMessage({ type: LightboxAppEvents.SEND_STYLES, payload: styles }, '*');
6
- },
7
- hideCloseButton: () => {
8
- globalThis.parent.postMessage({ type: LightboxAppEvents.HIDE_CLOSE_BUTTON }, '*');
9
- },
10
- close: (url) => {
11
- if (!url || LightboxApp.isInside())
12
- globalThis.parent.postMessage({ type: LightboxAppEvents.CLOSE }, '*');
13
- else
14
- globalThis.location.href = url;
15
- },
16
- emit: (type, payload) => {
17
- globalThis.parent.postMessage({ type, payload }, '*');
18
- return { close: LightboxApp.close };
19
- },
20
- };
@@ -1,4 +0,0 @@
1
- import { InitOptions, LightboxInstance } from '../types';
2
- export declare const LightboxClient: {
3
- init: (url: string, options?: InitOptions) => LightboxInstance;
4
- };