Simple React.js wrapper for StPageFlip library, for creating realistic and beautiful page turning effect.
Features of StPageFlip:
import HTMLFlipBook from "react-pageflip";
function MyBook(props) {
return (
<HTMLFlipBook width={300} height={500}>
<div className="demoPage">Page 1</div>
<div className="demoPage">Page 2</div>
<div className="demoPage">Page 3</div>
<div className="demoPage">Page 4</div>
</HTMLFlipBook>
);
}
const PageCover = React.forwardRef((props, ref) => {
return (
<div className="page page-cover" ref={ref} data-density="hard">
<div className="page-content">
<h2>{props.children}</h2>
</div>
</div>
);
});
const Page = React.forwardRef((props, ref) => {
return (
<div className="page" ref={ref}>
<div className="page-content">
<h2 className="page-header">Page header - {props.number}</h2>
<div className="page-image"></div>
<div className="page-text">{props.children}</div>
<div className="page-footer">{props.number + 1}</div>
</div>
</div>
);
});
class DemoBook extends React.Component {
constructor(props) {
super(props);
this.state = {
page: 0,
totalPage: 0,
};
}
nextButtonClick = () => {
this.flipBook.getPageFlip().flipNext();
};
prevButtonClick = () => {
this.flipBook.getPageFlip().flipPrev();
};
onPage = (e) => {
this.setState({
page: e.data,
});
};
componentDidMount() {
this.setState({
totalPage: this.flipBook.getPageFlip().getPageCount(),
});
}
render() {
return (
<div>
<HTMLFlipBook
width={550}
height={733}
size="stretch"
minWidth={315}
maxWidth={1000}
minHeight={400}
maxHeight={1533}
maxShadowOpacity={0.5}
showCover={true}
mobileScrollSupport={true}
onFlip={this.onPage}
onChangeOrientation={this.onChangeOrientation}
onChangeState={this.onChangeState}
className="demo-book"
ref={(el) => (this.flipBook = el)}
>
<PageCover>BOOK TITLE</PageCover>
<Page number={1}>Lorem ipsum...</Page>
<Page number={2}>Lorem ipsum...</Page>
/*...*/
<PageCover>THE END</PageCover>
</HTMLFlipBook>
<div className="container">
<div>
<button type="button" onClick={this.prevButtonClick}>
Previous page
</button>
[<span>{this.state.page}</span> of
<span>{this.state.totalPage}</span>]
<button type="button" onClick={this.nextButtonClick}>
Next page
</button>
</div>
<div>
State: <i>{this.state.state}</i>, orientation: <i>{this.state.orientation}</i>
</div>
</div>
</div>
);
}
}
npm install react-pageflip
import HTMLFlipBook from "react-pageflip";
function MyBook(props) {
return (
<HTMLFlipBook width={300} height={500}>
<div className="demoPage">Page 1</div>
<div className="demoPage">Page 2</div>
<div className="demoPage">Page 3</div>
<div className="demoPage">Page 4</div>
</HTMLFlipBook>
);
}
React.forwardRef
method. Like this:const Page = React.forwardRef((props, ref) => {
return (
<div className="demoPage" ref={ref}> /* ref required */
<h1>Page Header</h1>
<p>{props.children}</p>
<p>Page number: {props.number}</p>
</div>
);
});
function MyBook(props) {
return (
<HTMLFlipBook width={300} height={500}>
<Page number="1">Page text</Page>
<Page number="2">Page text</Page>
<Page number="3">Page text</Page>
<Page number="4">Page text</Page>
</HTMLFlipBook>
);
}
width: number
- required. Base page widthheight: number
- required. Base page heightsize:("fixed", "stretch")
- default: "fixed"
. Whether the book will be stretched under the parent element or notminWidth, maxWidth, minHeight, maxHeight: number
. You must set threshold values with size: "stretch"
drawShadow: bool
- default: true
. Draw shadows or not when page flippingflippingTime: number
(milliseconds) - default: 1000
. Flipping animation timeusePortrait: bool
- default: true
. Enable switching to portrait modestartZIndex: number
- default: 0
. Initial value to z-indexautoSize: bool
- default: true
. If this value is true, the parent element will be equal to the size of the bookmaxShadowOpacity: number[0..1]
- default: 1
. Shadow intensity (1: max intensity, 0: hidden shadows)showCover: boolean
- default: false
. If this value is true, the first and the last pages will be marked as hard and will be shown in single page modemobileScrollSupport: boolean
- default: true
. Disable content scrolling when touching a book on mobile devices...
class DemoBook extends React.Component {
onFlip(data) {
console.log('Current page: ' + data);
}
render() {
return (
<HTMLFlipBook
/* props */
onFlip={(e) => this.onFlip(e.data)}
>
/* ... pages */
</HTMLFlipBook>
)
}
}
flip: number
- triggered by page turningchangeOrientation: ("portrait", "landscape")
- triggered when page orientation changeschangeState: ("user_fold", "fold_corner", "flipping", "read")
- triggered when the state of the book changesdata: number | string
and object: PageFlip
To use methods, you need to get a PageFlip
object. This can be done using ref
and getPageFlip(): PageFlip
method
render() {
return (
<HTMLFlipBook
...
ref={(component) => (this.pageFlip = component)}
...
>
/* ... pages */
</HTMLFlipBook>
)
}
this.pageFlip.getPageFlip().flipNext();
Available methods:
Name | Parameters | Return value | Description |
---|---|---|---|
getPageCount |
| number | Get number of all pages |
getCurrentPageIndex |
| number | Get the current page number (starts at 0) |
turnToPage | pageNum: number | void | Turn to the specified page number (without animation) |
turnToNextPage |
| void | Turn to the next page (without animation) |
turnToPrevPage |
| void | Turn to the previous page (without animation) |
flip | pageNum: number, corner: 'top' | 'bottom' | void | Turn to the specified page (with animation) |
flipNext | corner: 'top' | 'bottom' | void | Turn to the next page (with animation) |
flipPrev | corner: 'top' | 'bottom' | void | Turn to the previous page (with animation) |
loadFromImages | images: ['path-to-image'...] | void | Load page from images |
loadFromHtml | items: NodeListOf | HTMLElement[] | void | Load page from html elements |
updateFromHtml | items: NodeListOf | HTMLElement[] | void | Update page from html elements new on 0.4.0 |
updateFromImages | images: ['path-to-image1.jpg', ...] | void | Update page from images new on 0.4.0 |
destroy |
| void | Destructor. Removes Parent HTML Element and all event handlers new on 0.4.0 |