Skip to content
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 @@ -42,6 +42,7 @@ import {
import PerpsSectionProvider from '../../feeds/perps/PerpsSectionProvider';
import SitesSearchFooter from '../../../../UI/Sites/components/SitesSearchFooter/SitesSearchFooter';
import { strings } from '../../../../../../locales/i18n';
import { useScreenTransitionComplete } from '../../../../hooks/useScreenTransitionComplete';
import { MAX_ITEMS_PER_SECTION } from '../../search/viewMoreLabel';
import type { ExploreSearchRouteParams } from './ExploreSearchScreen.types';

Expand Down Expand Up @@ -316,6 +317,9 @@ const ExploreSearchScreen: React.FC = () => {
const [searchQuery, setSearchQuery] = useState(
() => route.params?.initialQuery?.trim() ?? '',
);
// Gates the keyboard, which iOS paints dark grey mid-push, and the results
// subtree, whose mount blocks the JS thread while the screen slides in.
const isTransitionComplete = useScreenTransitionComplete();

const handleSearchCancel = useCallback(() => {
setSearchQuery('');
Expand All @@ -334,12 +338,15 @@ const ExploreSearchScreen: React.FC = () => {
searchQuery={searchQuery}
onSearchChange={setSearchQuery}
onCancel={handleSearchCancel}
autoFocus={isTransitionComplete}
/>
</Box>

<PerpsSectionProvider>
<ExploreSearchContent searchQuery={searchQuery} />
</PerpsSectionProvider>
{isTransitionComplete ? (
<PerpsSectionProvider>
<ExploreSearchContent searchQuery={searchQuery} />
</PerpsSectionProvider>
) : null}
</Box>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,25 @@ describeForPlatforms('ExploreSearchScreen - Component Tests', () => {
);
});

it('holds back the keyboard and the results subtree until the screen transition settles', async () => {
const { getByTestId, queryByTestId, findByTestId } =
renderExploreSearchScreenWithRoutes();

const searchInput = getByTestId(
TrendingViewSelectorsIDs.EXPLORE_VIEW_SEARCH_TEXT_INPUT,
);

expect(searchInput.props.autoFocus).toBe(false);
expect(queryByTestId(ExploreSearchScreenSelectorsIDs.PILL_ALL)).toBeNull();

// Once settled, the input takes focus and the results mount.
await findByTestId(ExploreSearchScreenSelectorsIDs.PILL_ALL);
expect(
getByTestId(TrendingViewSelectorsIDs.EXPLORE_VIEW_SEARCH_TEXT_INPUT).props
.autoFocus,
).toBe(true);
});

it('"All" pill is selected by default and pill row is present on mount', async () => {
const { getByTestId } = renderExploreSearchScreenWithRoutes();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,71 @@ describe('ExploreSearchBar', () => {

expect(input.props.autoFocus).toBe(true);
});

it('does not auto-focus the TextInput when autoFocus is disabled', () => {
const mockOnSearchChange = jest.fn();
const mockOnCancel = jest.fn();

const { getByTestId } = render(
<ExploreSearchBar
type="interactive"
searchQuery=""
onSearchChange={mockOnSearchChange}
onCancel={mockOnCancel}
autoFocus={false}
/>,
);

const input = getByTestId(
TrendingViewSelectorsIDs.EXPLORE_VIEW_SEARCH_TEXT_INPUT,
);

expect(input.props.autoFocus).toBe(false);
});

it('matches the keyboard appearance to the theme', () => {
const mockOnSearchChange = jest.fn();
const mockOnCancel = jest.fn();

const { getByTestId } = render(
<ExploreSearchBar
type="interactive"
searchQuery=""
onSearchChange={mockOnSearchChange}
onCancel={mockOnCancel}
/>,
);

const input = getByTestId(
TrendingViewSelectorsIDs.EXPLORE_VIEW_SEARCH_TEXT_INPUT,
);

expect(input.props.keyboardAppearance).toBe('light');
});

it('turns autoFocus on when the caller enables it after mount', () => {
const mockOnSearchChange = jest.fn();
const mockOnCancel = jest.fn();

const renderBar = (autoFocus: boolean) => (
<ExploreSearchBar
type="interactive"
searchQuery=""
onSearchChange={mockOnSearchChange}
onCancel={mockOnCancel}
autoFocus={autoFocus}
/>
);

const { getByTestId, rerender } = render(renderBar(false));

rerender(renderBar(true));

const input = getByTestId(
TrendingViewSelectorsIDs.EXPLORE_VIEW_SEARCH_TEXT_INPUT,
);
expect(input.props.autoFocus).toBe(true);
});
Comment thread
vinnyhoward marked this conversation as resolved.
Resolved
});

describe('basic functionality toggle', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { TouchableOpacity } from 'react-native';
import React, { useEffect, useRef } from 'react';
import { TouchableOpacity, type TextInput } from 'react-native';
import {
Box,
BoxFlexDirection,
Expand All @@ -13,7 +13,11 @@ import {
IconColor,
TextColor,
} from '@metamask/design-system-react-native';
import { useTailwind } from '@metamask/design-system-twrnc-preset';
import {
Theme,
useTailwind,
useTheme,
} from '@metamask/design-system-twrnc-preset';
import { useSelector } from 'react-redux';
import { strings } from '../../../../../../locales/i18n';
import { selectBasicFunctionalityEnabled } from '../../../../../selectors/settings';
Expand All @@ -35,6 +39,13 @@ interface ExploreSearchBarInteractiveProps {
placeholder?: string;
/** Tailwind gap class for the search + cancel row. Defaults to `gap-2`. */
rowTwClassName?: string;
/**
* Focus the input. Defaults to `true`; flipping it from `false` to `true`
* focuses the input then, so callers can hold the keyboard back until their
* screen transition finishes — opening it mid-push makes iOS paint the
* keyboard dark grey until the animation settles.
*/
autoFocus?: boolean;
}

type ExploreSearchBarProps =
Expand All @@ -43,12 +54,25 @@ type ExploreSearchBarProps =

const ExploreSearchBar: React.FC<ExploreSearchBarProps> = (props) => {
const tw = useTailwind();
const theme = useTheme();
// Left unset, the input keeps the system keyboard, which stays light in dark mode.
const keyboardAppearance = theme === Theme.Dark ? 'dark' : 'light';

const isBasicFunctionalityEnabled = useSelector(
selectBasicFunctionalityEnabled,
);
const isButtonMode = props.type === 'button';
const rowTwClassName = props.rowTwClassName ?? 'gap-2';
const shouldFocus = props.type === 'interactive' && (props.autoFocus ?? true);
const inputRef = useRef<TextInput>(null);

// `autoFocus` only applies on mount, so callers turning it on later need this.
useEffect(() => {
if (shouldFocus) {
inputRef.current?.focus();
}
}, [shouldFocus]);

const placeholder =
props.placeholder ??
(isBasicFunctionalityEnabled
Expand Down Expand Up @@ -103,13 +127,15 @@ const ExploreSearchBar: React.FC<ExploreSearchBarProps> = (props) => {
value={props.searchQuery}
onChangeText={props.onSearchChange}
placeholder={placeholder}
autoFocus={props.type === 'interactive'}
autoFocus={shouldFocus}
inputRef={inputRef}
onPressClearButton={() => {
props.onSearchChange('');
}}
clearButtonProps={{ testID: 'explore-search-clear-button' }}
inputProps={{
autoCapitalize: 'none',
keyboardAppearance,
testID: TrendingViewSelectorsIDs.EXPLORE_VIEW_SEARCH_TEXT_INPUT,
}}
/>
Expand Down
121 changes: 121 additions & 0 deletions app/components/hooks/useScreenTransitionComplete.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { renderHook, act } from '@testing-library/react-native';
import {
SCREEN_TRANSITION_FALLBACK_MS,
useScreenTransitionComplete,
} from './useScreenTransitionComplete';

type TransitionEndListener = (event: { data: { closing: boolean } }) => void;
type FocusListener = () => void;

const mockUnsubscribe = jest.fn();
const mockIsFocused = jest.fn(() => true);
let transitionListener: TransitionEndListener | undefined;
let focusListener: FocusListener | undefined;

const mockAddListener = jest.fn(
(eventName: string, listener: TransitionEndListener & FocusListener) => {
if (eventName === 'transitionEnd') {
transitionListener = listener;
}
if (eventName === 'focus') {
focusListener = listener;
}
return mockUnsubscribe;
},
);

jest.mock('@react-navigation/native', () => ({
useNavigation: () => ({
addListener: mockAddListener,
isFocused: mockIsFocused,
}),
}));

describe('useScreenTransitionComplete', () => {
beforeEach(() => {
jest.useFakeTimers();
jest.clearAllMocks();
mockIsFocused.mockReturnValue(true);
transitionListener = undefined;
focusListener = undefined;
});

afterEach(() => {
jest.useRealTimers();
});

it('returns false before the transition ends', () => {
const { result } = renderHook(() => useScreenTransitionComplete());

expect(result.current).toBe(false);
});

it('returns true when the opening transition ends', () => {
const { result } = renderHook(() => useScreenTransitionComplete());

act(() => transitionListener?.({ data: { closing: false } }));

expect(result.current).toBe(true);
});

it('stays false when the transition belongs to the screen closing', () => {
const { result } = renderHook(() => useScreenTransitionComplete());

act(() => transitionListener?.({ data: { closing: true } }));

expect(result.current).toBe(false);
});

it('returns true after the fallback delay when no event is emitted', () => {
const { result } = renderHook(() => useScreenTransitionComplete());

act(() => {
jest.advanceTimersByTime(SCREEN_TRANSITION_FALLBACK_MS);
});

expect(result.current).toBe(true);
});

it('stays false when the fallback fires after the screen lost focus', () => {
const { result } = renderHook(() => useScreenTransitionComplete());

// The screen is dismissed before its opening transition ever ends, so it is
// animating out while still mounted.
mockIsFocused.mockReturnValue(false);
act(() => {
jest.advanceTimersByTime(SCREEN_TRANSITION_FALLBACK_MS);
});

expect(result.current).toBe(false);
});

it('re-arms the fallback when the screen comes back into focus', () => {
const { result } = renderHook(() => useScreenTransitionComplete());

mockIsFocused.mockReturnValue(false);
act(() => {
jest.advanceTimersByTime(SCREEN_TRANSITION_FALLBACK_MS);
});
expect(result.current).toBe(false);

mockIsFocused.mockReturnValue(true);
act(() => focusListener?.());
act(() => {
jest.advanceTimersByTime(SCREEN_TRANSITION_FALLBACK_MS);
});

expect(result.current).toBe(true);
});

it('clears the fallback timer and both listeners on unmount', () => {
const { unmount } = renderHook(() => useScreenTransitionComplete());

unmount();

expect(mockUnsubscribe).toHaveBeenCalledTimes(2);
// The pending fallback must not fire a state update on the unmounted hook.
act(() => {
jest.advanceTimersByTime(SCREEN_TRANSITION_FALLBACK_MS);
});
});
});
Loading
Loading