@parafin/core 2.0.2 → 2.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.
package/index.ts CHANGED
@@ -1,4 +1,7 @@
1
- type BaseProps = { token: string } | { partner: string }
1
+ type BaseProps = ({ token: string } | { partner: string }) & {
2
+ dashboardTargetElementId?: string
3
+ onLinkOpened?: (url: string) => void
4
+ }
2
5
 
3
6
  type CapitalOrSpendProps = {
4
7
  product: 'capital' | 'spend_card' | 'cash_account'
@@ -61,9 +64,11 @@ export const openParafinDashboard = (
61
64
 
62
65
  const frame = document.createElement('iframe')
63
66
  frame.id = 'parafin-dashboard'
64
- frame.style.position = 'fixed'
65
- frame.style.top = '0'
66
- frame.style.left = '0'
67
+ if (!props.dashboardTargetElementId) {
68
+ frame.style.position = 'fixed'
69
+ frame.style.top = '0'
70
+ frame.style.left = '0'
71
+ }
67
72
  frame.style.width = '100%'
68
73
  frame.style.height = '100%'
69
74
  frame.style.border = 'none'
@@ -74,24 +79,47 @@ export const openParafinDashboard = (
74
79
  frame.src = url
75
80
  frame.allow = 'accelerometer; gyroscope; clipboard-read; clipboard-write'
76
81
 
77
- const closeParafinDashboard = (e: MessageEvent) => {
78
- if (e.origin === origin && e.data?.message === 'close-dashboard') {
79
- if ('orderId' in props) {
80
- props.onExit?.(props.orderId)
81
- } else if ('lineOfCreditApplicationId' in props) {
82
- props.onExit?.(props.lineOfCreditApplicationId)
83
- } else {
84
- props.onExit?.()
82
+ const dashboardTargetElement = props.dashboardTargetElementId
83
+ ? document.getElementById(props.dashboardTargetElementId)
84
+ : document.body
85
+
86
+ if (!dashboardTargetElement) {
87
+ throw new Error('Dashboard target element not found')
88
+ }
89
+
90
+ const messageListener = async (event: MessageEvent) => {
91
+ if (event.origin === origin) {
92
+ switch (event.data?.message) {
93
+ case 'close-dashboard':
94
+ if ('orderId' in props) {
95
+ props.onExit?.(props.orderId)
96
+ } else if ('lineOfCreditApplicationId' in props) {
97
+ props.onExit?.(props.lineOfCreditApplicationId)
98
+ } else {
99
+ props.onExit?.()
100
+ }
101
+ window.removeEventListener('message', messageListener)
102
+ frame.style.opacity = '0'
103
+ setTimeout(() => {
104
+ dashboardTargetElement.removeChild(frame)
105
+ document.body.style.removeProperty('overflow')
106
+ }, 200)
107
+ break
108
+ case 'link-opened':
109
+ if (props.onLinkOpened && event.data?.url) {
110
+ props.onLinkOpened(event.data.url)
111
+ }
112
+ break
85
113
  }
86
- window.removeEventListener('message', closeParafinDashboard)
87
- frame.style.opacity = '0'
88
- setTimeout(() => document.body.removeChild(frame), 200)
89
114
  }
90
115
  }
91
116
 
92
- window.addEventListener('message', closeParafinDashboard)
93
- document.body.appendChild(frame)
94
- setTimeout(() => (frame.style.opacity = '1'), 1)
117
+ window.addEventListener('message', messageListener)
118
+ dashboardTargetElement.appendChild(frame)
119
+ setTimeout(() => {
120
+ frame.style.opacity = '1'
121
+ document.body.style.overflow = 'hidden'
122
+ }, 1)
95
123
  } catch (error) {
96
124
  console.error('Error loading Parafin dashboard', error)
97
125
  }
package/out/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
- type BaseProps = {
1
+ type BaseProps = ({
2
2
  token: string;
3
3
  } | {
4
4
  partner: string;
5
+ }) & {
6
+ dashboardTargetElementId?: string;
7
+ onLinkOpened?: (url: string) => void;
5
8
  };
6
9
  type CapitalOrSpendProps = {
7
10
  product: 'capital' | 'spend_card' | 'cash_account';
package/out/index.js CHANGED
@@ -34,9 +34,11 @@ export const openParafinDashboard = (props) => {
34
34
  }
35
35
  const frame = document.createElement('iframe');
36
36
  frame.id = 'parafin-dashboard';
37
- frame.style.position = 'fixed';
38
- frame.style.top = '0';
39
- frame.style.left = '0';
37
+ if (!props.dashboardTargetElementId) {
38
+ frame.style.position = 'fixed';
39
+ frame.style.top = '0';
40
+ frame.style.left = '0';
41
+ }
40
42
  frame.style.width = '100%';
41
43
  frame.style.height = '100%';
42
44
  frame.style.border = 'none';
@@ -46,25 +48,46 @@ export const openParafinDashboard = (props) => {
46
48
  frame.style.transition = 'opacity 0.2s';
47
49
  frame.src = url;
48
50
  frame.allow = 'accelerometer; gyroscope; clipboard-read; clipboard-write';
49
- const closeParafinDashboard = (e) => {
50
- if (e.origin === origin && e.data?.message === 'close-dashboard') {
51
- if ('orderId' in props) {
52
- props.onExit?.(props.orderId);
53
- }
54
- else if ('lineOfCreditApplicationId' in props) {
55
- props.onExit?.(props.lineOfCreditApplicationId);
56
- }
57
- else {
58
- props.onExit?.();
51
+ const dashboardTargetElement = props.dashboardTargetElementId
52
+ ? document.getElementById(props.dashboardTargetElementId)
53
+ : document.body;
54
+ if (!dashboardTargetElement) {
55
+ throw new Error('Dashboard target element not found');
56
+ }
57
+ const messageListener = async (event) => {
58
+ if (event.origin === origin) {
59
+ switch (event.data?.message) {
60
+ case 'close-dashboard':
61
+ if ('orderId' in props) {
62
+ props.onExit?.(props.orderId);
63
+ }
64
+ else if ('lineOfCreditApplicationId' in props) {
65
+ props.onExit?.(props.lineOfCreditApplicationId);
66
+ }
67
+ else {
68
+ props.onExit?.();
69
+ }
70
+ window.removeEventListener('message', messageListener);
71
+ frame.style.opacity = '0';
72
+ setTimeout(() => {
73
+ dashboardTargetElement.removeChild(frame);
74
+ document.body.style.removeProperty('overflow');
75
+ }, 200);
76
+ break;
77
+ case 'link-opened':
78
+ if (props.onLinkOpened && event.data?.url) {
79
+ props.onLinkOpened(event.data.url);
80
+ }
81
+ break;
59
82
  }
60
- window.removeEventListener('message', closeParafinDashboard);
61
- frame.style.opacity = '0';
62
- setTimeout(() => document.body.removeChild(frame), 200);
63
83
  }
64
84
  };
65
- window.addEventListener('message', closeParafinDashboard);
66
- document.body.appendChild(frame);
67
- setTimeout(() => (frame.style.opacity = '1'), 1);
85
+ window.addEventListener('message', messageListener);
86
+ dashboardTargetElement.appendChild(frame);
87
+ setTimeout(() => {
88
+ frame.style.opacity = '1';
89
+ document.body.style.overflow = 'hidden';
90
+ }, 1);
68
91
  }
69
92
  catch (error) {
70
93
  console.error('Error loading Parafin dashboard', error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parafin/core",
3
- "version": "2.0.2",
3
+ "version": "2.2.0",
4
4
  "description": "Parafin embedded core",
5
5
  "author": "Parafin (https://www.parafin.com)",
6
6
  "module": "out/index.js",