Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSX support for custom UI #2258

Merged
merged 47 commits into from
May 1, 2024
Merged

Add JSX support for custom UI #2258

merged 47 commits into from
May 1, 2024

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Mar 10, 2024

This changes Snaps UI to use a new JSX based format. I've added a JSX-equivalent for each existing component (with some small differences, see below), and a simple renderer, which serialises the elements. Existing (now legacy) components are transformed into the JSX based format automatically, so under the hood we only need to support JSX.

Snaps JSX only supports the new JSX transform, i.e., react-jsx or react-jsxdev, with @metamask/snaps-sdk as import source. For development, using react-jsxdev is recommended as it will provide additional validation which is otherwise handled by MetaMask. The CLI has been updated to use react-jsx automatically.

Differences

There are some minor differences between the legacy components and JSX equivalents:

General

  • The legacy panel was replaced by Box, which is more in line with existing UI frameworks.
  • Most components don't accept a value, but use children instead.

Forms and form components

  • Button uses type instead of buttonType.
  • Button's secondary variant was replaced by destructive.
  • Input uses type instead of inputType.
  • Form does not accept Input directly, but rather expects a Field component with an Input. Field has props to set a label or error.

Images

  • Image now takes an optional alt prop.

Text and formatting

  • The new Text component does not support markdown. Instead, you can use the Bold, Italic, and Link components. The new component does not have a markdown prop.

Breaking changes

  • The interface content format in snaps-jest changed. When using toStrictEqual (or equivalent) matchers, the expected value needs to be updated. toRender works with the legacy and JSX format.

@Mrtenz Mrtenz force-pushed the mrtenz/jsx branch 3 times, most recently from 971bad9 to 507855f Compare April 23, 2024 11:48
@Mrtenz Mrtenz marked this pull request as ready for review April 23, 2024 14:15
@Mrtenz Mrtenz requested a review from a team as a code owner April 23, 2024 14:15
@FrederikBolding FrederikBolding self-requested a review April 23, 2024 14:27
if (
hasProperty(node, 'props') &&
isPlainObject(node.props) &&
hasProperty(node.props, 'children')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't a prop also be a component or do we not want to support that in our implementation?

I seem to recall some of the SIP having components as props

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the components use components in props at the moment.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Once we want to do that, how do we remember to add support in this file and others?

module.exports = deepmerge(baseConfig, {
preset: '@metamask/snaps-jest',

// Since `@metamask/snaps-jest` runs in the browser, we can't collect
Copy link
Member

@FrederikBolding FrederikBolding Apr 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still accurate? I guess we could collect something now potentially?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's accurate for the wrong reason. We still can't collect coverage information since the Snap runs in a separate thread.

@Mrtenz Mrtenz merged commit 9888dfc into main May 1, 2024
151 checks passed
@Mrtenz Mrtenz deleted the mrtenz/jsx branch May 1, 2024 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants