Skip to content

Commit df64b2b

Browse files
feat: Add changes to Snaps Address UI component (#27798)
## **Description** Add changes to Snaps Address UI component to support more flexible use cases. This PR needs release of the `snaps-sdk` after merging: MetaMask/snaps#2833 [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/27798?quickstart=1) ## **Related issues** Fixes: MetaMask/snaps#2758 ## **Manual testing steps** 1. Use custom Snap build from template-snap-monorepo and add few varieties of address component to test new capabilities. 2. Make sure UI is looking good. 3. Make sure that there are no regressions. ## **Screenshots/Recordings** ### **Before** ![Screenshot 2024-10-11 at 18 12 06](https://github.com/user-attachments/assets/4d498bd6-0ef0-445c-90ab-40918e63b421) ### **After** ![Screenshot 2024-10-15 at 14 34 16](https://github.com/user-attachments/assets/792244b6-300d-45e2-b7e1-91bbcb93a9a6) ![Screenshot 2024-10-15 at 14 34 28](https://github.com/user-attachments/assets/9c6776e1-f6b6-4888-864b-b95621e83069) ![Screenshot 2024-10-15 at 14 34 47](https://github.com/user-attachments/assets/054a9127-ce9a-43d9-ab06-61c4099990a6) ![Screenshot 2024-10-15 at 14 35 10](https://github.com/user-attachments/assets/4c829486-2195-4f14-947e-cbc3572eb822) ### Snap code used for testing #### (_Might be useful for QA_) ```typescript export const onRpcRequest: OnRpcRequestHandler = async ({ request }) => { switch (request.method) { case 'hello': return snap.request({ method: 'snap_dialog', params: { type: 'confirmation', content: ( <Box> <Heading size="lg">Some unknown address</Heading> <Text>Default address</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" /> <Text>Custom address (displayName=true)</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" displayName={true} /> <Text>Custom address (avatar=false)</Text> <Address address="0x1234567890123456789012345678901234567890" avatar={false} /> <Text>Custom address (displayName=true, avatar=false)</Text> <Address address="0x1234567890123456789012345678901234567890" displayName={true} avatar={false} /> <Text>Custom address (truncate=false)</Text> <Address address="0x1234567890123456789012345678901234567890" truncate={false} /> <Text>Custom address (truncate=false, avatar=false)</Text> <Address address="0x1234567890123456789012345678901234567890" truncate={false} avatar={false} /> <Heading size="lg">My address</Heading> <Text>Default address</Text> <Address address="0xBc641D258aA0e90F70AaDbcC4dE2D58d88029713" /> <Text>Custom address (displayName=true)</Text> <Address address="0xBc641D258aA0e90F70AaDbcC4dE2D58d88029713" displayName={true} /> <Text>Custom address (avatar=false)</Text> <Address address="0xBc641D258aA0e90F70AaDbcC4dE2D58d88029713" avatar={false} /> <Text>Custom address (displayName=true, avatar=false)</Text> <Address address="0xBc641D258aA0e90F70AaDbcC4dE2D58d88029713" displayName={true} avatar={false} /> <Text>Custom address (truncate=false)</Text> <Address address="0xBc641D258aA0e90F70AaDbcC4dE2D58d88029713" truncate={false} /> <Text>Custom address (truncate=false, avatar=false)</Text> <Address address="0xBc641D258aA0e90F70AaDbcC4dE2D58d88029713" truncate={false} avatar={false} /> <Heading size="lg">Contact address</Heading> <Text>Default address</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" /> <Text>Custom address (displayName=true)</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" displayName={true} /> <Text>Custom address (avatar=false)</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" avatar={false} /> <Text>Custom address (displayName=true, avatar=false)</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" displayName={true} avatar={false} /> <Text>Custom address (truncate=false)</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" truncate={false} /> <Text>Custom address (truncate=false, avatar=false)</Text> <Address address="0x1acC6721CF7642Eb37651F8019AC9a785C064002" truncate={false} avatar={false} /> </Box> ), }, }); default: throw new Error('Method not found.'); } }; ``` ## **Pre-merge author checklist** - [ ] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [ ] I've completed the PR template to the best of my ability - [ ] I’ve included tests if applicable - [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [ ] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --------- Co-authored-by: Guillaume Roux <[email protected]>
1 parent 94895b1 commit df64b2b

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

ui/components/app/snaps/snap-ui-address/__snapshots__/snap-ui-address.test.tsx.snap

+13
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ exports[`SnapUIAddress renders Bitcoin address 1`] = `
4646
</div>
4747
<p
4848
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
49+
style="line-break: anywhere;"
4950
>
5051
128Lkh3...Mp8p6
5152
</p>
@@ -67,6 +68,7 @@ exports[`SnapUIAddress renders Bitcoin address with blockie 1`] = `
6768
/>
6869
<p
6970
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
71+
style="line-break: anywhere;"
7072
>
7173
128Lkh3...Mp8p6
7274
</p>
@@ -120,6 +122,7 @@ exports[`SnapUIAddress renders Cosmos address 1`] = `
120122
</div>
121123
<p
122124
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
125+
style="line-break: anywhere;"
123126
>
124127
cosmos1...6hdc0
125128
</p>
@@ -141,6 +144,7 @@ exports[`SnapUIAddress renders Cosmos address with blockie 1`] = `
141144
/>
142145
<p
143146
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
147+
style="line-break: anywhere;"
144148
>
145149
cosmos1...6hdc0
146150
</p>
@@ -194,6 +198,7 @@ exports[`SnapUIAddress renders Ethereum address 1`] = `
194198
</div>
195199
<p
196200
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
201+
style="line-break: anywhere;"
197202
>
198203
0xab16a...Bfcdb
199204
</p>
@@ -215,6 +220,7 @@ exports[`SnapUIAddress renders Ethereum address with blockie 1`] = `
215220
/>
216221
<p
217222
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
223+
style="line-break: anywhere;"
218224
>
219225
0xab16a...Bfcdb
220226
</p>
@@ -268,6 +274,7 @@ exports[`SnapUIAddress renders Hedera address 1`] = `
268274
</div>
269275
<p
270276
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
277+
style="line-break: anywhere;"
271278
>
272279
0.0.123...zbhlt
273280
</p>
@@ -289,6 +296,7 @@ exports[`SnapUIAddress renders Hedera address with blockie 1`] = `
289296
/>
290297
<p
291298
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
299+
style="line-break: anywhere;"
292300
>
293301
0.0.123...zbhlt
294302
</p>
@@ -342,6 +350,7 @@ exports[`SnapUIAddress renders Polkadot address 1`] = `
342350
</div>
343351
<p
344352
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
353+
style="line-break: anywhere;"
345354
>
346355
5hmuyxw...egmfy
347356
</p>
@@ -363,6 +372,7 @@ exports[`SnapUIAddress renders Polkadot address with blockie 1`] = `
363372
/>
364373
<p
365374
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
375+
style="line-break: anywhere;"
366376
>
367377
5hmuyxw...egmfy
368378
</p>
@@ -416,6 +426,7 @@ exports[`SnapUIAddress renders Starknet address 1`] = `
416426
</div>
417427
<p
418428
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
429+
style="line-break: anywhere;"
419430
>
420431
0x02dd1...0ab57
421432
</p>
@@ -437,6 +448,7 @@ exports[`SnapUIAddress renders Starknet address with blockie 1`] = `
437448
/>
438449
<p
439450
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
451+
style="line-break: anywhere;"
440452
>
441453
0x02dd1...0ab57
442454
</p>
@@ -490,6 +502,7 @@ exports[`SnapUIAddress renders legacy Ethereum address 1`] = `
490502
</div>
491503
<p
492504
class="mm-box mm-text mm-text--body-md mm-box--color-inherit"
505+
style="line-break: anywhere;"
493506
>
494507
0xab16a...Bfcdb
495508
</p>

ui/components/app/snaps/snap-ui-address/snap-ui-address.tsx

+25-10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
AlignItems,
1010
Display,
1111
TextColor,
12+
TextVariant,
1213
} from '../../../../helpers/constants/design-system';
1314
import { shortenAddress } from '../../../../helpers/utils/util';
1415
import { toChecksumHexAddress } from '../../../../../shared/modules/hexstring-utils';
@@ -20,11 +21,17 @@ export type SnapUIAddressProps = {
2021
address: string;
2122
// This is not currently exposed to Snaps.
2223
avatarSize?: 'xs' | 'sm' | 'md' | 'lg';
24+
truncate?: boolean;
25+
displayName?: boolean;
26+
avatar?: boolean;
2327
};
2428

2529
export const SnapUIAddress: React.FunctionComponent<SnapUIAddressProps> = ({
2630
address,
2731
avatarSize = 'md',
32+
truncate = true,
33+
displayName = false,
34+
avatar = true,
2835
}) => {
2936
const caipIdentifier = useMemo(() => {
3037
if (isHexString(address)) {
@@ -41,15 +48,17 @@ export const SnapUIAddress: React.FunctionComponent<SnapUIAddressProps> = ({
4148
[caipIdentifier],
4249
);
4350

44-
const displayName = useDisplayName(parsed);
51+
const name = useDisplayName(parsed);
4552

46-
const value =
47-
displayName ??
48-
shortenAddress(
49-
parsed.chain.namespace === 'eip155'
50-
? toChecksumHexAddress(parsed.address)
51-
: parsed.address,
52-
);
53+
// For EVM addresses, we make sure they are checksummed.
54+
const transformedAddress =
55+
parsed.chain.namespace === 'eip155'
56+
? toChecksumHexAddress(parsed.address)
57+
: parsed.address;
58+
59+
const formattedAddress = truncate
60+
? shortenAddress(transformedAddress)
61+
: address;
5362

5463
return (
5564
<Box
@@ -58,8 +67,14 @@ export const SnapUIAddress: React.FunctionComponent<SnapUIAddressProps> = ({
5867
alignItems={AlignItems.center}
5968
gap={2}
6069
>
61-
<SnapUIAvatar address={caipIdentifier} size={avatarSize} />
62-
<Text color={TextColor.inherit}>{value}</Text>
70+
{avatar && <SnapUIAvatar address={caipIdentifier} size={avatarSize} />}
71+
<Text
72+
variant={TextVariant.bodyMd}
73+
color={TextColor.inherit}
74+
style={{ lineBreak: 'anywhere' }}
75+
>
76+
{displayName && name ? name : formattedAddress}
77+
</Text>
6378
</Box>
6479
);
6580
};

ui/components/app/snaps/snap-ui-renderer/components/address.ts

+3
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ export const address: UIComponentFactory<AddressElement> = ({ element }) => ({
66
props: {
77
address: element.props.address,
88
avatarSize: 'xs',
9+
truncate: element.props.truncate,
10+
displayName: element.props.displayName,
11+
avatar: element.props.avatar,
912
},
1013
});

0 commit comments

Comments
 (0)