motion icon indicating copy to clipboard operation
motion copied to clipboard

[FEATURE] Next.JS optimization

Open vunsh opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. I've been running into an issue while building Next.JS applications. Because of the nature of next and the usage of react hooks with framer-motion, it is necessary to provide 'use client' at the top of files using framer motion.

By forcing client side rendering with framer-motion, browser compatibility and performance becomes very spotty. For example, while a site on Opera using client side framer-motion on next may work fine, one on google chrome may not. From further investigation, this seems to be based on performance limits and how different browsers handle different things. Edge seems to have a higher success rate than chrome, while Opera I haven't run into any issues with at all.

When I say doesn't work, I mean the page will go completely unresponsive. You can see this in action by opening my personal site (work in progress) here: https://vun.sh

Depending on the browser you open this on, it will likely freeze entirely.

Describe the solution you'd like I'm not sure if this is possible, but perhaps a better next integration that allows for framer to work without being forced into client side rendering. I don't really have any specifics but if this is possible I thought I should at least bring it up.

Describe alternatives you've considered My only solution at the moment is to disable framer-motion in my projects entirely, which fixes the issue

Additional context N.A.

FAQs

Does Framer Motion support Preact?

It might work with Preact, but we don't explicitly support it, so if it does currently work you'd be using it at your own risk.

vunsh avatar Jun 26 '23 01:06 vunsh

I want to mention that "use client" has existed in react forever. Your pages can be server rendered and still use these components (which are later hydrated in the client). I won't disagree that it'd be nice to have your requested feature though.

aech12 avatar Jul 09 '23 21:07 aech12

This is almost certainly nothing to do with Framer Motion. There is no reason why a client component would make a page unresponsive, obviously React sites and apps usually run fully on the client and we don't see any issues.

mattgperry avatar Jan 05 '24 13:01 mattgperry

you can use this custom component Motion.tsx. I created this myself. If you want to use motion.div, you do not need to give the type, if you have other values ​​such as p,h1,h2 and so on, then give the type prop.

"use client";

import { motion, MotionProps } from "framer-motion";
import React from "react";

interface CustomMotionProps<Tag extends keyof JSX.IntrinsicElements>
  extends MotionProps {
  type?: Tag;
  children: React.ReactNode;
  className: string | undefined | null;
}

export const Motion = <Tag extends keyof JSX.IntrinsicElements>({
  type,
  children,
  className,
  ...props
}: CustomMotionProps<Tag>) => {
  const Component = type ? (motion as any)[type] : motion.div;
  return (
    <Component className={className} {...props}>
      {children}
    </Component>
  );
};

yusifaliyevpro avatar Jul 15 '24 07:07 yusifaliyevpro