Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dirty-breads-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Add `amountEditable` and `tokenEditable` props on `BuyWidget` component to disable token selection and token amount editing
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export function BuyWidgetEmbed({
buttonLabel,
receiverAddress,
country,
amountEditable,
tokenEditable,
}: {
chainId?: number;
tokenAddress?: Address;
Expand All @@ -34,6 +36,8 @@ export function BuyWidgetEmbed({
buttonLabel?: string;
receiverAddress?: Address;
country?: string;
amountEditable?: boolean;
tokenEditable?: boolean;
}) {
const client = useMemo(
() =>
Expand Down Expand Up @@ -68,6 +72,8 @@ export function BuyWidgetEmbed({
buttonLabel={buttonLabel}
receiverAddress={receiverAddress}
country={country}
amountEditable={amountEditable}
tokenEditable={tokenEditable}
onSuccess={() => {
sendMessageToParent({
source: "buy-widget",
Expand Down
10 changes: 10 additions & 0 deletions apps/dashboard/src/app/bridge/buy-widget/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ export default async function Page(props: {
const receiverAddress = parseQueryParams(searchParams.receiver, onlyAddress);
const country = parseQueryParams(searchParams.country, (v) => v);

// Editable params
const amountEditable = parseQueryParams(searchParams.amountEditable, (v) =>
v === "false" ? false : undefined,
);
const tokenEditable = parseQueryParams(searchParams.tokenEditable, (v) =>
v === "false" ? false : undefined,
);

return (
<BridgeProvidersLite forcedTheme={theme}>
<div className="flex min-h-screen items-center justify-center bg-background px-4 py-8">
Expand All @@ -90,6 +98,8 @@ export default async function Page(props: {
buttonLabel={buttonLabel}
receiverAddress={receiverAddress}
country={country}
amountEditable={amountEditable}
tokenEditable={tokenEditable}
/>
</div>
</BridgeProvidersLite>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const defaultOptions: BridgeComponentsPlaygroundOptions = {
transactionData: "",
currency: "USD",
showThirdwebBranding: true,
amountEditable: true,
tokenEditable: true,
},
theme: {
darkColorOverrides: {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const defaultOptions: BridgeComponentsPlaygroundOptions = {
receiverAddress: undefined,
currency: "USD",
showThirdwebBranding: true,
amountEditable: true,
tokenEditable: true,
},
theme: {
darkColorOverrides: {},
Expand Down
8 changes: 8 additions & 0 deletions apps/playground-web/src/app/bridge/components/CodeGen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ tokenId: 2n,
? quotes(options.payOptions.buttonLabel)
: undefined,
transaction: transaction,
amountEditable:
widget === "buy" && options.payOptions.amountEditable === false
? false
: undefined,
tokenEditable:
widget === "buy" && options.payOptions.tokenEditable === false
? false
: undefined,
};

return `\
Expand Down
40 changes: 40 additions & 0 deletions apps/playground-web/src/app/bridge/components/LeftSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,46 @@ export function LeftSection(props: {
</div>
</div>
</div>

{/* Editability Options */}
<div className="flex flex-col gap-3">
<Label>Editability Options</Label>
<div className="flex gap-4">
<div className="flex items-center space-x-2">
<Checkbox
checked={payOptions.tokenEditable}
id="token-editable"
onCheckedChange={(checked) => {
setOptions((v) => ({
...v,
payOptions: {
...v.payOptions,
tokenEditable: checked === true,
},
}));
}}
/>
<Label htmlFor="token-editable">Token Editable</Label>
</div>

<div className="flex items-center space-x-2">
<Checkbox
checked={payOptions.amountEditable}
id="amount-editable"
onCheckedChange={(checked) => {
setOptions((v) => ({
...v,
payOptions: {
...v.payOptions,
amountEditable: checked === true,
},
}));
}}
/>
<Label htmlFor="amount-editable">Amount Editable</Label>
</div>
</div>
</div>
</div>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ export function RightSection(props: {
currency={props.options.payOptions.currency}
showThirdwebBranding={props.options.payOptions.showThirdwebBranding}
receiverAddress={props.options.payOptions.receiverAddress}
amountEditable={props.options.payOptions.amountEditable}
tokenEditable={props.options.payOptions.tokenEditable}
key={JSON.stringify({
amount: props.options.payOptions.buyTokenAmount,
chain: props.options.payOptions.buyTokenChain,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,14 @@ export function buildBuyIframeUrl(options: BridgeComponentsPlaygroundOptions) {
);
}

// Editability options
if (options.payOptions.amountEditable === false) {
url.searchParams.set("amountEditable", "false");
}

if (options.payOptions.tokenEditable === false) {
url.searchParams.set("tokenEditable", "false");
}

return url.toString();
}
4 changes: 4 additions & 0 deletions apps/playground-web/src/app/bridge/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,9 @@ export type BridgeComponentsPlaygroundOptions = {
currency?: SupportedFiatCurrency;

showThirdwebBranding: boolean;

// Editability options
amountEditable: boolean;
tokenEditable: boolean;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const defaultOptions: BridgeComponentsPlaygroundOptions = {
currency: "USD",
showThirdwebBranding: true,
receiverAddress: undefined,
amountEditable: true,
tokenEditable: true,
},
theme: {
darkColorOverrides: {},
Expand Down
27 changes: 27 additions & 0 deletions apps/portal/src/app/bridge/buy-widget/iframe/page.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,33 @@ By default, the widget displays thirdweb branding at the bottom. You can hide th

<IframeCodePreview src="https://thirdweb.com/bridge/buy-widget?showThirdwebBranding=false" />

### Editability Options

You can control whether users can edit certain fields in the widget.


<Details summary="Token Selection">

By default, users can change the token they want to purchase.

You can disable this by setting the `tokenEditable` query parameter to `false`.

<IframeCodePreview src="https://thirdweb.com/bridge/buy-widget?chain=8453&tokenEditable=false" />

</Details>

<Details summary="Token Amount">

By default, users can edit the purchase amount.

You can disable this by setting the `amountEditable` query parameter to `false`.


<IframeCodePreview src="https://thirdweb.com/bridge/buy-widget?chain=8453&amount=0.01&amountEditable=false" />

</Details>


## Listening for Events

The buy widget iframe sends events to the parent window using `postMessage` when a purchase succeeds or fails.
Expand Down
18 changes: 18 additions & 0 deletions packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,16 @@
* Callback to be called when the user disconnects the active wallet.
*/
onDisconnect?: () => void;

/**
* By default the token amount is editable. Set this to false to disable editing the token amount
*/
amountEditable?: boolean;

/**
* By default the token selection is editable. Set this to false to disable editing the token selection.
*/
tokenEditable?: boolean;
};

/**
Expand Down Expand Up @@ -490,6 +500,12 @@
};
});

const amountEditable =
props.amountEditable === undefined ? true : props.amountEditable;

Check warning on line 504 in packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx#L503-L504

Added lines #L503 - L504 were not covered by tests

const tokenEditable =
props.tokenEditable === undefined ? true : props.tokenEditable;

Check warning on line 507 in packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx#L506-L507

Added lines #L506 - L507 were not covered by tests

if (screen.id === "1:buy-ui") {
return (
<FundWallet
Expand Down Expand Up @@ -525,6 +541,8 @@
setSelectedToken={setSelectedToken}
amountSelection={amountSelection}
setAmountSelection={setAmountSelection}
amountEditable={amountEditable}
tokenEditable={tokenEditable}

Check warning on line 545 in packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/BuyWidget.tsx#L544-L545

Added lines #L544 - L545 were not covered by tests
/>
);
}
Expand Down
80 changes: 51 additions & 29 deletions packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,16 @@
description: string | undefined;
image: string | undefined;
};

/**
* Whether the user can edit the amount. Defaults to true.
*/
amountEditable: boolean;

/**
* Whether the user can edit the token selection. Defaults to true.
*/
tokenEditable: boolean;
};

export type SelectedToken =
Expand Down Expand Up @@ -264,6 +274,8 @@
setDetailsModalOpen(true);
}}
currency={props.currency}
amountEditable={props.amountEditable}
tokenEditable={props.tokenEditable}

Check warning on line 278 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L277-L278

Added lines #L277 - L278 were not covered by tests
/>

{receiver && isReceiverDifferentFromActiveWallet && (
Expand Down Expand Up @@ -415,6 +427,8 @@
};
onWalletClick: () => void;
presetOptions: [number, number, number];
amountEditable: boolean;
tokenEditable: boolean;
}) {
const theme = useCustomTheme();
const chainQuery = useBridgeChain({
Expand Down Expand Up @@ -467,6 +481,7 @@
client={props.client}
onSelectToken={props.onSelectToken}
chain={chain}
disabled={props.tokenEditable === false}

Check warning on line 484 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L484

Added line #L484 was not covered by tests
/>

<Container px="md" py="md">
Expand All @@ -479,6 +494,7 @@
value,
});
}}
disabled={props.amountEditable === false}

Check warning on line 497 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L497

Added line #L497 was not covered by tests
style={{
border: "none",
boxShadow: "none",
Expand Down Expand Up @@ -527,6 +543,7 @@
value,
});
}}
disabled={props.amountEditable === false}

Check warning on line 546 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L546

Added line #L546 was not covered by tests
style={{
border: "none",
boxShadow: "none",
Expand All @@ -541,36 +558,41 @@
)}
</div>

<Spacer y="md" />

{/* suggested amounts */}
<Container flex="row" gap="xxs">
{props.presetOptions.map((amount) => (
<Button
disabled={!props.selectedToken?.data}
key={amount}
onClick={() =>
props.setAmount({
type: "usd",
value: String(amount),
})
}
style={{
backgroundColor: "transparent",
color: theme.colors.secondaryText,
fontSize: fontSize.xs,
fontWeight: 400,
borderRadius: radius.full,
gap: "0.5px",
padding: `${spacing.xxs} ${spacing.sm}`,
}}
variant="outline"
>
<span>{getFiatSymbol(props.currency)}</span>
<span>{amount}</span>
</Button>
))}
</Container>
{props.amountEditable && (
<>
<Spacer y="md" />
<Container flex="row" gap="xxs">
{props.presetOptions.map((amount) => (
<Button
disabled={
!props.selectedToken?.data || props.amountEditable === false

Check warning on line 569 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L562-L569

Added lines #L562 - L569 were not covered by tests
}
key={amount}
onClick={() =>
props.setAmount({
type: "usd",
value: String(amount),
})

Check warning on line 576 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L571-L576

Added lines #L571 - L576 were not covered by tests
}
style={{
backgroundColor: "transparent",
color: theme.colors.secondaryText,
fontSize: fontSize.xs,
fontWeight: 400,
borderRadius: radius.full,
gap: "0.5px",
padding: `${spacing.xxs} ${spacing.sm}`,
}}
variant="outline"

Check warning on line 587 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L578-L587

Added lines #L578 - L587 were not covered by tests
>
<span>{getFiatSymbol(props.currency)}</span>
<span>{amount}</span>
</Button>
))}
</Container>
</>

Check warning on line 594 in packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/Bridge/FundWallet.tsx#L589-L594

Added lines #L589 - L594 were not covered by tests
)}
</Container>

{/* balance */}
Expand Down
Loading
Loading