Skip to content

Missing typeof window guard in animateTarget causes TypeError in non-browser JS runtimes #3735

@Huxpro

Description

@Huxpro

Problem

In motion-dom, the animateTarget function in animation/interfaces/visual-element-target.mjs accesses window.MotionHandoffAnimation (line 62) without a typeof window guard:

// visual-element-target.mjs:62
if (window.MotionHandoffAnimation) {

When window is undefined (e.g., in non-browser JS runtimes, or when shadowed by an IIFE wrapper), this throws:

TypeError: Cannot read properties of undefined (reading 'MotionHandoffAnimation')

Other modules in the same codebase correctly guard against this. For example, render/utils/reduced-motion/index.mjs:

const isBrowser = typeof window !== "undefined";

Affected files

The following files in motion-dom access window without a typeof guard (all inside function bodies, not module-level):

  • animation/interfaces/visual-element-target.mjswindow.MotionHandoffAnimation (the immediate crash)
  • render/VisualElement.mjswindow.MotionCheckAppearSync
  • projection/node/HTMLProjectionNode.mjswindow, window.getComputedStyle
  • projection/node/create-projection-node.mjswindow.MotionHasOptimisedAnimation, window.MotionCancelOptimisedAnimation, window.innerWidth
  • render/dom/style-computed.mjswindow.getComputedStyle
  • render/html/HTMLVisualElement.mjswindow.getComputedStyle
  • animation/keyframes/DOMKeyframesResolver.mjswindow.pageYOffset, window.getComputedStyle
  • animation/keyframes/KeyframesResolver.mjswindow.scrollTo
  • animation/utils/css-variables-conversion.mjswindow.getComputedStyle
  • gestures/hover.mjswindow.addEventListener/removeEventListener
  • gestures/press/index.mjswindow.addEventListener/removeEventListener
  • resize/handle-window.mjswindow.innerWidth/innerHeight, window.addEventListener
  • utils/supports/scroll-timeline.mjswindow.ScrollTimeline

Context

This issue was discovered while using framer-motion/dom in Lynx, a cross-platform UI framework. Lynx's web runtime wraps main-thread scripts in an IIFE that shadows window to simulate a non-browser environment:

(function(){ "use strict"; const window = void 0; /* bundled code */ })()

The const window = void 0 creates an immutable lexical binding that shadows the global window. All bare window accesses inside this IIFE see undefined instead of the real window object.

typeof window is safe because typeof never throws, even on undefined values. The reduced-motion module already uses this pattern correctly.

Suggested fix

Add a typeof window !== "undefined" guard before accessing window properties. For example in visual-element-target.mjs:

// Before:
if (window.MotionHandoffAnimation) {

// After:
if (typeof window !== "undefined" && window.MotionHandoffAnimation) {

This matches the existing pattern used in reduced-motion/index.mjs and is a standard practice for code that may run outside a browser context.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions