@@ -2,6 +2,7 @@ import type { MotionState } from '@/state/motion-state'
22import { Feature } from '@/features'
33import { frame , press } from 'framer-motion/dom'
44import type { EventInfo } from 'framer-motion'
5+ import type { Options } from '@/types'
56
67export function extractEventInfo ( event : PointerEvent ) : EventInfo {
78 return {
@@ -35,30 +36,46 @@ function handlePressEvent(
3536
3637export class PressGesture extends Feature {
3738 isActive ( ) {
38- return Boolean ( this . state . options . press )
39+ return Boolean ( this . state . options . whilePress )
3940 }
4041
4142 constructor ( state : MotionState ) {
4243 super ( state )
4344 }
4445
4546 mount ( ) {
46- const element = this . state . element
47- if ( ! element )
48- return
49- this . unmount = press (
50- element ,
51- ( el , startEvent ) => {
52- handlePressEvent ( this . state , startEvent , 'Start' )
47+ this . register ( )
48+ }
49+
50+ update ( ) {
51+ const preProps = this . state . visualElement . prevProps as unknown as Options
52+ // Re-register if whilePress changes
53+ if ( preProps . whilePress !== this . state . options . whilePress ) {
54+ this . register ( )
55+ }
56+ }
57+
58+ register ( ) {
59+ // Unmount previous press handler
60+ this . unmount ( )
61+ if ( this . isActive ( ) ) {
62+ const element = this . state . element
63+ if ( ! element )
64+ return
65+ this . unmount = press (
66+ element ,
67+ ( el , startEvent ) => {
68+ handlePressEvent ( this . state , startEvent , 'Start' )
5369
54- return ( endEvent , { success } ) =>
55- handlePressEvent (
56- this . state ,
57- endEvent ,
58- success ? 'End' : 'Cancel' ,
59- )
60- } ,
61- { useGlobalTarget : this . state . options . globalPressTarget } ,
62- )
70+ return ( endEvent , { success } ) =>
71+ handlePressEvent (
72+ this . state ,
73+ endEvent ,
74+ success ? 'End' : 'Cancel' ,
75+ )
76+ } ,
77+ { useGlobalTarget : this . state . options . globalPressTarget } ,
78+ )
79+ }
6380 }
6481}
0 commit comments