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

feat: Add Tooltip Snap custom UI component #25413

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { SnapUIForm } from '../snaps/snap-ui-form';
import { SnapUIButton } from '../snaps/snap-ui-button';
import { SnapUIDropdown } from '../snaps/snap-ui-dropdown';
import { SnapUICheckbox } from '../snaps/snap-ui-checkbox';
import { SnapUITooltip } from '../snaps/snap-ui-tooltip';
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
import { SnapAccountSuccessMessage } from '../../../pages/confirmations/components/snap-account-success-message';
Expand Down Expand Up @@ -87,6 +88,7 @@ export const safeComponentList = {
SnapUIForm,
SnapUIDropdown,
SnapUICheckbox,
SnapUITooltip,
///: END:ONLY_INCLUDE_IF
///: BEGIN:ONLY_INCLUDE_IF(keyring-snaps)
CreateSnapAccount,
Expand Down
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { field } from './field';
import { dropdown } from './dropdown';
import { value } from './value';
import { checkbox } from './checkbox';
import { tooltip } from './tooltip';

export const COMPONENT_MAPPING = {
Box: box,
Expand All @@ -38,4 +39,5 @@ export const COMPONENT_MAPPING = {
Dropdown: dropdown,
Value: value,
Checkbox: checkbox,
Tooltip: tooltip,
};
23 changes: 23 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/tooltip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { JSXElement, Text, TooltipElement } from '@metamask/snaps-sdk/jsx';
import { getJsxChildren } from '@metamask/snaps-utils';
import { mapToTemplate } from '../utils';
import { UIComponentFactory } from './types';

export const tooltip: UIComponentFactory<TooltipElement> = ({
element,
...params
}) => ({
element: 'SnapUITooltip',
children: getJsxChildren(element).map((children) =>
mapToTemplate({ element: children as JSXElement, ...params }),
),
propComponents: {
content: mapToTemplate({
element:
typeof element.props.content === 'string'
? Text({ children: element.props.content })
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wrapped the content prop with the Text element to be able to pass it directly to the propComponents section. This reduces the bloat of having a logic to pass it somewhere else if it's a string. Furthermore we keep a consistent styling.

: element.props.content,
...params,
}),
},
});
1 change: 1 addition & 0 deletions ui/components/app/snaps/snap-ui-tooltip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './snap-ui-tooltip';
22 changes: 22 additions & 0 deletions ui/components/app/snaps/snap-ui-tooltip/snap-ui-tooltip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { FunctionComponent, ReactNode } from 'react';
import Tooltip from '../../../ui/tooltip';

export type SnapUITooltipProps = {
content: ReactNode;
};

export const SnapUITooltip: FunctionComponent<SnapUITooltipProps> = ({
content,
children,
}) => {
return (
<Tooltip
html={content}
position={'bottom'}
FrederikBolding marked this conversation as resolved.
Show resolved Hide resolved
// Avoid tooltip from taking up the full width of the container
style={{ display: 'inline-flex' }}
>
{children}
</Tooltip>
);
};