@placetopay/lightbox-sdk 2.0.0 → 2.0.1

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.
package/dist/config.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export declare const Config: {
2
2
  defaultDimension: {
3
- height: number;
4
- width: number;
3
+ height: string;
4
+ width: string;
5
5
  };
6
6
  };
@@ -10,22 +10,22 @@ export declare enum LightboxEvents {
10
10
  HIDE_CLOSE_BUTTON = "hideCloseButton"
11
11
  }
12
12
  export declare enum Styles {
13
- BACKGROUND_COLOR = "--placetopay-lightbox-background-color",
13
+ BACKDROP_COLOR = "--placetopay-lightbox-backdrop-color",
14
14
  ROUNDED = "--placetopay-lightbox-border-radius",
15
15
  MAX_HEIGHT = "--placetopay-lightbox-max-height",
16
16
  MAX_WIDTH = "--placetopay-lightbox-max-width"
17
17
  }
18
18
  export declare const Dimensions: {
19
19
  SM: {
20
- height: number;
21
- width: number;
20
+ height: string;
21
+ width: string;
22
22
  };
23
23
  MD: {
24
- height: number;
25
- width: number;
24
+ height: string;
25
+ width: string;
26
26
  };
27
27
  LG: {
28
- height: number;
29
- width: number;
28
+ height: string;
29
+ width: string;
30
30
  };
31
31
  };
package/dist/constants.js CHANGED
@@ -13,13 +13,13 @@ export var LightboxEvents;
13
13
  })(LightboxEvents || (LightboxEvents = {}));
14
14
  export var Styles;
15
15
  (function (Styles) {
16
- Styles["BACKGROUND_COLOR"] = "--placetopay-lightbox-background-color";
16
+ Styles["BACKDROP_COLOR"] = "--placetopay-lightbox-backdrop-color";
17
17
  Styles["ROUNDED"] = "--placetopay-lightbox-border-radius";
18
18
  Styles["MAX_HEIGHT"] = "--placetopay-lightbox-max-height";
19
19
  Styles["MAX_WIDTH"] = "--placetopay-lightbox-max-width";
20
20
  })(Styles || (Styles = {}));
21
21
  export const Dimensions = {
22
- SM: { height: 400, width: 320 },
23
- MD: { height: 640, width: 512 },
24
- LG: { height: 1000, width: 800 },
22
+ SM: { height: '400', width: '320' },
23
+ MD: { height: '640', width: '512' },
24
+ LG: { height: '1000', width: '800' },
25
25
  };
@@ -1,17 +1,15 @@
1
1
  import { Config } from '../config';
2
- import { Styles, ElementIds, Dimensions, LightboxEvents } from '../constants';
2
+ import { Styles, ElementIds, Dimensions, LightboxEvents as LE } from '../constants';
3
+ import { setStyle } from '../helpers';
3
4
  export const mountListener = (callbacks, styles) => {
4
5
  const listener = (event) => {
5
6
  var _a, _b, _c;
6
- if (event.data.type === LightboxEvents.CLOSE)
7
+ if (event.data.type === LE.CLOSE)
7
8
  unmountLightbox(listener, (_a = event.data.target) !== null && _a !== void 0 ? _a : event.origin);
8
- if (event.data.type === LightboxEvents.UPDATE_STYLES) {
9
- const receivedStyles = event.data.payload;
10
- 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) }));
11
- }
12
- if (event.data.type === LightboxEvents.HIDE_CLOSE_BUTTON) {
9
+ if (event.data.type === LE.UPDATE_STYLES)
10
+ mountStyles(Object.assign(Object.assign({}, event.data.payload), styles));
11
+ if (event.data.type === LE.HIDE_CLOSE_BUTTON)
13
12
  (_b = document.getElementById(ElementIds.CLOSE_BUTTON_ID)) === null || _b === void 0 ? void 0 : _b.remove();
14
- }
15
13
  (_c = callbacks[event.data.type]) === null || _c === void 0 ? void 0 : _c.call(callbacks, event.data.payload);
16
14
  };
17
15
  globalThis.addEventListener('message', listener);
@@ -34,34 +32,45 @@ export const mountLightbox = (url, styles, closeButtonEnabled) => {
34
32
  </svg>
35
33
  `;
36
34
  closeButton.addEventListener('click', () => {
37
- globalThis.postMessage({ type: LightboxEvents.CLOSE, target: new URL(url).origin }, '*');
35
+ globalThis.postMessage({ type: LE.CLOSE, target: new URL(url).origin }, '*');
38
36
  });
39
37
  wrapper.appendChild(closeButton);
40
38
  }
41
39
  };
42
- const mountStyles = (styles) => {
43
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
44
- const background = (_d = (_c = (_b = (_a = styles.background) === null || _a === void 0 ? void 0 : _a.color) === null || _b === void 0 ? void 0 : _b.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => '#' + r + r + g + g + b + b).substring(1).match(/.{2}/g)) === null || _c === void 0 ? void 0 : _c.map((x) => parseInt(x, 16))) !== null && _d !== void 0 ? _d : [107, 114, 128];
45
- background.push((_f = (_e = styles.background) === null || _e === void 0 ? void 0 : _e.opacity) !== null && _f !== void 0 ? _f : 0.75);
46
- document.documentElement.style.setProperty(Styles.BACKGROUND_COLOR, `rgb(${background.join(', ')})`);
47
- const rounded = (_g = styles.rounded) !== null && _g !== void 0 ? _g : 0;
48
- document.documentElement.style.setProperty(Styles.ROUNDED, `${rounded.toString()}px`);
49
- let height = (_h = styles.height) !== null && _h !== void 0 ? _h : Config.defaultDimension.height;
50
- if (styles.dimension)
51
- height = Dimensions[styles.dimension.toUpperCase()].height;
52
- document.documentElement.style.setProperty(Styles.MAX_HEIGHT, `${height.toString()}px`);
53
- let width = (_j = styles.width) !== null && _j !== void 0 ? _j : Config.defaultDimension.width;
54
- if (styles.dimension)
55
- width = Dimensions[styles.dimension.toUpperCase()].width;
56
- document.documentElement.style.setProperty(Styles.MAX_WIDTH, `${width.toString()}px`);
57
- };
58
40
  const unmountLightbox = (listener, origin) => {
59
41
  const element = document.getElementById(origin);
60
42
  if (element) {
61
43
  element.remove();
62
44
  globalThis.removeEventListener('message', listener);
63
- document.documentElement.style.removeProperty(Styles.BACKGROUND_COLOR);
45
+ document.documentElement.style.removeProperty(Styles.BACKDROP_COLOR);
64
46
  }
65
47
  else
66
48
  throw new Error(`Frame from "${origin}" not found`);
67
49
  };
50
+ const mountStyles = (styles) => {
51
+ var _a;
52
+ console.log('mountStyles', styles);
53
+ setStyle(Styles.BACKDROP_COLOR, buildBackdrop(styles.backdropColor, styles.backdropOpacity));
54
+ setStyle(Styles.ROUNDED, `${(_a = styles.rounded) !== null && _a !== void 0 ? _a : 0}px`);
55
+ setStyle(Styles.MAX_HEIGHT, buildDimension('height', styles.height, styles.dimension));
56
+ setStyle(Styles.MAX_WIDTH, buildDimension('width', styles.width, styles.dimension));
57
+ };
58
+ const buildBackdrop = (color, opacity) => {
59
+ var _a, _b;
60
+ const backdrop = (_b = (_a = color === null || color === void 0 ? void 0 : color.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i, (m, r, g, b) => '#' + r + r + g + g + b + b).substring(1).match(/.{2}/g)) === null || _a === void 0 ? void 0 : _a.map((x) => parseInt(x, 16))) !== null && _b !== void 0 ? _b : [107, 114, 128];
61
+ backdrop.push(opacity !== null && opacity !== void 0 ? opacity : 0.75);
62
+ return `rgba(${backdrop.join(', ')})`;
63
+ };
64
+ const buildDimension = (type, value = '', dimension) => {
65
+ let result = value;
66
+ if (dimension)
67
+ result = Dimensions[dimension.toUpperCase()][type];
68
+ if (result.match(/^\d+%$/) || result.match(/^\d+px$/))
69
+ return result;
70
+ if (result.match(/^\d+$/))
71
+ return `${result}px`;
72
+ if (value) {
73
+ console.warn(`Invalid ${type}. Must be a number, a number followed by "px", or a number followed by "%".`);
74
+ }
75
+ return `${Config.defaultDimension[type]}px`;
76
+ };
@@ -16,6 +16,7 @@ export const createLightbox = (url, options) => {
16
16
  callbacks: (_b = options === null || options === void 0 ? void 0 : options.callbacks) !== null && _b !== void 0 ? _b : {},
17
17
  closeButton: (_c = options === null || options === void 0 ? void 0 : options.closeButton) !== null && _c !== void 0 ? _c : true,
18
18
  styles: (_d = options === null || options === void 0 ? void 0 : options.styles) !== null && _d !== void 0 ? _d : {},
19
+ url: url,
19
20
  close: emitClose,
20
21
  updateStyles: emitUpdateStyles,
21
22
  hideCloseButton: emitHideCloseButton,
package/dist/helpers.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare const postMessage: (type: string, payload?: unknown, preventClose?: boolean) => void;
2
+ export declare const setStyle: (name: string, value: string) => void;
2
3
  export declare const redirectBasedOnDriver: (url: string) => void;
package/dist/helpers.js CHANGED
@@ -1,6 +1,9 @@
1
1
  export const postMessage = (type, payload, preventClose) => {
2
2
  return globalThis.parent.postMessage({ type, preventClose, payload }, '*');
3
3
  };
4
+ export const setStyle = (name, value) => {
5
+ document.documentElement.style.setProperty(name, value);
6
+ };
4
7
  export const redirectBasedOnDriver = (url) => {
5
8
  if (navigator.userAgent.match(/iPhone|iPad|iPod/i) ||
6
9
  /^((?!chrome|android).)*safari/i.test(navigator.userAgent) // regex from https://stackoverflow.com/a/23522755
package/dist/styles.css CHANGED
@@ -1,5 +1,5 @@
1
1
  .placetopay_lightbox_wrapper {
2
- background: var(--placetopay-lightbox-background-color);
2
+ background: var(--placetopay-lightbox-backdrop-color);
3
3
  position: fixed;
4
4
  display: flex;
5
5
  justify-content: center;
package/dist/types.d.ts CHANGED
@@ -3,14 +3,12 @@ export declare type ClientCallbacks = {
3
3
  [key: string]: ClientCallback | undefined;
4
4
  };
5
5
  export declare type LightboxStyles = {
6
- background?: {
7
- color?: string;
8
- opacity?: number;
9
- };
6
+ backdropColor?: string;
7
+ backdropOpacity?: number;
10
8
  dimension?: 'sm' | 'md' | 'lg';
11
- height?: number;
9
+ height?: string;
10
+ width?: string;
12
11
  rounded?: number;
13
- width?: number;
14
12
  };
15
13
  export declare type InitOptions = {
16
14
  allowRedirects?: boolean;
@@ -25,6 +23,7 @@ export declare type ApiStructure = {
25
23
  preventClose?: boolean;
26
24
  };
27
25
  export declare type LightboxInstance = Required<InitOptions> & {
26
+ url: string;
28
27
  on: (name: string, callback: ClientCallback) => void;
29
28
  open: () => void;
30
29
  close: (payload?: unknown) => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placetopay/lightbox-sdk",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
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",