Skip to content

Commit a8cd862

Browse files
david0xdFrederikBolding
authored andcommitted
feat(snaps): Add Snap UI Banner component (#29271)
Add Snap UI Banner component. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/29271?quickstart=1) Fixes: MetaMask/snaps#2939 1. Install test Snap from the code example provided and check the results. Example Snap code with Banners in the UI: ```typescript <Container> <Box> <Banner title="Success banner" severity="success"> <Text> <Icon name="arrow-right" color="primary" size="inherit" /> &nbsp;Here is the banner content! </Text> <Bold>Banner bold formatted text</Bold> <Italic>Banner italic formatted text</Italic> <Text> You can click here:&nbsp; <Link href="https://snaps.metamask.io/">Banner link</Link> </Text> <Button type="button">Banner Action Button</Button> </Banner> <Banner title="Info banner" severity="info"> <Text>Here is the banner content!</Text> </Banner> <Banner title="Warning banner" severity="warning"> <Text>Here is the banner content!</Text> </Banner> <Banner title="Danger banner" severity="danger"> <Text>Here is the banner content!</Text> </Banner> </Box> </Container> ``` Banner in Snap UI was not available before this PR. Nothing to show here. ![Screenshot 2024-12-17 at 12 34 42](https://github.com/user-attachments/assets/bdf291be-b41b-4e0a-964c-218dbd2dae5e) - [ ] 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. - [ ] 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 26da01d commit a8cd862

File tree

6 files changed

+77
-0
lines changed

6 files changed

+77
-0
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
@@ -33,6 +33,7 @@ import { Copyable } from '../snaps/copyable';
3333
import { SnapDelineator } from '../snaps/snap-delineator';
3434
import { SnapUIAddress } from '../snaps/snap-ui-address';
3535
import { SnapUIAvatar } from '../snaps/snap-ui-avatar';
36+
import { SnapUIBanner } from '../snaps/snap-ui-banner';
3637
import { SnapUIButton } from '../snaps/snap-ui-button';
3738
import { SnapUICard } from '../snaps/snap-ui-card';
3839
import { SnapUICheckbox } from '../snaps/snap-ui-checkbox';
@@ -89,6 +90,7 @@ export const safeComponentList = {
8990
SnapDelineator,
9091
SnapUIAddress,
9192
SnapUIAvatar,
93+
SnapUIBanner,
9294
SnapUIButton,
9395
SnapUICard,
9496
SnapUICheckbox,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './snap-ui-banner';
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import React, { ReactNode } from 'react';
2+
import { SnapUIBanner } from './snap-ui-banner';
3+
import { BannerAlertSeverity } from '../../../component-library';
4+
5+
export default {
6+
title: 'Components/App/Snaps/SnapUIBanner',
7+
component: SnapUIBanner,
8+
argTypes: {
9+
title: {
10+
control: 'text',
11+
},
12+
severity: {
13+
control: 'text',
14+
},
15+
children: {
16+
control: 'text',
17+
},
18+
},
19+
};
20+
21+
export const DefaultStory = (args: {
22+
title: string;
23+
severity: BannerAlertSeverity;
24+
children: ReactNode;
25+
}) => <SnapUIBanner {...args} />;
26+
27+
DefaultStory.storyName = 'Default';
28+
29+
DefaultStory.args = {
30+
title: 'Banner title',
31+
severity: 'info',
32+
children: 'Banner content.',
33+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { FunctionComponent } from 'react';
2+
import { BannerAlert, BannerAlertSeverity } from '../../../component-library';
3+
4+
export type SnapUIBannerProps = {
5+
severity: BannerAlertSeverity | undefined;
6+
title: string;
7+
};
8+
9+
export const SnapUIBanner: FunctionComponent<SnapUIBannerProps> = ({
10+
children,
11+
severity,
12+
title,
13+
}) => {
14+
return (
15+
<BannerAlert severity={severity} title={title}>
16+
{children}
17+
</BannerAlert>
18+
);
19+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { BannerElement, JSXElement } from '@metamask/snaps-sdk/jsx';
2+
import { getJsxChildren } from '@metamask/snaps-utils';
3+
import { mapToTemplate } from '../utils';
4+
import { UIComponentFactory } from './types';
5+
6+
export const banner: UIComponentFactory<BannerElement> = ({
7+
element,
8+
...params
9+
}) => {
10+
return {
11+
element: 'SnapUIBanner',
12+
children: getJsxChildren(element).map((children) =>
13+
mapToTemplate({ element: children as JSXElement, ...params }),
14+
),
15+
props: {
16+
title: element.props.title,
17+
severity: element.props.severity,
18+
},
19+
};
20+
};

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { selector } from './selector';
2727
import { icon } from './icon';
2828
import { section } from './section';
2929
import { avatar } from './avatar';
30+
import { banner } from './banner';
3031

3132
export const COMPONENT_MAPPING = {
3233
Box: box,
@@ -58,4 +59,5 @@ export const COMPONENT_MAPPING = {
5859
Container: container,
5960
Selector: selector,
6061
Section: section,
62+
Banner: banner,
6163
};

0 commit comments

Comments
 (0)