Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to properly import in next app #13

Closed
sagarkhadka opened this issue Sep 11, 2022 · 31 comments
Closed

How to properly import in next app #13

sagarkhadka opened this issue Sep 11, 2022 · 31 comments
Labels
question Further information is requested

Comments

@sagarkhadka
Copy link

I have done 'npm install mouse-follower --save' on my next app. How do I correctly import it. I managed to import somehow in _app.tsx but every time page refresh the line 'const cursor = new MouseFollower();' throws an server error and crasheh the web page. But if I commented it and uncomment it then it works fine. But after this if the page refresh it again shows the same error.

@sagarkhadka sagarkhadka reopened this Sep 11, 2022
@Drafteed Drafteed added the question Further information is requested label Sep 13, 2022
@Drafteed
Copy link
Member

Please show your code with import statement.

@sagarkhadka
Copy link
Author

I downloaded the file and copied all files from js and scss folder and imported them like this in _app.tsx

import Cursor from '../../public/assets/mouse/js/cursor'

function MyApp({ Component, pageProps }: AppProps) {
  const cursor = new Cursor()

  return (
    <>
      {/* <NavLink /> */}
      <Component {...pageProps} />
    </>
  )
}

This works fine and shows mouse follower as well but after making some changes in the components it crashes with this error
image

@Drafteed
Copy link
Member

You are using an older version of library from another repo. Please read the instructions carefully, you do not need to download anything manually.

@sagarkhadka
Copy link
Author

First I tried the same way shown in the instruction and got the same error above. After this I downloaded the file and still got the same result.

@1realjulius
Copy link

Do you have any instructions to add this to a next js app

@Giuseppetm
Copy link

This library manipulates document, and document is not available until client render.
With a useEffect on your index.js file (or _app.js if you are using custom structure) you solve this problem.

import MouseFollower from "mouse-follower";
import gsap from "gsap";

MouseFollower.registerGSAP(gsap);

export default function MyApp({ Component, pageProps }) {
    useEffect(() => {
        const cursor = new MouseFollower();
    })

    return (
        <>
            <Head>
            </Head>

            <Component {...pageProps} />
        </>
    )
}

@sagarkhadka
Copy link
Author

This works without any error thank you very much for the tip.
As you suggested, I import mouse-follower in _app.tsx. It does not show any error like previous. If I want to use different features like sticky or magnetic on different component how can i do it ?

@Giuseppetm
Copy link

This works without any error thank you very much for the tip. As you suggested, I import mouse-follower in _app.tsx. It does not show any error like previous. If I want to use different features like sticky or magnetic on different component how can i do it ?

It's explained in the library documentation.
Sticky effect is here.

<div data-cursor-stick>Hover me to stick cursor!</div>

If it doesn't work because of React try this:
<div data-cursor-stick="#stick-me">Hover <div id="stick-me">me</div> to stick cursor!</div>

@sagarkhadka
Copy link
Author

Thank You very much for the tip.

I think I found some bug. Or it might be just me who doesn't know how to use properly.

<div data-cursor-text="MY WORK" className={style.imageContainer}>

Here I am trying to achieve something like this

image

But, I am getting following result.

image

@Giuseppetm
Copy link

What's that class you are using? If you need to style something try to do it inside of the div.
Also make sure that the color of the text is not the same as the one of the cursor.

<div data-cursor-text="MY WORK">
      <div className={style.imageContainer}> .. </>
</div>

@sagarkhadka
Copy link
Author

sagarkhadka commented Oct 20, 2022

This is what I am getting.

FalconTechNepal.__.Leading.IT.company.in.Nepal.-.Brave.2022-10-20.12-07-07.mp4

And this is the code in the component

<section className={style.works}>
  <div className={`${style.cardBox} row row-cols-2`}>
    {work.map((ourWork, index) => (
      <div
        key={index}
        className={index % 2 === 0 ? `${style.box1}` : `${style.box2}`}>
        <div
          data-cursor='-color-red -lg'
          data-cursor-text='MY WORK'>
          <div className={style.imageContainer}>
            <Image
              src={ourWork}
              width={900}
              height={1000}
              objectFit='cover'
              alt=''
            />
          </div>
        </div>
      </div>
    ))}
  </div>
</section>

@Giuseppetm
Copy link

This issue is probably related to the fact you are using a map function with each element having the same data-cursor.
Try to use it on a single item (without map), if it works it means this is the problem. There probably is some documentation about having the cursor available for multiple items.

@sagarkhadka
Copy link
Author

sagarkhadka commented Oct 20, 2022

I don't think map function is a problem here. As you mentioned I tried this on another element which is not mapped and still the situation is same. Here is the screenshot

image

And the code is

<div data-cursor-text='App  Dev'>
    <Image src={laptop} alt='laptop' layout='responsive' />
</div>

@Giuseppetm
Copy link

Is the style initialized correctly? I had to import index.scss of the library from node_modules
@import "../node_modules/mouse-follower/src/scss/index.scss";

@sagarkhadka
Copy link
Author

I also imported index.scss in global.scss file
@import "/node_modules/mouse-follower/src/scss/index.scss";

@gaurdivit
Copy link

@sagarkhadka am unable to understand what to import and where to import...

@sagarkhadka
Copy link
Author

sagarkhadka commented Jan 30, 2023

@gaurdivit In next.js I did following things:

  1. npm install gsap and npm install mouse-follower
  2. In your _app.tsx/.jsx file import following
    import MouseFollower from 'mouse-follower' import gsap from 'gsap'
  3. After the import do the following in same file
    MouseFollower.registerGSAP(gsap) useEffect(() => { const cursor = new MouseFollower() }, [])
  4. And lastly in your global.css import the scss file for the mouse-follower
    @import "/node_modules/mouse-follower/src/scss/index.scss";

After doing all this you should be able to see mouse follower in your web project.

@h3ma209
Copy link

h3ma209 commented Feb 9, 2023

hello @sagarkhadka can you explain more about your solution please becuz i did exactly what you said but it doesnt work
it creates another scrollbar but doesnt show anything

@sagarkhadka
Copy link
Author

@h3ma209 after you mentioned the issue I tried in new fresh next js project and I also got the same issue as you. The mouse follower is not visible and is showing weird behavior like overflowing the page. Since the MouseFollower is using scss in their source file I tried npm i sass and renamed the global.css to global.scss and it is working as expected. So, try installing sass it will work.

@h3ma209
Copy link

h3ma209 commented Feb 10, 2023

it works thank you very much

@gaurdivit
Copy link

@sagarkhadka can I use it in react js?

@gaurdivit
Copy link

if yes then are the steps same as of next js?

@gaurdivit
Copy link

Syntax error: /Users/divitgaur/Documents/stuff/Personal/WEB/Projects/myown_portfolio/node_modules/mouse-follower/src/scss/index.scss Unknown word (102:11)
100 | transition:opacity 0.4s, transform 0.3s;
101 |

102 | #{$root}.-text &, #{$root}.-icon & {
| ^
103 | opacity:1;
104 | transform:scale(1);
WARNING in [eslint]
src/App.js
Line 15:11: 'cursor' is assigned a value but never used no-unused-vars

this error is showing @sagarkhadka

@sagarkhadka
Copy link
Author

sagarkhadka commented Feb 11, 2023

In react as well the step is same. I just checked in fresh vite react project it is working fine please check the following steps.

  1. npm i gsap mouse-follower sass
  2. rename your index.css to index.scss and include @import "/node_modules/mouse-follower/src/scss/index.scss";
  3. In your app.js
import gsap from 'gsap'
import MouseFollower from 'mouse-follower'

MouseFollower.registerGSAP(gsap)

useEffect(() => {
  const cursor = new MouseFollower()
}, [])

This should do the thing.

@h3ma209
Copy link

h3ma209 commented Feb 11, 2023

@sagarkhadka also please how can i put the image behind the title or text when it hovers
for example cubertos landing section
when i land on website shows the video and puts it behind the title instead of being over the title and hiding it
also how to resize it on hover

@gaurdivit
Copy link

It's working perfectly fine didn't change my app.css to app.scss
Thanks you so much @sagarkhadka
Wanna connect?

@h3ma209
Copy link

h3ma209 commented Feb 11, 2023

@sagarkhadka please can you answer my question

@sagarkhadka
Copy link
Author

@h3ma209 I haven't used this feature previously so it took time to know the issue. When image is shown on hover the mouse follower uses .-media class so in your scss file you can do .-media { z-index: -99 } which will put the images and videos you use to the back side of your text element. There might be some other ways too but this also works.

image

To resize you can do transform: scale(1.75) in ::before class of .-media. But, when doing this image is not filling the gap.

@h3ma209
Copy link

h3ma209 commented Feb 12, 2023

ok thank you it did work but also i wanted to the title to change color so its visible like
i tried using mix-blend-mode: difference but didn't work

@joshAntoniuk
Copy link

Is there a way to make this work with tailwind?

@sagarkhadka
Copy link
Author

sagarkhadka commented Jul 2, 2023

Hey @joshAntoniuk, what problem are you facing. I think even when using tailwind css, the process should be same as shown here. I tried on fresh next 13.4.4 with tailwind with the same process it is working. If you want to change the color or something I think you can do it using @apply rule of tailwind this in scss file

.mf-cursor {
  @apply text-emerald-600;

  @media screen and (max-width: 900px) {
    @apply hidden;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

7 participants