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

[BUG]: "useInView" docs typo #2793

Open
Keith-Web3 opened this issue Sep 7, 2024 · 2 comments
Open

[BUG]: "useInView" docs typo #2793

Keith-Web3 opened this issue Sep 7, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Keith-Web3
Copy link

Keith-Web3 commented Sep 7, 2024

1. Read the FAQs 👇

2. Describe the bug
It's more of a mistake in the example than a typo. To add a root to useInView, the example given was:

function Carousel() {
  const container = useRef(null)
  const ref = useRef(null)
  const isInView = useInView({ root: container })
  
  return (
    <div ref={container} style={{ overflow: "scroll" }}>
      <div ref={ref} />
    </div>
  )
}

But to make this work correctly, you're meant to pass the container ref as a second argument, like this:

function Carousel() {
  const container = useRef(null)
  const ref = useRef(null)
  const isInView = useInView(ref, { root: container })
  
  return (
    <div ref={container} style={{ overflow: "scroll" }}>
      <div ref={ref} />
    </div>
  )
}

Give a clear and concise description of what the bug is.

3. IMPORTANT: Provide a CodeSandbox reproduction of the bug
The docs site seems to be closed source so I can't. However, this example can be found here

A CodeSandbox minimal reproduction will allow us to quickly follow the reproduction steps. Without one, this bug report won't be accepted.

4. Steps to reproduce

Steps to reproduce the behavior:

  1. Go to https://www.framer.com/motion/use-in-view/#options
  2. See the typo

5. Expected behavior
The correct implementation should be:

function Carousel() {
  const container = useRef(null)
  const ref = useRef(null)
  const isInView = useInView(ref, { root: container })
  
  return (
    <div ref={container} style={{ overflow: "scroll" }}>
      <div ref={ref} />
    </div>
  )
}

A clear and concise description of what you expected to happen.

6. Video or screenshots

If applicable, add a video or screenshots to help explain the bug.

7. Environment details

If applicable, let us know which OS, browser, browser version etc you're using.

FAQs

React Server Components "use client" error

If you're importing motion or m into a React Server Component environment, ensure you're importing from framer-motion/client instead of framer-motion.

import * as motion from "framer-motion/client"
import * as m from "framer-motion/m"

Framer Motion won't install

Different versions of Framer Motion are compatible with different versions of React.

React 19: 12.0.0-alpha.0 or higher
React 18: 7.0.0 to 11.x
React 17: 6.x or lower

height: "auto" is jumping

Animating to/from auto requires measuring the DOM. There's no perfect way to do this and if you have also applied padding to the same element, these measurements might be wrong.

The recommended solution is to move padding to a child element. See this issue for the full discussion.

Preact isn't working

Framer Motion isn't compatible with Preact.

AnimatePresence isn't working

Have all of its immediate children got a unique key prop that remains the same for that component every render?

// Bad: The index could be given to a different component if the order of items changes
<AnimatePresence>
    {items.map((item, index) => (
        <Component key={index} />
    ))}
</AnimatePresence>
// Good: The item ID is unique to each component
<AnimatePresence>
    {items.map((item, index) => (
        <Component key={item.id} />
    ))}
</AnimatePresence>

Is the AnimatePresence correctly outside of the controlling conditional? AnimatePresence must be rendered whenever you expect an exit animation to run - it can't do so if it's unmounted!

// Bad: AnimatePresence is unmounted - exit animations won't run
{
    isVisible && (
        <AnimatePresence>
            <Component />
        </AnimatePresence>
    )
}
// Good: Only the children are unmounted - exit animations will run
<AnimatePresence>{isVisible && <Component />}</AnimatePresence>
@Keith-Web3 Keith-Web3 added the bug Something isn't working label Sep 7, 2024
@rafaelklaessen
Copy link

Maybe you could open a PR?

@Keith-Web3
Copy link
Author

@rafaelklaessen I would but the site is closed source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants