-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix: alignment of Snaps icons within ButtonLink/SnapUIButton component #14243
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
Open
Daniel-Cross
wants to merge
30
commits into
main
Choose a base branch
from
3244-alignment-is-off-on-some-snapuibuttons
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
147edd5
changed SnapUIButton component to a custom component that works with …
Daniel-Cross 9d280d9
fixed lint issues
Daniel-Cross 252dee3
updated snapshots
Daniel-Cross bde49a6
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross bdbd6b8
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross 48bbe09
added tests
Daniel-Cross 01b93a3
fixed merge conflicts
Daniel-Cross 5862765
made changes to address the alignment and colour inheritance
Daniel-Cross 0718eeb
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross dc24a00
updated types
Daniel-Cross 3289ecd
changed type to interface
Daniel-Cross 2ef9b13
removed log and changed any
Daniel-Cross cf494ed
snapshot update
Daniel-Cross 795ab2a
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross 8b8c459
reverted comment
Daniel-Cross 5779a82
removed centre alignment
Daniel-Cross 6628758
updated snapshot
Daniel-Cross 87af066
revert button while fix
Daniel-Cross afe910e
updated tests to support new structure
Daniel-Cross fdbfaa4
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross c29d582
removed colours from snapuibutton as it's now been moved to button.ts
Daniel-Cross 39fce31
addressed lint issues in test
Daniel-Cross 9fcc492
linting on tests
Daniel-Cross 6cd0137
linting issue
Daniel-Cross 97b23b5
snapshot update
Daniel-Cross e1b84cc
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross c76a6d8
added tests
Daniel-Cross b03005d
linting issue
Daniel-Cross 82004b0
changed type to interface
Daniel-Cross 51e9d20
Merge branch 'main' into 3244-alignment-is-off-on-some-snapuibuttons
Daniel-Cross File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
app/components/Snaps/SnapUIButton/SnapUIButton.test.tsx
This file contains hidden or 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,202 @@ | ||
import React from 'react'; | ||
import { render, fireEvent } from '@testing-library/react-native'; | ||
import { SnapUIButton } from './SnapUIButton'; | ||
import { useSnapInterfaceContext } from '../SnapInterfaceContext'; | ||
import { ButtonType, UserInputEventType } from '@metamask/snaps-sdk'; | ||
import Text, { | ||
TextColor, | ||
} from '../../../component-library/components/Texts/Text'; | ||
import { View } from 'react-native'; | ||
import AnimatedLottieView from 'lottie-react-native'; | ||
|
||
jest.mock('../SnapInterfaceContext', () => ({ | ||
useSnapInterfaceContext: jest.fn(), | ||
})); | ||
|
||
jest.mock('../../../util/theme', () => ({ | ||
useTheme: jest.fn().mockReturnValue({ | ||
colors: { | ||
background: { default: '#FFFFFF' }, | ||
border: { muted: '#CCCCCC', default: '#DDDDDD' }, | ||
text: { default: '#000000' }, | ||
primary: { default: '#0376C9' }, | ||
error: { default: '#D73A49' }, | ||
}, | ||
}), | ||
})); | ||
|
||
describe('SnapUIButton', () => { | ||
const mockHandleEvent = jest.fn(); | ||
const mockOnPress = jest.fn(); | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(useSnapInterfaceContext as jest.Mock).mockReturnValue({ | ||
handleEvent: mockHandleEvent, | ||
}); | ||
}); | ||
|
||
const getColorForTextVariant = (color: TextColor): string => { | ||
switch (color) { | ||
case TextColor.Info: | ||
return '#0376C9'; | ||
case TextColor.Error: | ||
return '#D73A49'; | ||
case TextColor.Muted: | ||
return '#000000'; | ||
default: | ||
return '#000000'; | ||
} | ||
}; | ||
|
||
const createStyledText = ( | ||
text: string, | ||
color: TextColor = TextColor.Info, | ||
) => ( | ||
<Text color={color} style={{ color: getColorForTextVariant(color) }}> | ||
{text} | ||
</Text> | ||
); | ||
|
||
it('renders correctly with text children', () => { | ||
const { getByText } = render( | ||
<SnapUIButton>{createStyledText('Test Button')}</SnapUIButton>, | ||
); | ||
|
||
expect(getByText('Test Button')).toBeTruthy(); | ||
}); | ||
|
||
it('renders correctly when disabled', () => { | ||
const { getByText, getByTestId } = render( | ||
<SnapUIButton disabled testID="disabled-button"> | ||
{createStyledText('Disabled Button', TextColor.Muted)} | ||
</SnapUIButton>, | ||
); | ||
|
||
expect(getByText('Disabled Button')).toBeTruthy(); | ||
expect(getByTestId('disabled-button').props.disabled).toBe(true); | ||
}); | ||
|
||
it('renders with loading state', () => { | ||
const { UNSAFE_getByType } = render( | ||
<SnapUIButton loading>{createStyledText('Loading Button')}</SnapUIButton>, | ||
); | ||
|
||
expect(UNSAFE_getByType(AnimatedLottieView)).toBeTruthy(); | ||
}); | ||
|
||
it('calls onPress and handles ButtonClickEvent when pressed', () => { | ||
const { getByText } = render( | ||
<SnapUIButton onPress={mockOnPress} name="test-button"> | ||
{createStyledText('Click Me')} | ||
</SnapUIButton>, | ||
); | ||
|
||
fireEvent.press(getByText('Click Me')); | ||
|
||
expect(mockOnPress).toHaveBeenCalledTimes(1); | ||
expect(mockHandleEvent).toHaveBeenCalledWith({ | ||
event: UserInputEventType.ButtonClickEvent, | ||
name: 'test-button', | ||
}); | ||
}); | ||
|
||
it('handles FormSubmitEvent when button type is Submit', () => { | ||
const { getByText } = render( | ||
<SnapUIButton | ||
type={ButtonType.Submit} | ||
name="test-button" | ||
form="test-form" | ||
> | ||
{createStyledText('Submit Form')} | ||
</SnapUIButton>, | ||
); | ||
|
||
fireEvent.press(getByText('Submit Form')); | ||
|
||
expect(mockHandleEvent).toHaveBeenCalledWith({ | ||
event: UserInputEventType.ButtonClickEvent, | ||
name: 'test-button', | ||
}); | ||
expect(mockHandleEvent).toHaveBeenCalledWith({ | ||
event: UserInputEventType.FormSubmitEvent, | ||
name: 'test-form', | ||
}); | ||
}); | ||
|
||
it('renders icon component correctly', () => { | ||
const MockIcon = () => <View testID="mock-icon" />; | ||
MockIcon.displayName = 'Icon'; | ||
|
||
const { getByTestId } = render( | ||
<SnapUIButton testID="icon-button"> | ||
<MockIcon /> | ||
</SnapUIButton>, | ||
); | ||
|
||
expect(getByTestId('icon-button')).toBeTruthy(); | ||
expect(getByTestId('mock-icon')).toBeTruthy(); | ||
}); | ||
|
||
it('renders destructive variant button correctly', () => { | ||
const { getByTestId } = render( | ||
<SnapUIButton testID="destructive-button"> | ||
{createStyledText('Destructive Button', TextColor.Error)} | ||
</SnapUIButton>, | ||
); | ||
|
||
expect(getByTestId('destructive-button')).toBeTruthy(); | ||
}); | ||
|
||
it('renders with custom style', () => { | ||
const customStyle = { backgroundColor: '#FF0000', borderRadius: 16 }; | ||
const { getByTestId } = render( | ||
<SnapUIButton style={customStyle} testID="styled-button"> | ||
{createStyledText('Styled Button')} | ||
</SnapUIButton>, | ||
); | ||
|
||
const button = getByTestId('styled-button'); | ||
expect(button.props.style.backgroundColor).toBe('#FF0000'); | ||
expect(button.props.style.borderRadius).toBe(16); | ||
}); | ||
|
||
it('handles non-string, non-icon children properly', () => { | ||
const NonStringComponent = () => <View testID="non-string-component" />; | ||
|
||
const { getByTestId } = render( | ||
<SnapUIButton testID="custom-children-button"> | ||
<NonStringComponent /> | ||
</SnapUIButton>, | ||
); | ||
|
||
expect(getByTestId('custom-children-button')).toBeTruthy(); | ||
expect(getByTestId('non-string-component')).toBeTruthy(); | ||
}); | ||
|
||
it('sets proper accessibility properties', () => { | ||
const { getByTestId } = render( | ||
<SnapUIButton testID="accessible-button" name="button-name"> | ||
{createStyledText('Accessible Button')} | ||
</SnapUIButton>, | ||
); | ||
|
||
const button = getByTestId('accessible-button'); | ||
expect(button.props.accessible).toBe(true); | ||
expect(button.props.accessibilityRole).toBe('button'); | ||
expect(button.props.accessibilityLabel).toBe('button-name'); | ||
}); | ||
|
||
it('provides fallback to name for accessibilityLabel when children is not a string', () => { | ||
const NonStringComponent = () => <View testID="non-string-component" />; | ||
|
||
const { getByTestId } = render( | ||
<SnapUIButton testID="accessible-button" name="button-name"> | ||
<NonStringComponent /> | ||
</SnapUIButton>, | ||
); | ||
|
||
const button = getByTestId('accessible-button'); | ||
expect(button.props.accessibilityLabel).toBe('button-name'); | ||
}); | ||
}); |
This file contains hidden or 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 |
---|---|---|
@@ -1,45 +1,46 @@ | ||
import React, { FunctionComponent } from 'react'; | ||
import { ButtonType, UserInputEventType } from '@metamask/snaps-sdk'; | ||
import ButtonLink from '../../../component-library/components/Buttons/Button/variants/ButtonLink'; | ||
import { ButtonLinkProps } from '../../../component-library/components/Buttons/Button/variants/ButtonLink/ButtonLink.types'; | ||
import { | ||
TouchableOpacity, | ||
StyleSheet, | ||
ViewStyle, | ||
StyleProp, | ||
TouchableOpacityProps, | ||
} from 'react-native'; | ||
import { useSnapInterfaceContext } from '../SnapInterfaceContext'; | ||
import Text, { | ||
TextColor, | ||
TextVariant, | ||
} from '../../../component-library/components/Texts/Text'; | ||
import AnimatedLottieView from 'lottie-react-native'; | ||
import { useTheme } from '../../../util/theme'; | ||
|
||
export interface SnapUIButtonProps { | ||
export interface SnapUIButtonProps extends TouchableOpacityProps { | ||
name?: string; | ||
loading?: boolean; | ||
type?: ButtonType; | ||
form?: string; | ||
variant: keyof typeof COLORS; | ||
textVariant?: TextVariant; | ||
style?: StyleProp<ViewStyle>; | ||
disabled?: boolean; | ||
onPress?: () => void; | ||
children?: React.ReactNode; | ||
testID?: string; | ||
} | ||
|
||
const COLORS = { | ||
primary: TextColor.Info, | ||
destructive: TextColor.Error, | ||
disabled: TextColor.Muted, | ||
}; | ||
|
||
export const SnapUIButton: FunctionComponent< | ||
SnapUIButtonProps & ButtonLinkProps | ||
> = ({ | ||
export const SnapUIButton: FunctionComponent<SnapUIButtonProps> = ({ | ||
name, | ||
children, | ||
form, | ||
type = ButtonType.Button, | ||
variant = 'primary', | ||
disabled = false, | ||
loading = false, | ||
textVariant, | ||
style, | ||
onPress, | ||
testID, | ||
...props | ||
}) => { | ||
const { handleEvent } = useSnapInterfaceContext(); | ||
const { colors } = useTheme(); | ||
|
||
const handlePress = () => { | ||
onPress?.(); | ||
|
||
handleEvent({ | ||
event: UserInputEventType.ButtonClickEvent, | ||
name, | ||
|
@@ -54,34 +55,54 @@ export const SnapUIButton: FunctionComponent< | |
} | ||
}; | ||
|
||
const overriddenVariant = disabled ? 'disabled' : variant; | ||
const styles = StyleSheet.create({ | ||
button: { | ||
backgroundColor: colors.background.default, | ||
borderRadius: 8, | ||
paddingVertical: 8, | ||
flexDirection: 'row', | ||
alignItems: 'center', | ||
...(style as ViewStyle), | ||
}, | ||
loadingAnimation: { | ||
width: 24, | ||
height: 24, | ||
}, | ||
}); | ||
|
||
const color = COLORS[overriddenVariant as keyof typeof COLORS]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We still want to support the colors for the Snap UI button, but we may have to do it at the mapper level once #14355 lands |
||
if (loading) { | ||
return ( | ||
<TouchableOpacity | ||
style={styles.button} | ||
disabled | ||
accessible | ||
accessibilityRole="button" | ||
accessibilityLabel={typeof children === 'string' ? children : name} | ||
testID={testID} | ||
{...props} | ||
> | ||
<AnimatedLottieView | ||
source={{ uri: './loading.json' }} | ||
autoPlay | ||
loop | ||
style={styles.loadingAnimation} | ||
/> | ||
</TouchableOpacity> | ||
); | ||
} | ||
|
||
return ( | ||
<ButtonLink | ||
{...props} | ||
id={name} | ||
<TouchableOpacity | ||
style={styles.button} | ||
onPress={handlePress} | ||
disabled={disabled} | ||
label={ | ||
loading ? ( | ||
<AnimatedLottieView | ||
source={{ uri: './loading.json' }} | ||
autoPlay | ||
loop | ||
// eslint-disable-next-line react-native/no-inline-styles | ||
style={{ | ||
width: 24, | ||
height: 24, | ||
}} | ||
/> | ||
) : ( | ||
<Text color={color} variant={textVariant}> | ||
{children} | ||
</Text> | ||
) | ||
} | ||
/> | ||
accessible | ||
accessibilityRole="button" | ||
accessibilityLabel={typeof children === 'string' ? children : name} | ||
testID={testID} | ||
{...props} | ||
> | ||
{children} | ||
</TouchableOpacity> | ||
); | ||
}; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
?