Skip to content

Commit 8e9a835

Browse files
committed
cleaner, avoid a crash if the mark does not return a node
1 parent d23842c commit 8e9a835

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

src/interactions/brush.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ function brushTransform(mode, {selectionMode = "data", ...options}) {
5858
S = S.filter((i) => y0 <= Ym[i] && Yl[i] <= y1);
5959
}
6060
// Only one facet can be active at a time; clear the others.
61-
if (type === "start") for (let i = 0; i < cancels.length; ++i) if (i !== (d.index.fi ?? 0)) cancels[i]();
61+
if (type === "start") for (const target of targets) if (target !== this) target._cancelBrush();
6262

63-
d.display.replaceWith((d.display = next.call(this, S ?? [], scales, values, dimensions, context)));
63+
d.display?.replaceWith((d.display = next.call(this, S ?? [], scales, values, dimensions, context)));
6464

6565
// Update the plot’s value if the selection has changed.
6666
if (selectionMode === "data") {
@@ -84,19 +84,25 @@ function brushTransform(mode, {selectionMode = "data", ...options}) {
8484
}
8585
});
8686

87-
states.set(svg, (state = {brush, cancels: [], selection: null}));
87+
states.set(svg, (state = {brush, targets: [], selection: null}));
8888
}
89-
const {brush, cancels} = state;
89+
const {brush, targets} = state;
9090
const display = next.call(this, [], scales, values, dimensions, context);
9191

9292
// Create a wrapper for the elements to display, and a target that will
9393
// carry the brush. Save references to the display and index of the current
9494
// facet.
95-
const wrapper = create("svg:g", context).attr("aria-label", display.getAttribute("aria-label"));
95+
const wrapper = create("svg:g", context);
9696
const target = create("svg:g", context).attr("aria-label", "brush").datum({display, index}).call(brush);
97-
display.removeAttribute("aria-label");
98-
wrapper.append(() => display);
99-
cancels[index.fi ?? 0] = () => target.call(brush.move, null);
97+
const node = target.node();
98+
node._cancelBrush = () => target.call(brush.move, null);
99+
targets.push(node);
100+
101+
if (display) {
102+
const al = display.getAttribute("aria-label");
103+
al && (wrapper.attr("aria-label", al), display.removeAttribute("aria-label"));
104+
wrapper.append(() => display);
105+
}
100106

101107
// When the plot is complete, append the target element to the top
102108
// (z-index), translate it to match the facet’s frame position, and

0 commit comments

Comments
 (0)