Skip to content

Commit d628761

Browse files
authored
fixes dynamically placed label bug with a MutationObserver (#1750)
1 parent 1482072 commit d628761

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

web-common/src/components/data-graphic/actions/outline.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ export function outline(
6161
const observer = new MutationObserver(() => {
6262
clonedElement.setAttribute("x", node.getAttribute("x"));
6363
clonedElement.setAttribute("y", node.getAttribute("y"));
64-
clonedElement.setAttribute("text-anchor", node.getAttribute("text-anchor"));
64+
if (node.getAttribute("text-anchor")) {
65+
clonedElement.setAttribute(
66+
"text-anchor",
67+
node.getAttribute("text-anchor")
68+
);
69+
}
70+
6571
if (node.getAttribute("dx")) {
6672
clonedElement.setAttribute("dx", node.getAttribute("dx"));
6773
}

web-common/src/components/data-graphic/guides/DynamicallyPlacedLabel.svelte

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,26 +37,36 @@
3737
elementHeight = bb.height;
3838
elementX = bb.x;
3939
elementY = bb.y;
40-
4140
if (location === "right" && elementX + elementWidth > $config.plotRight) {
4241
xOffset.set(-elementWidth - buffer);
4342
} else {
4443
xOffset.set(buffer);
4544
}
4645
}
4746
48-
let observer;
47+
let resize;
48+
let mutation;
4949
5050
onMount(() => {
51-
observer = new ResizeObserver(() => {
51+
// resize if element updates.
52+
resize = new ResizeObserver(() => {
5253
if (element) update();
5354
});
54-
observer.observe(element);
55+
// reposition if element DOM parameters change.
56+
mutation = new MutationObserver(() => {
57+
update();
58+
});
59+
mutation.observe(element, {
60+
attributes: true,
61+
childList: true,
62+
});
63+
resize.observe(element);
5564
update();
5665
});
5766
5867
onDestroy(() => {
59-
observer.unobserve(element);
68+
resize.unobserve(element);
69+
mutation.disconnect();
6070
});
6171
6272
$: trueX = rx || $xScale(x);

0 commit comments

Comments
 (0)