@placetopay/lightbox-sdk 2.1.0 → 2.1.2

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.
@@ -13,5 +13,8 @@ export declare enum Styles {
13
13
  BACKDROP_COLOR = "--placetopay-lightbox-backdrop-color",
14
14
  RADIUS = "--placetopay-lightbox-border-radius",
15
15
  MAX_HEIGHT = "--placetopay-lightbox-max-height",
16
- MAX_WIDTH = "--placetopay-lightbox-max-width"
16
+ MAX_WIDTH = "--placetopay-lightbox-max-width",
17
+ POSITION = "--placetopay-lightbox-position",
18
+ WRAPPER_WIDTH = "--placetopay-lightbox-wrapper-width",
19
+ WRAPPER_HEIGHT = "--placetopay-lightbox-wrapper-height"
17
20
  }
package/dist/constants.js CHANGED
@@ -17,4 +17,7 @@ export var Styles;
17
17
  Styles["RADIUS"] = "--placetopay-lightbox-border-radius";
18
18
  Styles["MAX_HEIGHT"] = "--placetopay-lightbox-max-height";
19
19
  Styles["MAX_WIDTH"] = "--placetopay-lightbox-max-width";
20
+ Styles["POSITION"] = "--placetopay-lightbox-position";
21
+ Styles["WRAPPER_WIDTH"] = "--placetopay-lightbox-wrapper-width";
22
+ Styles["WRAPPER_HEIGHT"] = "--placetopay-lightbox-wrapper-height";
20
23
  })(Styles || (Styles = {}));
@@ -1,3 +1,3 @@
1
1
  import { MountLightboxOptions } from '../types';
2
- export declare const mountLightbox: ({ url, callbacks, styles, closeButtonEnabled, enforceStyles }: MountLightboxOptions) => void;
2
+ export declare const mountLightbox: ({ id, url, element, callbacks, styles, closeButtonEnabled, enforceStyles, }: MountLightboxOptions) => void;
3
3
  export declare const unmountLightbox: (target: string) => void;
@@ -1,15 +1,21 @@
1
1
  import { Styles, ElementIds, LightboxEvents as LE } from '../constants';
2
2
  import { setStyle, unsetStyle } from '../helpers';
3
3
  let listener;
4
- export const mountLightbox = ({ url, callbacks, styles, closeButtonEnabled, enforceStyles }) => {
4
+ export const mountLightbox = ({ id, url, element, callbacks, styles, closeButtonEnabled, enforceStyles, }) => {
5
5
  const wrapper = document.createElement('div');
6
- wrapper.id = new URL(url).origin;
6
+ wrapper.id = id;
7
7
  wrapper.className = ElementIds.WRAPPER_ID;
8
+ if (element !== document.body) {
9
+ wrapper.style.setProperty(Styles.POSITION, 'absolute');
10
+ wrapper.style.setProperty(Styles.WRAPPER_HEIGHT, '100%');
11
+ wrapper.style.setProperty(Styles.WRAPPER_WIDTH, '100%');
12
+ }
8
13
  const iframe = document.createElement('iframe');
9
14
  iframe.src = url;
10
15
  iframe.id = ElementIds.IFRAME_ID;
11
16
  updateStyles(styles);
12
- document.body.appendChild(wrapper);
17
+ console.log({ isBody: element === document.body });
18
+ element.appendChild(wrapper);
13
19
  wrapper.appendChild(iframe);
14
20
  let closeButton;
15
21
  if (closeButtonEnabled) {
@@ -17,17 +23,17 @@ export const mountLightbox = ({ url, callbacks, styles, closeButtonEnabled, enfo
17
23
  closeButton.id = ElementIds.CLOSE_BUTTON_ID;
18
24
  closeButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" stroke="#4b5563" stroke-width="2"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18 18 6M6 6l12 12"/></svg>`;
19
25
  closeButton.addEventListener('click', () => {
20
- unmountLightbox(new URL(url).origin);
26
+ unmountLightbox(id);
21
27
  });
22
28
  wrapper.appendChild(closeButton);
23
29
  }
24
- mountListener({ callbacks, styles, closeButton, enforceStyles });
30
+ mountListener({ id, callbacks, styles, closeButton, enforceStyles });
25
31
  };
26
- const mountListener = ({ callbacks, styles, closeButton, enforceStyles }) => {
32
+ const mountListener = ({ id, callbacks, styles, closeButton, enforceStyles }) => {
27
33
  listener = (event) => {
28
34
  var _a;
29
35
  if (event.data.type === LE.CLOSE)
30
- unmountLightbox(event.origin);
36
+ unmountLightbox(id);
31
37
  if (event.data.type === LE.UPDATE_STYLES) {
32
38
  const newStyles = enforceStyles
33
39
  ? Object.assign(Object.assign({}, event.data.payload), styles) : Object.assign(Object.assign({}, styles), event.data.payload);
@@ -56,11 +62,14 @@ const clearStyles = () => {
56
62
  unsetStyle(Styles.MAX_WIDTH);
57
63
  };
58
64
  const updateStyles = (styles) => {
59
- var _a, _b, _c, _d, _e;
65
+ var _a, _b, _c, _d, _e, _f, _g, _h;
60
66
  setStyle(Styles.BACKDROP_COLOR, buildBackdrop((_a = styles.backdropColor) !== null && _a !== void 0 ? _a : '#000000', (_b = styles.backdropOpacity) !== null && _b !== void 0 ? _b : 0.7));
61
67
  setStyle(Styles.RADIUS, `${(_c = styles.radius) !== null && _c !== void 0 ? _c : 0}px`);
62
68
  setStyle(Styles.MAX_HEIGHT, buildDimension('height', (_d = styles.height) !== null && _d !== void 0 ? _d : 640));
63
69
  setStyle(Styles.MAX_WIDTH, buildDimension('width', (_e = styles.width) !== null && _e !== void 0 ? _e : 512));
70
+ setStyle(Styles.POSITION, (_f = styles.position) !== null && _f !== void 0 ? _f : 'fixed');
71
+ setStyle(Styles.WRAPPER_WIDTH, buildDimension('width', (_g = styles.wrapperWidth) !== null && _g !== void 0 ? _g : '100vw'));
72
+ setStyle(Styles.WRAPPER_HEIGHT, buildDimension('height', (_h = styles.wrapperHeight) !== null && _h !== void 0 ? _h : '100vh'));
64
73
  };
65
74
  const buildBackdrop = (color, opacity) => {
66
75
  var _a;
@@ -70,7 +79,7 @@ const buildBackdrop = (color, opacity) => {
70
79
  };
71
80
  const buildDimension = (type, value) => {
72
81
  const result = String(value);
73
- if (result.match(/^\d+%$/) || result.match(/^\d+px$/))
82
+ if (result.match(/^\d+(vh|vw|px|%)$/))
74
83
  return result;
75
84
  if (result.match(/^\d+$/))
76
85
  return `${result}px`;
@@ -9,12 +9,14 @@ export const emitClose = (payload, preventClose) => {
9
9
  };
10
10
  export const emit = ({ type, payload, preventClose }) => postMessage(type, payload, preventClose);
11
11
  export const createLightbox = (url, options) => {
12
- var _a, _b, _c, _d;
12
+ var _a, _b, _c, _d, _e, _f;
13
13
  const lightbox = {
14
- allowRedirects: (_a = options === null || options === void 0 ? void 0 : options.allowRedirects) !== null && _a !== void 0 ? _a : true,
15
- callbacks: (_b = options === null || options === void 0 ? void 0 : options.callbacks) !== null && _b !== void 0 ? _b : {},
16
- closeButton: (_c = options === null || options === void 0 ? void 0 : options.closeButton) !== null && _c !== void 0 ? _c : true,
17
- styles: (_d = options === null || options === void 0 ? void 0 : options.styles) !== null && _d !== void 0 ? _d : {},
14
+ id: (_a = options === null || options === void 0 ? void 0 : options.id) !== null && _a !== void 0 ? _a : new URL(url).origin,
15
+ element: (_b = options === null || options === void 0 ? void 0 : options.element) !== null && _b !== void 0 ? _b : document.body,
16
+ allowRedirects: (_c = options === null || options === void 0 ? void 0 : options.allowRedirects) !== null && _c !== void 0 ? _c : true,
17
+ callbacks: (_d = options === null || options === void 0 ? void 0 : options.callbacks) !== null && _d !== void 0 ? _d : {},
18
+ closeButton: (_e = options === null || options === void 0 ? void 0 : options.closeButton) !== null && _e !== void 0 ? _e : true,
19
+ styles: (_f = options === null || options === void 0 ? void 0 : options.styles) !== null && _f !== void 0 ? _f : {},
18
20
  url: url,
19
21
  close: () => unmountLightbox(url),
20
22
  updateStyles,
@@ -27,7 +29,9 @@ export const createLightbox = (url, options) => {
27
29
  if (lightbox.allowRedirects)
28
30
  redirectBasedOnDriver(url);
29
31
  mountLightbox({
32
+ id: lightbox.id,
30
33
  url,
34
+ element: lightbox.element,
31
35
  callbacks: lightbox.callbacks,
32
36
  styles: lightbox.styles,
33
37
  closeButtonEnabled: lightbox.closeButton,
package/dist/helpers.js CHANGED
@@ -11,6 +11,9 @@ export const redirectBasedOnDriver = (url) => {
11
11
  if (navigator.userAgent.match(/iPhone|iPad|iPod/i) ||
12
12
  /^((?!chrome|android).)*safari/i.test(navigator.userAgent) // regex from https://stackoverflow.com/a/23522755
13
13
  ) {
14
+ if (window.self !== window.top) {
15
+ window.parent.postMessage({ type: "placetopay-lightbox:redirect", url }, "*"); // dont change this, it would be a broken change
16
+ }
14
17
  location.href = url;
15
18
  }
16
19
  };
package/dist/styles.css CHANGED
@@ -1,13 +1,13 @@
1
1
  .placetopay_lightbox_wrapper {
2
2
  background: var(--placetopay-lightbox-backdrop-color);
3
- position: fixed;
3
+ position: var(--placetopay-lightbox-position);
4
4
  display: flex;
5
5
  justify-content: center;
6
6
  align-items: center;
7
7
  top: 0;
8
8
  left: 0;
9
- width: 100vw;
10
- height: 100vh;
9
+ width: var(--placetopay-lightbox-wrapper-width);
10
+ height: var(--placetopay-lightbox-wrapper-height);
11
11
  z-index: 999;
12
12
  user-select: none;
13
13
  }
package/dist/types.d.ts CHANGED
@@ -6,6 +6,9 @@ export type LightboxStyles = Partial<{
6
6
  height: string | number;
7
7
  width: string | number;
8
8
  radius: number;
9
+ position: 'absolute' | 'fixed';
10
+ wrapperWidth: string | number;
11
+ wrapperHeight: string | number;
9
12
  }>;
10
13
  export type ApiStructure = {
11
14
  type: string;
@@ -17,6 +20,8 @@ export type InternalOptions = Partial<{
17
20
  enforceStyles: boolean;
18
21
  }>;
19
22
  export type ExposedOptions = Partial<{
23
+ id: string;
24
+ element: HTMLElement;
20
25
  allowRedirects: boolean;
21
26
  callbacks: ClientCallbacks;
22
27
  closeButton: boolean;
@@ -24,13 +29,16 @@ export type ExposedOptions = Partial<{
24
29
  }>;
25
30
  export type InitialOptions = ExposedOptions & InternalOptions;
26
31
  export type MountLightboxOptions = {
32
+ id: string;
27
33
  url: string;
34
+ element: HTMLElement;
28
35
  callbacks: ClientCallbacks;
29
36
  styles: LightboxStyles;
30
37
  closeButtonEnabled: boolean;
31
38
  enforceStyles: boolean;
32
39
  };
33
40
  export type MountListenerOptions = {
41
+ id: string;
34
42
  callbacks: ClientCallbacks;
35
43
  styles: LightboxStyles;
36
44
  closeButton: HTMLButtonElement;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placetopay/lightbox-sdk",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
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",