Giter Club home page Giter Club logo

Comments (2)

gozenc avatar gozenc commented on July 26, 2024 2

You can use a custom hook for your images in your page component. Here's a hook I created:

import React from "react";

export default function useInView(ref: React.RefObject<HTMLDivElement>) {
  const [inView, setInView] = React.useState(false);

  React.useEffect(() => {
    if (!ref.current) return;
    const observer = new window.IntersectionObserver(
      ([entry]) => {
        if (entry.isIntersecting) {
          if (inView) {
            observer.disconnect();
          }
          setInView(true);
          return;
        }
      },
      {
        root: null,
        threshold: 0.1,
      }
    );

    observer.observe(ref.current);
    return () => {
      observer.disconnect();
    };
  }, [ref]);

  return { inView };
}

and I am using this in component by passing ref:

interface PageBookletProps {
  title: string;
  forceImgLoad?: boolean;
  img1Url: string;
  img2Url?: string;
}

const PageBooklet = React.forwardRef((props: PageBookletProps, ref: any) => {
  const container = React.useRef<HTMLDivElement>(null);
  const { inView } = useInView(container);

  return (
    <div ref={ref}>
      <div
        ref={container}
        className="h-full w-full bg-cover bg-top bg-no-repeat flex items-center justify-center"
        style={{ backgroundImage: "url('assets/booklet-bg.JPG')" }}
      >
        <div className="h-[70%] w-full flex flex-col justify-between">
          <div className="h-[90%] flex justify-evenly">
            <div className="h-full">
              <img
                className="h-full border-8 border-slate-400"
                alt="First Image"
                src={
                  props.forceImgLoad
                    ? props.img1Url
                    : inView
                    ? props.img1Url
                    : undefined
                }
              />
            </div>
            {props.img2Url && (
              <div className="h-full">
                <img
                  className="h-full border-8 border-slate-400"
                  alt="Second Image"
                  src={inView ? props.img2Url : undefined}
                />
              </div>
            )}
          </div>
          <h1 className="font-bold uppercase text-slate-400">{props.title}</h1>
        </div>
      </div>
    </div>
  );
});

export default PageBooklet;

with this hook, it only loads when page is flipped. I created this because I am displaying a booklet with more than 850 pages with images.

from react-pageflip.

Adnanmi-3125 avatar Adnanmi-3125 commented on July 26, 2024

Is there any update on this or maybe a way through which this can be implemented

I want to load a pdf of 200 pages with this

any help would be appreciated

from react-pageflip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.