Skip to content

Commit

Permalink
feat(snaps): Add loading variant to Snap UI button (#28997)
Browse files Browse the repository at this point in the history
## **Description**
This PR enables loading button variant for Snaps UI.

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28997?quickstart=1)

#### Notes
- Targets Snaps release integration PR:
#29275

## **Related issues**
Fixes: MetaMask/snaps#2694

## **Related PR dependencies**
Snaps monorepo: MetaMask/snaps#2930

## **Manual testing steps**
1. Create some example Snap for testing and add Snap UI Button with
`loading` variant.
Example: 
```TypeScript
<Button variant="loading">Loading button</Button>
```

## **Screenshots/Recordings**
### **Before**
Loading button variant was not available before.

### **After**

https://github.com/user-attachments/assets/5afea22c-1951-4475-a908-aa5b97eafb6b


## **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.
  • Loading branch information
david0xd authored and FrederikBolding committed Dec 20, 2024
1 parent a97f2b4 commit bf1e4d6
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
18 changes: 16 additions & 2 deletions ui/components/app/snaps/snap-ui-button/snap-ui-button.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import React, { FunctionComponent, MouseEvent as ReactMouseEvent } from 'react';
import classnames from 'classnames';
import { ButtonType, UserInputEventType } from '@metamask/snaps-sdk';
import { ButtonLinkProps, Text } from '../../../component-library';
import {
ButtonLinkProps,
Icon,
IconName,
Text,
} from '../../../component-library';
import {
FontWeight,
TextColor,
Expand All @@ -11,6 +16,7 @@ import { useSnapInterfaceContext } from '../../../../contexts/snaps';
export type SnapUIButtonProps = {
name?: string;
textVariant: ButtonLinkProps<'button'>['variant'];
loading?: boolean;
};

const COLORS = {
Expand All @@ -27,6 +33,7 @@ export const SnapUIButton: FunctionComponent<
type = ButtonType.Button,
variant = 'primary',
disabled = false,
loading = false,
className = '',
textVariant,
...props
Expand Down Expand Up @@ -63,7 +70,14 @@ export const SnapUIButton: FunctionComponent<
variant={textVariant}
{...props}
>
{children}
{loading ? (
<Icon
name={IconName.Loading}
style={{ animation: 'spin 1.2s linear infinite' }}
/>
) : (
children
)}
</Text>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
Button,
ButtonProps,
ButtonSize,
Icon,
IconName,
IconSize,
} from '../../../component-library';
import {
Expand All @@ -35,6 +37,7 @@ export const SnapUIFooterButton: FunctionComponent<
name,
children,
disabled = false,
loading = false,
isSnapAction = false,
type,
variant = ButtonVariant.Primary,
Expand Down Expand Up @@ -85,10 +88,17 @@ export const SnapUIFooterButton: FunctionComponent<
flexDirection: FlexDirection.Row,
}}
>
{isSnapAction && !hideSnapBranding && (
{isSnapAction && !hideSnapBranding && !loading && (
<SnapIcon snapId={snapId} avatarSize={IconSize.Sm} marginRight={2} />
)}
{children}
{loading ? (
<Icon
name={IconName.Loading}
style={{ animation: 'spin 1.2s linear infinite' }}
/>
) : (
children
)}
</Button>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const button: UIComponentFactory<ButtonElement> = ({
variant: element.props.variant,
name: element.props.name,
disabled: element.props.disabled,
loading: element.props.loading,
textVariant:
element.props.size === 'sm' ? TextVariant.bodySm : TextVariant.bodyMd,
},
Expand Down

0 comments on commit bf1e4d6

Please sign in to comment.