Skip to content

Fixing Framer Motion TypeScript Definitions To Allow Custom CSS Variables in style Property #1

@mazwrld

Description

@mazwrld

Issue: Framer Motion TypeScript Definitions Should Allow Custom CSS Variables in style Property

Description

The current TypeScript definitions for Framer Motion do not allow for the use of custom CSS variables in the style property of initial and animate objects. This limitation causes TypeScript errors when using custom CSS variables in these properties. The error message received is: "Object literal may only specify known properties, and ''--x'' does not exist in type 'string[] | AnimationControls | TargetAndTransition'."

Proposed Fix

Modify the TypeScript definitions to allow for custom CSS variables in the style property by using a custom type definition for the style property.

Steps to Reproduce

  1. Use custom CSS variables in the style property of initial or animate objects in a Framer Motion component.
  2. Run TypeScript checks or linting with ESLint.
  3. Observe TypeScript errors related to the use of custom CSS variables.

Example

import { motion, MotionProps } from 'framer-motion'

type CustomStyle = {
  '--x': string;
};

type CustomMotionProps = MotionProps & {
  initial: {
    style: CustomStyle;
    scale: number;
  };
  animate: {
    style: CustomStyle;
  };
};

const initialProps: CustomMotionProps = {
  initial: {
    style: {
      '--x': '100%',
    },
    scale: 1,
  },
  animate: {
    style: {
      '--x': '-100%',
    },
  },
};

// Use `initialProps` in a Framer Motion component

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions