Skip to content

feat: Add logic for new requireScroll prop for Snap Footer component #29863

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

Draft
wants to merge 44 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
fb1ef7c
add logic for scroll arrow and disabling footer
hmalik88 Jan 22, 2025
6689d13
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Jan 24, 2025
22be388
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Jan 24, 2025
328b3ac
add AvatarIcon to component mapping
hmalik88 Jan 29, 2025
da08c07
update ui component type
hmalik88 Jan 29, 2025
3692446
update mapToTemplate to add refs
hmalik88 Jan 29, 2025
063b985
update useScrollRequired with an enabled option
hmalik88 Jan 29, 2025
1f3d3fb
updated snap ui renderer to handle content with required scroll
hmalik88 Jan 29, 2025
38efda3
add avatar icon snap ui component
hmalik88 Jan 29, 2025
5ac14f9
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Jan 29, 2025
513d77d
lint fix
hmalik88 Jan 29, 2025
6aa2a73
performance improvements
hmalik88 Jan 29, 2025
45ada75
move scroll handling logic into separate hook
hmalik88 Jan 29, 2025
e6ed619
add documentation
hmalik88 Jan 29, 2025
d659a38
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Jan 29, 2025
470c38e
fix flicker for non-scroll-required content
hmalik88 Jan 29, 2025
f4f3dc9
add comments
hmalik88 Jan 29, 2025
3f352b3
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Jan 30, 2025
dafe6aa
move hook
hmalik88 Feb 4, 2025
6bfd930
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 4, 2025
46ab45e
move scroll logic into context and create footer wrapper
hmalik88 Feb 5, 2025
ffa89c8
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 5, 2025
995774c
add missing props
hmalik88 Feb 5, 2025
89ac089
change logic to have arrow/buttons disabled only on first scroll
hmalik88 Feb 5, 2025
fb755a3
add new hasMeasured prop to useScrollRequired to use for triggering s…
hmalik88 Feb 6, 2025
2f4f7d1
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 6, 2025
f7bac01
removed unnecessary snap ui footer component
hmalik88 Feb 6, 2025
d414b6d
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 13, 2025
21c8e10
remove useScrollHandling and move logic back into snap-ui-renderer fo…
hmalik88 Feb 15, 2025
4f240ed
add comment
hmalik88 Feb 15, 2025
272d239
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 15, 2025
2b8faa3
change property name to make onMeasure callback more extensible, adde…
hmalik88 Feb 15, 2025
7166bec
destructure original variable name and change
hmalik88 Feb 15, 2025
1697674
fix tests
hmalik88 Feb 15, 2025
dde70f1
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 27, 2025
172e802
update per code review
hmalik88 Feb 28, 2025
3b460ca
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Feb 28, 2025
91f83f7
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Mar 3, 2025
f6ba4c5
apply code review
hmalik88 Mar 3, 2025
74bd5b1
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Mar 11, 2025
355fe1e
refactor
hmalik88 Mar 11, 2025
8e0b3d7
update tests
hmalik88 Mar 11, 2025
49c45bc
Merge branch 'main' into hm/integrate-require-scroll
hmalik88 Mar 25, 2025
0191696
update condition
hmalik88 Mar 25, 2025
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
10 changes: 10 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/avatar-icon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { JSXElement } from '@metamask/snaps-sdk/jsx';
import { UIComponentFactory } from './types';

export const avatarIcon: UIComponentFactory<JSXElement> = ({ element }) => ({
element: 'AvatarIcon',
props: {
// Define props once we expose this component to snap developers
...element.props,
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const getDefaultButtons = (
footer: FooterElement,
t: (value: string) => string,
onCancel?: () => void,
disabled?: boolean,
) => {
const children = getJsxChildren(footer);

Expand All @@ -47,6 +48,7 @@ const getDefaultButtons = (
onCancel,
variant: ButtonVariant.Secondary,
isSnapAction: false,
disabled,
},
children: t('cancel'),
};
Expand All @@ -59,9 +61,12 @@ export const footer: UIComponentFactory<FooterElement> = ({
element,
t,
onCancel,
requireScroll = false,
isScrolledToBottom = false,
...params
}) => {
const defaultButtons = getDefaultButtons(element, t, onCancel);
const disabled = requireScroll && !isScrolledToBottom;
const defaultButtons = getDefaultButtons(element, t, onCancel, disabled);

const providedChildren = getJsxChildren(element);
const footerChildren: UIComponent[] = (
Expand All @@ -81,6 +86,7 @@ export const footer: UIComponentFactory<FooterElement> = ({
? ButtonVariant.Secondary
: ButtonVariant.Primary,
isSnapAction: true,
disabled,
},
children: buttonMapped.children,
};
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 @@ -28,6 +28,7 @@ import { icon } from './icon';
import { section } from './section';
import { avatar } from './avatar';
import { banner } from './banner';
import { avatarIcon } from './avatar-icon';
import { skeleton } from './skeleton';

export const COMPONENT_MAPPING = {
Expand Down Expand Up @@ -61,5 +62,6 @@ export const COMPONENT_MAPPING = {
Selector: selector,
Section: section,
Banner: banner,
AvatarIcon: avatarIcon,
Skeleton: skeleton,
};
2 changes: 2 additions & 0 deletions ui/components/app/snaps/snap-ui-renderer/components/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export type UIComponentParams<T extends JSXElement> = {
};
t: (key: string) => string;
contentBackgroundColor: string | undefined;
isScrolledToBottom?: boolean | undefined;
requireScroll?: boolean | undefined;
};

export type UIComponent = {
Expand Down
191 changes: 160 additions & 31 deletions ui/components/app/snaps/snap-ui-renderer/snap-ui-renderer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { memo, useMemo } from 'react';
import React, { memo, useMemo, useCallback } from 'react';
import PropTypes from 'prop-types';
import { useSelector } from 'react-redux';
import { Container } from '@metamask/snaps-sdk/jsx';
Expand All @@ -7,7 +7,7 @@ import { isEqual } from 'lodash';
import MetaMaskTemplateRenderer from '../../metamask-template-renderer/metamask-template-renderer';
import { SnapDelineator } from '../snap-delineator';
import { getSnapMetadata, getMemoizedInterface } from '../../../../selectors';
import { Box } from '../../../component-library';
import { Box, IconName } from '../../../component-library';
import { DelineatorType } from '../../../../helpers/constants/snaps';

import { SnapInterfaceContextProvider } from '../../../../contexts/snaps';
Expand All @@ -17,11 +17,80 @@ import {
BackgroundColor,
BlockSize,
Display,
IconColor,
JustifyContent,
} from '../../../../helpers/constants/design-system';
import { useI18nContext } from '../../../../hooks/useI18nContext';
import { useScrollRequired } from '../../../../hooks/useScrollRequired';
import { useScrollHandling } from '../../../../hooks/useScrollHandling';
import { mapToExtensionCompatibleColor, mapToTemplate } from './utils';

/**
* This hook is used to process the content of the Snap UI Renderer.
* It adds the scroll arrow and the footer to the content if required.
*
* @param {object} rawContent - The raw content of the Snap UI Renderer.
* @param {object} scrollState - The state of the scroll handling.
* @param {object} scrollArrow - The scroll arrow component.
* @param {boolean} requireScroll - Whether the content requires scrolling.
* @returns The processed content.
*/
const useProcessedContent = (
rawContent,
scrollState,
scrollArrow,
requireScroll,
) => {
return useMemo(() => {
const baseContent =
rawContent?.type === 'Container' || !rawContent
? rawContent
: Container({ children: rawContent });

if (requireScroll) {
const children = [...baseContent.props.children];
const footer = {
...children[children.length - 1],
props: {
...children[children.length - 1].props,
isScrolledToBottom: scrollState.buttonsEnabled,
requireScroll: true,
},
};

children[children.length - 1] = footer;

if (scrollState.showArrow) {
children.splice(-1, 0, scrollArrow);
}

return {
...baseContent,
props: {
...baseContent.props,
children,
},
};
}

return baseContent;
}, [rawContent, scrollState, scrollArrow, requireScroll]);
};

const LoadingSpinner = memo(function LoadingSpinner() {
return (
Copy link
Member

Choose a reason for hiding this comment

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

Why is this moved out?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For readability

Copy link
Contributor Author

Choose a reason for hiding this comment

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

and memoizing because it's static no need to re-create on every render

<Box
display={Display.Flex}
justifyContent={JustifyContent.center}
alignItems={AlignItems.center}
height={BlockSize.Full}
width={BlockSize.Full}
>
<PulseLoader />
</Box>
);
});

// Component that maps Snaps UI JSON format to MetaMask Template Renderer format
const SnapUIRendererComponent = ({
snapId,
Expand All @@ -44,21 +113,76 @@ const SnapUIRendererComponent = ({
}) => {
const t = useI18nContext();

const { name: snapName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const interfaceState = useSelector(
(state) => getMemoizedInterface(state, interfaceId),
// We only want to update the state if the content has changed.
// We do this to avoid useless re-renders.
(oldState, newState) => isEqual(oldState.content, newState.content),
);
const rawContent = interfaceState?.content;
const content =
rawContent?.type === 'Container' || !rawContent
? rawContent
: Container({ children: rawContent });

const requireScroll =
interfaceState?.content?.props?.children?.[1]?.props?.requireScroll;

const { isScrollable, scrollToBottom, onScroll, ref } = useScrollRequired(
[],
// Only enable hook if the content requires scrolling
{ enabled: requireScroll },
);

const { name: snapName } = useSelector((state) =>
getSnapMetadata(state, snapId),
);

const { scrollState, handleScroll, handleScrollToBottom } = useScrollHandling(
requireScroll,
isScrollable,
);

const wrappedOnScroll = useCallback(
(e) => {
handleScroll(e, onScroll);
},
[handleScroll, onScroll],
);

const handleScrollToBottomClick = useCallback(() => {
handleScrollToBottom(scrollToBottom);
}, [handleScrollToBottom, scrollToBottom]);

const scrollArrow = useMemo(() => {
if (!requireScroll || !scrollState.showArrow) {
return null;
}

return {
type: 'AvatarIcon',
key: 'snap-ui-renderer__scroll-arrow',
props: {
iconName: IconName.Arrow2Down,
backgroundColor: BackgroundColor.infoDefault,
color: IconColor.primaryInverse,
className: 'snap-ui-renderer__scroll-button',
onClick: handleScrollToBottomClick,
style: {
cursor: 'pointer',
position: 'absolute',
left: 0,
right: 0,
marginLeft: 'auto',
marginRight: 'auto',
zIndex: 'auto',
bottom: '84px',
},
},
};
}, [handleScrollToBottomClick, requireScroll, scrollState.showArrow]);

const processedContent = useProcessedContent(
interfaceState?.content,
scrollState,
scrollArrow,
requireScroll,
);

const promptLegacyProps = useMemo(
() =>
Expand All @@ -72,44 +196,47 @@ const SnapUIRendererComponent = ({

const backgroundColor =
contentBackgroundColor ??
mapToExtensionCompatibleColor(content?.props?.backgroundColor) ??
mapToExtensionCompatibleColor(processedContent?.props?.backgroundColor) ??
BackgroundColor.backgroundAlternative;

// The renderer should only have a footer if there is a default cancel action
// or if the footer component has been used.
const hasFooter =
onCancel || processedContent?.props?.children?.[1] !== undefined;

const sections = useMemo(
() =>
content &&
processedContent &&
mapToTemplate({
map: {},
element: content,
element: processedContent,
onCancel,
useFooter,
promptLegacyProps,
t,
contentBackgroundColor: backgroundColor,
// We have to pass the props at the top level to ensure the children components get the correct props.
requireScroll,
isScrolledToBottom: scrollState.buttonsEnabled,
}),
[content, onCancel, useFooter, promptLegacyProps, t, backgroundColor],
[
processedContent,
onCancel,
useFooter,
promptLegacyProps,
t,
backgroundColor,
scrollState.buttonsEnabled,
requireScroll,
],
);

if (isLoading || !content) {
return (
<Box
display={Display.Flex}
justifyContent={JustifyContent.center}
alignItems={AlignItems.center}
height={BlockSize.Full}
width={BlockSize.Full}
>
<PulseLoader />
</Box>
);
if (isLoading || !processedContent) {
return <LoadingSpinner />;
}

const { state: initialState, context } = interfaceState;

// The renderer should only have a footer if there is a default cancel action
// or if the footer component has been used.
const hasFooter = onCancel || content?.props?.children?.[1] !== undefined;

return useDelineator ? (
<SnapDelineator
snapName={snapName}
Expand Down Expand Up @@ -140,6 +267,8 @@ const SnapUIRendererComponent = ({
>
<Box
className="snap-ui-renderer__content"
ref={ref}
onScroll={wrappedOnScroll}
height={BlockSize.Full}
backgroundColor={backgroundColor}
style={{
Expand Down
2 changes: 1 addition & 1 deletion ui/components/app/snaps/snap-ui-renderer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { UIComponent } from './components/types';

export type MapToTemplateParams = {
map: Record<string, number>;
element: JSXElement;
element: JSXElement & { ref?: React.Ref<unknown> };
form?: string;
useFooter?: boolean;
onCancel?: () => void;
Expand Down
Loading
Loading