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

fix(AndroidTV:TemplateStarter): Fix intial focus and prevent focus loss #1571

Merged
merged 1 commit into from
May 28, 2024
Merged
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
10 changes: 8 additions & 2 deletions packages/template-starter/src/app/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useContext, useEffect, useState } from 'react';
import { Text, Image, View, PixelRatio, TouchableOpacity, StatusBar, ScrollView } from 'react-native';
import React, { useContext, useEffect, useRef, useState } from 'react';
import { Text, Image, View, PixelRatio, TouchableOpacity, StatusBar, ScrollView, findNodeHandle } from 'react-native';
import { Api } from '@rnv/renative';
import { ICON_LOGO, CONFIG, ThemeProvider, ThemeContext, testProps } from '../config';
import packageJson from '../../package.json';
Expand All @@ -11,6 +11,7 @@ const App = () => (
);

const AppThemed = () => {
const buttonRef = useRef(null);
const { theme, toggle, dark } = useContext(ThemeContext);

const [pixelRatio, setPixelRatio] = useState(1);
Expand Down Expand Up @@ -51,8 +52,13 @@ const AppThemed = () => {
}`}</Text>
<Text style={theme.styles.textH3}>{`pixelRatio: ${pixelRatio}, ${fontScale}`}</Text>
<TouchableOpacity
ref={buttonRef}
onPress={toggle}
style={theme.styles.button}
// Set the initial AndroidTV and tvOS focus to be on the button
hasTVPreferredFocus
// On AndroidTV going up can appear as lost focus, so block focus up
nextFocusUp={findNodeHandle(buttonRef.current) || undefined}
{...testProps('template-starter-home-screen-try-my-button')}
>
<Text style={theme.styles.buttonText}>Try me!</Text>
Expand Down
Loading