Skip to content

Commit dbba64d

Browse files
committed
add JSdoc, export component
1 parent 0b70ce8 commit dbba64d

File tree

4 files changed

+132
-3
lines changed

4 files changed

+132
-3
lines changed

packages/thirdweb/src/exports/react.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ export {
141141
CheckoutWidget,
142142
type CheckoutWidgetProps,
143143
} from "../react/web/ui/Bridge/CheckoutWidget.js";
144+
export {
145+
SwapWidget,
146+
type SwapWidgetProps,
147+
} from "../react/web/ui/Bridge/swap-widget/SwapWidget.js";
144148
export {
145149
TransactionWidget,
146150
type TransactionWidgetProps,

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/SwapWidget.tsx

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import type { CompletedStatusResult } from "../../../../core/hooks/useStepExecut
1616
import { webWindowAdapter } from "../../../adapters/WindowAdapter.js";
1717
import { EmbedContainer } from "../../ConnectWallet/Modal/ConnectEmbed.js";
1818
import { DynamicHeight } from "../../components/DynamicHeight.js";
19-
import type { LocaleId } from "../../types.js";
2019
import { ErrorBanner } from "../ErrorBanner.js";
2120
import { PaymentDetails } from "../payment-details/PaymentDetails.js";
2221
import { SuccessScreen } from "../payment-success/SuccessScreen.js";
@@ -27,18 +26,120 @@ import { SwapUI } from "./swap-ui.js";
2726
import type { SwapWidgetConnectOptions } from "./types.js";
2827
import { useBridgeChains } from "./use-bridge-chains.js";
2928

30-
type SwapWidgetProps = {
29+
export type SwapWidgetProps = {
30+
/**
31+
* A client is the entry point to the thirdweb SDK.
32+
* It is required for all other actions.
33+
* You can create a client using the `createThirdwebClient` function. Refer to the [Creating a Client](https://portal.thirdweb.com/typescript/v5/client) documentation for more information.
34+
*
35+
* You must provide a `clientId` or `secretKey` in order to initialize a client. Pass `clientId` if you want for client-side usage and `secretKey` for server-side usage.
36+
*
37+
* ```tsx
38+
* import { createThirdwebClient } from "thirdweb";
39+
*
40+
* const client = createThirdwebClient({
41+
* clientId: "<your_client_id>",
42+
* })
43+
* ```
44+
*/
3145
client: ThirdwebClient;
46+
/**
47+
* Set the theme for the `SwapWidget` component. By default it is set to `"dark"`
48+
*
49+
* theme can be set to either `"dark"`, `"light"` or a custom theme object.
50+
* You can also import [`lightTheme`](https://portal.thirdweb.com/references/typescript/v5/lightTheme)
51+
* or [`darkTheme`](https://portal.thirdweb.com/references/typescript/v5/darkTheme)
52+
* functions from `thirdweb/react` to use the default themes as base and overrides parts of it.
53+
* @example
54+
* ```ts
55+
* import { lightTheme } from "thirdweb/react";
56+
*
57+
* const customTheme = lightTheme({
58+
* colors: {
59+
* modalBg: 'red'
60+
* }
61+
* })
62+
*
63+
* function Example() {
64+
* return <SwapWidget client={client} theme={customTheme} />
65+
* }
66+
* ```
67+
*/
3268
theme?: "light" | "dark" | Theme;
3369
className?: string;
34-
locale?: LocaleId;
70+
/**
71+
* The currency to use for the payment.
72+
* @default "USD"
73+
*/
3574
currency?: SupportedFiatCurrency;
3675
style?: React.CSSProperties;
76+
/**
77+
* Whether to show thirdweb branding in the widget.
78+
* @default true
79+
*/
3780
showThirdwebBranding?: boolean;
81+
/**
82+
* Callback to be called when the swap is successful.
83+
*/
3884
onSuccess?: () => void;
85+
/**
86+
* Callback to be called when user encounters an error when swapping.
87+
*/
3988
onError?: (error: Error) => void;
89+
/**
90+
* Callback to be called when the user cancels the purchase.
91+
*/
4092
onCancel?: () => void;
4193
connectOptions?: SwapWidgetConnectOptions;
94+
/**
95+
* The prefill Buy and/or Sell tokens for the swap widget. If `tokenAddress` is not provided, the native token will be used
96+
*
97+
* @example
98+
*
99+
* ### Set an ERC20 token as the buy token
100+
* ```ts
101+
* <SwapWidget client={client} prefill={{
102+
* buyToken: {
103+
* chainId: 8453,
104+
* tokenAddress: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
105+
* },
106+
* }} />
107+
* ```
108+
*
109+
* ### Set a native token as the sell token
110+
*
111+
* ```ts
112+
* <SwapWidget client={client} prefill={{
113+
* sellToken: {
114+
* chainId: 8453,
115+
* },
116+
* }} />
117+
* ```
118+
*
119+
* ### Set 0.1 Base USDC as the buy token
120+
* ```ts
121+
* <SwapWidget client={client} prefill={{
122+
* buyToken: {
123+
* chainId: 8453,
124+
* amount: "0.1",
125+
* tokenAddress: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
126+
* },
127+
* }} />
128+
* ```
129+
*
130+
* ### Set Base USDC as the buy token and Base native token as the sell token
131+
* ```ts
132+
* <SwapWidget client={client} prefill={{
133+
* buyToken: {
134+
* chainId: 8453,
135+
* tokenAddress: "0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
136+
* },
137+
* sellToken: {
138+
* chainId: 8453,
139+
* },
140+
* }} />
141+
* ```
142+
*/
42143
prefill?: {
43144
buyToken?: {
44145
tokenAddress?: string;
@@ -148,6 +249,11 @@ function SwapWidgetContent(props: SwapWidgetProps) {
148249
if (screen.id === "1:swap-ui" || !activeWalletInfo) {
149250
return (
150251
<SwapUI
252+
showThirdwebBranding={
253+
props.showThirdwebBranding === undefined
254+
? true
255+
: props.showThirdwebBranding
256+
}
151257
client={props.client}
152258
theme={props.theme || "dark"}
153259
connectOptions={props.connectOptions}

packages/thirdweb/src/react/web/ui/Bridge/swap-widget/swap-ui.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { useWalletBalance } from "../../../../core/hooks/others/useWalletBalance.js";
2727
import { ConnectButton } from "../../ConnectWallet/ConnectButton.js";
2828
import { ArrowUpDownIcon } from "../../ConnectWallet/icons/ArrowUpDownIcon.js";
29+
import { PoweredByThirdweb } from "../../ConnectWallet/PoweredByTW.js";
2930
import { Container } from "../../components/basic.js";
3031
import { Button } from "../../components/buttons.js";
3132
import { Input } from "../../components/formElements.js";
@@ -50,6 +51,7 @@ type SwapUIProps = {
5051
theme: Theme | "light" | "dark";
5152
connectOptions: SwapWidgetConnectOptions | undefined;
5253
currency: SupportedFiatCurrency;
54+
showThirdwebBranding: boolean;
5355
onSwap: (
5456
quote: Buy.quote.Result | Sell.quote.Result,
5557
selection: {
@@ -449,6 +451,13 @@ export function SwapUIBase(
449451
Swap
450452
</Button>
451453
)}
454+
455+
{props.showThirdwebBranding ? (
456+
<div>
457+
<Spacer y="md" />
458+
<PoweredByThirdweb />
459+
</div>
460+
) : null}
452461
</Container>
453462
);
454463
}

packages/thirdweb/src/stories/Bridge/Swap/SwapWidget.stories.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ export function LightMode() {
2323
return <SwapWidget client={storyClient} currency="JPY" theme="light" />;
2424
}
2525

26+
export function NoThirdwebBranding() {
27+
return (
28+
<SwapWidget
29+
client={storyClient}
30+
currency="JPY"
31+
showThirdwebBranding={false}
32+
/>
33+
);
34+
}
35+
2636
export function CustomTheme() {
2737
return (
2838
<SwapWidget

0 commit comments

Comments
 (0)