Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Skeleton to Snaps UI components #29764

Merged
merged 10 commits into from
Jan 27, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
import { SnapAccountRedirect } from '../../../pages/snap-account-redirect';
import { CreateNamedSnapAccount } from '../../multichain/create-named-snap-account';
import SnapAuthorshipHeader from '../snaps/snap-authorship-header';
import { Skeleton } from '../../component-library/skeleton';
///: END:ONLY_INCLUDE_IF

export const safeComponentList = {
Expand Down Expand Up @@ -108,6 +109,7 @@ export const safeComponentList = {
SnapUITooltip,
span: 'span',
Spinner,
Skeleton,
Text,
TextArea,
TextField,
Expand Down
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { icon } from './icon';
import { section } from './section';
import { avatar } from './avatar';
import { banner } from './banner';
import { skeleton } from './skeleton';

export const COMPONENT_MAPPING = {
Box: box,
Expand Down Expand Up @@ -60,4 +61,5 @@ export const COMPONENT_MAPPING = {
Selector: selector,
Section: section,
Banner: banner,
Skeleton: skeleton,
};
21 changes: 21 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/skeleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { SkeletonElement } from '@metamask/snaps-sdk/jsx';
import { BorderRadius } from '../../../../../helpers/constants/design-system';
import { mapSnapBorderRadiusToExtensionBorderRadius } from '../utils';
import { UIComponentFactory } from './types';

const DEFAULT_SKELETON_WIDTH = '100%';
const DEFAULT_SKELETON_HEIGHT = 22;
const DEFAULT_SKELETON_BORDER_RADIUS = BorderRadius.MD;

export const skeleton: UIComponentFactory<SkeletonElement> = ({ element }) => {
return {
element: 'Skeleton',
props: {
width: element.props.width ?? DEFAULT_SKELETON_WIDTH,
height: element.props.height ?? DEFAULT_SKELETON_HEIGHT,
borderRadius: element.props.borderRadius
? mapSnapBorderRadiusToExtensionBorderRadius(element.props.borderRadius)
: DEFAULT_SKELETON_BORDER_RADIUS,
},
};
};
25 changes: 24 additions & 1 deletion ui/components/app/snaps/snap-ui-renderer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { sha256 } from '@noble/hashes/sha256';
import { NonEmptyArray, bytesToHex, remove0x } from '@metamask/utils';
import { unescape as unescapeEntities } from 'he';
import { ChangeEvent as ReactChangeEvent } from 'react';
import { BackgroundColor } from '../../../../helpers/constants/design-system';
import {
BackgroundColor,
BorderRadius,
} from '../../../../helpers/constants/design-system';
import { COMPONENT_MAPPING } from './components';
import { UIComponent } from './components/types';

Expand Down Expand Up @@ -156,3 +159,23 @@ export const mapToExtensionCompatibleColor = (color: string) => {
};
return color ? backgroundColorMapping[color] : undefined;
};

/**
* Map Snap custom size for border radius to extension compatible size.
*
* @param snapBorderRadius - Snap custom color.
* @returns String, representing border radius size from design system.
*/
export const mapSnapBorderRadiusToExtensionBorderRadius = (
snapBorderRadius: string | undefined,
): BorderRadius => {
switch (snapBorderRadius) {
case 'none':
default:
return BorderRadius.none;
case 'medium':
return BorderRadius.MD;
case 'full':
return BorderRadius.full;
}
};
Loading