@bloxwap/hig 0.2.0 → 0.5.1
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/README.md +1 -1
- package/chart/README.md +3 -3
- package/contracts/README.md +1 -1
- package/contracts/dist/messages.d.ts +138 -0
- package/contracts/dist/messages.js +48 -2
- package/css.d.ts +2 -0
- package/feedback/README.md +1 -1
- package/feedback/assets/error.wav +0 -0
- package/feedback/assets/manifest.json +29 -19
- package/feedback/assets/tick.wav +0 -0
- package/feedback/assets/toggle.wav +0 -0
- package/feedback/dist/contract.d.ts +1 -57
- package/feedback/dist/contract.js +2 -1
- package/feedback/dist/native.d.ts +2 -1
- package/feedback/dist/presets.d.ts +57 -0
- package/feedback/dist/presets.js +1 -0
- package/native/README.md +6 -1
- package/native/dist/animated-number.js +11 -7
- package/native/dist/brand.d.ts +1 -1
- package/native/dist/brand.js +4 -3
- package/native/dist/button.d.ts +3 -2
- package/native/dist/button.js +8 -6
- package/native/dist/data.d.ts +18 -1
- package/native/dist/data.js +22 -2
- package/native/dist/field.d.ts +1 -0
- package/native/dist/field.js +18 -5
- package/native/dist/gesture-sheet.js +5 -3
- package/native/dist/icons.d.ts +21 -0
- package/native/dist/icons.js +20 -0
- package/native/dist/navigation.d.ts +1 -0
- package/native/dist/navigation.js +49 -7
- package/native/dist/overlay.js +3 -3
- package/native/dist/pickers.d.ts +54 -0
- package/native/dist/pickers.js +60 -0
- package/native/dist/provider.d.ts +25 -6
- package/native/dist/provider.js +19 -4
- package/native/dist/radio-group.d.ts +17 -1
- package/native/dist/radio-group.js +45 -7
- package/native/dist/recipes.d.ts +3 -0
- package/native/dist/recipes.js +15 -2
- package/native/dist/selection.d.ts +1 -0
- package/native/dist/selection.js +1 -0
- package/native/dist/slider.js +3 -2
- package/native/dist/status.d.ts +16 -5
- package/native/dist/status.js +55 -15
- package/native/dist/surface.d.ts +2 -1
- package/native/dist/surface.js +4 -3
- package/native/dist/swap-panel.d.ts +125 -0
- package/native/dist/swap-panel.js +177 -0
- package/native/dist/swap-parts.d.ts +86 -0
- package/native/dist/swap-parts.js +66 -0
- package/native/dist/swap-shared.d.ts +65 -0
- package/native/dist/swap-shared.js +76 -0
- package/native/dist/swap-sheet.d.ts +14 -0
- package/native/dist/swap-sheet.js +26 -0
- package/native/dist/swap-views.d.ts +41 -0
- package/native/dist/swap-views.js +37 -0
- package/native/dist/switch.d.ts +1 -1
- package/native/dist/switch.js +20 -13
- package/native/dist/wallet-address.d.ts +37 -0
- package/native/dist/wallet-address.js +50 -0
- package/native-fonts/README.md +1 -1
- package/native-fonts/dist/index.d.ts +1 -1
- package/native-fonts/dist/index.js +1 -1
- package/native-fonts/fonts/manifest.json +1 -1
- package/og-fonts/README.md +2 -2
- package/og-fonts/dist/index.d.ts +1 -1
- package/og-fonts/dist/index.js +1 -1
- package/og-fonts/fonts/manifest.json +1 -1
- package/package.json +181 -63
- package/react/README.md +22 -1
- package/react/dist/animated-number.js +2 -1
- package/react/dist/brand.d.ts +1 -1
- package/react/dist/brand.js +3 -2
- package/react/dist/button.d.ts +2 -2
- package/react/dist/button.js +1 -1
- package/react/dist/data.d.ts +15 -0
- package/react/dist/data.js +13 -0
- package/react/dist/field.d.ts +1 -0
- package/react/dist/field.js +2 -1
- package/react/dist/navigation.js +1 -1
- package/react/dist/notify.d.ts +14 -0
- package/react/dist/notify.js +6 -0
- package/react/dist/recipes/install-command.d.ts +1 -0
- package/react/dist/recipes/install-command.js +8 -1
- package/react/dist/recipes/pickers.d.ts +57 -0
- package/react/dist/recipes/pickers.js +78 -0
- package/react/dist/recipes/swap/amount-card.d.ts +40 -0
- package/react/dist/recipes/swap/amount-card.js +7 -0
- package/react/dist/recipes/swap/picker.d.ts +26 -0
- package/react/dist/recipes/swap/picker.js +20 -0
- package/react/dist/recipes/swap/settings.d.ts +24 -0
- package/react/dist/recipes/swap/settings.js +25 -0
- package/react/dist/recipes/swap/shared.d.ts +98 -0
- package/react/dist/recipes/swap/shared.js +106 -0
- package/react/dist/recipes/swap-panel.d.ts +127 -0
- package/react/dist/recipes/swap-panel.js +170 -0
- package/react/dist/recipes/swap-sheet.d.ts +16 -0
- package/react/dist/recipes/swap-sheet.js +22 -0
- package/react/dist/recipes/trade-entry.js +1 -1
- package/react/dist/recipes.d.ts +3 -1
- package/react/dist/recipes.js +3 -1
- package/react/dist/select.js +2 -0
- package/react/dist/selection.d.ts +13 -0
- package/react/dist/selection.js +7 -0
- package/react/dist/status.d.ts +15 -5
- package/react/dist/status.js +8 -4
- package/react/dist/styles.css +406 -92
- package/react/dist/toast.d.ts +17 -0
- package/react/dist/toast.js +21 -0
- package/react/dist/wallet-address.d.ts +23 -0
- package/react/dist/wallet-address.js +24 -0
- package/tokens/README.md +9 -1
- package/tokens/dist/brand.d.ts +19 -0
- package/tokens/dist/brand.js +24 -0
- package/tokens/dist/index.d.ts +9 -1
- package/tokens/dist/index.js +8 -1
- package/tokens/dist/manifest.json +4 -1
- package/tokens/dist/tokens.css +1 -0
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type StyleProp, type ViewStyle } from 'react-native';
|
|
3
|
+
import { type SwapAction, type SwapChain, type SwapDenomination, type SwapFee, type SwapToken, type SwapView, type SwapWallet } from './swap-shared.js';
|
|
4
|
+
export { swapAction, swapParentView, type SwapAction, type SwapChain, type SwapDenomination, type SwapFee, type SwapToken, type SwapView, type SwapWallet } from './swap-shared.js';
|
|
5
|
+
/** What a host surface lends the panel. A sheet passes its backend's list, so list drags hand off to the sheet, and sizes itself, so the panel does not animate its own height. */
|
|
6
|
+
export { SwapHostContext } from './swap-parts.js';
|
|
7
|
+
/** A fixed place the assets go, in place of a token the user picks: a wallet, a staking pool, a trading account. Shown as a compact To card with no amount. */
|
|
8
|
+
export interface SwapDestination {
|
|
9
|
+
title: string;
|
|
10
|
+
/** One muted line under the title, such as the chain or a rate. */
|
|
11
|
+
subtitle?: string;
|
|
12
|
+
/** Image URL. Without one, `address` draws the wallet's colour disc. */
|
|
13
|
+
icon?: string;
|
|
14
|
+
/** A wallet address; shown shortened under the subtitle. */
|
|
15
|
+
address?: string;
|
|
16
|
+
/** Muted trailing text, such as an arrival time or a rate. */
|
|
17
|
+
detail?: string;
|
|
18
|
+
/** Makes the card a button with a trailing chevron, for example to change the recipient. */
|
|
19
|
+
onPress?: () => void;
|
|
20
|
+
/** Still to be chosen: the card shows `title` / `subtitle` as a placeholder and the action stays disabled, asking for a destination. */
|
|
21
|
+
pending?: boolean;
|
|
22
|
+
}
|
|
23
|
+
/** One label and value row of the details card. */
|
|
24
|
+
export interface SwapDetail {
|
|
25
|
+
label: string;
|
|
26
|
+
value: string;
|
|
27
|
+
}
|
|
28
|
+
/** The receive view: the host's address and its rendered QR code. HIG ships no QR encoder. */
|
|
29
|
+
export interface SwapReceive {
|
|
30
|
+
address: string;
|
|
31
|
+
qr?: React.ReactNode; /** Host clipboard adapter. When set, the whole view copies the address. */
|
|
32
|
+
onCopy?: () => Promise<void> | void;
|
|
33
|
+
}
|
|
34
|
+
export interface SwapPanelProps {
|
|
35
|
+
/** Catalog for the To side, and for the From side unless `fromTokens` is given. */
|
|
36
|
+
tokens: readonly SwapToken[];
|
|
37
|
+
/** Assets offered on the From side, typically the connected wallet's holdings. */
|
|
38
|
+
fromTokens?: readonly SwapToken[];
|
|
39
|
+
fromTokensLoading?: boolean;
|
|
40
|
+
/** Chain filter chips. Derived from the tokens, in first-seen order, when omitted. */
|
|
41
|
+
chains?: readonly SwapChain[];
|
|
42
|
+
/** Token ids for the picker's Hot section, in rank order. */
|
|
43
|
+
hotTokenIds?: readonly string[];
|
|
44
|
+
/** The chosen pair. Controlled when `from` / `to` is set (`null` is an empty side); otherwise the panel keeps it, seeded from the defaults. */
|
|
45
|
+
from?: SwapToken | null;
|
|
46
|
+
to?: SwapToken | null;
|
|
47
|
+
defaultFrom?: SwapToken | null;
|
|
48
|
+
defaultTo?: SwapToken | null;
|
|
49
|
+
onFromChange?: (token: SwapToken | null) => void;
|
|
50
|
+
onToChange?: (token: SwapToken | null) => void;
|
|
51
|
+
/** From amount in token units. Optional: set it to resync the entry, for example to `'0'` after a submit. */
|
|
52
|
+
amount?: string;
|
|
53
|
+
/** Reports the From amount in token units whenever the entry, denomination or price changes it. */
|
|
54
|
+
onAmountChange?: (units: string) => void;
|
|
55
|
+
/** To amount in token units from the host's quote. Until it arrives the panel shows a price estimate. */
|
|
56
|
+
received?: number | null;
|
|
57
|
+
/** Total fee in USD from the host's quote, and its breakdown for the settings sheet. */
|
|
58
|
+
fee?: number | null;
|
|
59
|
+
fees?: readonly SwapFee[];
|
|
60
|
+
quoteLoading?: boolean;
|
|
61
|
+
/** Slippage tolerance as a fraction (0.005 is 0.5%). Omit `onSlippageChange` to show it read-only. */
|
|
62
|
+
slippage?: number;
|
|
63
|
+
onSlippageChange?: (slippage: number) => void;
|
|
64
|
+
/** Default denomination of both amounts. Uncontrolled when omitted. */
|
|
65
|
+
denomination?: SwapDenomination;
|
|
66
|
+
onDenominationChange?: (denomination: SwapDenomination) => void;
|
|
67
|
+
wallet?: SwapWallet;
|
|
68
|
+
onConnect?: () => void;
|
|
69
|
+
onDisconnect?: () => void;
|
|
70
|
+
recipient?: string;
|
|
71
|
+
onRecipientChange?: (address: string) => void;
|
|
72
|
+
onScanRecipient?: () => void;
|
|
73
|
+
/** Enables the receive view, offered in the From picker before a wallet is connected. */
|
|
74
|
+
receive?: SwapReceive;
|
|
75
|
+
receiving?: boolean;
|
|
76
|
+
onReceivingChange?: (receiving: boolean) => void;
|
|
77
|
+
/** The host validates, quotes and executes. HIG never submits a transaction. */
|
|
78
|
+
onSubmit: (action: SwapAction) => void;
|
|
79
|
+
submitting?: boolean;
|
|
80
|
+
/** Called after the panel clears its entry and both tokens (through `onFromChange(null)` / `onToChange(null)`), so the host can drop its quote. */
|
|
81
|
+
onReset?: () => void;
|
|
82
|
+
/** On native there is no hardware keyboard to rely on, so `auto` shows the keypad; `never` hides it. */
|
|
83
|
+
/** `false` fixes both amounts to `denomination` (dollars by default): the conversion capsule under each amount and the denomination setting are not shown. For hosts that only have one of the two, for example no token price. */
|
|
84
|
+
denominationSwitch?: boolean;
|
|
85
|
+
/** A fixed destination in place of the To token. The To card becomes compact (no picker, no amount), the panel is one-way, and the action is ready once the From side is. `onSubmit` receives `'send'`. */
|
|
86
|
+
destination?: SwapDestination;
|
|
87
|
+
/** Label and value rows on the bottom card, such as a staked balance or a fee tier. With no To token and no destination they are the bottom card on their own, and the panel behaves as it does for a destination. */
|
|
88
|
+
details?: readonly SwapDetail[];
|
|
89
|
+
/** A line under the cards: a limit, a delay, a warning. `noteTone` tints it. */
|
|
90
|
+
note?: React.ReactNode;
|
|
91
|
+
noteTone?: 'muted' | 'warning' | 'negative';
|
|
92
|
+
/** Heading above the cards, for a panel that is its own screen or sheet. `onBack` adds a Back control before it. */
|
|
93
|
+
title?: string;
|
|
94
|
+
onBack?: () => void;
|
|
95
|
+
/** `false` leaves the action button on its own, full width: no settings and no reset button. */
|
|
96
|
+
controls?: boolean;
|
|
97
|
+
/** Decoration before the action text. */
|
|
98
|
+
actionIcon?: React.ReactNode;
|
|
99
|
+
/** Keep a side's token fixed: its card shows the token but does not open the picker. Use with `from` / `to` or their defaults. */
|
|
100
|
+
lockFrom?: boolean;
|
|
101
|
+
lockTo?: boolean;
|
|
102
|
+
/** Names the action when it is ready, in place of Send, Swap or Bridge: for example Stake, Deposit or Withdraw. The host supplies the translated text. */
|
|
103
|
+
actionLabel?: string;
|
|
104
|
+
/** One direction only, top to bottom: the control between the cards becomes a down arrow instead of the flip button, and the pair cannot be exchanged. */
|
|
105
|
+
oneWay?: boolean;
|
|
106
|
+
keypad?: 'auto' | 'always' | 'never';
|
|
107
|
+
/** Web only (⌘F / ⌘K picker shortcuts); accepted and ignored so one example serves both platforms. */
|
|
108
|
+
shortcuts?: boolean;
|
|
109
|
+
/** Web only (page-wide digit capture); accepted and ignored. */
|
|
110
|
+
captureKeyboard?: boolean;
|
|
111
|
+
/** Host rows for the settings view, such as language or theme. */
|
|
112
|
+
settings?: React.ReactNode;
|
|
113
|
+
/** The view on show. Pickers and settings push inside the panel's own bounds; Back pops. Uncontrolled when omitted. */
|
|
114
|
+
view?: SwapView;
|
|
115
|
+
onViewChange?: (view: SwapView) => void;
|
|
116
|
+
/** Web only; accepted and ignored. */
|
|
117
|
+
className?: string;
|
|
118
|
+
style?: StyleProp<ViewStyle>;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Send, swap and bridge in one panel: a From card and a To card joined by a flip control, an amount entered in dollars
|
|
122
|
+
* or token units through the keypad, token pickers, settings, and one action that names itself from the chosen pair.
|
|
123
|
+
* Presentation only. The host owns tokens, balances, prices, the quote, the wallet and the transaction.
|
|
124
|
+
*/
|
|
125
|
+
export declare function SwapPanel({ tokens, fromTokens, fromTokensLoading, chains, hotTokenIds, from: fromProp, to: toProp, defaultFrom, defaultTo, onFromChange, onToChange, amount, onAmountChange, received, fee, fees, quoteLoading, slippage, onSlippageChange, denomination, onDenominationChange, wallet, onConnect, onDisconnect, recipient, onRecipientChange, onScanRecipient, receive, receiving, onReceivingChange, onSubmit, submitting, onReset, keypad, destination, details, note, noteTone, title, onBack, controls, actionIcon, lockFrom, lockTo, actionLabel, oneWay: oneWayProp, denominationSwitch, settings, view: viewProp, onViewChange, style }: SwapPanelProps): React.JSX.Element;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useContext, useEffect, useMemo, useRef, useState } from 'react';
|
|
3
|
+
import { Animated, BackHandler, Easing, Platform, Pressable, StyleSheet, View } from 'react-native';
|
|
4
|
+
import { formatNumber } from '@bloxwap/hig/contracts';
|
|
5
|
+
import { Raised, useColors, useControllable, useHIG, useMessages } from './provider.js';
|
|
6
|
+
import { Text } from './surface.js';
|
|
7
|
+
import { Button, IconButton } from './button.js';
|
|
8
|
+
import { AnimatedNumberReadout } from './animated-number.js';
|
|
9
|
+
import { Toast, useTransientNotice } from './status.js';
|
|
10
|
+
import { VerticalAddress, addressKind } from './data.js';
|
|
11
|
+
import { withAlpha } from '@bloxwap/hig/tokens';
|
|
12
|
+
import { DownIcon, FlipIcon, MenuIcon, ResetIcon, SmallChevron, SwapCapsule, SwapHeader, SwapHostContext, SwapKeypad, TokenArt } from './swap-parts.js';
|
|
13
|
+
import { SwapPicker, SwapSettings } from './swap-views.js';
|
|
14
|
+
import { addressColor, applyAmountKey, formatPercent, shortAddress, swapAction, swapParentView, testAmount, trimNumber, unitsToEntry } from './swap-shared.js';
|
|
15
|
+
export { swapAction, swapParentView } from './swap-shared.js';
|
|
16
|
+
/** What a host surface lends the panel. A sheet passes its backend's list, so list drags hand off to the sheet, and sizes itself, so the panel does not animate its own height. */
|
|
17
|
+
export { SwapHostContext } from './swap-parts.js';
|
|
18
|
+
/**
|
|
19
|
+
* Send, swap and bridge in one panel: a From card and a To card joined by a flip control, an amount entered in dollars
|
|
20
|
+
* or token units through the keypad, token pickers, settings, and one action that names itself from the chosen pair.
|
|
21
|
+
* Presentation only. The host owns tokens, balances, prices, the quote, the wallet and the transaction.
|
|
22
|
+
*/
|
|
23
|
+
export function SwapPanel({ tokens, fromTokens, fromTokensLoading, chains, hotTokenIds, from: fromProp, to: toProp, defaultFrom = null, defaultTo = null, onFromChange, onToChange, amount, onAmountChange, received, fee, fees, quoteLoading, slippage = 0.005, onSlippageChange, denomination, onDenominationChange, wallet, onConnect, onDisconnect, recipient, onRecipientChange, onScanRecipient, receive, receiving, onReceivingChange, onSubmit, submitting = false, onReset, keypad = 'auto', destination, details, note, noteTone = 'muted', title, onBack, controls = true, actionIcon, lockFrom = false, lockTo = false, actionLabel, oneWay: oneWayProp = false, denominationSwitch = true, settings, view: viewProp, onViewChange, style }) {
|
|
24
|
+
const env = useHIG();
|
|
25
|
+
const { locale, direction } = env;
|
|
26
|
+
const colors = useColors();
|
|
27
|
+
const t = useMessages();
|
|
28
|
+
const [from, setFrom] = useControllable(fromProp, defaultFrom, onFromChange);
|
|
29
|
+
const [to, setTo] = useControllable(toProp, defaultTo, onToChange);
|
|
30
|
+
const [defaultMode, setDefaultMode] = useControllable(denomination, 'usd', onDenominationChange);
|
|
31
|
+
const [isReceiving, setReceiving] = useControllable(receiving, false, onReceivingChange);
|
|
32
|
+
// `amount` seeds the first render too, in the default denomination at the initial From token's price.
|
|
33
|
+
const [entry, setEntry] = useState(() => amount === undefined ? '0' : unitsToEntry(Number(amount) || 0, defaultMode, from?.price ?? 0));
|
|
34
|
+
const [fromMode, setFromMode] = useState(defaultMode);
|
|
35
|
+
const [toMode, applyToMode] = useState(defaultMode);
|
|
36
|
+
const host = useContext(SwapHostContext);
|
|
37
|
+
const [ownView, keepView] = useState('main');
|
|
38
|
+
const view = viewProp ?? ownView;
|
|
39
|
+
const show = (next) => { if (next === view)
|
|
40
|
+
return; if (viewProp === undefined)
|
|
41
|
+
keepView(next); onViewChange?.(next); };
|
|
42
|
+
const back = () => show(swapParentView(view));
|
|
43
|
+
// Android back pops a pushed view before anything behind the panel handles it.
|
|
44
|
+
const pop = useRef(back);
|
|
45
|
+
pop.current = back;
|
|
46
|
+
useEffect(() => { if (view === 'main')
|
|
47
|
+
return; const subscription = BackHandler.addEventListener('hardwareBackPress', () => { pop.current(); return true; }); return () => subscription.remove(); }, [view === 'main']);
|
|
48
|
+
const { notice, show: showNotice, dismiss: dismissNotice } = useTransientNotice();
|
|
49
|
+
const fromPrice = from?.price ?? 0, toPrice = to?.price ?? 0, input = Number(entry) || 0;
|
|
50
|
+
const units = fromMode === 'token' ? input : fromPrice > 0 ? input / fromPrice : 0, usd = units * fromPrice, unitsText = trimNumber(units);
|
|
51
|
+
const showReceive = isReceiving && !!receive;
|
|
52
|
+
// Report the amount in token units; resync the entry when the host sets a different amount.
|
|
53
|
+
const reported = useRef(amount ?? '0');
|
|
54
|
+
useEffect(() => { if (reported.current !== unitsText) {
|
|
55
|
+
reported.current = unitsText;
|
|
56
|
+
onAmountChange?.(unitsText);
|
|
57
|
+
} }, [unitsText]);
|
|
58
|
+
useEffect(() => { if (amount === undefined || Number(amount) === Number(reported.current))
|
|
59
|
+
return; reported.current = trimNumber(Number(amount) || 0); setEntry(unitsToEntry(Number(amount) || 0, fromMode, fromPrice)); }, [amount]);
|
|
60
|
+
const sameAsset = !!from && !!to && from.id === to.id;
|
|
61
|
+
const receivedUnits = sameAsset ? units : received ?? (toPrice > 0 ? usd / toPrice : 0);
|
|
62
|
+
const receivedUsd = toPrice > 0 ? receivedUnits * toPrice : sameAsset ? usd : 0;
|
|
63
|
+
const action = swapAction(from, to);
|
|
64
|
+
const insufficient = !!from && (from.balance ?? 0) > 0 && units > (from.balance ?? 0);
|
|
65
|
+
// A destination or a details card stands in for the To token: one way, and ready from the From side alone.
|
|
66
|
+
const fixed = !!destination || (!!details && !to);
|
|
67
|
+
const oneWay = oneWayProp || fixed;
|
|
68
|
+
const ready = !!from && (!!to || fixed) && !destination?.pending && units > 0 && !insufficient;
|
|
69
|
+
const actionText = showReceive ? t.swapSendFromWallet : !from || (!to && !fixed) ? t.swapChooseToken : destination?.pending ? t.swapChooseDestination : units <= 0 ? t.swapEnterAmount : insufficient ? t.swapInsufficient(from.symbol) : actionLabel ?? (action === 'swap' && !fixed ? t.swapActionSwap : action === 'bridge' && !fixed ? t.swapActionBridge : t.swapActionSend);
|
|
70
|
+
const pristine = (lockFrom || !from) && (lockTo || !to) && input === 0;
|
|
71
|
+
// Updater form: several keys can land before the next render, and each must build on the last.
|
|
72
|
+
const latest = useRef(fromMode);
|
|
73
|
+
latest.current = fromMode;
|
|
74
|
+
const press = (key) => setEntry(current => applyAmountKey(current, key, latest.current));
|
|
75
|
+
const switchFromMode = (mode) => { if (mode === fromMode)
|
|
76
|
+
return; setEntry(mode === 'usd' ? trimNumber(usd, 2) : trimNumber(units)); setFromMode(mode); };
|
|
77
|
+
const changeDenomination = (mode) => { setDefaultMode(mode); switchFromMode(mode); applyToMode(mode); };
|
|
78
|
+
// A new default applies to an untouched entry at once; a typed amount keeps its denomination until reset.
|
|
79
|
+
useEffect(() => { if (input === 0) {
|
|
80
|
+
setFromMode(defaultMode);
|
|
81
|
+
applyToMode(defaultMode);
|
|
82
|
+
} }, [defaultMode]);
|
|
83
|
+
const reset = () => { setEntry('0'); setFromMode(defaultMode); applyToMode(defaultMode); if (isReceiving)
|
|
84
|
+
setReceiving(false); if (!lockFrom)
|
|
85
|
+
setFrom(null); if (!lockTo)
|
|
86
|
+
setTo(null); onReset?.(); };
|
|
87
|
+
const flip = () => { if (!from || !to)
|
|
88
|
+
return; setFrom(to); setTo(from); };
|
|
89
|
+
const submit = () => { if (submitting)
|
|
90
|
+
return; if (showReceive) {
|
|
91
|
+
onConnect?.();
|
|
92
|
+
return;
|
|
93
|
+
} if (ready)
|
|
94
|
+
onSubmit(fixed ? 'send' : action); };
|
|
95
|
+
// A copied address shows as a transient toast capsule (about 4s, dismissible), announced the way Toast announces any notice.
|
|
96
|
+
const copyAddress = () => { if (!receive?.onCopy)
|
|
97
|
+
return; Promise.resolve().then(receive.onCopy).then(() => showNotice(t.addressCopied)).catch(() => { }); };
|
|
98
|
+
const chainList = useMemo(() => { if (chains)
|
|
99
|
+
return chains; const seen = new Map(); for (const token of tokens)
|
|
100
|
+
if (!seen.has(token.chainId))
|
|
101
|
+
seen.set(token.chainId, { chainId: token.chainId, name: token.chain, icon: token.chainIcon }); return [...seen.values()]; }, [chains, tokens]);
|
|
102
|
+
const usdText = (value) => formatNumber(value, locale, { style: 'currency', currency: 'USD' });
|
|
103
|
+
const fraction = entry.includes('.') ? Math.min(entry.length - entry.indexOf('.') - 1, fromMode === 'usd' ? 2 : 8) : 0;
|
|
104
|
+
const small = { fontSize: 14, lineHeight: 19, color: colors.muted };
|
|
105
|
+
// An account (a token with a `caption`) reads name over caption beside one disc, and offers Max only.
|
|
106
|
+
const account = (side, token, locked) => _jsxs(_Fragment, { children: [_jsxs(View, { pointerEvents: 'none', testID: `swap-account-${side}`, style: { flex: 1, minWidth: 0, flexDirection: 'row', alignItems: 'center', gap: 12 }, children: [_jsx(View, { accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants', children: _jsx(TokenArt, { uri: token.icon, symbol: token.symbol, size: 40 }) }), _jsxs(View, { style: { flex: 1, minWidth: 0 }, children: [_jsxs(View, { style: { flexDirection: 'row', alignItems: 'center', gap: 4 }, children: [_jsx(Text, { style: { fontWeight: '600', lineHeight: 21, flexShrink: 1 }, children: token.name }), locked ? null : _jsx(View, { testID: `swap-account-chevron-${side}`, children: _jsx(SmallChevron, { color: colors.muted }) })] }), _jsx(Text, { style: small, children: token.caption })] })] }), side === 'from' && (token.balance ?? 0) > 0 ? _jsx(SwapCapsule, { testID: 'swap-max', onPress: () => setEntry(unitsToEntry(token.balance ?? 0, fromMode, fromPrice)), children: _jsx(Text, { style: small, children: t.swapMax }) }) : null] });
|
|
107
|
+
const header = (side, token) => _jsxs(_Fragment, { children: [_jsxs(View, { pointerEvents: 'none', style: { flex: 1, minWidth: 0, flexDirection: 'row', alignItems: 'flex-start', gap: 12 }, children: [_jsx(View, { accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants', style: { alignItems: 'center' }, children: [wallet?.address ? _jsx(View, { style: { width: 28, height: 28, borderRadius: 14, backgroundColor: addressColor(wallet.address), borderWidth: 2, borderColor: colors.surface } }) : null, token.chainIcon ? _jsx(TokenArt, { uri: token.chainIcon, size: 28, ring: colors.surface }) : null, _jsx(TokenArt, { uri: token.icon, symbol: token.symbol, size: 28, ring: colors.surface })].filter(Boolean).map((layer, index) => _jsx(View, { style: { marginTop: index ? -14 : 0 }, children: layer }, index)) }), _jsxs(View, { style: { flex: 1, minWidth: 0 }, children: [wallet?.address ? _jsx(Text, { style: small, children: shortAddress(wallet.address) }) : null, _jsx(Text, { style: small, children: token.chain }), _jsx(Text, { style: { fontWeight: '600', lineHeight: 21 }, children: token.name })] })] }), side === 'from' ? ((token.balance ?? 0) > 0 ? _jsxs(View, { pointerEvents: 'box-none', style: { alignItems: 'flex-end', gap: 8, maxWidth: '60%' }, children: [_jsxs(View, { pointerEvents: 'box-none', style: { flexDirection: 'row', flexWrap: 'wrap', justifyContent: 'flex-end', gap: 8 }, children: [_jsx(SwapCapsule, { testID: 'swap-test', onPress: () => setEntry(unitsToEntry(testAmount(token), fromMode, fromPrice)), children: _jsx(Text, { style: small, children: t.swapTest }) }), _jsx(SwapCapsule, { testID: 'swap-max', onPress: () => setEntry(unitsToEntry(token.balance ?? 0, fromMode, fromPrice)), children: _jsx(Text, { style: small, children: t.swapMax }) })] }), _jsx(View, { pointerEvents: 'none', children: _jsxs(Text, { style: small, children: [formatNumber(token.balance, locale, { maximumFractionDigits: 6 }), " ", token.symbol] }) })] }) : null)
|
|
108
|
+
: _jsxs(View, { pointerEvents: 'box-none', style: { alignItems: 'flex-end', gap: 8, maxWidth: '60%' }, children: [_jsx(SwapCapsule, { testID: 'swap-slippage', onPress: () => show(onSlippageChange ? 'slippage' : 'settings'), children: _jsx(Text, { style: small, children: t.swapSlippage(formatPercent(slippage)) }) }), _jsx(View, { pointerEvents: 'none', children: _jsx(Text, { style: small, children: t.swapFee(quoteLoading ? '…' : fee != null ? usdText(fee) : '—') }) })] })] });
|
|
109
|
+
const receiveView = (view) => {
|
|
110
|
+
const body = _jsxs(_Fragment, { children: [view.qr ? _jsx(View, { style: { width: 132, height: 132, padding: 8, borderRadius: 16, backgroundColor: colors.background }, children: view.qr }) : null, _jsx(View, { accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants', children: _jsx(VerticalAddress, { address: view.address, rowHeight: addressKind(view.address) === 'ethereum' ? 22 : 16 }) })] });
|
|
111
|
+
const layout = { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-evenly', gap: 12, paddingTop: 8, paddingHorizontal: 16, paddingBottom: 20 };
|
|
112
|
+
return view.onCopy ? _jsx(Pressable, { accessibilityRole: 'button', accessibilityLabel: `${t.copyAddress}: ${view.address}`, testID: 'swap-receive', onPress: copyAddress, style: ({ pressed }) => [layout, { opacity: pressed ? 0.8 : 1 }], children: body }) : _jsx(View, { accessible: true, accessibilityLabel: view.address, testID: 'swap-receive', style: layout, children: body });
|
|
113
|
+
};
|
|
114
|
+
const card = (side) => {
|
|
115
|
+
const token = side === 'from' ? from : to, label = side === 'from' ? t.swapFrom : t.swapTo, mode = side === 'from' ? fromMode : toMode, locked = side === 'from' ? lockFrom : lockTo;
|
|
116
|
+
const cardUnits = side === 'from' ? units : receivedUnits, cardUsd = side === 'from' ? usd : receivedUsd;
|
|
117
|
+
const shown = side === 'from' ? input : mode === 'token' ? receivedUnits : receivedUsd;
|
|
118
|
+
const options = { minimumFractionDigits: side === 'from' ? fraction : 0, maximumFractionDigits: mode === 'usd' ? 2 : 8, useGrouping: false };
|
|
119
|
+
const prefix = mode === 'usd' ? '$' : '', suffix = side === 'from' && entry.endsWith('.') ? '.' : '';
|
|
120
|
+
return _jsx(View, { testID: `swap-card-${side}`, style: { backgroundColor: colors.surface, borderRadius: 24, overflow: 'hidden' }, children: _jsxs(Raised, { children: [_jsxs(View, { children: [locked ? null : _jsx(Pressable, { accessibilityRole: 'button', accessibilityLabel: t.swapSelectToken(label), testID: `swap-slot-${side}`, onPress: () => show(side), style: StyleSheet.absoluteFill }), _jsx(View, { pointerEvents: 'box-none', style: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', gap: 12, minHeight: 84, padding: 16 }, children: token ? token.caption !== undefined ? account(side, token, locked) : header(side, token) : _jsx(View, { pointerEvents: 'none', children: _jsxs(Text, { accessible: false, style: { fontSize: 40, lineHeight: 46, fontWeight: '500', letterSpacing: -0.8, color: colors.faint }, children: [label, "\u2026"] }) }) })] }), side === 'from' && showReceive && receive ? receiveView(receive) : side === 'to' && showReceive ? null : _jsxs(View, { children: [locked ? null : _jsx(Pressable, { accessible: false, importantForAccessibility: 'no', testID: `swap-area-${side}`, onPress: () => show(side), style: StyleSheet.absoluteFill }), _jsxs(View, { pointerEvents: 'box-none', style: { alignItems: 'center', gap: 8, paddingTop: 4, paddingHorizontal: 16, paddingBottom: 20 }, children: [_jsx(View, { pointerEvents: 'none', accessible: true, accessibilityLabel: t.swapAmount(label), accessibilityValue: { text: `${prefix}${formatNumber(shown, locale, options)}${suffix}` }, testID: `swap-amount-${side}`, children: _jsx(AnimatedNumberReadout, { value: shown, prefix: prefix, suffix: suffix, options: options, tone: side === 'to' ? 'muted' : 'text', maxFontSizeMultiplier: 1.5, style: { fontSize: 44, lineHeight: 50, fontWeight: '700', letterSpacing: -0.9, textAlign: 'center' } }) }), denominationSwitch && _jsxs(SwapCapsule, { label: t.swapSwitchDenomination, testID: `swap-mode-${side}`, onPress: () => side === 'from' ? switchFromMode(fromMode === 'usd' ? 'token' : 'usd') : applyToMode(toMode === 'usd' ? 'token' : 'usd'), children: [_jsx(Text, { accessible: false, style: [small, { opacity: 0.5 }], children: "=" }), _jsx(Text, { testID: `swap-conversion-${side}`, style: small, children: mode === 'token' ? usdText(cardUsd) : `${formatNumber(cardUnits, locale, { maximumFractionDigits: 8, useGrouping: false })}${token ? ` ${token.symbol}` : ''}` }), _jsx(FlipIcon, { color: colors.muted, size: 14 })] })] })] })] }) });
|
|
121
|
+
};
|
|
122
|
+
// The fixed bottom card is one compact row for the destination (artwork, where it goes, a trailing detail and fee), label and value rows, or both.
|
|
123
|
+
const fixedCard = () => {
|
|
124
|
+
if (!fixed)
|
|
125
|
+
return null;
|
|
126
|
+
const art = !destination ? null : destination.icon ? _jsx(TokenArt, { uri: destination.icon, size: 40 }) : destination.address ? _jsx(View, { style: { width: 40, height: 40, borderRadius: 20, backgroundColor: addressColor(destination.address) } }) : _jsx(View, { style: { width: 40, height: 40, borderRadius: 20, backgroundColor: colors.nested, alignItems: 'center', justifyContent: 'center' }, children: _jsx(Text, { style: { fontSize: 12, lineHeight: 16, fontWeight: '600', color: colors.muted }, children: destination.title.slice(0, 2) }) });
|
|
127
|
+
const row = destination ? _jsxs(View, { pointerEvents: 'none', style: { flexDirection: 'row', alignItems: 'center', gap: 12, minHeight: 84, padding: 16 }, children: [_jsx(View, { accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants', children: art }), _jsxs(View, { style: { flex: 1, minWidth: 0 }, children: [_jsx(Text, { testID: 'swap-destination-title', style: { fontWeight: destination.pending ? '500' : '600', lineHeight: 21 }, children: destination.title }), destination.subtitle ? _jsx(Text, { style: small, children: destination.subtitle }) : null, destination.address ? _jsx(Text, { style: small, children: shortAddress(destination.address) }) : null] }), destination.detail || fee !== undefined || destination.onPress ? _jsxs(View, { style: { flexDirection: 'row', alignItems: 'center', gap: 8, maxWidth: '60%' }, children: [_jsxs(View, { style: { alignItems: 'flex-end', gap: 8, flexShrink: 1 }, children: [destination.detail ? _jsx(Text, { style: [small, { textAlign: 'right' }], children: destination.detail }) : null, fee !== undefined ? _jsx(Text, { testID: 'swap-destination-fee', style: [small, { textAlign: 'right' }], children: t.swapFee(quoteLoading ? '…' : fee != null ? usdText(fee) : '—') }) : null] }), destination.onPress ? _jsx(View, { testID: 'swap-destination-chevron', children: _jsx(SmallChevron, { color: colors.muted, trailing: true }) }) : null] }) : null] }) : null;
|
|
128
|
+
const rows = details && details.length > 0 ? _jsx(View, { testID: 'swap-details', style: { gap: 12, padding: 16, paddingTop: destination ? 0 : 16 }, children: details.map(detail => _jsxs(View, { style: { flexDirection: 'row', justifyContent: 'space-between', gap: 12 }, children: [_jsx(Text, { style: { color: colors.muted, lineHeight: 21 }, children: detail.label }), _jsx(Text, { style: { lineHeight: 21, textAlign: 'right', flexShrink: 1 }, children: detail.value })] }, detail.label)) }) : null;
|
|
129
|
+
const shape = { backgroundColor: colors.surface, borderRadius: 24, overflow: 'hidden' };
|
|
130
|
+
// With a pressable destination the whole card is the button; the rows inside it are read as part of it.
|
|
131
|
+
return destination?.onPress ? _jsx(Pressable, { accessibilityRole: 'button', accessibilityLabel: destination.title, testID: 'swap-destination', onPress: destination.onPress, style: ({ pressed }) => [shape, { opacity: pressed ? 0.85 : 1 }], children: _jsxs(Raised, { children: [row, rows] }) }) : _jsx(View, { testID: 'swap-destination', style: shape, children: _jsxs(Raised, { children: [row, rows] }) });
|
|
132
|
+
};
|
|
133
|
+
const fromCard = card('from'), toCard = fixedCard() ?? card('to');
|
|
134
|
+
const tone = noteTone === 'warning' ? colors.warning : noteTone === 'negative' ? colors.negative : null;
|
|
135
|
+
const main = _jsxs(View, { testID: 'swap-view-main', children: [title ? _jsx(View, { style: { marginBottom: 12 }, children: _jsx(SwapHeader, { title: title, onBack: onBack }) }) : null, fromCard, _jsx(View, { style: { height: 8, zIndex: 1, alignItems: 'center' }, children: _jsx(View, { style: { position: 'absolute', top: -15, width: 38, height: 38, borderRadius: 19, backgroundColor: colors.background, alignItems: 'center', justifyContent: 'center' }, children: oneWay ? _jsx(View, { testID: 'swap-one-way', style: { width: 32, height: 32, borderRadius: 16, alignItems: 'center', justifyContent: 'center', backgroundColor: colors.nested }, children: _jsx(DownIcon, { color: colors.muted }) }) : _jsx(Pressable, { accessibilityRole: 'button', accessibilityLabel: t.swapFlip, accessibilityState: { disabled: !from || !to }, disabled: !from || !to, testID: 'swap-flip', onPress: flip, hitSlop: 6, style: ({ pressed }) => ({ width: 32, height: 32, borderRadius: 16, alignItems: 'center', justifyContent: 'center', backgroundColor: !from || !to ? colors.surface : colors.nested, opacity: pressed ? 0.85 : 1 }), children: _jsx(FlipIcon, { color: !from || !to ? colors.faint : colors.muted }) }) }) }), toCard, note ? _jsx(View, { testID: 'swap-note', style: tone ? { marginTop: 8, paddingVertical: 12, paddingHorizontal: 16, borderRadius: 16, backgroundColor: withAlpha(tone, 0.14) } : { marginTop: 8, paddingHorizontal: 4 }, children: _jsx(Text, { style: { fontSize: 14, lineHeight: 21, color: tone ?? colors.muted }, children: note }) }) : null, !showReceive && keypad !== 'never' ? _jsx(SwapKeypad, { onKey: press, label: t.swapKeypad, deleteLabel: t.swapDelete }) : null, _jsxs(View, { style: { flexDirection: 'row', alignItems: 'center', gap: 8, marginTop: 16 }, children: [controls ? _jsx(IconButton, { intent: 'secondary', label: t.swapSettings, suppressFeedback: true, testID: 'swap-settings-button', onPress: () => show('settings'), children: _jsx(MenuIcon, { color: colors.text }) }) : null, _jsxs(Button, { busy: submitting, disabled: !showReceive && !ready, testID: 'swap-action', onPress: submit, style: { flex: 1, alignSelf: 'auto' }, children: [actionIcon ? _jsx(View, { testID: 'swap-action-icon', accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants', children: actionIcon }, 'icon') : null, actionText] }), controls ? _jsx(IconButton, { intent: 'secondary', label: t.swapReset, disabled: pristine || submitting, testID: 'swap-reset', onPress: reset, children: _jsx(ResetIcon, { color: colors.text }) }) : null] }), notice ? _jsx(View, { style: { marginTop: 8, alignItems: 'center' }, children: _jsx(Toast, { notices: [notice], onDismiss: dismissNotice }) }) : null] });
|
|
136
|
+
const body = view === 'main' ? main
|
|
137
|
+
: view === 'from' ? _jsx(SwapPicker, { side: view, onBack: back, tokens: fromTokens ?? tokens, loading: fromTokensLoading, chains: chainList, hotTokenIds: hotTokenIds, selected: from, onSelect: setFrom, wallet: wallet, onConnect: onConnect, onDisconnect: onDisconnect, receiving: isReceiving, onReceivingChange: receive ? setReceiving : undefined })
|
|
138
|
+
: view === 'to' ? _jsx(SwapPicker, { side: view, onBack: back, tokens: tokens, chains: chainList, hotTokenIds: hotTokenIds, selected: to, onSelect: setTo, wallet: wallet, recipient: recipient, onRecipientChange: onRecipientChange, onScanRecipient: onScanRecipient })
|
|
139
|
+
: _jsx(SwapSettings, { view: view, onViewChange: show, onBack: back, denomination: defaultMode, onDenominationChange: denominationSwitch ? changeDenomination : undefined, slippage: slippage, onSlippageChange: onSlippageChange, fee: fee, fees: fees, quoteLoading: quoteLoading, children: settings });
|
|
140
|
+
return _jsx(View, { testID: 'swap-panel', style: [{ direction, width: '100%', maxWidth: 448, alignSelf: 'center' }, style], children: _jsx(SwapStage, { view: view, animateHeight: host.animateHeight !== false, children: body }) });
|
|
141
|
+
}
|
|
142
|
+
const viewDepth = (view) => view === 'main' ? 0 : view === 'slippage' || view === 'fees' ? 2 : 1;
|
|
143
|
+
const stageEasing = Easing.bezier(0.22, 1, 0.36, 1);
|
|
144
|
+
/** Shows one view at a time. A push slides in from the trailing side and a pop from the leading side while the container height eases to the new view; reduced motion swaps at once. */
|
|
145
|
+
function SwapStage({ view, animateHeight, children }) {
|
|
146
|
+
const { reducedMotion, direction } = useHIG();
|
|
147
|
+
const shown = useRef(view), travel = useRef(0), slide = useRef(new Animated.Value(1)).current, height = useRef(new Animated.Value(0)).current, measured = useRef(null);
|
|
148
|
+
const [sized, setSized] = useState(false);
|
|
149
|
+
if (shown.current !== view)
|
|
150
|
+
travel.current = (viewDepth(view) >= viewDepth(shown.current) ? 1 : -1) * (direction === 'rtl' ? -1 : 1);
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (shown.current === view)
|
|
153
|
+
return;
|
|
154
|
+
shown.current = view;
|
|
155
|
+
if (reducedMotion) {
|
|
156
|
+
slide.setValue(1);
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
slide.setValue(0);
|
|
160
|
+
Animated.timing(slide, { toValue: 1, duration: 260, easing: stageEasing, useNativeDriver: Platform.OS !== 'web' }).start();
|
|
161
|
+
}, [view, reducedMotion, slide]);
|
|
162
|
+
const sizing = animateHeight && !reducedMotion;
|
|
163
|
+
const onLayout = (event) => {
|
|
164
|
+
const next = event.nativeEvent.layout.height;
|
|
165
|
+
if (!sizing || !(next > 0))
|
|
166
|
+
return;
|
|
167
|
+
if (measured.current === null)
|
|
168
|
+
height.setValue(next);
|
|
169
|
+
else if (next !== measured.current)
|
|
170
|
+
Animated.timing(height, { toValue: next, duration: 260, easing: stageEasing, useNativeDriver: false }).start();
|
|
171
|
+
measured.current = next;
|
|
172
|
+
if (!sized)
|
|
173
|
+
setSized(true);
|
|
174
|
+
};
|
|
175
|
+
const fitted = sizing && sized;
|
|
176
|
+
return _jsx(Animated.View, { testID: 'swap-stage', style: fitted ? { height, overflow: 'hidden' } : undefined, children: _jsx(Animated.View, { onLayout: onLayout, style: [fitted ? { position: 'absolute', left: 0, right: 0, top: 0 } : null, { opacity: slide, transform: [{ translateX: slide.interpolate({ inputRange: [0, 1], outputRange: [travel.current * 48, 0] }) }] }], children: children }) });
|
|
177
|
+
}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { type FlatListProps } from 'react-native';
|
|
3
|
+
import type { SwapToken } from './swap-shared.js';
|
|
4
|
+
/** What a host surface supplies to the swap views: a sheet passes its backend's list so drags hand off to the sheet, and turns off the panel's own height animation. */
|
|
5
|
+
export declare const SwapHostContext: React.Context<{
|
|
6
|
+
listComponent?: React.ComponentType<FlatListProps<SwapToken>>;
|
|
7
|
+
animateHeight?: boolean;
|
|
8
|
+
}>;
|
|
9
|
+
/** One arrow, top to bottom: a one-way panel. */
|
|
10
|
+
export declare function DownIcon({ color, size }: {
|
|
11
|
+
color: string;
|
|
12
|
+
size?: number;
|
|
13
|
+
}): React.JSX.Element;
|
|
14
|
+
/** Two opposed arrows: exchange the pair, or switch the denomination. */
|
|
15
|
+
export declare function FlipIcon({ color, size }: {
|
|
16
|
+
color: string;
|
|
17
|
+
size?: number;
|
|
18
|
+
}): React.JSX.Element;
|
|
19
|
+
export declare function MenuIcon({ color }: {
|
|
20
|
+
color: string;
|
|
21
|
+
}): React.JSX.Element;
|
|
22
|
+
/** An open ring with an arrowhead at its gap. */
|
|
23
|
+
export declare function ResetIcon({ color }: {
|
|
24
|
+
color: string;
|
|
25
|
+
}): React.JSX.Element;
|
|
26
|
+
/** Keypad delete: a key outline holding an x. */
|
|
27
|
+
export declare function DeleteIcon({ color }: {
|
|
28
|
+
color: string;
|
|
29
|
+
}): React.JSX.Element;
|
|
30
|
+
/** Four viewfinder corners. */
|
|
31
|
+
export declare function ScanIcon({ color }: {
|
|
32
|
+
color: string;
|
|
33
|
+
}): React.JSX.Element;
|
|
34
|
+
/** A wallet: a rounded body with a clasp on the trailing edge. */
|
|
35
|
+
export declare function WalletIcon({ color }: {
|
|
36
|
+
color: string;
|
|
37
|
+
}): React.JSX.Element;
|
|
38
|
+
/** A padlock: a shackle over a body. */
|
|
39
|
+
export declare function LockIcon({ color }: {
|
|
40
|
+
color: string;
|
|
41
|
+
}): React.JSX.Element;
|
|
42
|
+
/** A doorway with an arrow leaving it. */
|
|
43
|
+
export declare function LeaveIcon({ color }: {
|
|
44
|
+
color: string;
|
|
45
|
+
}): React.JSX.Element;
|
|
46
|
+
/** Circular artwork from a URL, or the symbol's first letters when the host has none. */
|
|
47
|
+
export declare function TokenArt({ uri, symbol, size, ring }: {
|
|
48
|
+
uri?: string;
|
|
49
|
+
symbol?: string;
|
|
50
|
+
size: number;
|
|
51
|
+
ring?: string;
|
|
52
|
+
}): React.JSX.Element;
|
|
53
|
+
/** Token artwork with its chain badge. */
|
|
54
|
+
export declare function TokenIcon({ token, size }: {
|
|
55
|
+
token: SwapToken;
|
|
56
|
+
size?: number;
|
|
57
|
+
}): React.JSX.Element;
|
|
58
|
+
/** Small capsule action that sits on a card: Max, Test, the slippage setting and the denomination switch. 32dp tall with a 44dp target. */
|
|
59
|
+
export declare function SwapCapsule({ children, onPress, label, testID }: {
|
|
60
|
+
children: React.ReactNode;
|
|
61
|
+
onPress: () => void;
|
|
62
|
+
label?: string;
|
|
63
|
+
testID?: string;
|
|
64
|
+
}): React.JSX.Element;
|
|
65
|
+
/** Numeric keypad: a 3 by 4 grid of 56dp keys. Emits `0`–`9`, `.` or `back`. */
|
|
66
|
+
export declare function SwapKeypad({ onKey, label, deleteLabel }: {
|
|
67
|
+
onKey: (key: string) => void;
|
|
68
|
+
label: string;
|
|
69
|
+
deleteLabel: string;
|
|
70
|
+
}): React.JSX.Element;
|
|
71
|
+
/** A view's heading: the title, with Back before it when the view can be left. Shared by the pushed views and a titled main view. */
|
|
72
|
+
export declare function SwapHeader({ title, onBack }: {
|
|
73
|
+
title: string;
|
|
74
|
+
onBack?: () => void;
|
|
75
|
+
}): React.JSX.Element;
|
|
76
|
+
/** A small chevron: down marks a side that can be changed, trailing marks a card that opens something. Mirrors in RTL. */
|
|
77
|
+
export declare function SmallChevron({ color, trailing }: {
|
|
78
|
+
color: string;
|
|
79
|
+
trailing?: boolean;
|
|
80
|
+
}): React.JSX.Element;
|
|
81
|
+
/** A pushed view inside the panel: Back and the view title, then the body. There is no close control; Back pops. */
|
|
82
|
+
export declare function SwapSubview({ title, onBack, testID, children }: React.PropsWithChildren<{
|
|
83
|
+
title: string;
|
|
84
|
+
onBack: () => void;
|
|
85
|
+
testID?: string;
|
|
86
|
+
}>): React.JSX.Element;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { createContext } from 'react';
|
|
3
|
+
import { Image, Pressable, View } from 'react-native';
|
|
4
|
+
import { useColors, useFills, useHIG, useMessages } from './provider.js';
|
|
5
|
+
import { Heading, Text } from './surface.js';
|
|
6
|
+
import { IconButton } from './button.js';
|
|
7
|
+
import { ChevronIcon } from './icons.js';
|
|
8
|
+
/** What a host surface supplies to the swap views: a sheet passes its backend's list so drags hand off to the sheet, and turns off the panel's own height animation. */
|
|
9
|
+
export const SwapHostContext = createContext({});
|
|
10
|
+
// Outline symbols drawn from plain views, like ./icons: decorative, so the control that holds them carries the name.
|
|
11
|
+
const hidden = { accessible: false, accessibilityElementsHidden: true, importantForAccessibility: 'no-hide-descendants', pointerEvents: 'none' };
|
|
12
|
+
const stroke = 1.75;
|
|
13
|
+
const centre = { alignItems: 'center', justifyContent: 'center' };
|
|
14
|
+
const crossAngles = ['45deg', '-45deg'];
|
|
15
|
+
function Arrow({ color, down, height }) { return _jsxs(View, { style: { width: 7, height, alignItems: 'center', transform: down ? [{ rotate: '180deg' }] : undefined }, children: [_jsx(View, { style: { position: 'absolute', top: 1, width: 5, height: 5, borderLeftWidth: stroke, borderTopWidth: stroke, borderColor: color, transform: [{ rotate: '45deg' }] } }), _jsx(View, { style: { width: stroke, height, borderRadius: stroke, backgroundColor: color } })] }); }
|
|
16
|
+
/** One arrow, top to bottom: a one-way panel. */
|
|
17
|
+
export function DownIcon({ color, size = 16 }) { return _jsx(View, { ...hidden, style: { width: size, height: size, ...centre }, children: _jsx(Arrow, { color: color, down: true, height: size - 3 }) }); }
|
|
18
|
+
/** Two opposed arrows: exchange the pair, or switch the denomination. */
|
|
19
|
+
export function FlipIcon({ color, size = 16 }) { return _jsxs(View, { ...hidden, style: { width: size, height: size, flexDirection: 'row', gap: 1, ...centre }, children: [_jsx(Arrow, { color: color, down: true, height: size - 3 }), _jsx(Arrow, { color: color, height: size - 3 })] }); }
|
|
20
|
+
export function MenuIcon({ color }) { return _jsx(View, { ...hidden, style: { width: 20, height: 20, gap: 3.5, ...centre }, children: [0, 1, 2].map(index => _jsx(View, { style: { width: 14, height: stroke, borderRadius: stroke, backgroundColor: color } }, index)) }); }
|
|
21
|
+
/** An open ring with an arrowhead at its gap. */
|
|
22
|
+
export function ResetIcon({ color }) { return _jsxs(View, { ...hidden, style: { width: 20, height: 20, ...centre }, children: [_jsx(View, { style: { width: 14, height: 14, borderRadius: 7, borderWidth: stroke, borderColor: color, borderTopColor: 'transparent', transform: [{ rotate: '-45deg' }] } }), _jsx(View, { style: { position: 'absolute', left: 2, top: 3, width: 5, height: 5, borderLeftWidth: stroke, borderBottomWidth: stroke, borderColor: color } })] }); }
|
|
23
|
+
/** Keypad delete: a key outline holding an x. */
|
|
24
|
+
export function DeleteIcon({ color }) { return _jsx(View, { ...hidden, style: { width: 26, height: 18, borderRadius: 5, borderWidth: stroke, borderColor: color, ...centre }, children: crossAngles.map(angle => _jsx(View, { style: { position: 'absolute', width: 9, height: stroke, borderRadius: stroke, backgroundColor: color, transform: [{ rotate: angle }] } }, angle)) }); }
|
|
25
|
+
/** Four viewfinder corners. */
|
|
26
|
+
export function ScanIcon({ color }) { const corner = (style) => _jsx(View, { style: [{ position: 'absolute', width: 6, height: 6, borderColor: color }, style] }); return _jsxs(View, { ...hidden, style: { width: 18, height: 18 }, children: [corner({ left: 0, top: 0, borderLeftWidth: stroke, borderTopWidth: stroke, borderTopLeftRadius: 3 }), corner({ right: 0, top: 0, borderRightWidth: stroke, borderTopWidth: stroke, borderTopRightRadius: 3 }), corner({ left: 0, bottom: 0, borderLeftWidth: stroke, borderBottomWidth: stroke, borderBottomLeftRadius: 3 }), corner({ right: 0, bottom: 0, borderRightWidth: stroke, borderBottomWidth: stroke, borderBottomRightRadius: 3 }), _jsx(View, { style: { position: 'absolute', left: 0, right: 0, top: 8, height: stroke, borderRadius: stroke, backgroundColor: color } })] }); }
|
|
27
|
+
/** A wallet: a rounded body with a clasp on the trailing edge. */
|
|
28
|
+
export function WalletIcon({ color }) { return _jsx(View, { ...hidden, style: { width: 20, height: 20, ...centre }, children: _jsx(View, { style: { width: 16, height: 12, borderRadius: 3, borderWidth: stroke, borderColor: color, alignItems: 'flex-end', justifyContent: 'center' }, children: _jsx(View, { style: { width: 5, height: 4, borderTopLeftRadius: 2, borderBottomLeftRadius: 2, borderWidth: stroke, borderRightWidth: 0, borderColor: color, marginEnd: -stroke } }) }) }); }
|
|
29
|
+
/** A padlock: a shackle over a body. */
|
|
30
|
+
export function LockIcon({ color }) { return _jsxs(View, { ...hidden, style: { width: 16, height: 16, alignItems: 'center', justifyContent: 'flex-end' }, children: [_jsx(View, { style: { width: 8, height: 7, borderTopLeftRadius: 4, borderTopRightRadius: 4, borderWidth: stroke, borderBottomWidth: 0, borderColor: color } }), _jsx(View, { style: { width: 12, height: 8, borderRadius: 2, borderWidth: stroke, borderColor: color } })] }); }
|
|
31
|
+
/** A doorway with an arrow leaving it. */
|
|
32
|
+
export function LeaveIcon({ color }) { return _jsxs(View, { ...hidden, style: { width: 20, height: 20, flexDirection: 'row', alignItems: 'center' }, children: [_jsx(View, { style: { width: 8, height: 14, borderWidth: stroke, borderRightWidth: 0, borderColor: color, borderTopLeftRadius: 3, borderBottomLeftRadius: 3 } }), _jsx(View, { style: { width: 9, height: stroke, borderRadius: stroke, backgroundColor: color, marginStart: -2 } }), _jsx(View, { style: { width: 5, height: 5, borderRightWidth: stroke, borderTopWidth: stroke, borderColor: color, marginStart: -5, transform: [{ rotate: '45deg' }] } })] }); }
|
|
33
|
+
/** Circular artwork from a URL, or the symbol's first letters when the host has none. */
|
|
34
|
+
export function TokenArt({ uri, symbol, size, ring }) {
|
|
35
|
+
const colors = useColors();
|
|
36
|
+
const shape = { width: size, height: size, borderRadius: size / 2, backgroundColor: colors.nested, ...(ring ? { borderWidth: 2, borderColor: ring } : {}) };
|
|
37
|
+
return uri ? _jsx(Image, { accessible: false, accessibilityIgnoresInvertColors: true, source: { uri }, style: shape }) : _jsx(View, { style: [shape, centre], children: symbol ? _jsx(Text, { accessible: false, maxFontSizeMultiplier: 1, style: { fontSize: size >= 40 ? 12 : 9, lineHeight: size >= 40 ? 16 : 12, fontWeight: '600', color: colors.muted }, children: symbol.slice(0, 3) }) : null });
|
|
38
|
+
}
|
|
39
|
+
/** Token artwork with its chain badge. */
|
|
40
|
+
export function TokenIcon({ token, size = 44 }) { const colors = useColors(); return _jsxs(View, { ...hidden, style: { width: size, height: size }, children: [_jsx(TokenArt, { uri: token.icon, symbol: token.symbol, size: size }), token.chainIcon ? _jsx(View, { style: { position: 'absolute', end: 0, bottom: 0 }, children: _jsx(TokenArt, { uri: token.chainIcon, size: 18, ring: colors.overlay }) }) : null] }); }
|
|
41
|
+
/** Small capsule action that sits on a card: Max, Test, the slippage setting and the denomination switch. 32dp tall with a 44dp target. */
|
|
42
|
+
export function SwapCapsule({ children, onPress, label, testID }) {
|
|
43
|
+
const fills = useFills();
|
|
44
|
+
const { direction } = useHIG();
|
|
45
|
+
return _jsx(Pressable, { accessibilityRole: 'button', accessibilityLabel: label, testID: testID, onPress: onPress, hitSlop: { top: 6, bottom: 6 }, style: ({ pressed }) => ({ direction, flexDirection: 'row', alignItems: 'center', gap: 6, minHeight: 32, paddingVertical: 4, paddingHorizontal: 12, borderRadius: 9999, backgroundColor: pressed ? fills.raised : fills.well }), children: children });
|
|
46
|
+
}
|
|
47
|
+
const keys = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '0', 'back'];
|
|
48
|
+
/** Numeric keypad: a 3 by 4 grid of 56dp keys. Emits `0`–`9`, `.` or `back`. */
|
|
49
|
+
export function SwapKeypad({ onKey, label, deleteLabel }) {
|
|
50
|
+
const colors = useColors();
|
|
51
|
+
// Always left to right: a keypad keeps its telephone layout in RTL locales.
|
|
52
|
+
return _jsx(View, { accessibilityLabel: label, testID: 'swap-keypad', style: { direction: 'ltr', flexDirection: 'row', flexWrap: 'wrap', paddingTop: 8 }, children: keys.map(key => _jsx(Pressable, { accessibilityRole: 'keyboardkey', accessibilityLabel: key === 'back' ? deleteLabel : key, testID: `swap-key-${key}`, onPress: () => onKey(key), style: ({ pressed }) => ({ width: '33.333%', height: 56, borderRadius: 16, backgroundColor: pressed ? colors.nested : 'transparent', ...centre }), children: key === 'back' ? _jsx(DeleteIcon, { color: colors.text }) : _jsx(Text, { accessible: false, style: { fontSize: 24, lineHeight: 30, fontWeight: '600' }, children: key }) }, key)) });
|
|
53
|
+
}
|
|
54
|
+
/** A view's heading: the title, with Back before it when the view can be left. Shared by the pushed views and a titled main view. */
|
|
55
|
+
export function SwapHeader({ title, onBack }) {
|
|
56
|
+
const colors = useColors();
|
|
57
|
+
const t = useMessages();
|
|
58
|
+
const { direction } = useHIG();
|
|
59
|
+
return _jsxs(View, { testID: 'swap-header', style: { flexDirection: 'row', alignItems: 'center', gap: 4, minHeight: 44, paddingStart: onBack ? 0 : 4 }, children: [onBack ? _jsx(IconButton, { intent: 'ghost', label: t.back, suppressFeedback: true, testID: 'swap-back', onPress: onBack, children: _jsx(View, { style: { transform: [{ rotate: direction === 'rtl' ? '-90deg' : '90deg' }] }, children: _jsx(ChevronIcon, { color: colors.text }) }) }) : null, _jsx(Heading, { style: { fontWeight: '700', flexShrink: 1 }, children: title })] });
|
|
60
|
+
}
|
|
61
|
+
/** A small chevron: down marks a side that can be changed, trailing marks a card that opens something. Mirrors in RTL. */
|
|
62
|
+
export function SmallChevron({ color, trailing }) { const { direction } = useHIG(); return _jsx(View, { ...hidden, style: { width: 16, height: 16, ...centre, transform: trailing ? [{ rotate: direction === 'rtl' ? '90deg' : '-90deg' }] : undefined }, children: _jsx(View, { style: { width: 7, height: 7, borderRightWidth: stroke, borderBottomWidth: stroke, borderColor: color, transform: [{ translateY: -1.5 }, { rotate: '45deg' }] } }) }); }
|
|
63
|
+
/** A pushed view inside the panel: Back and the view title, then the body. There is no close control; Back pops. */
|
|
64
|
+
export function SwapSubview({ title, onBack, testID, children }) {
|
|
65
|
+
return _jsxs(View, { testID: testID, style: { gap: 16 }, children: [_jsx(SwapHeader, { title: title, onBack: onBack }), children] });
|
|
66
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/** An asset on one chain. The host supplies every value; HIG formats and lays it out. */
|
|
2
|
+
export interface SwapToken {
|
|
3
|
+
/** Unique per asset and chain, for example `${chainId}-${address}`. */
|
|
4
|
+
id: string;
|
|
5
|
+
symbol: string;
|
|
6
|
+
name: string;
|
|
7
|
+
/** Chain display name and identity. Two tokens on the same `chainId` swap; different chains bridge. */
|
|
8
|
+
chain: string;
|
|
9
|
+
chainId: string | number;
|
|
10
|
+
/** Image URLs. A missing token icon falls back to the symbol's first letters. */
|
|
11
|
+
icon?: string;
|
|
12
|
+
chainIcon?: string;
|
|
13
|
+
/** Holdings in token units, when the host knows them. Enables Max, Test and the balance check. */
|
|
14
|
+
balance?: number | null;
|
|
15
|
+
/** USD per unit, when the host knows it. Enables the dollar and unit conversion. */
|
|
16
|
+
price?: number | null;
|
|
17
|
+
/** Presents the side as an account rather than a token: this line (for example `$22.72 available`) sits under the name, the artwork is a single disc, and only Max is offered. */
|
|
18
|
+
caption?: string;
|
|
19
|
+
/** Extra text matched by the picker search, such as the contract address. */
|
|
20
|
+
keywords?: string;
|
|
21
|
+
}
|
|
22
|
+
export interface SwapChain {
|
|
23
|
+
chainId: string | number;
|
|
24
|
+
name: string;
|
|
25
|
+
icon?: string;
|
|
26
|
+
}
|
|
27
|
+
/** Same asset: send. Different asset on one chain: swap. Different asset across chains: bridge. */
|
|
28
|
+
export type SwapAction = 'send' | 'swap' | 'bridge';
|
|
29
|
+
export type SwapDenomination = 'usd' | 'token';
|
|
30
|
+
/** The panel's view stack: the main form, a side's token picker, or a settings view. Exactly one shows at a time. */
|
|
31
|
+
export type SwapView = 'main' | 'from' | 'to' | 'settings' | 'slippage' | 'fees';
|
|
32
|
+
/** Where Back leads: slippage and fees return to settings, everything else to the main form. */
|
|
33
|
+
export declare const swapParentView: (view: SwapView) => SwapView;
|
|
34
|
+
export interface SwapWallet {
|
|
35
|
+
connected: boolean;
|
|
36
|
+
address?: string | null;
|
|
37
|
+
}
|
|
38
|
+
export interface SwapFee {
|
|
39
|
+
label: string;
|
|
40
|
+
usd: number;
|
|
41
|
+
}
|
|
42
|
+
export declare const slippagePresets: readonly [0.001, 0.005, 0.01];
|
|
43
|
+
export declare function swapAction(from: SwapToken | null, to: SwapToken | null): SwapAction;
|
|
44
|
+
/** A clean numeric string: no trailing zeros, at most `digits` decimals. */
|
|
45
|
+
export declare const trimNumber: (value: number, digits?: number) => string;
|
|
46
|
+
/** Keeps digits and one decimal point, and strips leading zeros; never returns an empty string. */
|
|
47
|
+
export declare function sanitizeAmount(raw: string): string;
|
|
48
|
+
/** Token units shown in the active denomination. Dollars floor to the cent so Max never resolves to more than the balance. */
|
|
49
|
+
export declare function unitsToEntry(units: number, mode: SwapDenomination, price: number): string;
|
|
50
|
+
/** Applies one keypad key (`0`–`9`, `.`, `back`) to an amount string. Dollars stop at two decimals. */
|
|
51
|
+
export declare function applyAmountKey(current: string, key: string, mode: SwapDenomination): string;
|
|
52
|
+
/** Case-insensitive substring or in-order subsequence match. */
|
|
53
|
+
export declare function fuzzyMatch(query: string, text: string): boolean;
|
|
54
|
+
/** A slippage fraction as a percent string: 0.005 is `0.5%`. */
|
|
55
|
+
export declare const formatPercent: (fraction: number) => string;
|
|
56
|
+
/** A small trial amount: 1% of holdings, capped at about one dollar's worth. */
|
|
57
|
+
export declare function testAmount(token: SwapToken): number;
|
|
58
|
+
export declare const shortAddress: (address: string) => string;
|
|
59
|
+
/** A stable flat colour for a wallet address, so the same wallet always looks the same. */
|
|
60
|
+
export declare function addressColor(address: string): string;
|
|
61
|
+
/** Picker rows: hot tokens lead the unfiltered list; any search or chain filter gives one flat list. */
|
|
62
|
+
export declare function pickerRows(tokens: readonly SwapToken[], query: string, chainId: string | number | null, hotTokenIds?: readonly string[]): {
|
|
63
|
+
rows: SwapToken[];
|
|
64
|
+
hotCount: number;
|
|
65
|
+
};
|