Skip to content

Commit

Permalink
fix: prevent browser scroll while scrolling over the tooltip (#5414)
Browse files Browse the repository at this point in the history
* fix: prevent browser scroll while scrolling over the tooltip

* fix: lint

* fix page scroll if tooltip scroll reaches to end

* move logic to preventParentScroll util function
  • Loading branch information
oykuyilmaz authored Dec 11, 2023
1 parent aac600d commit c6475c0
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ var dom = require("./lib/dom");
var snippetManager = require("./snippets").snippetManager;
var config = require("./config");
var event = require("./lib/event");
var preventParentScroll = require("./lib/scroll").preventParentScroll;

/**
* @typedef BaseCompletion
Expand Down Expand Up @@ -622,13 +623,14 @@ class Autocomplete {
this.tooltipNode = dom.createElement("div");
this.tooltipNode.style.margin = "0";
this.tooltipNode.style.pointerEvents = "auto";
this.tooltipNode.style.overscrollBehavior = "contain";
this.tooltipNode.tabIndex = -1;
this.tooltipNode.onblur = this.blurListener.bind(this);
this.tooltipNode.onclick = this.onTooltipClick.bind(this);
this.tooltipNode.id = "doc-tooltip";
this.tooltipNode.setAttribute("role", "tooltip");
// prevent editor scroll if tooltip is inside an editor
this.tooltipNode.addEventListener("wheel", event.stopPropagation);
this.tooltipNode.addEventListener("wheel", preventParentScroll);
}
var theme = this.editor.renderer.theme;
this.tooltipNode.className = "ace_tooltip ace_doc-tooltip " +
Expand Down
1 change: 1 addition & 0 deletions src/css/editor-css.js
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,7 @@ module.exports = `
pointer-events: none;
overflow: auto;
max-width: min(60em, 66vw);
overscroll-behavior: contain;
}
.ace_tooltip pre {
white-space: pre-wrap;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/scroll.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports.preventParentScroll = function preventParentScroll(event) {
event.stopPropagation();
var target = event.currentTarget;
var contentOverflows = target.scrollHeight > target.clientHeight;
if (!contentOverflows) {
event.preventDefault();
}
};
5 changes: 3 additions & 2 deletions src/tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
var dom = require("./lib/dom");
var event = require("./lib/event");
var Range = require("./range").Range;
var preventParentScroll = require("./lib/scroll").preventParentScroll;

var CLASSNAME = "ace_tooltip";

Expand Down Expand Up @@ -211,8 +212,8 @@ class HoverTooltip extends Tooltip {
el.addEventListener("blur", function() {
if (!el.contains(document.activeElement)) this.hide();
}.bind(this));
el.addEventListener("wheel", event.stopPropagation);

el.addEventListener("wheel", preventParentScroll);
}

/**
Expand Down

0 comments on commit c6475c0

Please sign in to comment.