Skip to content

Commit 94d822b

Browse files
authored
refactor(interaction): Enhance touch event handling with passive options
- Updated touch event bindings to use passive options for improved performance. Fix #4055
1 parent 2a6c5e1 commit 94d822b

File tree

9 files changed

+353
-14
lines changed

9 files changed

+353
-14
lines changed

src/ChartInternal/ChartInternal.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ export default class ChartInternal {
423423

424424
$el.svg
425425
.on("click", onclick?.bind($$.api) || null)
426-
.on(isTouch ? "touchstart" : "mouseenter", onover?.bind($$.api) || null)
426+
.on(isTouch ? "touchstart" : "mouseenter", onover?.bind($$.api) || null,
427+
isTouch ? {passive: true} : undefined)
427428
.on(isTouch ? "touchend" : "mouseleave", onout?.bind($$.api) || null);
428429
}
429430

src/ChartInternal/interactions/eventrect.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export default {
125125
const preventThreshold = (!isNaN(preventDefault) && preventDefault) || null;
126126
let startPx;
127127

128+
// Determine passive option based on preventDefault setting
129+
// If preventDefault is needed, passive must be false
130+
const passiveOption = !isPrevented && preventThreshold === null;
131+
128132
const preventEvent = event => {
129133
const eventType = event.type;
130134
const touch = event.changedTouches[0];
@@ -156,7 +160,7 @@ export default {
156160
.on("touchstart", event => {
157161
state.event = event;
158162
$$.updateEventRect();
159-
})
163+
}, {passive: passiveOption})
160164
.on("touchstart.eventRect touchmove.eventRect", event => {
161165
state.event = event;
162166

@@ -174,7 +178,7 @@ export default {
174178
} else {
175179
unselectRect();
176180
}
177-
}, true)
181+
}, {passive: passiveOption})
178182
.on("touchend.eventRect", event => {
179183
state.event = event;
180184

@@ -183,7 +187,7 @@ export default {
183187
state.cancelClick && (state.cancelClick = false);
184188
}
185189
}
186-
}, true);
190+
}, {passive: passiveOption});
187191

188192
svg.on("touchstart", event => {
189193
state.event = event;
@@ -192,7 +196,7 @@ export default {
192196
if (target && target !== eventRect.node()) {
193197
unselectRect();
194198
}
195-
});
199+
}, {passive: passiveOption});
196200
},
197201

198202
/**

src/ChartInternal/internals/tooltip.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,6 @@ export default {
591591
// hide tooltip
592592
tooltip
593593
.style("display", "none")
594-
.style("visibility", "hidden") // for IE9
595594
.datum(null);
596595

597596
callFn(config.tooltip_onhidden, api, selectedData);

src/ChartInternal/shape/arc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1185,7 +1185,7 @@ export default {
11851185
$$.callOverOutForTouch(arcData);
11861186

11871187
isUndefined(id) ? unselectArc() : selectArc(this, arcData, id);
1188-
});
1188+
}, {passive: true});
11891189
}
11901190
},
11911191

src/ChartInternal/shape/funnel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ export default {
232232
$$.showTooltip([data], event.target);
233233
/^(touchstart|mouseover)$/.test(event.type) && $$.setOverOut(true, data);
234234
}
235-
})
235+
}, isTouch ? {passive: true} : undefined)
236236
.on(isTouch ? "touchend" : "mouseout", event => {
237237
const data = getTarget(event);
238238

src/ChartInternal/shape/radar.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,11 @@ export default {
373373

374374
$$.selectRectForSingle(svg.node(), index);
375375
isMouse ? $$.setOverOut(true, index) : $$.callOverOutForTouch(index);
376-
})
376+
}, isMouse ? undefined : {passive: true})
377377
.on("mouseout", isMouse ? hide : null);
378378

379379
if (!isMouse) {
380-
svg.on("touchstart", hide);
380+
svg.on("touchstart", hide, {passive: true});
381381
}
382382
},
383383

src/ChartInternal/shape/treemap.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export default {
144144
$$.showTooltip([data], event.currentTarget);
145145
/^(touchstart|mouseover)$/.test(event.type) && $$.setOverOut(true, data);
146146
}
147-
})
147+
}, isTouch ? {passive: true} : undefined)
148148
.on(isTouch ? "touchend" : "mouseout", event => {
149149
const data = getTarget(event);
150150

src/config/Options/interaction/interaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ export default {
1717
* @property {boolean} [interaction.brighten=true] Make brighter for the selected area (ex. 'pie' type data selected area)
1818
* @property {boolean} [interaction.inputType.mouse=true] enable or disable mouse interaction
1919
* @property {boolean} [interaction.inputType.touch=true] enable or disable touch interaction
20-
* @property {boolean|number} [interaction.inputType.touch.preventDefault=false] enable or disable to call event.preventDefault on touchstart & touchmove event. It's usually used to prevent document scrolling.
20+
* @property {boolean|number} [interaction.inputType.touch.preventDefault=false] enable or disable to call event.preventDefault on touchstart & touchmove event. It's usually used to prevent document scrolling.<br>
21+
* - **NOTE**: When `true` is set, touch events are bound with [`{passive: false}`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#using_passive_listeners) option.
2122
* @property {boolean} [interaction.onout=true] Enable or disable "onout" event.<br>
2223
* When is disabled, defocus(hiding tooltip, focused gridline, etc.) event won't work.
2324
* @see [Demo: touch.preventDefault](https://naver.github.io/billboard.js/demo/#Interaction.PreventScrollOnTouch)

0 commit comments

Comments
 (0)