St Page Flip react-pageflip


Simple React.js wrapper for StPageFlip library, for creating realistic and beautiful page turning effect.

Features of StPageFlip:

  • - Works with simple images on canvas and complex HTML blocks
  • - Has simple API and flexible configuration
  • - Compatible with mobile devices
  • - Supports landscape and portrait screen mode
  • - Supports soft and hard page types (only in HTML mode)
  • - No dependencies
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>
      );
    }
  }

Installation


You can install the latest version using npm:
npm install react-pageflip

Basic Usage


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>
  );
}

Advanced Usage


You can define pages as a component, but in this case you should use 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>
  );
}

Props


To set configuration use these props:

Events


You can use the following events:
...
class DemoBook extends React.Component {
    onFlip(data) {
        console.log('Current page: ' + data);
    }

    render() {
        return (
            <HTMLFlipBook
             /* props */
             onFlip={(e) => this.onFlip(e.data)}
            >
            /* ... pages */
            </HTMLFlipBook>
        )
    }
}
Available events:
Event object has two fields: data: number | string and object: PageFlip

Methods


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>
    )
  }
For example - flipping to the next page:
this.pageFlip.getPageFlip().flipNext();

Available methods:

NameParametersReturn valueDescription
getPageCountnumberGet number of all pages
getCurrentPageIndexnumberGet the current page number (starts at 0)
turnToPagepageNum: numbervoidTurn to the specified page number (without animation)
turnToNextPagevoidTurn to the next page (without animation)
turnToPrevPagevoidTurn to the previous page (without animation)
flippageNum: number, corner: 'top' | 'bottom'voidTurn to the specified page (with animation)
flipNextcorner: 'top' | 'bottom'voidTurn to the next page (with animation)
flipPrevcorner: 'top' | 'bottom'voidTurn to the previous page (with animation)
loadFromImagesimages: ['path-to-image'...]voidLoad page from images
loadFromHtmlitems: NodeListOf | HTMLElement[]voidLoad page from html elements
updateFromHtmlitems: NodeListOf | HTMLElement[]voidUpdate page from html elements new on 0.4.0
updateFromImagesimages: ['path-to-image1.jpg', ...]voidUpdate page from images new on 0.4.0
destroyvoidDestructor. Removes Parent HTML Element and all event handlers new on 0.4.0

Contacts


Oleg,
oleg.litovski9@gmail.com
Github