@@ -202,6 +202,24 @@ export default function EpubViewer(props: {
202202 ) ;
203203 const previous = props . book . chapters . findLastIndex ( ( chapter , i ) => i < index && chapter . linear ) ;
204204 const next = props . book . chapters . findIndex ( ( chapter , i ) => i > index && chapter . linear ) ;
205+ const reader = useRef < HTMLDivElement > ( null ) ;
206+ // A button that disables itself when pressed would drop keyboard focus out of the reader.
207+ const keepFocusIf = ( disables : boolean ) => {
208+ if ( disables ) reader . current ?. focus ( { preventScroll : true } ) ;
209+ } ;
210+ const stepChapter = ( target : number ) => {
211+ const forward = target > index ;
212+ keepFocusIf (
213+ ! props . book . chapters . some (
214+ ( chapter , i ) => chapter . linear && ( forward ? i > target : i < target ) ,
215+ ) ,
216+ ) ;
217+ setLocation ( { index : target } ) ;
218+ } ;
219+ const changeZoom = ( value : number , limit : number ) => {
220+ keepFocusIf ( value === limit ) ;
221+ setZoom ( value ) ;
222+ } ;
205223 return (
206224 < div className = "flex min-h-0 flex-1 flex-col" >
207225 { props . book . chapters . length > 1 || props . book . contents . length > 1 ? (
@@ -212,7 +230,7 @@ export default function EpubViewer(props: {
212230 < FileSurfaceAction
213231 label = { gt `Previous chapter` }
214232 disabled = { previous < 0 }
215- onPress = { ( ) => setLocation ( { index : previous } ) }
233+ onPress = { ( ) => stepChapter ( previous ) }
216234 >
217235 { props . book . rtl ? (
218236 < ChevronRightIcon className = "size-3.5" />
@@ -245,7 +263,7 @@ export default function EpubViewer(props: {
245263 < FileSurfaceAction
246264 label = { gt `Next chapter` }
247265 disabled = { next < 0 }
248- onPress = { ( ) => setLocation ( { index : next } ) }
266+ onPress = { ( ) => stepChapter ( next ) }
249267 >
250268 { props . book . rtl ? (
251269 < ChevronLeftIcon className = "size-3.5" />
@@ -263,50 +281,58 @@ export default function EpubViewer(props: {
263281 < FileSurfaceAction
264282 label = { gt `Decrease reading size` }
265283 disabled = { zoom <= 0.5 }
266- onPress = { ( ) => setZoom ( zoom - 0.25 ) }
284+ onPress = { ( ) => changeZoom ( zoom - 0.25 , 0.5 ) }
267285 >
268286 < MinusIcon className = "size-3.5" />
269287 </ FileSurfaceAction >
270288 < span className = "min-w-10 text-center tabular-nums" > { Math . round ( zoom * 100 ) } %</ span >
271289 < FileSurfaceAction
272290 label = { gt `Increase reading size` }
273291 disabled = { zoom >= 2 }
274- onPress = { ( ) => setZoom ( zoom + 0.25 ) }
292+ onPress = { ( ) => changeZoom ( zoom + 0.25 , 2 ) }
275293 >
276294 < PlusIcon className = "size-3.5" />
277295 </ FileSurfaceAction >
278296 < FileSurfaceAction
279297 label = { gt `Reset reading size` }
280298 disabled = { zoom === 1 }
281- onPress = { ( ) => setZoom ( 1 ) }
299+ onPress = { ( ) => changeZoom ( 1 , 1 ) }
282300 >
283301 < RotateCcwIcon className = "size-3.5" />
284302 </ FileSurfaceAction >
285303 </ div >
286- { page ? (
287- < ReadingFrame
288- key = { index }
289- page = { page }
290- name = { `${ props . name } : ${ chapterLabel ( index ) } ` }
291- location = { location }
292- zoom = { zoom }
293- navigate = { setLocation }
294- />
295- ) : result && "message" in result ? (
296- < div
297- role = "alert"
298- className = "flex flex-1 flex-col items-center justify-center gap-1 px-6 text-center"
299- >
300- < p className = "text-sm font-medium" >
301- < T > No preview for this file</ T >
302- </ p >
303- < p className = "max-w-sm text-xs text-muted-foreground" >
304- { previewFailureText ( result , messages ) }
305- </ p >
306- </ div >
307- ) : (
308- < FileSurfaceLoading />
309- ) }
304+ < div
305+ ref = { reader }
306+ role = "region"
307+ aria-label = { props . name }
308+ tabIndex = { - 1 }
309+ className = "flex min-h-0 flex-1 flex-col outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring"
310+ >
311+ { page ? (
312+ < ReadingFrame
313+ key = { index }
314+ page = { page }
315+ name = { `${ props . name } : ${ chapterLabel ( index ) } ` }
316+ location = { location }
317+ zoom = { zoom }
318+ navigate = { setLocation }
319+ />
320+ ) : result && "message" in result ? (
321+ < div
322+ role = "alert"
323+ className = "flex flex-1 flex-col items-center justify-center gap-1 px-6 text-center"
324+ >
325+ < p className = "text-sm font-medium" >
326+ < T > No preview for this file</ T >
327+ </ p >
328+ < p className = "max-w-sm text-xs text-muted-foreground" >
329+ { previewFailureText ( result , messages ) }
330+ </ p >
331+ </ div >
332+ ) : (
333+ < FileSurfaceLoading />
334+ ) }
335+ </ div >
310336 </ div >
311337 ) ;
312338}
0 commit comments