Skip to content

Commit 7880117

Browse files
committed
Query the anchor elements only inside the page
1 parent b96c224 commit 7880117

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

components/FancyArrow.vue

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref } from "vue";
2+
import { ref, computed } from "vue";
33
import { useElementPosition, type SnapPosition } from "./use-element-position";
44
import { useRoughArrow } from "./use-rough-arrow";
55
@@ -22,13 +22,18 @@ const props = defineProps<{
2222
seed?: number | string;
2323
}>();
2424
25-
const container = ref<SVGSVGElement>();
25+
const root = ref<HTMLElement>();
26+
const slideContainer = computed(() => {
27+
return root.value?.closest(".slidev-page") ?? undefined;
28+
});
29+
30+
const svgContainer = ref<SVGSVGElement>();
2631
2732
const point1 = props.id1
28-
? useElementPosition(container, props.id1, props.pos1)
33+
? useElementPosition(slideContainer, svgContainer, props.id1, props.pos1)
2934
: ref({ x: Number(props.x1 ?? 0), y: Number(props.y1 ?? 0) });
3035
const point2 = props.id2
31-
? useElementPosition(container, props.id2, props.pos2)
36+
? useElementPosition(slideContainer, svgContainer, props.id2, props.pos2)
3237
: ref({ x: Number(props.x2 ?? 0), y: Number(props.y2 ?? 0) });
3338
3439
const { arcSvg, textPosition } = useRoughArrow({
@@ -45,10 +50,10 @@ const { arcSvg, textPosition } = useRoughArrow({
4550
</script>
4651

4752
<template>
48-
<div v-if="point1 && point2" style="position: absolute; top: 0; left: 0">
49-
<!-- Use <div v-if> as a root element above because <template v-if> doesn't work with v-click on Slidev -->
53+
<div ref="root" style="position: absolute; top: 0; left: 0">
5054
<svg
51-
ref="container"
55+
v-if="point1 && point2"
56+
ref="svgContainer"
5257
:class="props.color ? `text-${props.color}` : ''"
5358
style="
5459
position: absolute;
@@ -62,7 +67,7 @@ const { arcSvg, textPosition } = useRoughArrow({
6267
<g v-html="arcSvg" />
6368
</svg>
6469
<div
65-
v-if="$slots.default && textPosition"
70+
v-if="point1 && point2 && $slots.default && textPosition"
6671
:style="{
6772
position: 'absolute',
6873
left: `${textPosition.x}px`,

components/use-element-position.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type SnapPosition =
1212
| "bottomright";
1313

1414
export function useElementPosition(
15+
slideContainer: Ref<Element | undefined>,
1516
rootElement: Ref<SVGSVGElement | undefined>,
1617
id: string,
1718
pos?: SnapPosition,
@@ -22,7 +23,7 @@ export function useElementPosition(
2223

2324
const update = () => {
2425
if (!elem.value) {
25-
elem.value = document.getElementById(id);
26+
elem.value = slideContainer.value?.querySelector(`#${id}`) ?? null;
2627
if (elem.value) {
2728
const observer = new MutationObserver(update);
2829
observer.observe(elem.value, { attributes: true });

0 commit comments

Comments
 (0)