@placetopay/lightbox-sdk 2.0.3 → 2.1.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.
@@ -11,21 +11,7 @@ export declare enum LightboxEvents {
11
11
  }
12
12
  export declare enum Styles {
13
13
  BACKDROP_COLOR = "--placetopay-lightbox-backdrop-color",
14
- ROUNDED = "--placetopay-lightbox-border-radius",
14
+ RADIUS = "--placetopay-lightbox-border-radius",
15
15
  MAX_HEIGHT = "--placetopay-lightbox-max-height",
16
16
  MAX_WIDTH = "--placetopay-lightbox-max-width"
17
17
  }
18
- export declare const Dimensions: {
19
- SM: {
20
- height: string;
21
- width: string;
22
- };
23
- MD: {
24
- height: string;
25
- width: string;
26
- };
27
- LG: {
28
- height: string;
29
- width: string;
30
- };
31
- };
package/dist/constants.js CHANGED
@@ -14,12 +14,7 @@ export var LightboxEvents;
14
14
  export var Styles;
15
15
  (function (Styles) {
16
16
  Styles["BACKDROP_COLOR"] = "--placetopay-lightbox-backdrop-color";
17
- Styles["ROUNDED"] = "--placetopay-lightbox-border-radius";
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
20
  })(Styles || (Styles = {}));
21
- export const Dimensions = {
22
- SM: { height: '400', width: '320' },
23
- MD: { height: '640', width: '512' },
24
- LG: { height: '1000', width: '800' },
25
- };
@@ -1,3 +1,3 @@
1
- import { ClientCallbacks, LightboxStyles } from '../types';
2
- export declare const mountListener: (callbacks: ClientCallbacks, styles: LightboxStyles) => void;
3
- export declare const mountLightbox: (url: string, styles: LightboxStyles, closeButtonEnabled: boolean) => void;
1
+ import { MountLightboxOptions } from '../types';
2
+ export declare const mountLightbox: ({ url, callbacks, styles, closeButtonEnabled, enforceStyles }: MountLightboxOptions) => void;
3
+ export declare const unmountLightbox: (target: string) => void;
@@ -1,75 +1,78 @@
1
- import { Config } from '../config';
2
- import { Styles, ElementIds, Dimensions, LightboxEvents as LE } from '../constants';
3
- import { setStyle } from '../helpers';
4
- export const mountListener = (callbacks, styles) => {
5
- const listener = (event) => {
6
- var _a, _b, _c;
7
- if (event.data.type === LE.CLOSE)
8
- unmountLightbox(listener, (_a = event.data.target) !== null && _a !== void 0 ? _a : event.origin);
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)
12
- (_b = document.getElementById(ElementIds.CLOSE_BUTTON_ID)) === null || _b === void 0 ? void 0 : _b.remove();
13
- (_c = callbacks[event.data.type]) === null || _c === void 0 ? void 0 : _c.call(callbacks, event.data.payload);
14
- };
15
- globalThis.addEventListener('message', listener);
16
- };
17
- export const mountLightbox = (url, styles, closeButtonEnabled) => {
1
+ import { Styles, ElementIds, LightboxEvents as LE } from '../constants';
2
+ import { setStyle, unsetStyle } from '../helpers';
3
+ let listener;
4
+ export const mountLightbox = ({ url, callbacks, styles, closeButtonEnabled, enforceStyles }) => {
18
5
  const wrapper = document.createElement('div');
19
6
  wrapper.id = new URL(url).origin;
20
7
  wrapper.className = ElementIds.WRAPPER_ID;
21
8
  const iframe = document.createElement('iframe');
22
9
  iframe.src = url;
23
10
  iframe.id = ElementIds.IFRAME_ID;
24
- mountStyles(styles);
11
+ updateStyles(styles);
25
12
  document.body.appendChild(wrapper);
26
13
  wrapper.appendChild(iframe);
14
+ let closeButton;
27
15
  if (closeButtonEnabled) {
28
- const closeButton = document.createElement('button');
16
+ closeButton = document.createElement('button');
29
17
  closeButton.id = ElementIds.CLOSE_BUTTON_ID;
30
- closeButton.innerHTML = `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="#4b5563" height="24px" width="24px">
31
- <path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12" />
32
- </svg>
33
- `;
18
+ 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>`;
34
19
  closeButton.addEventListener('click', () => {
35
- globalThis.postMessage({ type: LE.CLOSE, target: new URL(url).origin }, '*');
20
+ unmountLightbox(new URL(url).origin);
36
21
  });
37
22
  wrapper.appendChild(closeButton);
38
23
  }
24
+ mountListener({ callbacks, styles, closeButton, enforceStyles });
25
+ };
26
+ const mountListener = ({ callbacks, styles, closeButton, enforceStyles }) => {
27
+ listener = (event) => {
28
+ var _a;
29
+ if (event.data.type === LE.CLOSE)
30
+ unmountLightbox(event.origin);
31
+ if (event.data.type === LE.UPDATE_STYLES) {
32
+ const newStyles = enforceStyles
33
+ ? Object.assign(Object.assign({}, event.data.payload), styles) : Object.assign(Object.assign({}, styles), event.data.payload);
34
+ updateStyles(newStyles);
35
+ }
36
+ if (event.data.type === LE.HIDE_CLOSE_BUTTON)
37
+ closeButton === null || closeButton === void 0 ? void 0 : closeButton.remove();
38
+ (_a = callbacks[event.data.type]) === null || _a === void 0 ? void 0 : _a.call(callbacks, event.data.payload);
39
+ };
40
+ globalThis.addEventListener('message', listener);
39
41
  };
40
- const unmountLightbox = (listener, origin) => {
41
- const element = document.getElementById(origin);
42
+ export const unmountLightbox = (target) => {
43
+ const element = document.getElementById(target);
42
44
  if (element) {
43
45
  element.remove();
46
+ clearStyles();
44
47
  globalThis.removeEventListener('message', listener);
45
- document.documentElement.style.removeProperty(Styles.BACKDROP_COLOR);
46
48
  }
47
49
  else
48
- throw new Error(`Frame from "${origin}" not found`);
50
+ throw new Error(`Frame from "${target}" not found`);
49
51
  };
50
- const mountStyles = (styles) => {
51
- var _a;
52
- setStyle(Styles.BACKDROP_COLOR, buildBackdrop(styles.backdropColor, styles.backdropOpacity));
53
- setStyle(Styles.ROUNDED, `${(_a = styles.rounded) !== null && _a !== void 0 ? _a : 0}px`);
54
- setStyle(Styles.MAX_HEIGHT, buildDimension('height', styles.height, styles.dimension));
55
- setStyle(Styles.MAX_WIDTH, buildDimension('width', styles.width, styles.dimension));
52
+ const clearStyles = () => {
53
+ unsetStyle(Styles.BACKDROP_COLOR);
54
+ unsetStyle(Styles.RADIUS);
55
+ unsetStyle(Styles.MAX_HEIGHT);
56
+ unsetStyle(Styles.MAX_WIDTH);
57
+ };
58
+ const updateStyles = (styles) => {
59
+ var _a, _b, _c, _d, _e;
60
+ 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
+ setStyle(Styles.RADIUS, `${(_c = styles.radius) !== null && _c !== void 0 ? _c : 0}px`);
62
+ setStyle(Styles.MAX_HEIGHT, buildDimension('height', (_d = styles.height) !== null && _d !== void 0 ? _d : 640));
63
+ setStyle(Styles.MAX_WIDTH, buildDimension('width', (_e = styles.width) !== null && _e !== void 0 ? _e : 512));
56
64
  };
57
65
  const buildBackdrop = (color, opacity) => {
58
- var _a, _b;
59
- 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];
60
- backdrop.push(opacity !== null && opacity !== void 0 ? opacity : 0.75);
66
+ var _a;
67
+ const backdrop = (_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));
68
+ backdrop.push(opacity);
61
69
  return `rgba(${backdrop.join(', ')})`;
62
70
  };
63
- const buildDimension = (type, value = '', dimension) => {
64
- let result = value.toString();
65
- if (dimension)
66
- result = Dimensions[dimension.toUpperCase()][type];
71
+ const buildDimension = (type, value) => {
72
+ const result = String(value);
67
73
  if (result.match(/^\d+%$/) || result.match(/^\d+px$/))
68
74
  return result;
69
75
  if (result.match(/^\d+$/))
70
76
  return `${result}px`;
71
- if (value) {
72
- console.warn(`Invalid ${type}. Must be a number, a number followed by "px", or a number followed by "%".`);
73
- }
74
- return `${Config.defaultDimension[type]}px`;
77
+ console.warn(`Invalid ${type}. Must be a number, a number followed by "px", or a number followed by "%".`);
75
78
  };
@@ -1,11 +1,7 @@
1
- import { InitOptions, LightboxInstance, ApiStructure, LightboxStyles } from '../types';
1
+ import { InitialOptions, LightboxInstance, ApiStructure, LightboxStyles } from '../types';
2
2
  export declare const isInside: () => boolean;
3
- export declare const emitClose: (payload?: unknown) => void;
4
- export declare const emitUpdateStyles: (styles: LightboxStyles) => void;
5
- export declare const emitHideCloseButton: () => void;
6
- export declare const emit: ({ type, payload, preventClose }: ApiStructure) => {
7
- close: (payload?: unknown) => void;
8
- };
9
- export declare const createLightbox: (url: string, options?: InitOptions & {
10
- launch?: boolean;
11
- }) => LightboxInstance;
3
+ export declare const updateStyles: (styles: LightboxStyles) => void;
4
+ export declare const hideCloseButton: () => void;
5
+ export declare const emitClose: (payload?: unknown, preventClose?: boolean) => void;
6
+ export declare const emit: ({ type, payload, preventClose }: ApiStructure) => void;
7
+ export declare const createLightbox: (url: string, options?: InitialOptions) => LightboxInstance;
@@ -1,14 +1,13 @@
1
- import { LightboxEvents } from '../constants';
1
+ import { LightboxEvents as LE } from '../constants';
2
2
  import { postMessage, redirectBasedOnDriver } from '../helpers';
3
- import { mountLightbox, mountListener } from './assemblers';
3
+ import { mountLightbox, unmountLightbox } from './assemblers';
4
4
  export const isInside = () => globalThis.location !== globalThis.parent.location;
5
- export const emitClose = (payload) => postMessage(LightboxEvents.CLOSE, payload);
6
- export const emitUpdateStyles = (styles) => postMessage(LightboxEvents.UPDATE_STYLES, styles);
7
- export const emitHideCloseButton = () => postMessage(LightboxEvents.HIDE_CLOSE_BUTTON);
8
- export const emit = ({ type, payload, preventClose }) => {
9
- postMessage(type, payload, preventClose);
10
- return { close: emitClose };
5
+ export const updateStyles = (styles) => postMessage(LE.UPDATE_STYLES, styles);
6
+ export const hideCloseButton = () => postMessage(LE.HIDE_CLOSE_BUTTON);
7
+ export const emitClose = (payload, preventClose) => {
8
+ postMessage(LE.CLOSE, payload, preventClose);
11
9
  };
10
+ export const emit = ({ type, payload, preventClose }) => postMessage(type, payload, preventClose);
12
11
  export const createLightbox = (url, options) => {
13
12
  var _a, _b, _c, _d;
14
13
  const lightbox = {
@@ -17,17 +16,23 @@ export const createLightbox = (url, options) => {
17
16
  closeButton: (_c = options === null || options === void 0 ? void 0 : options.closeButton) !== null && _c !== void 0 ? _c : true,
18
17
  styles: (_d = options === null || options === void 0 ? void 0 : options.styles) !== null && _d !== void 0 ? _d : {},
19
18
  url: url,
20
- close: emitClose,
21
- updateStyles: emitUpdateStyles,
22
- hideCloseButton: emitHideCloseButton,
19
+ close: () => unmountLightbox(url),
20
+ updateStyles,
21
+ hideCloseButton,
23
22
  on: (name, callback) => {
24
23
  lightbox.callbacks[name] = callback;
25
24
  },
26
25
  open: () => {
26
+ var _a;
27
27
  if (lightbox.allowRedirects)
28
28
  redirectBasedOnDriver(url);
29
- mountListener(lightbox.callbacks, lightbox.styles);
30
- mountLightbox(url, lightbox.styles, lightbox.closeButton);
29
+ mountLightbox({
30
+ url,
31
+ callbacks: lightbox.callbacks,
32
+ styles: lightbox.styles,
33
+ closeButtonEnabled: lightbox.closeButton,
34
+ enforceStyles: (_a = options === null || options === void 0 ? void 0 : options.enforceStyles) !== null && _a !== void 0 ? _a : false,
35
+ });
31
36
  },
32
37
  };
33
38
  if (options === null || options === void 0 ? void 0 : options.launch)
package/dist/helpers.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export declare const postMessage: (type: string, payload?: unknown, preventClose?: boolean) => void;
2
2
  export declare const setStyle: (name: string, value: string) => void;
3
+ export declare const unsetStyle: (name: string) => void;
3
4
  export declare const redirectBasedOnDriver: (url: string) => void;
package/dist/helpers.js CHANGED
@@ -4,6 +4,9 @@ export const postMessage = (type, payload, preventClose) => {
4
4
  export const setStyle = (name, value) => {
5
5
  document.documentElement.style.setProperty(name, value);
6
6
  };
7
+ export const unsetStyle = (name) => {
8
+ document.documentElement.style.removeProperty(name);
9
+ };
7
10
  export const redirectBasedOnDriver = (url) => {
8
11
  if (navigator.userAgent.match(/iPhone|iPad|iPod/i) ||
9
12
  /^((?!chrome|android).)*safari/i.test(navigator.userAgent) // regex from https://stackoverflow.com/a/23522755
package/dist/types.d.ts CHANGED
@@ -1,28 +1,42 @@
1
- export declare type ClientCallback = (payload?: unknown) => void;
2
- export declare type ClientCallbacks = {
3
- [key: string]: ClientCallback | undefined;
4
- };
5
- export declare type LightboxStyles = {
6
- backdropColor?: string;
7
- backdropOpacity?: number;
8
- dimension?: 'sm' | 'md' | 'lg';
9
- height?: string | number;
10
- width?: string | number;
11
- rounded?: number;
12
- };
13
- export declare type InitOptions = {
14
- allowRedirects?: boolean;
15
- callbacks?: ClientCallbacks;
16
- closeButton?: boolean;
17
- styles?: LightboxStyles;
18
- };
19
- export declare type ApiStructure = {
1
+ export type ClientCallback = (payload?: unknown) => void;
2
+ export type ClientCallbacks = Record<string, ClientCallback | undefined>;
3
+ export type LightboxStyles = Partial<{
4
+ backdropColor: string;
5
+ backdropOpacity: number;
6
+ height: string | number;
7
+ width: string | number;
8
+ radius: number;
9
+ }>;
10
+ export type ApiStructure = {
20
11
  type: string;
21
- target?: string;
22
12
  payload?: unknown;
23
13
  preventClose?: boolean;
24
14
  };
25
- export declare type LightboxInstance = Required<InitOptions> & {
15
+ export type InternalOptions = Partial<{
16
+ launch: boolean;
17
+ enforceStyles: boolean;
18
+ }>;
19
+ export type ExposedOptions = Partial<{
20
+ allowRedirects: boolean;
21
+ callbacks: ClientCallbacks;
22
+ closeButton: boolean;
23
+ styles: LightboxStyles;
24
+ }>;
25
+ export type InitialOptions = ExposedOptions & InternalOptions;
26
+ export type MountLightboxOptions = {
27
+ url: string;
28
+ callbacks: ClientCallbacks;
29
+ styles: LightboxStyles;
30
+ closeButtonEnabled: boolean;
31
+ enforceStyles: boolean;
32
+ };
33
+ export type MountListenerOptions = {
34
+ callbacks: ClientCallbacks;
35
+ styles: LightboxStyles;
36
+ closeButton: HTMLButtonElement;
37
+ enforceStyles: boolean;
38
+ };
39
+ export type LightboxInstance = Required<ExposedOptions> & {
26
40
  url: string;
27
41
  on: (name: string, callback: ClientCallback) => void;
28
42
  open: () => void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placetopay/lightbox-sdk",
3
- "version": "2.0.3",
3
+ "version": "2.1.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",
@@ -25,6 +25,6 @@
25
25
  },
26
26
  "homepage": "https://placetopay-org.github.io/lightbox-sdk",
27
27
  "devDependencies": {
28
- "typescript": "^4.8.3"
28
+ "typescript": "^5.1.6"
29
29
  }
30
30
  }
package/dist/config.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export declare const Config: {
2
- defaultDimension: {
3
- height: string;
4
- width: string;
5
- };
6
- };
package/dist/config.js DELETED
@@ -1,4 +0,0 @@
1
- import { Dimensions } from './constants';
2
- export const Config = {
3
- defaultDimension: Dimensions.MD,
4
- };