Skip to content

Commit

Permalink
lazy load features
Browse files Browse the repository at this point in the history
  • Loading branch information
t0rbik committed Nov 28, 2024
1 parent 75d6235 commit 10f2ed5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/components/layout/LeftNav.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HTMLAttributes, Ref, forwardRef } from "react";
import { useMatchRoute, createLink } from "@tanstack/react-router";
import { motion, MotionProps, useReducedMotion } from "framer-motion";
import { m, MotionProps, useReducedMotion } from "framer-motion";

import { cn } from "@/utils/cn";
import {
Expand All @@ -14,7 +14,7 @@ const MotionLinkForwardRef = forwardRef(
props: MotionProps & HTMLAttributes<HTMLAnchorElement>,
ref: Ref<HTMLAnchorElement>,
) => {
return <motion.a {...props} ref={ref} />;
return <m.a {...props} ref={ref} />;
},
);
MotionLinkForwardRef.displayName = "MotionLinkForwardRef";
Expand Down Expand Up @@ -63,7 +63,7 @@ export function LeftNav() {
to: routeTitleToPathMapping[item as RouteTitle].to,
fuzzy: true,
}) ? (
<motion.div
<m.div
layoutId="tab-indicator"
className="absolute inset-0 -z-10 rounded-xl bg-grey10inverse dark:bg-grey10"
/>
Expand Down Expand Up @@ -101,7 +101,7 @@ export function LeftNav() {
to: "/sentinel",
fuzzy: true,
}) ? (
<motion.div
<m.div
layoutId="tab-indicator"
className="absolute inset-0 -z-10 rounded-xl bg-grey10inverse dark:bg-grey10"
/>
Expand Down
17 changes: 13 additions & 4 deletions src/components/providers/FramerMotionProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import { LazyMotion, domMax } from "framer-motion";
import { LazyMotion } from "framer-motion";

// NOTE: Possible to reduce bundle size,
// see: https://www.framer.com/motion/guide-reduce-bundle-size/
// NOTE: Reduced initial bundle size.
// Lazy load rest of the Framer Motion features after the initial render.
// See: https://motion.dev/docs/react-reduce-bundle-size

// Make sure to return the specific export containing the feature bundle.
const loadFeatures = () =>
import("framer-motion").then((module) => module.domMax);

export const FramerMotionProvider = ({
children,
}: {
children: React.ReactNode;
}) => {
return <LazyMotion features={domMax}>{children}</LazyMotion>;
return (
<LazyMotion features={loadFeatures} strict>
{children}
</LazyMotion>
);
};

0 comments on commit 10f2ed5

Please sign in to comment.