Skip to content

Commit 6375f80

Browse files
authored
fix: wrap ConnectQRHardware in SafeAreaView (#30092)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until this PR meets the canonical Definition of Ready For Review in `docs/readme/ready-for-review.md`. In short: the template must be materially complete (not just section titles present), all status checks must be currently passing, and the only expected follow-up commits must be reviewer-driven. --> ## **Description** This PR updates the the QRConnectHardware to use `SafeAreaView` <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: updates the view of the QRConnectHardware to be aware of notches and bottom screen menus. ## **Related issues** Fixes: ## **Manual testing steps** Not applicable ## **Screenshots/Recordings** <img width="1080" height="2316" alt="Screenshot_20260513_182003_MetaMask (1)" src="https://github.com/user-attachments/assets/54a24da3-b121-4247-b6aa-e1db903f8ee8" /> <img width="1080" height="2316" alt="Screenshot_20260513_182607_MetaMask (1)" src="https://github.com/user-attachments/assets/ece772b2-548a-4a0f-b95f-619d3160ca8a" /> TBD ## **Pre-merge author checklist** <!-- Every checklist item must be consciously assessed before marking this PR as "Ready for review". A checked box means you deliberately considered that responsibility, not that you literally performed every action listed. Unchecked boxes are ambiguous: they are not an implicit "N/A" and they are not a silent "skip". See `docs/readme/ready-for-review.md` for the full checklist semantics. --> - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. #### Performance checks (if applicable) - [ ] I've tested on Android - Ideally on a mid-range device; emulator is acceptable - [ ] I've tested with a power user scenario - Use these [power-user SRPs](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/edit-v2/401401446401?draftShareId=9d77e1e1-4bdc-4be1-9ebb-ccd916988d93) to import wallets with many accounts and tokens - [ ] I've instrumented key operations with Sentry traces for production performance metrics - See [`trace()`](/app/util/trace.ts) for usage and [`addToken`](/app/components/Views/AddAsset/components/AddCustomToken/AddCustomToken.tsx#L274) for an example For performance guidelines and tooling, see the [Performance Guide](https://consensyssoftware.atlassian.net/wiki/spaces/TL1/pages/400085549067/Performance+Guide+for+Engineers). ## **Pre-merge reviewer checklist** <!-- Reviewer checklist items follow the same semantics as the author checklist: an unchecked box is ambiguous, a checked box means the reviewer consciously assessed that responsibility. See `docs/readme/ready-for-review.md`. --> - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Low risk UI/layout change limited to the `ConnectQRHardware` screen; main risk is unintended spacing on some devices due to safe-area edge configuration. > > **Overview** > Updates `ConnectQRHardware` to rely on `SafeAreaView` for notch/edge handling instead of manually applying `insets.top`, and explicitly limits safe-area padding to `top/left/right`. > > Adds `testID`s for the new safe-area container and header, and extends the test suite with safe-area-context mocks plus assertions that header top margin is removed and the container excludes the bottom edge in both instruction and account-selector states. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit b6f3016. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent ef84315 commit 6375f80

3 files changed

Lines changed: 108 additions & 11 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
export const ConnectQRHardwareSelectorsIDs = {
2+
CONTAINER: 'connect-qr-hardware-container',
3+
HEADER: 'connect-qr-hardware-header',
24
CONTINUE_BUTTON: 'qr-continue-button',
35
};

app/components/Views/ConnectQRHardware/index.test.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import renderWithProvider from '../../../util/test/renderWithProvider';
33
import Engine from '../../../core/Engine';
44
import ConnectQRHardware from './index';
5+
import { StyleSheet } from 'react-native';
56
import { fireEvent, act, waitFor } from '@testing-library/react-native';
67
import { ConnectQRHardwareSelectorsIDs } from './ConnectQRHardware.testIds';
78
import { backgroundState } from '../../../util/test/initial-root-state';
@@ -37,6 +38,30 @@ jest.mock('../../../components/hooks/useAnalytics/useAnalytics', () => ({
3738
}),
3839
}));
3940

41+
jest.mock('react-native-safe-area-context', () => {
42+
const ReactActual = jest.requireActual('react');
43+
const { View: MockView } = jest.requireActual('react-native');
44+
const inset = { top: 44, right: 0, bottom: 0, left: 0 };
45+
const frame = { x: 0, y: 0, width: 390, height: 844 };
46+
47+
return {
48+
...jest.requireActual('react-native-safe-area-context'),
49+
SafeAreaInsetsContext: ReactActual.createContext(inset),
50+
SafeAreaFrameContext: ReactActual.createContext(frame),
51+
SafeAreaView: ({ children, ...props }: { children: React.ReactNode }) =>
52+
ReactActual.createElement(MockView, props, children),
53+
SafeAreaProvider: ({ children, ...props }: { children: React.ReactNode }) =>
54+
ReactActual.createElement(MockView, props, children),
55+
SafeAreaConsumer: ({
56+
children,
57+
}: {
58+
children: (safeAreaInsets: typeof inset) => React.ReactNode;
59+
}) => children(inset),
60+
useSafeAreaInsets: () => inset,
61+
useSafeAreaFrame: () => frame,
62+
};
63+
});
64+
4065
jest.mock(
4166
'../../../core/HardwareWallet/contexts/HardwareWalletContext',
4267
() => ({
@@ -272,6 +297,70 @@ describe('ConnectQRHardware', () => {
272297
);
273298
});
274299

300+
it('does not add header top margin when SafeAreaView handles top inset', async () => {
301+
mockKeyringController.getAccounts.mockResolvedValue([]);
302+
303+
const { getByTestId } = renderWithProvider(
304+
<ConnectQRHardware navigation={mockedNavigate} />,
305+
{ state: mockInitialState },
306+
);
307+
308+
await waitFor(() => {
309+
expect(mockKeyringController.getAccounts).toHaveBeenCalledTimes(1);
310+
});
311+
312+
const header = getByTestId(ConnectQRHardwareSelectorsIDs.HEADER);
313+
314+
expect(header).toBeOnTheScreen();
315+
expect(StyleSheet.flatten(header.props.style).marginTop).toBeUndefined();
316+
});
317+
318+
it('excludes bottom edge from parent SafeAreaView because instruction owns bottom spacing', async () => {
319+
mockKeyringController.getAccounts.mockResolvedValue([]);
320+
321+
const { getByTestId } = renderWithProvider(
322+
<ConnectQRHardware navigation={mockedNavigate} />,
323+
{ state: mockInitialState },
324+
);
325+
326+
await waitFor(() => {
327+
expect(mockKeyringController.getAccounts).toHaveBeenCalledTimes(1);
328+
});
329+
330+
const safeAreaContainer = getByTestId(
331+
ConnectQRHardwareSelectorsIDs.CONTAINER,
332+
);
333+
334+
expect(safeAreaContainer.props.edges).toStrictEqual([
335+
'top',
336+
'left',
337+
'right',
338+
]);
339+
});
340+
341+
it('excludes bottom edge from parent SafeAreaView when account selector owns bottom spacing', async () => {
342+
mockKeyringController.getAccounts.mockResolvedValue([]);
343+
344+
const { getByTestId } = renderWithProvider(
345+
<ConnectQRHardware navigation={mockedNavigate} />,
346+
{ state: mockInitialState },
347+
);
348+
349+
const button = getByTestId(ConnectQRHardwareSelectorsIDs.CONTINUE_BUTTON);
350+
351+
await act(async () => {
352+
fireEvent.press(button);
353+
});
354+
355+
await waitFor(() => {
356+
expect(mockQrKeyring.getFirstPage).toHaveBeenCalledTimes(1);
357+
});
358+
359+
expect(
360+
getByTestId(ConnectQRHardwareSelectorsIDs.CONTAINER).props.edges,
361+
).toStrictEqual(['top', 'left', 'right']);
362+
});
363+
275364
it('renders first page correctly when user clicks `continue` button', async () => {
276365
mockKeyringController.getAccounts.mockResolvedValue([]);
277366

app/components/Views/ConnectQRHardware/index.tsx

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import React, {
66
useState,
77
} from 'react';
88
import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
9-
import {
10-
type EdgeInsets,
11-
useSafeAreaInsets,
12-
} from 'react-native-safe-area-context';
9+
import { SafeAreaView, type Edge } from 'react-native-safe-area-context';
1310
import Engine from '../../../core/Engine';
1411
import AnimatedQRScannerModal from '../../UI/QRHardware/AnimatedQRScanner';
1512
import AccountSelector from '../../UI/HardwareWallet/AccountSelector';
@@ -35,6 +32,7 @@ import { QrScanRequestType } from '@metamask/eth-qr-keyring';
3532
import { withQrKeyring } from '../../../core/QrKeyring/QrKeyring';
3633
import { getChecksumAddress } from '@metamask/utils';
3734
import { getConnectedDevicesCount } from '../../../core/HardwareWallets/analytics';
35+
import { ConnectQRHardwareSelectorsIDs } from './ConnectQRHardware.testIds';
3836
import { useHardwareWallet } from '../../../core/HardwareWallet/contexts/HardwareWalletContext';
3937
import { HardwareWalletType } from '@metamask/hw-wallet-sdk';
4038
import { useQrScanErrorForwarding } from '../../../core/HardwareWallet/hooks/useQrScanErrorForwarding';
@@ -48,15 +46,16 @@ interface IConnectQRHardwareProps {
4846
route?: any;
4947
}
5048

51-
const createStyles = (colors: ThemeColors, insets: EdgeInsets) =>
49+
const SAFE_AREA_EDGES: Edge[] = ['top', 'left', 'right'];
50+
51+
const createStyles = (colors: ThemeColors) =>
5252
StyleSheet.create({
5353
container: {
5454
flex: 1,
5555
flexDirection: 'column',
5656
alignItems: 'center',
5757
},
5858
header: {
59-
marginTop: insets.top,
6059
flexDirection: 'row',
6160
width: '100%',
6261
paddingHorizontal: 32,
@@ -94,8 +93,7 @@ const ConnectQRHardware = ({ navigation, route }: IConnectQRHardwareProps) => {
9493
const { colors } = useTheme();
9594
const { trackEvent, createEventBuilder } = useAnalytics();
9695
const { setTargetWalletType, setQrScanRetryHandler } = useHardwareWallet();
97-
const insets = useSafeAreaInsets();
98-
const styles = createStyles(colors, insets);
96+
const styles = createStyles(colors);
9997
const hideMarketingContent = route?.params?.hideMarketingContent ?? false;
10098

10199
const [isScanning, setIsScanning] = useState(false);
@@ -112,6 +110,7 @@ const ConnectQRHardware = ({ navigation, route }: IConnectQRHardwareProps) => {
112110
}, []);
113111

114112
const [existingAccounts, setExistingAccounts] = useState<string[]>([]);
113+
const safeAreaEdges = SAFE_AREA_EDGES;
115114

116115
useEffect(() => {
117116
setTargetWalletType(HardwareWalletType.Qr);
@@ -313,8 +312,15 @@ const ConnectQRHardware = ({ navigation, route }: IConnectQRHardwareProps) => {
313312

314313
return (
315314
<Fragment>
316-
<View style={styles.container}>
317-
<View style={styles.header}>
315+
<SafeAreaView
316+
style={styles.container}
317+
edges={safeAreaEdges}
318+
testID={ConnectQRHardwareSelectorsIDs.CONTAINER}
319+
>
320+
<View
321+
style={styles.header}
322+
testID={ConnectQRHardwareSelectorsIDs.HEADER}
323+
>
318324
<Icon
319325
name="qrcode"
320326
size={42}
@@ -347,7 +353,7 @@ const ConnectQRHardware = ({ navigation, route }: IConnectQRHardwareProps) => {
347353
title={strings('connect_qr_hardware.select_accounts')}
348354
/>
349355
)}
350-
</View>
356+
</SafeAreaView>
351357
<AnimatedQRScannerModal
352358
visible={isScanning}
353359
purpose={QrScanRequestType.PAIR}

0 commit comments

Comments
 (0)