Skip to content

Commit 33db784

Browse files
authored
feat: Add Skeleton to Snaps UI components (#29764)
## **Description** Add Skeleton to Snaps UI components. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29764?quickstart=1) ## **Related issues** Fixes: MetaMask/snaps#2940 ## **Related Pull Requests** Snaps PR: MetaMask/snaps#3024 ## **Manual testing steps** 1. Make and install test Snap with source code that uses Skeleton component. 2. Check if it matches expectations. Snap UI JSX code used for testing: ```typescript <Container> <Box> <Text>Skeleton inside text component:</Text> <Text> <Skeleton /> </Text> <Text>Classic Skeleton inside a box:</Text> <Skeleton /> <Text>Top level Skeleton: </Text> <Skeleton height={32} width="100%" /> <Skeleton height={16} width="50%" borderRadius="none" /> <Skeleton height={16} width="25%" borderRadius="medium" /> <Skeleton height={32} width={32} borderRadius="full" /> <Text>Skeleton inside Section: </Text> <Section> <Skeleton height={32} width="100%" /> <Skeleton height={16} width="50%" borderRadius="none" /> <Skeleton height={16} width="25%" borderRadius="medium" /> <Skeleton height={32} width={32} borderRadius="full" /> </Section> <Text>Skeleton inside Row: </Text> <Row label="Row"> <Skeleton height={22} width="30%" /> </Row> <Row label="Row"> <Text> <Skeleton height={22} width={40} /> </Text> </Row> </Box> </Container> ``` ## **Screenshots/Recordings** ### **Before** Skeleton was not available before. Nothing to show here. ### **After** ![Screenshot 2025-01-17 at 13 09 22](https://github.com/user-attachments/assets/fb1be740-f06d-4da0-80ec-7fb3c5cbf1d3) ## **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/main/.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/main/.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.
1 parent 40cb868 commit 33db784

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

ui/components/app/metamask-template-renderer/safe-component-list.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
import { SnapAccountRedirect } from '../../../pages/snap-account-redirect';
6161
import { CreateNamedSnapAccount } from '../../multichain/create-named-snap-account';
6262
import SnapAuthorshipHeader from '../snaps/snap-authorship-header';
63+
import { Skeleton } from '../../component-library/skeleton';
6364
///: END:ONLY_INCLUDE_IF
6465

6566
export const safeComponentList = {
@@ -108,6 +109,7 @@ export const safeComponentList = {
108109
SnapUITooltip,
109110
span: 'span',
110111
Spinner,
112+
Skeleton,
111113
Text,
112114
TextArea,
113115
TextField,

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { icon } from './icon';
2828
import { section } from './section';
2929
import { avatar } from './avatar';
3030
import { banner } from './banner';
31+
import { skeleton } from './skeleton';
3132

3233
export const COMPONENT_MAPPING = {
3334
Box: box,
@@ -60,4 +61,5 @@ export const COMPONENT_MAPPING = {
6061
Selector: selector,
6162
Section: section,
6263
Banner: banner,
64+
Skeleton: skeleton,
6365
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { SkeletonElement } from '@metamask/snaps-sdk/jsx';
2+
import { BorderRadius } from '../../../../../helpers/constants/design-system';
3+
import { mapSnapBorderRadiusToExtensionBorderRadius } from '../utils';
4+
import { UIComponentFactory } from './types';
5+
6+
const DEFAULT_SKELETON_WIDTH = '100%';
7+
const DEFAULT_SKELETON_HEIGHT = 22;
8+
const DEFAULT_SKELETON_BORDER_RADIUS = BorderRadius.MD;
9+
10+
export const skeleton: UIComponentFactory<SkeletonElement> = ({ element }) => {
11+
return {
12+
element: 'Skeleton',
13+
props: {
14+
width: element.props.width ?? DEFAULT_SKELETON_WIDTH,
15+
height: element.props.height ?? DEFAULT_SKELETON_HEIGHT,
16+
borderRadius: element.props.borderRadius
17+
? mapSnapBorderRadiusToExtensionBorderRadius(element.props.borderRadius)
18+
: DEFAULT_SKELETON_BORDER_RADIUS,
19+
},
20+
};
21+
};

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import { sha256 } from '@noble/hashes/sha256';
55
import { NonEmptyArray, bytesToHex, remove0x } from '@metamask/utils';
66
import { unescape as unescapeEntities } from 'he';
77
import { ChangeEvent as ReactChangeEvent } from 'react';
8-
import { BackgroundColor } from '../../../../helpers/constants/design-system';
8+
import {
9+
BackgroundColor,
10+
BorderRadius,
11+
} from '../../../../helpers/constants/design-system';
912
import { COMPONENT_MAPPING } from './components';
1013
import { UIComponent } from './components/types';
1114

@@ -156,3 +159,23 @@ export const mapToExtensionCompatibleColor = (color: string) => {
156159
};
157160
return color ? backgroundColorMapping[color] : undefined;
158161
};
162+
163+
/**
164+
* Map Snap custom size for border radius to extension compatible size.
165+
*
166+
* @param snapBorderRadius - Snap custom color.
167+
* @returns String, representing border radius size from design system.
168+
*/
169+
export const mapSnapBorderRadiusToExtensionBorderRadius = (
170+
snapBorderRadius: string | undefined,
171+
): BorderRadius => {
172+
switch (snapBorderRadius) {
173+
case 'none':
174+
default:
175+
return BorderRadius.none;
176+
case 'medium':
177+
return BorderRadius.MD;
178+
case 'full':
179+
return BorderRadius.full;
180+
}
181+
};

0 commit comments

Comments
 (0)