Skip to content

Commit 6069bb0

Browse files
authored
Add backgroundColor property to Container component (#2950)
This PR adds `backgroundColor` property to `Container` component. Related task: #2906 Related extension PR: MetaMask/metamask-extension#29095 ### Notes - Background colors that can be used are predefined as: `default` and `alternative`. - Background colors are meant to mirror `backgroundDefault` and `backgroundAlternative` colors from MetaMask extension design system (https://github.com/MetaMask/metamask-extension/blob/main/ui/helpers/constants/design-system.ts#L54). - Color names are simplified on the Snaps side to make it easier for developers to use. ### Examples (experiments with extension integration) Confirmation example: ![Screenshot 2024-12-11 at 13 14 02](https://github.com/user-attachments/assets/f2b77202-d9c4-403e-87a1-ad36d44299c9) Source code used for content: ```typescript return snap.request({ method: 'snap_dialog', params: { content: ( <Container backgroundColor="default"> <Box> <Text>Testing container background.</Text> <Button variant="primary">Primary button</Button> <Button variant="destructive">Destructive button</Button> <Button disabled={true}>Disabled button</Button> </Box> <Footer> <Button>Accept</Button> <Button name="footer_button">Cancel</Button> </Footer> </Container> ), }, }); ``` Transaction insight example where background color is deliberately ignored: ![Screenshot 2024-12-11 at 13 07 40](https://github.com/user-attachments/assets/b7b3a593-8407-4f92-a629-edd85c8f88dc) Source code used for content: ```typescript return { content: ( <Container backgroundColor="alternative"> <Box> <Text>Testing container background on transaction insight.</Text> <Text>Normal transaction.</Text> </Box> </Container> ), severity: SeverityLevel.Critical, }; ```
1 parent 6861362 commit 6069bb0

File tree

6 files changed

+21
-4
lines changed

6 files changed

+21
-4
lines changed

packages/examples/packages/browserify-plugin/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "VLL7J8JE/MX0j3B7FCAGtrJ/LYXYSnq88hKUr8mMfFQ=",
10+
"shasum": "Hk6cbofZemMiX1hbyOek/sMo3kLlgCATBaLReHnTjys=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/examples/packages/browserify/snap.manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"url": "https://github.com/MetaMask/snaps.git"
88
},
99
"source": {
10-
"shasum": "mAFLwxqRmE5DHBRs1/nOM91YpcmeEHkyqksRrXTgjJY=",
10+
"shasum": "OMGLit1mel4LuH9aYDSlSOoT7ZO6IsJXQjorv4NYgeQ=",
1111
"location": {
1212
"npm": {
1313
"filePath": "dist/bundle.js",

packages/snaps-sdk/src/jsx/components/Container.test.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Text } from './Text';
77
describe('Container', () => {
88
it('renders a container element with a box', () => {
99
const result = (
10-
<Container>
10+
<Container backgroundColor="alternative">
1111
<Box>
1212
<Text>Hello world!</Text>
1313
</Box>
@@ -18,6 +18,7 @@ describe('Container', () => {
1818
type: 'Container',
1919
key: null,
2020
props: {
21+
backgroundColor: 'alternative',
2122
children: {
2223
type: 'Box',
2324
key: null,

packages/snaps-sdk/src/jsx/components/Container.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ import type { GenericSnapElement } from '../component';
22
import { createSnapComponent } from '../component';
33
import type { FooterElement } from './Footer';
44

5+
/**
6+
* Definition of container background colors.
7+
*/
8+
export type ContainerBackgroundColor = 'default' | 'alternative';
9+
510
/**
611
* The props of the {@link Container} component.
712
*
813
* @property children - The Box and the Footer or the Box element.
914
*/
1015
export type ContainerProps = {
1116
children: [GenericSnapElement, FooterElement] | GenericSnapElement;
17+
backgroundColor?: ContainerBackgroundColor | undefined;
1218
};
1319

1420
const TYPE = 'Container';
@@ -17,10 +23,11 @@ const TYPE = 'Container';
1723
* A container component, which is used to create a container with a box and a footer.
1824
*
1925
* @param props - The props of the component.
26+
* @param props.backgroundColor - The color of the background.
2027
* @param props.children - The Box and the Footer or the Box element.
2128
* @returns A container element.
2229
* @example
23-
* <Container>
30+
* <Container backgroundColor="default">
2431
* <Box>
2532
* <Text>Hello world!</Text>
2633
* </Box>

packages/snaps-sdk/src/jsx/validation.test.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,12 @@ describe('ContainerStruct', () => {
759759
<Container>
760760
<Text>Hello world!</Text>
761761
</Container>,
762+
<Container backgroundColor="default">
763+
<Text>Hello world!</Text>
764+
</Container>,
765+
<Container backgroundColor="alternative">
766+
<Text>Hello world!</Text>
767+
</Container>,
762768
<Container>
763769
<Text>Hello world!</Text>
764770
<Footer>

packages/snaps-sdk/src/jsx/validation.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,9 @@ export const ContainerStruct: Describe<ContainerElement> = element(
877877
[GenericSnapElement, FooterElement] | GenericSnapElement,
878878
null
879879
>,
880+
backgroundColor: optional(
881+
nullUnion([literal('default'), literal('alternative')]),
882+
),
880883
},
881884
);
882885

0 commit comments

Comments
 (0)