Skip to content

Commit

Permalink
Always use passive events for hover (#2923)
Browse files Browse the repository at this point in the history
* Always use passive events

* Cleaning cast
  • Loading branch information
mattgperry authored Dec 4, 2024
1 parent 6042f14 commit a1bec5a
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions packages/framer-motion/src/gestures/hover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,31 @@ import { extractEventInfo } from "../events/event-info"
function handleHoverEvent(
node: VisualElement<Element>,
event: PointerEvent,
isActive: boolean
lifecycle: "Start" | "End"
) {
const { props } = node

if (node.animationState && props.whileHover) {
node.animationState.setActive("whileHover", isActive)
node.animationState.setActive("whileHover", lifecycle === "Start")
}

const callback = props[isActive ? "onHoverStart" : "onHoverEnd"]
const eventName = ("onHover" + lifecycle) as "onHoverStart" | "onHoverEnd"
const callback = props[eventName]
if (callback) {
frame.postRender(() => callback(event, extractEventInfo(event)))
}
}

export class HoverGesture extends Feature<Element> {
mount() {
const { current, props } = this.node
const { current } = this.node
if (!current) return

this.unmount = hover(
current,
(startEvent) => {
handleHoverEvent(this.node, startEvent, true)
this.unmount = hover(current, (startEvent) => {
handleHoverEvent(this.node, startEvent, "Start")

return (endEvent) =>
handleHoverEvent(this.node, endEvent, false)
},
{ passive: !props.onHoverStart && !props.onHoverEnd }
)
return (endEvent) => handleHoverEvent(this.node, endEvent, "End")
})
}

unmount() {}
Expand Down

0 comments on commit a1bec5a

Please sign in to comment.