Skip to content

Commit

Permalink
Merge pull request #1571 from flexn-io/fix/androidtv-template-starter…
Browse files Browse the repository at this point in the history
…-initial-focus

fix(AndroidTV:TemplateStarter): Fix intial focus and prevent focus loss
  • Loading branch information
pavjacko authored May 28, 2024
2 parents 8e25edf + 9df6eb1 commit d9f732b
Showing 1 changed file with 8 additions and 2 deletions.
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

0 comments on commit d9f732b

Please sign in to comment.