11'use strict' ;
2- import { Text , ScrollView } from 'react-native' ;
2+ import { Text , ScrollView , View , Dimensions } from 'react-native' ;
33import React from 'react' ;
44import { Example } from '../components/Example' ;
55import { Page } from '../components/Page' ;
@@ -9,6 +9,37 @@ import {usePageFocusManagement} from '../hooks/usePageFocusManagement';
99export const ScrollViewExamplePage : React . FunctionComponent < { navigation ?: any } > = ( { navigation} ) => {
1010 const firstScrollViewExampleRef = usePageFocusManagement ( navigation ) ;
1111 const { colors} = useTheme ( ) ;
12+
13+ // Refs and state for keyboard-focusable ScrollViews
14+ const horizontalScrollViewRef = React . useRef < ScrollView > ( null ) ;
15+ const [ scrollOffset , setScrollOffset ] = React . useState ( 0 ) ;
16+
17+ // Keyboard event handler for horizontal ScrollView
18+ const handleHorizontalScrollViewKeyDown = ( e : any ) => {
19+ if ( ! horizontalScrollViewRef . current ) return ;
20+
21+ const windowWidth = Dimensions . get ( 'window' ) . width ;
22+ const scrollAmount = windowWidth * 0.1 ; // 10% of window width
23+
24+ if ( e . nativeEvent . key === 'ArrowLeft' ) {
25+ e . preventDefault ( ) ;
26+ const newOffset = Math . max ( 0 , scrollOffset - scrollAmount ) ;
27+ setScrollOffset ( newOffset ) ;
28+ horizontalScrollViewRef . current . scrollTo ( {
29+ x : newOffset ,
30+ animated : true ,
31+ } ) ;
32+ } else if ( e . nativeEvent . key === 'ArrowRight' ) {
33+ e . preventDefault ( ) ;
34+ const newOffset = scrollOffset + scrollAmount ;
35+ setScrollOffset ( newOffset ) ;
36+ horizontalScrollViewRef . current . scrollTo ( {
37+ x : newOffset ,
38+ animated : true ,
39+ } ) ;
40+ }
41+ } ;
42+
1243 const example1jsx = `<ScrollView style={{height: 40}}>
1344 <Text>
1445 Here is a very long snippet of text. The goal is for this text to be
@@ -130,6 +161,52 @@ export const ScrollViewExamplePage: React.FunctionComponent<{navigation?: any}>
130161 </ Text >
131162 </ ScrollView >
132163 </ Example >
164+ < Example title = "A keyboard-focusable horizontal ScrollView." code = { `<View>
165+ <Text style={{marginBottom: 8, color: 'gray', fontSize: 12}}>
166+ Focus this ScrollView with Tab, then use Arrow Left/Right keys to scroll
167+ </Text>
168+ <ScrollView
169+ ref={horizontalScrollViewRef}
170+ style={{height: 40, borderWidth: 1, borderColor: 'gray'}}
171+ horizontal={true}
172+ onKeyDown={handleHorizontalScrollViewKeyDown}
173+ keyboardEvents={['keyDown']}
174+ focusable={true}
175+ onScroll={(event) => setScrollOffset(event.nativeEvent.contentOffset.x)}
176+ scrollEventThrottle={16}>
177+ <Text>
178+ Here is a very long snippet of text that requires horizontal scrolling...
179+ </Text>
180+ </ScrollView>
181+ </View>` } >
182+ < View >
183+ < Text style = { { marginBottom : 8 , color : colors . text , fontSize : 12 } } >
184+ Focus this ScrollView with Tab, then use Arrow Left/Right keys to scroll
185+ </ Text >
186+ < ScrollView
187+ ref = { horizontalScrollViewRef }
188+ style = { { height : 40 , borderWidth : 1 , borderColor : colors . border } }
189+ horizontal = { true }
190+ { ...( {
191+ onKeyDown : handleHorizontalScrollViewKeyDown ,
192+ keyboardEvents : [ 'keyDown' ] ,
193+ focusable : true ,
194+ } as any ) }
195+ onScroll = { ( event : any ) => setScrollOffset ( event . nativeEvent . contentOffset . x ) }
196+ scrollEventThrottle = { 16 } >
197+ < Text style = { { color : colors . text } } >
198+ Here is a very long snippet of text. The goal is for this text to be
199+ too long to fit inside this view which has a width restriction, so you
200+ can use the arrow keys to scroll horizontally when this ScrollView is focused.
201+ This demonstrates keyboard navigation support for horizontal ScrollViews.
202+ You can press Tab to focus this ScrollView, then use the Left and Right arrow
203+ keys to scroll the content horizontally. This is very useful for accessibility
204+ and keyboard-only navigation. Let's make this text even longer to demonstrate
205+ the scrolling functionality more effectively.
206+ </ Text >
207+ </ ScrollView >
208+ </ View >
209+ </ Example >
133210 < Example title = "A disabled ScrollView." code = { example3jsx } >
134211 < ScrollView style = { { height : 40 } } scrollEnabled = { false } >
135212 < Text style = { { color : colors . text } } >
0 commit comments