-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(snaps): Add custom dialog (#26304)
## **Description** This PR adds the support for custom dialogs. Major changes: - Add support for `Footer` and `Container` custom UI components - Refactor `snap-ui-renderer` to handle display of the snap header - Add the new `snap_resolveInterface` snap RPC method - Add the new `default` dialog type [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/26304?quickstart=1) ## **Related issues** Fixes: ## **Manual testing steps** 1. Go to test-snaps 2. trigger the `custom` snap dialog ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **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/develop/.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/develop/.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. --------- Co-authored-by: Maarten Zuidhoorn <maarten@zuidhoorn.com> Co-authored-by: Frederik Bolding <frederik.bolding@gmail.com>
- Loading branch information
1 parent
0c2a55c
commit a1d3b3c
Showing
26 changed files
with
599 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
.snap-footer-button { | ||
&.mm-button-primary { | ||
box-shadow: none; | ||
color: var(--color-text-alternative); | ||
background-color: var(--color-icon-default); | ||
|
||
&:hover:not(&--disabled) { | ||
opacity: 80%; | ||
box-shadow: none; | ||
} | ||
|
||
&:active:not(&--disabled) { | ||
opacity: 60%; | ||
} | ||
} | ||
|
||
&.mm-button-secondary { | ||
border-color: var(--color-icon-default); | ||
color: var(--color-icon-default); | ||
|
||
& span { | ||
color: var(--color-icon-default); | ||
} | ||
|
||
&:hover:not(&--disabled) { | ||
border-color: var(--color-icon-default); | ||
color: var(--color-background-default); | ||
background-color: var(--color-background-default-hover); | ||
box-shadow: none; | ||
|
||
& span { | ||
color: var(--color-icon-default); | ||
} | ||
} | ||
|
||
&:active:not(&--disabled) { | ||
border-color: var(--color-icon-default); | ||
opacity: 60%; | ||
|
||
& span { | ||
color: var(--color-icon-default); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './snap-footer-button'; |
58 changes: 58 additions & 0 deletions
58
ui/components/app/snaps/snap-footer-button/snap-footer-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React, { FunctionComponent, MouseEvent as ReactMouseEvent } from 'react'; | ||
import { ButtonVariant, UserInputEventType } from '@metamask/snaps-sdk'; | ||
import { Button, ButtonSize, IconSize } from '../../../component-library'; | ||
import { | ||
AlignItems, | ||
Display, | ||
FlexDirection, | ||
} from '../../../../helpers/constants/design-system'; | ||
import { useSnapInterfaceContext } from '../../../../contexts/snaps'; | ||
import { SnapIcon } from '../snap-icon'; | ||
|
||
type SnapFooterButtonProps = { | ||
name?: string; | ||
isSnapAction?: boolean; | ||
onCancel?: () => void; | ||
}; | ||
|
||
export const SnapFooterButton: FunctionComponent<SnapFooterButtonProps> = ({ | ||
onCancel, | ||
name, | ||
children, | ||
isSnapAction = false, | ||
...props | ||
}) => { | ||
const { handleEvent, snapId } = useSnapInterfaceContext(); | ||
|
||
const handleSnapAction = (event: ReactMouseEvent<HTMLElement>) => { | ||
event.preventDefault(); | ||
|
||
handleEvent({ | ||
event: UserInputEventType.ButtonClickEvent, | ||
name, | ||
}); | ||
}; | ||
|
||
const handleClick = isSnapAction ? handleSnapAction : onCancel; | ||
|
||
return ( | ||
<Button | ||
className="snap-footer-button" | ||
{...props} | ||
size={ButtonSize.Lg} | ||
block | ||
variant={isSnapAction ? ButtonVariant.Primary : ButtonVariant.Secondary} | ||
onClick={handleClick} | ||
textProps={{ | ||
display: Display.Flex, | ||
alignItems: AlignItems.center, | ||
flexDirection: FlexDirection.Row, | ||
}} | ||
> | ||
{isSnapAction && ( | ||
<SnapIcon snapId={snapId} avatarSize={IconSize.Xs} marginRight={2} /> | ||
)} | ||
{children} | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './snap-icon'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
|
||
import { useSelector } from 'react-redux'; | ||
import { | ||
getSnapMetadata, | ||
getTargetSubjectMetadata, | ||
} from '../../../../selectors'; | ||
import { getAvatarFallbackLetter } from '../../../../helpers/utils/util'; | ||
import { | ||
AvatarBase, | ||
AvatarBaseSize, | ||
AvatarFavicon, | ||
AvatarFaviconProps, | ||
AvatarFaviconSize, | ||
IconSize, | ||
} from '../../../component-library'; | ||
import { | ||
AlignItems, | ||
BackgroundColor, | ||
Display, | ||
JustifyContent, | ||
TextColor, | ||
} from '../../../../helpers/constants/design-system'; | ||
|
||
type SnapIconProps = { | ||
snapId: string; | ||
avatarSize?: IconSize; | ||
borderWidth?: number; | ||
className?: string; | ||
badgeBackgroundColor?: BackgroundColor; | ||
} & Omit<AvatarFaviconProps<'span'>, 'name'>; | ||
|
||
export const SnapIcon: FunctionComponent<SnapIconProps> = ({ | ||
snapId, | ||
avatarSize = IconSize.Lg, | ||
...props | ||
}) => { | ||
const subjectMetadata = useSelector((state) => | ||
getTargetSubjectMetadata(state, snapId), | ||
); | ||
|
||
const { name: snapName } = useSelector((state) => | ||
/* @ts-expect-error wrong type on selector. */ | ||
getSnapMetadata(state, snapId), | ||
); | ||
|
||
const iconUrl = subjectMetadata?.iconUrl; | ||
|
||
// We choose the first non-symbol char as the fallback icon. | ||
const fallbackIcon = getAvatarFallbackLetter(snapName); | ||
|
||
return iconUrl ? ( | ||
<AvatarFavicon | ||
style={{ | ||
backgroundColor: 'var(--color-background-alternative-hover)', | ||
}} | ||
src={iconUrl} | ||
name={snapName} | ||
{...props} | ||
size={avatarSize as unknown as AvatarFaviconSize} | ||
/> | ||
) : ( | ||
<AvatarBase | ||
display={Display.Flex} | ||
alignItems={AlignItems.center} | ||
justifyContent={JustifyContent.center} | ||
color={TextColor.textAlternative} | ||
style={{ | ||
borderWidth: '0px', | ||
backgroundColor: 'var(--color-background-alternative-hover)', | ||
}} | ||
{...props} | ||
size={avatarSize as unknown as AvatarBaseSize} | ||
> | ||
{fallbackIcon} | ||
</AvatarBase> | ||
); | ||
}; |
Oops, something went wrong.