File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -80,6 +80,35 @@ const $_className = 'className';
80
80
81
81
let unstableWrapperId : number = 0 ;
82
82
let ROOT : Component < any > | null = null ;
83
+ let delegatedEvents : Record < string , WeakMap < HTMLElement , ( e : Event ) => void > > = {
84
+ click : new WeakMap ( ) ,
85
+ } ;
86
+
87
+ function handleDelegatedEvent ( e : Event ) {
88
+ let target = e . target as HTMLElement ;
89
+ let maxDepth = 3 ;
90
+ while ( target && target !== document . body && maxDepth > 0 ) {
91
+ maxDepth -- ;
92
+ if ( delegatedEvents . click . has ( target ) ) {
93
+ break ;
94
+ }
95
+ target = target . parentElement ! ;
96
+ }
97
+ const fn = delegatedEvents . click . get ( target ) ;
98
+ if ( fn ) {
99
+ fn ( e ) ;
100
+ }
101
+ }
102
+
103
+ if ( ! IN_SSR_ENV ) {
104
+ Object . keys ( delegatedEvents ) . forEach ( ( name ) => {
105
+ document . addEventListener ( name , handleDelegatedEvent ) ;
106
+ } ) ;
107
+ }
108
+
109
+ export function $_delegateEvent ( element : HTMLElement , name : string , fn : ( e : Event ) => void ) {
110
+ delegatedEvents [ name ] . set ( element , fn ) ;
111
+ }
83
112
84
113
export function $_TO_VALUE ( reference : unknown ) {
85
114
if ( isFn ( reference ) ) {
@@ -324,6 +353,10 @@ function $ev(
324
353
}
325
354
}
326
355
} else {
356
+ if ( eventName === 'click' ) {
357
+ $_delegateEvent ( element , eventName , fn as EventListener ) ;
358
+ return ;
359
+ }
327
360
// event case (on modifier)
328
361
if ( RUN_EVENT_DESTRUCTORS_FOR_SCOPED_NODES ) {
329
362
destructors . push (
You can’t perform that action at this time.
0 commit comments