framer-motion#MotionValue TypeScript Examples

The following examples show how to use framer-motion#MotionValue. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useWrapperScroll.ts    From tesla-homepage-ui-clone with MIT License 6 votes vote down vote up
useWrapperScroll = (): {
  scrollY: MotionValue<number>;
  scrollYProgress: MotionValue<number>;
} => {
  const { wrapperRef } = useContext(ModelsContext);

  const scrollY = useMotionValue(0);
  const scrollYProgress = useMotionValue(0);

  useEffect(() => {
    if (wrapperRef.current) {
      const updateScrollValue = () => {
        if (wrapperRef.current) {
          const { scrollTop, scrollHeight, offsetHeight } = wrapperRef.current;

          const fullScroll = scrollHeight - offsetHeight;

          scrollY.set(scrollTop);
          scrollYProgress.set(scrollTop / fullScroll);
        }
      };
      wrapperRef.current.addEventListener('scroll', updateScrollValue);

      return () => wrapperRef?.current?.removeEventListener('scroll', updateScrollValue);
    }
  }, [wrapperRef, scrollY, scrollYProgress]);

  return { scrollY, scrollYProgress };
}
Example #2
Source File: useInViewScroll.ts    From framer-motion-hooks with MIT License 6 votes vote down vote up
useInViewScroll = (
  el: RefObject<HTMLElement>,
  options: IOptions = {}
): MotionValue<number> => {
  const progress = useMotionValue(0)
  const { scrollY } = useViewportScroll()

  useEffect(() => {
    const handleScrollProgress = () => {
      const node = el.current
      if (!node) return

      const threshold = options.threshold || 0

      const elPosY = node.getBoundingClientRect().top + scrollY.get()
      const elHeight = node.scrollHeight

      const viewIntersect = Math.max(elPosY - window.innerHeight, 0)
      const current = scrollY.get() - viewIntersect - threshold
      const total = Math.min(window.innerHeight, elPosY) + elHeight - threshold

      const quotient = current / total

      if (quotient > 0 && quotient < 1) {
        progress.set(quotient)
      }
    }

    handleScrollProgress()
    const unsubscribeFromScroll = scrollY.onChange(handleScrollProgress)

    return () => unsubscribeFromScroll()
  }, [el, options])

  return progress
}
Example #3
Source File: useMotionAsState.ts    From framer-motion-hooks with MIT License 6 votes vote down vote up
useMotionAsState = (motionValue: MotionValue) => {
  const [state, setState] = useState()

  useEffect(() => {
    const handleChange = () => setState(motionValue.get())

    const unsubscribe = motionValue.onChange(handleChange)

    return () => unsubscribe()
  }, [])

  return state
}
Example #4
Source File: index.tsx    From tesla-homepage-ui-clone with MIT License 5 votes vote down vote up
Footer = ({ opacity }: { opacity: MotionValue<number> }) => {
  const footerLinks = [
    {
      text: `Tesla © ${new Date().getFullYear()}`,
      link: 'https://www.tesla.com/about',
      showOnMobile: true,
    },
    {
      text: `Privacy & Legal`,
      link: 'https://www.tesla.com/about/legal',
      showOnMobile: true,
    },
    {
      text: `Contact`,
      link: 'https://www.tesla.com/contact',
      showOnMobile: false,
    },
    {
      text: `Careers`,
      link: 'https://www.tesla.com/careers',
      showOnMobile: false,
    },
    {
      text: `Get Newsletter`,
      link: 'https://www.tesla.com/updates',
      showOnMobile: false,
    },
    {
      text: `News`,
      link: 'https://www.tesla.com/blog',
      showOnMobile: true,
    },
    {
      text: `Forums`,
      link: 'https://forums.tesla.com/',
      showOnMobile: false,
    },
    {
      text: `Locations`,
      link: 'https://www.tesla.com/findus/list',
      showOnMobile: false,
    },
  ];
  return (
    <Container style={{ opacity }}>
      <ul>
        {footerLinks.map(item => (
          <li key={item.text} className={item.showOnMobile ? 'show' : 'hide'}>
            <a href={item.link} target="_blank" rel="noopener noreferrer">
              {item.text}
            </a>
          </li>
        ))}
      </ul>
      <ul>
        <li className="copyright">
          {`Designed by  `}
          <a href="http://github.com/leoronne" target="_blank" rel="noopener noreferrer">
            Leonardo Ronne
          </a>
          , for study purposes
        </li>
      </ul>
    </Container>
  );
}