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

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.
Files changed (33) hide show
  1. package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
  2. package/dist/components/HeaderButtons/HomeButtons.d.ts.map +1 -1
  3. package/dist/components/Layout/Layout.d.ts.map +1 -1
  4. package/dist/components/NotificationContent/NotificationContent.styles.d.ts.map +1 -1
  5. package/dist/constants/searchParams.d.ts +2 -1
  6. package/dist/constants/searchParams.d.ts.map +1 -1
  7. package/dist/hooks/useIframe/index.d.ts +2 -0
  8. package/dist/hooks/useIframe/index.d.ts.map +1 -0
  9. package/dist/hooks/useIframe/useIframe.d.ts +9 -0
  10. package/dist/hooks/useIframe/useIframe.d.ts.map +1 -0
  11. package/dist/hooks/useIframe/useIframe.helpers.d.ts +2 -0
  12. package/dist/hooks/useIframe/useIframe.helpers.d.ts.map +1 -0
  13. package/dist/hooks/useIframe/useIframe.types.d.ts +10 -0
  14. package/dist/hooks/useIframe/useIframe.types.d.ts.map +1 -0
  15. package/dist/hooks/useSyncUrlAndStore/useSyncUrlAndStore.d.ts.map +1 -1
  16. package/dist/index.js +2 -2
  17. package/dist/index.js.map +4 -4
  18. package/dist/store/slices/config.d.ts +8 -0
  19. package/dist/store/slices/config.d.ts.map +1 -1
  20. package/package.json +2 -2
  21. package/src/components/ConfirmWalletsModal/WalletList.tsx +4 -2
  22. package/src/components/HeaderButtons/HomeButtons.tsx +2 -1
  23. package/src/components/Layout/Layout.styles.ts +1 -1
  24. package/src/components/Layout/Layout.tsx +19 -1
  25. package/src/components/NotificationContent/NotificationContent.styles.ts +1 -0
  26. package/src/components/Quote/Quote.tsx +1 -1
  27. package/src/constants/searchParams.ts +2 -0
  28. package/src/hooks/useIframe/index.ts +1 -0
  29. package/src/hooks/useIframe/useIframe.helpers.ts +3 -0
  30. package/src/hooks/useIframe/useIframe.ts +55 -0
  31. package/src/hooks/useIframe/useIframe.types.ts +13 -0
  32. package/src/hooks/useSyncUrlAndStore/useSyncUrlAndStore.ts +11 -1
  33. package/src/store/slices/config.ts +28 -1
@@ -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.7",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -25,7 +25,7 @@
25
25
  "@rango-dev/queue-manager-core": "^0.25.0",
26
26
  "@rango-dev/queue-manager-rango-preset": "^0.26.0",
27
27
  "@rango-dev/queue-manager-react": "^0.25.0",
28
- "@rango-dev/ui": "^0.27.1-next.4",
28
+ "@rango-dev/ui": "^0.27.1-next.5",
29
29
  "@rango-dev/wallets-react": "^0.12.0",
30
30
  "@rango-dev/wallets-shared": "^0.26.0",
31
31
  "bignumber.js": "^9.1.1",
@@ -1,4 +1,3 @@
1
- /* eslint-disable @typescript-eslint/no-magic-numbers */
2
1
  import type { PropTypes } from './WalletList.type';
3
2
  import type { Wallet } from '../../types';
4
3
  import type { WalletInfo } from '@rango-dev/ui';
@@ -42,6 +41,7 @@ import {
42
41
  WalletImageContainer,
43
42
  } from './WalletList.styles';
44
43
 
44
+ const ACCOUNT_ADDRESS_MAX_CHARACTERS = 7;
45
45
  export function WalletList(props: PropTypes) {
46
46
  const { chain, isSelected, selectWallet, limit, onShowMore } = props;
47
47
  const { config } = useAppStore();
@@ -147,7 +147,9 @@ export function WalletList(props: PropTypes) {
147
147
  walletType: wallet.type,
148
148
  chain,
149
149
  });
150
- const conciseAddress = address ? getConciseAddress(address) : '';
150
+ const conciseAddress = address
151
+ ? getConciseAddress(address, ACCOUNT_ADDRESS_MAX_CHARACTERS)
152
+ : '';
151
153
 
152
154
  const experimentalChain = isExperimentalChain(blockchains(), chain);
153
155
 
@@ -39,7 +39,8 @@ export function HomeButtons(props: HomeButtonsPropTypes) {
39
39
 
40
40
  {!isNotificationsHidden && (
41
41
  <Popover
42
- align="center"
42
+ align="end"
43
+ alignOffset={-88}
43
44
  collisionPadding={{ right: 20, left: 20 }}
44
45
  container={getContainer()}
45
46
  content={<NotificationContent />}>
@@ -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}
@@ -3,6 +3,7 @@ import { styled } from '@rango-dev/ui';
3
3
  export const Container = styled('div', {
4
4
  padding: '$10',
5
5
  width: '350px',
6
+ maxWidth: '90vw',
6
7
  minHeight: '150px',
7
8
  });
8
9
 
@@ -190,7 +190,7 @@ export function Quote(props: QuoteProps) {
190
190
  title={
191
191
  error?.type === QuoteErrorType.BRIDGE_LIMIT
192
192
  ? error?.recommendation
193
- : i18n.t('Slippage Error:')
193
+ : i18n.t(`Slippage ${stepHasError ? 'Error' : 'Warning'}:`)
194
194
  }
195
195
  footer={
196
196
  <FooterAlert>
@@ -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
  };