-
Notifications
You must be signed in to change notification settings - Fork 137
/
Paging.tsx
38 lines (36 loc) · 1.27 KB
/
Paging.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { useState, useRef } from 'react'
import { ReactReader } from '../../lib/index'
import type { NavItem, Rendition } from 'epubjs'
import { DEMO_URL, DEMO_NAME } from '../components/config'
import { Example } from '../components/Example'
export const Paging = () => {
const [page, setPage] = useState('')
const rendition = useRef<Rendition | undefined>(undefined)
const toc = useRef<NavItem[]>([])
const [location, setLocation] = useState<string | number>(0)
return (
<Example title="Paging example" actions={<div>{page}</div>}>
<ReactReader
url={DEMO_URL}
title={DEMO_NAME}
location={location}
tocChanged={(_toc) => (toc.current = _toc)}
locationChanged={(loc: string) => {
setLocation(loc)
if (rendition.current && toc.current) {
const { displayed, href } = rendition.current.location.start
const chapter = toc.current.find((item) => item.href === href)
setPage(
`Page ${displayed.page} of ${displayed.total} in chapter ${
chapter ? chapter.label : 'n/a'
}`
)
}
}}
getRendition={(_rendition: Rendition) => {
rendition.current = _rendition
}}
/>
</Example>
)
}