React Reader is a react-wrapper for epub.js - an iframe based epub-reader that can run in browser, cordova and other web-based enviroments.
npm i react-reader
At the minimum you will need to give ReactReader
you will need this:
import React, { useState } from 'react'
import { ReactReader } from 'react-reader'
export const App = () => {
const [location, setLocation] = useState<string | number>(0)
return (
<div style={{ height: '100vh' }}>
<ReactReader
url="https://react-reader.metabits.no/files/alice.epub"
location={location}
locationChanged={(epubcfi: string) => setLocation(epubcfi)}
/>
</div>
)
}
This will render a reader like this:
title
[string] - the title of the book, displayed above the reading-canvasshowToc
[boolean] - whether to show the toc / toc-navreaderStyles
[object] - override the default stylesepubViewStyles
[object] - override the default styles for inner EpubViewswipeable
[boolean, default false] - enable swiping left/right with react-swipeable. Warning this will disable interacting with epub.js iframe content like selectionurl
[string, required] - url to the epub-file, if its on another domain, remember to add cors for the file. Epubjs fetch this by a http-call, so it need to be public available.loadingView
[element] - if you want to customize the loadingViewlocation
[string, number, null] - set / update location of the epublocationChanged
[func] - a function that receives the current location while user is reading. This function is called everytime the page changes, and also when it first renders.tocChanged
[func] - when the reader has parsed the book you will receive an array of the chaptersepubInitOptions
[object] - pass custom properties to the epub init function, see epub.jsepubOptions
[object] - pass custom properties to the epub rendition, see epub.js's book.renderTo functiongetRendition
[func] - when epubjs has rendered the epub-file you can get access to the epubjs-rendition object hereisRTL
[boolean] - support for RTL reading direction, thanks to @ktpm489EpubView
is just the iframe-view from EpubJS if you would like to build the reader yourself, see above for props
ReactReader
is now fully written in Typescript, so your editor should give you information of types for all props.
(thanks to for earlier contributions @rafaelsaback)
See Basic example:
Use a state from local-storage.
Override styles for ReactReader and Epub.js for multiple themes
Epub.js is only rendering the current chapter so we can only get the current page and number of pages within this chapter.
This shows how to hook into epubJS annotations object and let the user highlight selection and store this in a list where user can go to a selection or delete it.
EpubJS will try to parse the epub-file you pass to it, but if the server send wrong mine-types or the file does not contain .epub
you can use the epubInitOptions prop to force reading it right.
import React from 'react'
import { ReactReader } from 'react-reader'
const App = () => {
return (
<div style={{ height: '100vh' }}>
<ReactReader
url="/my-epub-service"
epubInitOptions={{
openAs: 'epub',
}}
/>
</div>
)
}
Pass options for this into epubJS in the prop epubOptions
Quick reference for manager and flow options:
enum ManagerOptions {
default = 'default', // Default setting, use when flow is set to auto/paginated.
continuous = 'continuous', // Renders stuff offscreen, use when flow is set to "scrolled".
}
enum FlowOptions {
default = 'auto', // Based on OPF settings, defaults to "paginated"
paginated = 'paginated', // Left to right, paginated rendering. Better paired with the default manager.
scrolled = 'scrolled', // Scrolled viewing, works best with "continuous" manager.
}
Things will look weird if you use the wrong manager/flow combination.
EpubJS is a browser-based epub-reader and it works by rendering the current epub-chapter into an iframe, and then by css-columns it will display the current page.
epub 2
standard, but most epub 3
features should work since its based on regular html-tags, but there can be more issues with those See Epub on WikipediaAlso be aware that the epub-standard is basically a zip of html-files, and there is a range in quality. Most publishers create pretty ok epubs, but in some older books there could be errors that will make rendering fails.
A tip if you have problems with not valid epub-files is to override the build in DOMParser and modify the markup-string passed to its parseFromString function. This example fixes a not valid <title/>
tag in an old epub, which would render as a blank page if not fixed:
const DOMParser = window.DOMParser
class OwnParser {
parseFromString(markup, mime) {
if (markup.indexOf('<title/>') !== -1) {
markup = markup.replace('<title/>', '');
}
return new DOMParser().parseFromString(markup, mime)
}
}
window.DOMParser = OwnParser
Epubjs is rendering the epub-content inside and iframe which defaults to sandbox="allow-same-origin"
, to enable opening links or running javascript in an epub, you will need to pass some extra params in the epubOptions
prop.
<ReactReader
url={localFile}
epubOptions={{
allowPopups: true, // Adds `allow-popups` to sandbox-attribute
allowScriptedContent: true, // Adds `allow-scripts` to sandbox-attribute
}}
/>