@placetopay/lightbox-sdk 0.1.0 → 0.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.
@@ -1,3 +1,5 @@
1
+ import { ClientStyles } from '@/types';
1
2
  export declare const LightboxApp: {
2
3
  emit: (type: string, data: unknown) => void;
4
+ sendStyles: (styles: ClientStyles) => void;
3
5
  };
@@ -1,5 +1,9 @@
1
+ import { LightboxAppEvents } from '@/constants';
1
2
  export var LightboxApp = {
2
3
  emit: function (type, data) {
3
- globalThis.parent.postMessage({ type: type, data: data }, '*');
4
+ globalThis.parent.postMessage({ event: LightboxAppEvents.EMIT, type: type, data: data }, '*');
5
+ },
6
+ sendStyles: function (styles) {
7
+ globalThis.parent.postMessage({ event: LightboxAppEvents.SEND_STYLES, styles: styles }, '*');
4
8
  },
5
9
  };
@@ -1,3 +1,3 @@
1
1
  import { ClientCallbacks, ClientStyles } from '@/types';
2
- export declare const mountListener: (callbacks: ClientCallbacks) => void;
2
+ export declare const mountListeners: (callbacks: ClientCallbacks, styles: ClientStyles) => void;
3
3
  export declare const mountIFrameElement: (url: string, styles: ClientStyles) => void;
@@ -1,35 +1,64 @@
1
- import { Styles, Ids } from '@/constants';
2
- export var mountListener = function (callbacks) {
1
+ var __assign = (this && this.__assign) || function () {
2
+ __assign = Object.assign || function(t) {
3
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
4
+ s = arguments[i];
5
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
6
+ t[p] = s[p];
7
+ }
8
+ return t;
9
+ };
10
+ return __assign.apply(this, arguments);
11
+ };
12
+ import { Config } from '@/config';
13
+ import { Styles, ElementIds, Dimensions, LightboxAppEvents } from '@/constants';
14
+ export var mountListeners = function (callbacks, styles) {
3
15
  if (!callbacks)
4
16
  return;
5
- var listener = function (event) {
17
+ var callbacksListener = function (event) {
6
18
  var _a;
19
+ if (event.data.event !== LightboxAppEvents.EMIT)
20
+ return;
7
21
  (_a = callbacks[event.data.type]) === null || _a === void 0 ? void 0 : _a.call(callbacks, event.data.data);
8
- unmountLightbox(listener);
22
+ unmountLightbox(callbacksListener);
23
+ };
24
+ var stylesListener = function (event) {
25
+ if (event.data.event !== LightboxAppEvents.SEND_STYLES)
26
+ return;
27
+ mountStyles(__assign(__assign(__assign({}, event.data.styles), styles), { background: __assign(__assign({}, event.data.styles.background), styles.background) }));
28
+ globalThis.removeEventListener('message', stylesListener);
9
29
  };
10
- globalThis.addEventListener('message', listener);
30
+ globalThis.addEventListener('message', callbacksListener);
31
+ globalThis.addEventListener('message', stylesListener);
11
32
  };
12
33
  export var mountIFrameElement = function (url, styles) {
13
34
  var wrapper = document.createElement('div');
14
- wrapper.id = Ids.WRAPPER_ID;
35
+ wrapper.id = ElementIds.WRAPPER_ID;
15
36
  var iframe = document.createElement('iframe');
16
37
  iframe.src = url;
17
- iframe.id = Ids.IFRAME_ID;
38
+ iframe.id = ElementIds.IFRAME_ID;
18
39
  mountStyles(styles);
19
40
  document.body.appendChild(wrapper);
20
41
  wrapper.appendChild(iframe);
21
42
  };
22
43
  var mountStyles = function (styles) {
23
- var _a, _b, _c, _d, _e, _f, _g;
44
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
24
45
  var 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, function (m, r, g, b) { return '#' + r + r + g + g + b + b; }).substring(1).match(/.{2}/g)) === null || _c === void 0 ? void 0 : _c.map(function (x) { return parseInt(x, 16); })) !== null && _d !== void 0 ? _d : [107, 114, 128];
25
46
  background.push((_f = (_e = styles.background) === null || _e === void 0 ? void 0 : _e.opacity) !== null && _f !== void 0 ? _f : 0.75);
26
47
  document.documentElement.style.setProperty(Styles.BACKGROUND_COLOR, "rgb(".concat(background.join(', '), ")"));
27
48
  var rounded = (_g = styles.rounded) !== null && _g !== void 0 ? _g : 0;
28
49
  document.documentElement.style.setProperty(Styles.ROUNDED, "".concat(rounded.toString(), "px"));
50
+ var height = (_h = styles.height) !== null && _h !== void 0 ? _h : Config.defaultDimension.height;
51
+ if (styles.dimension)
52
+ height = Dimensions[styles.dimension.toUpperCase()].height;
53
+ document.documentElement.style.setProperty(Styles.MAX_HEIGHT, "".concat(height.toString(), "px"));
54
+ var width = (_j = styles.width) !== null && _j !== void 0 ? _j : Config.defaultDimension.width;
55
+ if (styles.dimension)
56
+ width = Dimensions[styles.dimension.toUpperCase()].width;
57
+ document.documentElement.style.setProperty(Styles.MAX_WIDTH, "".concat(width.toString(), "px"));
29
58
  };
30
59
  var unmountLightbox = function (listener) {
31
60
  var _a;
32
- (_a = document.getElementById(Ids.WRAPPER_ID)) === null || _a === void 0 ? void 0 : _a.remove();
61
+ (_a = document.getElementById(ElementIds.WRAPPER_ID)) === null || _a === void 0 ? void 0 : _a.remove();
33
62
  globalThis.removeEventListener('message', listener);
34
63
  document.documentElement.style.removeProperty(Styles.BACKGROUND_COLOR);
35
64
  };
@@ -1,12 +1,16 @@
1
- import { mountIFrameElement, mountListener } from './assemblers';
1
+ import { redirectBasedOnDriver } from '@/helpers';
2
+ import { mountIFrameElement, mountListeners } from './assemblers';
2
3
  export var LightboxClient = {
3
4
  init: function (url, options) {
4
- var _a, _b;
5
+ var _a, _b, _c;
5
6
  var lightbox = {
6
7
  callbacks: (_a = options === null || options === void 0 ? void 0 : options.callbacks) !== null && _a !== void 0 ? _a : {},
7
- styles: (_b = options === null || options === void 0 ? void 0 : options.styles) !== null && _b !== void 0 ? _b : {},
8
+ allowRedirects: (_b = options === null || options === void 0 ? void 0 : options.allowRedirects) !== null && _b !== void 0 ? _b : true,
9
+ styles: (_c = options === null || options === void 0 ? void 0 : options.styles) !== null && _c !== void 0 ? _c : {},
8
10
  open: function () {
9
- mountListener(lightbox.callbacks);
11
+ if (lightbox.allowRedirects)
12
+ redirectBasedOnDriver(url);
13
+ mountListeners(lightbox.callbacks, lightbox.styles);
10
14
  mountIFrameElement(url, lightbox.styles);
11
15
  },
12
16
  };
@@ -0,0 +1,6 @@
1
+ export declare const Config: {
2
+ defaultDimension: {
3
+ height: number;
4
+ width: number;
5
+ };
6
+ };
@@ -0,0 +1,4 @@
1
+ import { Dimensions } from './constants';
2
+ export var Config = {
3
+ defaultDimension: Dimensions.MD,
4
+ };
@@ -1,9 +1,29 @@
1
+ export declare const ElementIds: {
2
+ WRAPPER_ID: string;
3
+ IFRAME_ID: string;
4
+ STYLES_ID: string;
5
+ };
6
+ export declare enum LightboxAppEvents {
7
+ EMIT = "emit",
8
+ SEND_STYLES = "sendStyles"
9
+ }
1
10
  export declare const Styles: {
2
11
  BACKGROUND_COLOR: string;
3
12
  ROUNDED: string;
13
+ MAX_HEIGHT: string;
14
+ MAX_WIDTH: string;
4
15
  };
5
- export declare const Ids: {
6
- WRAPPER_ID: string;
7
- IFRAME_ID: string;
8
- STYLES_ID: string;
16
+ export declare const Dimensions: {
17
+ SM: {
18
+ height: number;
19
+ width: number;
20
+ };
21
+ MD: {
22
+ height: number;
23
+ width: number;
24
+ };
25
+ LG: {
26
+ height: number;
27
+ width: number;
28
+ };
9
29
  };
@@ -1,9 +1,21 @@
1
+ export var ElementIds = {
2
+ WRAPPER_ID: 'placetopay_lightbox_wrapper',
3
+ IFRAME_ID: 'placetopay_lightbox',
4
+ STYLES_ID: 'placetopay-lightbox',
5
+ };
6
+ export var LightboxAppEvents;
7
+ (function (LightboxAppEvents) {
8
+ LightboxAppEvents["EMIT"] = "emit";
9
+ LightboxAppEvents["SEND_STYLES"] = "sendStyles";
10
+ })(LightboxAppEvents || (LightboxAppEvents = {}));
1
11
  export var Styles = {
2
12
  BACKGROUND_COLOR: '--placetopay-lightbox-background-color',
3
13
  ROUNDED: '--placetopay-lightbox-border-radius',
14
+ MAX_HEIGHT: '--placetopay-lightbox-max-height',
15
+ MAX_WIDTH: '--placetopay-lightbox-max-width',
4
16
  };
5
- export var Ids = {
6
- WRAPPER_ID: 'placetopay_lightbox_wrapper',
7
- IFRAME_ID: 'placetopay_lightbox',
8
- STYLES_ID: 'placetopay-lightbox',
17
+ export var Dimensions = {
18
+ SM: { height: 400, width: 320 },
19
+ MD: { height: 640, width: 512 },
20
+ LG: { height: 1000, width: 800 },
9
21
  };
@@ -0,0 +1 @@
1
+ export declare const redirectBasedOnDriver: (url: string) => void;
@@ -0,0 +1,7 @@
1
+ export var redirectBasedOnDriver = function (url) {
2
+ if (navigator.userAgent.match(/iPhone|iPad|iPod/i) ||
3
+ /^((?!chrome|android).)*safari/i.test(navigator.userAgent) // regex from https://stackoverflow.com/a/23522755
4
+ ) {
5
+ location.href = url;
6
+ }
7
+ };
@@ -1,3 +1,4 @@
1
+ import { LightboxAppEvents } from './constants';
1
2
  export declare type ClientCallback = (data: unknown) => void;
2
3
  export declare type ClientCallbacks = {
3
4
  [key: string]: ClientCallback | undefined;
@@ -15,14 +16,17 @@ export declare type ClientStyles = {
15
16
  export declare type InitOptions = {
16
17
  dispatch?: boolean;
17
18
  callbacks?: ClientCallbacks;
19
+ allowRedirects?: boolean;
18
20
  styles?: ClientStyles;
19
21
  };
20
22
  export declare type ApiStructure = {
23
+ event: LightboxAppEvents;
21
24
  data: unknown;
22
25
  type: string;
23
26
  };
24
27
  export declare type LightboxInstance = {
25
28
  styles: ClientStyles;
29
+ allowRedirects: boolean;
26
30
  callbacks: ClientCallbacks;
27
31
  open: () => void;
28
32
  };
@@ -9,6 +9,7 @@
9
9
  width: 100vw;
10
10
  height: 100vh;
11
11
  z-index: 999;
12
+ user-select: none;
12
13
  }
13
14
 
14
15
  #placetopay_lightbox {
@@ -16,4 +17,10 @@
16
17
  border-radius: var(--placetopay-lightbox-border-radius);
17
18
  overflow: hidden;
18
19
  background-color: white;
20
+ height: 100%;
21
+ width: 100%;
22
+ max-height: var(--placetopay-lightbox-max-height);
23
+ max-width: var(--placetopay-lightbox-max-width);
24
+ overflow: auto;
25
+ box-shadow: 0px 0px 60px rgba(0, 0, 0, 0.5);
19
26
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@placetopay/lightbox-sdk",
3
- "version": "0.1.0",
3
+ "version": "0.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",
@@ -28,6 +28,7 @@
28
28
  "keywords": [
29
29
  "lightbox",
30
30
  "sdk",
31
+ "placetopay",
31
32
  "javascript",
32
33
  "typescript"
33
34
  ],