@rango-dev/widget-embedded 0.21.1-next.5 → 0.21.1-next.6

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,9 +1,17 @@
1
1
  import type { WidgetConfig } from '../../types';
2
2
  import type { StateCreatorWithInitialData } from '../app';
3
3
  export declare const DEFAULT_CONFIG: WidgetConfig;
4
+ interface IframeConfigs {
5
+ clientUrl?: string;
6
+ }
4
7
  export type ConfigSlice = {
5
8
  config: WidgetConfig;
6
9
  updateConfig: (config: WidgetConfig) => void;
10
+ iframe: {
11
+ clientUrl?: string;
12
+ };
13
+ updateIframe: <K extends keyof IframeConfigs>(name: K, value: IframeConfigs[K]) => void;
7
14
  };
8
15
  export declare const createConfigSlice: StateCreatorWithInitialData<WidgetConfig, ConfigSlice, ConfigSlice>;
16
+ export {};
9
17
  //# sourceMappingURL=config.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/store/slices/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,QAAQ,CAAC;AAE1D,eAAO,MAAM,cAAc,EAAE,YAM5B,CAAC;AAEF,MAAM,MAAM,WAAW,GAAG;IACxB,MAAM,EAAE,YAAY,CAAC;IAErB,YAAY,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;CAC9C,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,2BAA2B,CACzD,YAAY,EACZ,WAAW,EACX,WAAW,CAiBZ,CAAC"}
1
+ {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../../../src/store/slices/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,QAAQ,CAAC;AAE1D,eAAO,MAAM,cAAc,EAAE,YAM5B,CAAC;AAEF,UAAU,aAAa;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAMD,MAAM,MAAM,WAAW,GAAG;IAExB,MAAM,EAAE,YAAY,CAAC;IACrB,YAAY,EAAE,CAAC,MAAM,EAAE,YAAY,KAAK,IAAI,CAAC;IAG7C,MAAM,EAAE;QAAE,SAAS,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC/B,YAAY,EAAE,CAAC,CAAC,SAAS,MAAM,aAAa,EAC1C,IAAI,EAAE,CAAC,EACP,KAAK,EAAE,aAAa,CAAC,CAAC,CAAC,KACpB,IAAI,CAAC;CACX,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,2BAA2B,CACzD,YAAY,EACZ,WAAW,EACX,WAAW,CA6BZ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rango-dev/widget-embedded",
3
- "version": "0.21.1-next.5",
3
+ "version": "0.21.1-next.6",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -47,4 +47,4 @@
47
47
  "publishConfig": {
48
48
  "access": "public"
49
49
  }
50
- }
50
+ }
@@ -6,7 +6,7 @@ export const Container = styled('div', {
6
6
  display: 'flex',
7
7
  flexDirection: 'column',
8
8
  borderRadius: '$primary',
9
- width: '100vh',
9
+ width: '100vw',
10
10
  minWidth: '300px',
11
11
  maxWidth: '390px',
12
12
  boxShadow: '15px 15px 15px 0px rgba(0, 0, 0, 0.05)',
@@ -7,6 +7,8 @@ import { BottomLogo, Divider, Header } from '@rango-dev/ui';
7
7
  import React, { useEffect, useRef } from 'react';
8
8
 
9
9
  import { WIDGET_UI_ID } from '../../constants';
10
+ import { useIframe } from '../../hooks/useIframe';
11
+ import { isAppLoadedIntoIframe } from '../../hooks/useIframe/useIframe.helpers';
10
12
  import { useNavigateBack } from '../../hooks/useNavigateBack';
11
13
  import { useTheme } from '../../hooks/useTheme';
12
14
  import { useAppStore } from '../../store/AppStore';
@@ -24,6 +26,7 @@ import { Container, Content, Footer } from './Layout.styles';
24
26
 
25
27
  function Layout(props: PropsWithChildren<PropTypes>) {
26
28
  const { children, header, footer, hasLogo = true, height = 'fixed' } = props;
29
+ const { connectHeightObserver, disconnectHeightObserver } = useIframe();
27
30
  const connectedWallets = useWalletsStore.use.connectedWallets();
28
31
  const {
29
32
  config: { features, theme },
@@ -60,6 +63,20 @@ function Layout(props: PropsWithChildren<PropTypes>) {
60
63
  typeof header.hasBackButton === 'undefined' || header.hasBackButton;
61
64
 
62
65
  const scrollViewRef = useRef<HTMLDivElement>(null);
66
+ const containerRef = useRef<HTMLDivElement>(null);
67
+
68
+ useEffect(() => {
69
+ const isIframe = isAppLoadedIntoIframe();
70
+
71
+ // This feature only will be available in an iframe context.
72
+ if (isIframe && containerRef.current) {
73
+ connectHeightObserver(containerRef.current);
74
+ }
75
+
76
+ return () => {
77
+ disconnectHeightObserver();
78
+ };
79
+ }, []);
63
80
 
64
81
  useEffect(() => {
65
82
  scrollViewRef.current?.addEventListener(
@@ -79,7 +96,8 @@ function Layout(props: PropsWithChildren<PropTypes>) {
79
96
  <Container
80
97
  height={height}
81
98
  id={WIDGET_UI_ID.SWAP_BOX_ID}
82
- className={activeTheme()}>
99
+ className={activeTheme()}
100
+ ref={containerRef}>
83
101
  <Header
84
102
  prefix={<>{showBackButton && <BackButton onClick={navigateBack} />}</>}
85
103
  title={header.title}
@@ -5,4 +5,6 @@ export enum SearchParams {
5
5
  TO_TOKEN = 'toToken',
6
6
  FROM_AMOUNT = 'fromAmount',
7
7
  AUTO_CONNECT = 'autoConnect',
8
+ // iframe environments
9
+ CLIENT_URL = 'clientUrl',
8
10
  }
@@ -0,0 +1 @@
1
+ export { useIframe } from './useIframe';
@@ -0,0 +1,3 @@
1
+ export function isAppLoadedIntoIframe() {
2
+ return window.self !== window.top;
3
+ }
@@ -0,0 +1,55 @@
1
+ import type { Messages } from './useIframe.types';
2
+
3
+ import { useRef } from 'react';
4
+
5
+ import { useAppStore } from '../../store/AppStore';
6
+
7
+ import { isAppLoadedIntoIframe } from './useIframe.helpers';
8
+
9
+ interface UseIframe {
10
+ send: (message: Messages) => void;
11
+ connectHeightObserver: (element: HTMLElement) => void;
12
+ disconnectHeightObserver: () => void;
13
+ }
14
+
15
+ function useIframe(): UseIframe {
16
+ const heightObserver = useRef<ResizeObserver | null>(null);
17
+ const { iframe } = useAppStore();
18
+ const isMessagePassingAvailable = isAppLoadedIntoIframe() && iframe.clientUrl;
19
+
20
+ const send = (message: Messages) => {
21
+ if (isMessagePassingAvailable) {
22
+ window.top?.postMessage(message, iframe.clientUrl!);
23
+ }
24
+ };
25
+
26
+ const connectHeightObserver = (element: HTMLElement) => {
27
+ heightObserver.current = new ResizeObserver((entries) => {
28
+ for (const entry of entries) {
29
+ send({
30
+ type: 'widget_height',
31
+ data: {
32
+ height: entry.contentRect.height,
33
+ },
34
+ });
35
+ }
36
+ });
37
+
38
+ heightObserver.current.observe(element);
39
+ };
40
+
41
+ const disconnectHeightObserver = () => {
42
+ if (heightObserver.current) {
43
+ heightObserver.current.disconnect();
44
+ heightObserver.current = null;
45
+ }
46
+ };
47
+
48
+ return {
49
+ send,
50
+ connectHeightObserver,
51
+ disconnectHeightObserver,
52
+ };
53
+ }
54
+
55
+ export { useIframe };
@@ -0,0 +1,13 @@
1
+ type Message<T, D> = {
2
+ type: T;
3
+ data: D;
4
+ };
5
+ type WidgetHeightMessage = Message<
6
+ 'widget_height',
7
+ {
8
+ height: number;
9
+ }
10
+ >;
11
+
12
+ // Add new types of messages to this union.
13
+ export type Messages = WidgetHeightMessage;
@@ -33,6 +33,7 @@ export function useSyncUrlAndStore() {
33
33
  const blockchains = useAppStore().blockchains();
34
34
  const tokens = useAppStore().tokens();
35
35
  const isInRouterContext = useInRouterContext();
36
+ const { updateIframe } = useAppStore();
36
37
 
37
38
  const getUrlSearchParams = () => {
38
39
  const fromAmount = searchParams.get(SearchParams.FROM_AMOUNT);
@@ -41,6 +42,7 @@ export function useSyncUrlAndStore() {
41
42
  const toBlockchain = searchParams.get(SearchParams.TO_BLOCKCHAIN);
42
43
  const toToken = searchParams.get(SearchParams.TO_TOKEN);
43
44
  const autoConnect = searchParams.get(SearchParams.AUTO_CONNECT);
45
+ const clientUrl = searchParams.get(SearchParams.CLIENT_URL);
44
46
 
45
47
  return {
46
48
  fromAmount,
@@ -49,6 +51,7 @@ export function useSyncUrlAndStore() {
49
51
  toBlockchain,
50
52
  toToken,
51
53
  autoConnect,
54
+ clientUrl,
52
55
  };
53
56
  };
54
57
 
@@ -64,7 +67,7 @@ export function useSyncUrlAndStore() {
64
67
  };
65
68
 
66
69
  useEffect(() => {
67
- const { autoConnect } = getUrlSearchParams();
70
+ const { autoConnect, clientUrl } = getUrlSearchParams();
68
71
  if (isInRouterContext && fetchMetaStatus === 'success') {
69
72
  updateUrlSearchParams({
70
73
  [SearchParams.FROM_BLOCKCHAIN]: fromBlockchain?.name,
@@ -73,6 +76,7 @@ export function useSyncUrlAndStore() {
73
76
  [SearchParams.TO_TOKEN]: tokenToSearchParam(toToken),
74
77
  [SearchParams.FROM_AMOUNT]: inputAmount,
75
78
  [SearchParams.AUTO_CONNECT]: autoConnect ?? undefined,
79
+ [SearchParams.CLIENT_URL]: clientUrl ?? undefined,
76
80
  });
77
81
  }
78
82
  }, [
@@ -132,4 +136,10 @@ export function useSyncUrlAndStore() {
132
136
  }
133
137
  }
134
138
  }, [fetchMetaStatus]);
139
+
140
+ // We run this only once, because if the app is embedded into an iframe, the data in url for iframe will not change.
141
+ useEffect(() => {
142
+ const { clientUrl } = getUrlSearchParams();
143
+ updateIframe('clientUrl', clientUrl || undefined);
144
+ }, []);
135
145
  }
@@ -9,10 +9,25 @@ export const DEFAULT_CONFIG: WidgetConfig = {
9
9
  customDestination: true,
10
10
  };
11
11
 
12
+ interface IframeConfigs {
13
+ clientUrl?: string;
14
+ }
15
+
16
+ const DEFAULT_IFRAME_CONFIGS: IframeConfigs = {
17
+ clientUrl: undefined,
18
+ };
19
+
12
20
  export type ConfigSlice = {
21
+ // What user can set directly.
13
22
  config: WidgetConfig;
14
-
15
23
  updateConfig: (config: WidgetConfig) => void;
24
+
25
+ // What we are setting based on enviroments.
26
+ iframe: { clientUrl?: string };
27
+ updateIframe: <K extends keyof IframeConfigs>(
28
+ name: K,
29
+ value: IframeConfigs[K]
30
+ ) => void;
16
31
  };
17
32
 
18
33
  export const createConfigSlice: StateCreatorWithInitialData<
@@ -22,6 +37,7 @@ export const createConfigSlice: StateCreatorWithInitialData<
22
37
  > = (initialData, set, get) => {
23
38
  return {
24
39
  config: { ...DEFAULT_CONFIG, ...initialData },
40
+ iframe: DEFAULT_IFRAME_CONFIGS,
25
41
 
26
42
  // Actions
27
43
  updateConfig: (nextConfig: WidgetConfig) => {
@@ -34,5 +50,16 @@ export const createConfigSlice: StateCreatorWithInitialData<
34
50
  },
35
51
  });
36
52
  },
53
+
54
+ updateIframe: (name, value) => {
55
+ const currentIframeConfig = get().iframe;
56
+
57
+ set({
58
+ iframe: {
59
+ ...currentIframeConfig,
60
+ [name]: value,
61
+ },
62
+ });
63
+ },
37
64
  };
38
65
  };