Giter VIP home page Giter VIP logo

Comments (11)

olegpix avatar olegpix commented on June 16, 2024 5
image

Hi!
Please check out the official documentation here Preline - Next.js
You should add the following code to prevent TS warnings:

import { IStaticMethods } from "preline/preline";
declare global {
  interface Window {
    HSStaticMethods: IStaticMethods;
  }
}

from preline.

MeromTh avatar MeromTh commented on June 16, 2024 2

Try setTimeout to 200? or something like this

useEffect(() => {
  if (document.readyState === 'complete') {
    window.HSStaticMethods.autoInit()
  }
}, [path])

from preline.

VishalLudhrani avatar VishalLudhrani commented on June 16, 2024 1

I see many of us are getting confused by HSStaticMethods.
It's just a way of saying that you need to import HS (eg.: HSAccordion) and call autoInit on its instance.

Here's the PrelineLoader.jsx from one of my (Next.js) projects:

"use client";

import HSAccordion from "@preline/accordion";
import { usePathname } from "next/navigation";
import { HSDropdown } from "preline/preline";
import { useEffect } from "react";

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    import("preline/preline");
  }, []);

  useEffect(() => {
    setTimeout(() => {
      HSAccordion.autoInit();
      HSDropdown.autoInit();
    }, 100)
  }, [path])

  return <></>
}

I hope this helps. :)

from preline.

VishalLudhrani avatar VishalLudhrani commented on June 16, 2024 1

@VishalLudhrani So do I need to add HSDropdown instance if I want to ad the dropdown components? I'm still not getting them to work.

Yup. That is how it worked for me.

If you weren't able to get those to work, here's an alternative approach:

"use client";

import { usePathname } from "next/navigation";
import { useEffect } from "react";

const isBrowser = typeof window !== undefined;

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    if (isBrowser) {
      // if this component is rendered on a browser, import preline
      import("preline/preline");
    }
  }, [])

  useEffect(() => {
    setTimeout(() => {
      if (isBrowser) {
        // if this component is rendered on a browser, import relevant preline plugins
        import("preline/preline").then(({ HSAccordion, HSDropdown, HSCollapse }) => {
          HSAccordion.autoInit();
          HSDropdown.autoInit();
          HSCollapse.autoInit();
        })
      }
    }, 100)
  }, [path])

  return <></>
}

@Wigglor

from preline.

ollebergkvist avatar ollebergkvist commented on June 16, 2024

Also seeing this

from preline.

luguanhui avatar luguanhui commented on June 16, 2024

Also seeing this

from preline.

jordankimsey avatar jordankimsey commented on June 16, 2024

I'm facing the same issue.

from preline.

Wigglor avatar Wigglor commented on June 16, 2024

@VishalLudhrani So do I need to add HSDropdown instance if I want to ad the dropdown components? I'm still not getting them to work.

from preline.

insivika avatar insivika commented on June 16, 2024

I see many of us are getting confused by HSStaticMethods. It's just a way of saying that you need to import HS (eg.: HSAccordion) and call autoInit on its instance.

Here's the PrelineLoader.jsx from one of my (Next.js) projects:

"use client";

import HSAccordion from "@preline/accordion";
import { usePathname } from "next/navigation";
import { HSDropdown } from "preline/preline";
import { useEffect } from "react";

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    import("preline/preline");
  }, []);

  useEffect(() => {
    setTimeout(() => {
      HSAccordion.autoInit();
      HSDropdown.autoInit();
    }, 100)
  }, [path])

  return <></>
}

I hope this helps. :)

Hi can you please explain how HSAccordion is related to HSStaticMethods?

from preline.

VishalLudhrani avatar VishalLudhrani commented on June 16, 2024

@insivika HSStaticMethods could be a placeholder (just a way to say your plugin would go here).
You would need to replace it with all the plugins you're using in your project.

So, in my case, I replaced it with HSAccordion and HSDropdown since I was using both.

[EDIT]:
This was just my assumption, which seemed to work for me.

from preline.

Nishitbaria avatar Nishitbaria commented on June 16, 2024

I see many of us are getting confused by HSStaticMethods. It's just a way of saying that you need to import HS (eg.: HSAccordion) and call autoInit on its instance.
Here's the PrelineLoader.jsx from one of my (Next.js) projects:

"use client";

import HSAccordion from "@preline/accordion";
import { usePathname } from "next/navigation";
import { HSDropdown } from "preline/preline";
import { useEffect } from "react";

export default function PrelineLoader() {
  const path = usePathname();

  useEffect(() => {
    import("preline/preline");
  }, []);

  useEffect(() => {
    setTimeout(() => {
      HSAccordion.autoInit();
      HSDropdown.autoInit();
    }, 100)
  }, [path])

  return <></>
}

I hope this helps. :)

Hi can you please explain how HSAccordion is related to HSStaticMethods?

This Code is work for me ✨

from preline.

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.