forked from MetaMask/metamask-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document Snaps dialogs and home pages (MetaMask#1302)
* Document Snaps dialogs and home pages * address reviewer feedback
- Loading branch information
1 parent
feee275
commit 6a26c0e
Showing
18 changed files
with
240 additions
and
119 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,124 @@ | ||
--- | ||
description: Display custom alert, confirmation, or prompt screens in MetaMask. | ||
sidebar_position: 2 | ||
--- | ||
|
||
# Dialogs | ||
|
||
You can display a dialog in the MetaMask UI using the | ||
[`snap_dialog`](../../reference/snaps-api.md#snap_dialog) API method. | ||
Dialogs can contain [custom UI](index.md) and [interactive UI](interactive-ui.md) components. | ||
|
||
There are three types of dialogs: [alerts](#display-an-alert-dialog), | ||
[confirmations](#display-a-confirmation-dialog), and [prompts](#display-a-prompt-dialog). | ||
|
||
:::caution | ||
Dialogs do not work when MetaMask is locked. | ||
To check if MetaMask is locked, use | ||
[`snap_getClientStatus`](../../reference/snaps-api.md#snap_getclientstatus). | ||
::: | ||
|
||
## Request permission to display dialogs | ||
|
||
To display dialogs, first request the [`snap_dialog`](../../reference/snaps-api.md#snap_dialog) permission. | ||
Add the following to your Snap's manifest file: | ||
|
||
```json title="snap.manifest.json" | ||
"initialPermissions": { | ||
"snap_dialog": {} | ||
} | ||
``` | ||
|
||
## Display an alert dialog | ||
|
||
To display an alert that can only be acknowledged, call | ||
[`snap_dialog`](../../reference/snaps-api.md#snap_dialog) with `type: "alert"`. | ||
The following example displays custom UI that alerts the user when something happens in the system: | ||
|
||
```javascript title="index.js" | ||
import { panel, text, heading } from "@metamask/snaps-sdk"; | ||
|
||
await snap.request({ | ||
method: "snap_dialog", | ||
params: { | ||
type: "alert", | ||
content: panel([ | ||
heading("Something happened in the system"), | ||
text("The thing that happened is..."), | ||
]), | ||
}, | ||
}); | ||
|
||
// Code that should execute after the alert has been acknowledged. | ||
``` | ||
|
||
<p align="center"> | ||
<img src={require("../../assets/alert-dialog.png").default} alt="Alert dialog example" width="360px" style={{border: "1px solid #DCDCDC"}} /> | ||
</p> | ||
|
||
## Display a confirmation dialog | ||
|
||
To display a confirmation that can be accepted or rejected, call | ||
[`snap_dialog`](../../reference/snaps-api.md#snap_dialog) with `type: "confirmation"`. | ||
The following example displays custom UI that asks the user to confirm whether they would like to | ||
take an action: | ||
|
||
```javascript title="index.js" | ||
import { panel, text, heading } from "@metamask/snaps-sdk"; | ||
|
||
const result = await snap.request({ | ||
method: "snap_dialog", | ||
params: { | ||
type: "confirmation", | ||
content: panel([ | ||
heading("Would you like to take the action?"), | ||
text("The action is..."), | ||
]), | ||
}, | ||
}); | ||
|
||
if (result === true) { | ||
// Do the action. | ||
} | ||
``` | ||
|
||
<p align="center"> | ||
<img src={require("../../assets/confirmation-dialog.png").default} alt="Confirmation dialog example" width="360px" style={{border: "1px solid #DCDCDC"}} /> | ||
</p> | ||
|
||
## Display a prompt dialog | ||
|
||
To display a prompt where the user can enter a text response, call | ||
[`snap_dialog`](../../reference/snaps-api.md#snap_dialog) with `type: "prompt"`. | ||
Prompt dialogs also accept a `placeholder` value that displays in the input field when no text is entered. | ||
|
||
The following example displays custom UI that prompts the user to enter a wallet address: | ||
|
||
```javascript title="index.js" | ||
import { panel, text, heading } from "@metamask/snaps-sdk"; | ||
|
||
const walletAddress = await snap.request({ | ||
method: "snap_dialog", | ||
params: { | ||
type: "prompt", | ||
content: panel([ | ||
heading("What is the wallet address?"), | ||
text("Please enter the wallet address to be monitored"), | ||
]), | ||
placeholder: "0x123...", | ||
}, | ||
}); | ||
|
||
// walletAddress will be a string containing the address entered by the user. | ||
``` | ||
|
||
<p align="center"> | ||
<img src={require("../../assets/prompt-dialog.png").default} alt="Prompt dialog example" width="360px" style={{border: "1px solid #DCDCDC"}} /> | ||
</p> | ||
|
||
## Example | ||
|
||
See the [`@metamask/dialog-example-snap`](https://github.com/MetaMask/snaps/tree/main/packages/examples/packages/dialogs) | ||
package for a full example of implementing dialogs. | ||
This example exposes a [custom JSON-RPC API](../../learn/about-snaps/apis.md#custom-json-rpc-apis) | ||
for dapps to display dialogs. |
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,56 @@ | ||
--- | ||
description: Display a dedicated UI page in MetaMask for your Snap. | ||
sidebar_position: 3 | ||
--- | ||
|
||
# Home pages | ||
|
||
You can display a dedicated UI, or "home page," for your Snap within MetaMask. | ||
This is useful for introducing your Snap to end users. | ||
Users can access your home page through the Snaps menu in MetaMask. | ||
Snap home pages can contain [custom UI](index.md) and [interactive UI](interactive-ui.md) | ||
components. | ||
|
||
## Steps | ||
|
||
### 1. Request permission to display a home page | ||
|
||
Request the [`endowment:page-home`](../../reference/permissions.md#endowmentpage-home) permission. | ||
Add the following to your Snap's manifest file: | ||
|
||
```json title="snap.manifest.json" | ||
"initialPermissions": { | ||
"endowment:page-home": {} | ||
} | ||
``` | ||
|
||
### 2. Create a home page | ||
|
||
Expose an [`onHomePage`](../../reference/entry-points.md#onhomepage) entry point, which returns the | ||
custom or interactive UI to be shown. | ||
MetaMask calls this method when a user selects your Snap name in the Snaps menu. | ||
|
||
The following example displays custom UI that welcomes the user to the Snap's home page: | ||
|
||
```typescript title="index.ts" | ||
import type { OnHomePageHandler } from "@metamask/snaps-sdk"; | ||
import { panel, text, heading } from "@metamask/snaps-sdk"; | ||
|
||
export const onHomePage: OnHomePageHandler = async () => { | ||
return { | ||
content: panel([ | ||
heading("Hello world!"), | ||
text("Welcome to my Snap home page!"), | ||
]), | ||
}; | ||
}; | ||
``` | ||
|
||
<p align="center"> | ||
<img src={require("../../assets/home-page.png").default} alt="Home page example" width="360px" style={{border: "1px solid #DCDCDC"}} /> | ||
</p> | ||
|
||
## Example | ||
|
||
See the [`@metamask/home-page-example-snap`](https://github.com/MetaMask/snaps/tree/main/packages/examples/packages/home-page) | ||
package for a full example of implementing a Snap home page. |
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
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
Oops, something went wrong.