Skip to content

Commit

Permalink
pointer-events: none when not sticky (observablehq#1697)
Browse files Browse the repository at this point in the history
* pointer-events: none when not sticky

* add missing context in call to applyIndirectStyles

* regenerate snapshots

* greedy context copy

---------

Co-authored-by: Philippe Rivière <fil@rezo.net>
  • Loading branch information
mbostock and Fil authored Jun 19, 2023
1 parent dbb7d59 commit e785986
Show file tree
Hide file tree
Showing 82 changed files with 136 additions and 132 deletions.
7 changes: 5 additions & 2 deletions src/interactions/pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op
// outermost render function because it will re-render dynamically in
// response to pointer events.
render: composeRender(function (index, scales, values, dimensions, context, next) {
context = {...context, pointerSticky: false};
const svg = context.ownerSVGElement;
const {data} = context.getMarkState(this);

Expand Down Expand Up @@ -68,6 +69,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op

let i; // currently focused index
let g; // currently rendered mark
let s; // currently rendered stickiness
let f; // current animation frame

// When faceting, if more than one pointer would be visible, only show
Expand Down Expand Up @@ -97,8 +99,9 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op
}

function render(ii) {
if (i === ii) return; // the tooltip hasn’t moved
if (i === ii && s === state.sticky) return; // the tooltip hasn’t moved
i = ii;
s = context.pointerSticky = state.sticky;
const I = i == null ? [] : [i];
if (faceted) (I.fx = index.fx), (I.fy = index.fy), (I.fi = index.fi);
const r = next(I, scales, values, dimensions, context);
Expand Down Expand Up @@ -144,7 +147,7 @@ function pointerK(kx, ky, {x, y, px, py, maxRadius = 40, channels, render, ...op
if (i == null) return; // not pointing
if (state.sticky && state.roots.some((r) => r?.contains(event.target))) return; // stay sticky
if (state.sticky) (state.sticky = false), state.renders.forEach((r) => r(null)); // clear all pointers
else state.sticky = true;
else (state.sticky = true), render(i);
event.stopImmediatePropagation(); // suppress other pointers
}

Expand Down
2 changes: 1 addition & 1 deletion src/marks/rule.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class RuleX extends Mark {
const {width, height, marginTop, marginRight, marginLeft, marginBottom} = dimensions;
const {insetTop, insetBottom} = this;
return create("svg:g", context)
.call(applyIndirectStyles, this, dimensions)
.call(applyIndirectStyles, this, dimensions, context)
.call(applyTransform, this, {x: X && x}, offset, 0)
.call((g) =>
g
Expand Down
3 changes: 2 additions & 1 deletion src/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,8 @@ export function applyIndirectStyles(selection, mark, dimensions, context) {
applyAttr(selection, "shape-rendering", mark.shapeRendering);
applyAttr(selection, "filter", mark.imageFilter);
applyAttr(selection, "paint-order", mark.paintOrder);
applyAttr(selection, "pointer-events", mark.pointerEvents);
const {pointerEvents = context.pointerSticky === false ? "none" : undefined} = mark;
applyAttr(selection, "pointer-events", pointerEvents);
}

export function applyDirectStyles(selection, mark) {
Expand Down
2 changes: 1 addition & 1 deletion test/output/autoArea.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoAreaColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoAreaColorColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoAreaColorName.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoAreaColorValue.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoAreaStackColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoAutoHistogram.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBar.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarColorReducer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarMode.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarNoReducer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarNonZeroReducer.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarStackColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarStackColorConstant.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoBarStackColorField.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoChannels.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoConnectedScatterplot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDot.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDotBin.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDotColor.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions test/output/autoDotFacet.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions test/output/autoDotFacet2.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDotGroup.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDotOrdCont.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDotOrdinal.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/output/autoDotSize.svg
2 changes: 1 addition & 1 deletion test/output/autoDotSize2.svg
2 changes: 1 addition & 1 deletion test/output/autoDotUnsortedDate.svg
2 changes: 1 addition & 1 deletion test/output/autoDotZero.svg
2 changes: 1 addition & 1 deletion test/output/autoHeatmap.svg
2 changes: 1 addition & 1 deletion test/output/autoHeatmapOrdCont.svg
2 changes: 1 addition & 1 deletion test/output/autoHeatmapOrdinal.svg
2 changes: 1 addition & 1 deletion test/output/autoHistogram.svg
2 changes: 1 addition & 1 deletion test/output/autoHistogramDate.svg
Loading

0 comments on commit e785986

Please sign in to comment.