### Install Project Dependencies Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/packages/example/README.md Run this command to install all necessary project dependencies. ```bash yarn install ``` -------------------------------- ### Install react-tv-space-navigation Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/tutorial.md Install the package using npm or yarn. This package has no external dependencies. ```bash npm install react-tv-space-navigation # or yarn add react-tv-space-navigation ``` -------------------------------- ### Run Web Application Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/packages/example/README.md Command to run the web version of the project, starting a development server with Webpack. ```bash yarn web ``` -------------------------------- ### Run TVOS Application Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/packages/example/README.md Commands to start the TV application on Apple TV or Android TV using React Native's Metro bundler. ```bash yarn start ``` ```bash yarn ios ``` ```bash yarn android ``` -------------------------------- ### SpatialNavigationVirtualizedGrid Usage Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/api.md Example of how to use the SpatialNavigationVirtualizedGrid component with its essential props. ```APIDOC ## SpatialNavigationVirtualizedGrid ### Description Component for rendering a virtualized grid with spatial navigation capabilities. ### Props - `data` (Array) - Required - The data array to be rendered. - `renderItem` (Function) - Required - A function that renders each item in the grid. - `numberOfColumns` (number) - Required - The number of columns in the grid. - `itemHeight` (number) - Required - The height of each item in the grid. - `additionalItemsRendered` (number) - Optional - Number of additional items to render beyond the viewport. - `onEndReachedThresholdRowsNumber` (number) - Optional - Threshold for triggering `onEndReached` event in rows. - `rowContainerStyle` (object) - Optional - Styles to apply to the row container. ``` -------------------------------- ### Configure Remote Control for Web Source: https://context7.com/bamlab/react-tv-space-navigation/llms.txt Register platform-specific key event listeners. This must be called once at app startup before any spatial navigation components are rendered. This example shows configuration for the web platform using keyboard events. ```tsx import { Directions, SpatialNavigation } from 'react-tv-space-navigation'; // Web platform example — call this once at app entry point SpatialNavigation.configureRemoteControl({ remoteControlSubscriber: (callback) => { const mapping: Record = { ArrowRight: Directions.RIGHT, ArrowLeft: Directions.LEFT, ArrowUp: Directions.UP, ArrowDown: Directions.DOWN, Enter: Directions.ENTER, }; const handler = (keyEvent: KeyboardEvent) => { const direction = mapping[keyEvent.code]; if (direction !== undefined) { callback(direction); } }; window.addEventListener('keydown', handler); return handler; // returned value is passed to unsubscriber }, remoteControlUnsubscriber: (handler) => { window.removeEventListener('keydown', handler); }, }); ``` -------------------------------- ### Configure Remote Control for Web Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/api.md Configure the remote control interaction for SpatialNavigation components using configureRemoteControl. This example sets up arrow key listeners for web environments. ```jsx SpatialNavigation.configureRemoteControl({ remoteControlSubscriber: (callback) => { const mapping = { ArrowRight: Directions.RIGHT, ArrowLeft: Directions.LEFT, ArrowUp: Directions.UP, ArrowDown: Directions.DOWN, }; const eventId = window.addEventListener('keydown', (keyEvent) => { callback(mapping[keyEvent.code]); }); return eventId; }, remoteControlUnsubscriber: (eventId) => { window.removeEventListener('keydown', eventId); }, }); ``` -------------------------------- ### SpatialNavigationNode with Active State Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/api.md This example shows how to use SpatialNavigationNode to conditionally style a container based on whether any of its child nodes are active. Note that using 'isActive' on virtualized nodes is not recommended. ```jsx {({ isActive }) => ( )} ``` -------------------------------- ### SpatialNavigationNode Usage Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/api.md Example of how to use the SpatialNavigationNode component to manage focus and selection within a navigation hierarchy. It accepts props like onFocus, onSelect, orientation, and isFocusable. ```APIDOC ## SpatialNavigationNode ### Description Manages spatial navigation for its children, handling focus and selection events. ### Props - **onFocus** (function) - Callback when the node gains focus. - **onSelect** (function) - Callback when the node is selected. - **orientation** (string) - 'horizontal' or 'vertical' to define navigation direction. - **isFocusable** (boolean) - Determines if the node can receive focus. ### Render Props - **isFocused** (boolean) - True if the node is currently focused. - **isRootActive** (boolean) - True if the node is the active root in the navigation tree. - **isActive** (boolean) - True if the node or one of its descendants is focused (use with caution on virtualized nodes). ### Example ```jsx console.log('Node gained focus')} onSelect={() => console.log('Node was selected')} orientation="horizontal" isFocusable={true} > {({ isFocused, isRootActive }) => Hello World!} ``` ``` -------------------------------- ### SpatialNavigationView with Horizontal Direction Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/api.md Illustrates the usage of SpatialNavigationView to manage the orientation of child navigation nodes. This example sets the direction to horizontal, affecting how nodes are laid out and navigated. ```jsx const FocusableNode = () => ( {({ isFocused }) => Hello World!} ); {/* Nodes will be horizontal, and flex view will be row */} ; ``` -------------------------------- ### Provide Accessibility Props for Focusable Components Source: https://github.com/bamlab/react-tv-space-navigation/blob/main/docs/api.md Use this hook to get accessibility properties for a focusable component. It includes workarounds for TalkBack compatibility, requiring an initial 'Enter' press for custom focus. ```tsx const Button = () => { // You can't use the hook directly in `FocusableButton` since it needs to access // the current SpatialNavigationNode's context const accessibilityProps = useSpatialNavigatorFocusableAccessibilityProps(); return ; }; const FocusableButton = () => { // Do not put the hook here! return {({isFocused}) =>