Skip to content

Commit 963b543

Browse files
committed
Fix rendering timing
1 parent 9f2dadc commit 963b543

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

components/use-element-position.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
onWatcherCleanup,
77
type Ref,
88
} from "vue";
9-
import { useSlideContext, onSlideEnter } from "@slidev/client";
9+
import { useSlideContext, useIsSlideActive } from "@slidev/client";
1010

1111
export type SnapPosition =
1212
| "top"
@@ -25,27 +25,16 @@ export function useElementPosition(
2525
pos?: SnapPosition,
2626
): Ref<{ x: number; y: number } | undefined> {
2727
const { $scale } = useSlideContext();
28+
const isSlideActive = useIsSlideActive();
29+
2830
const elem = computed(() => {
2931
return slideContainer.value?.querySelector(selector) ?? null;
3032
});
31-
watch(
32-
elem,
33-
(newVal) => {
34-
if (newVal) {
35-
const observer = new MutationObserver(update);
36-
observer.observe(newVal, { attributes: true });
3733

38-
onWatcherCleanup(() => {
39-
observer.disconnect();
40-
});
41-
}
42-
},
43-
{ immediate: true },
44-
);
4534
const point = ref<{ x: number; y: number } | undefined>(undefined);
4635

4736
const update = () => {
48-
if (!rootElement.value || !elem.value) {
37+
if (!isSlideActive.value || !rootElement.value || !elem.value) {
4938
point.value = undefined;
5039
return;
5140
}
@@ -77,12 +66,28 @@ export function useElementPosition(
7766
}
7867
};
7968

80-
onSlideEnter(() => {
69+
watch(isSlideActive, () => {
8170
setTimeout(() => {
71+
// This `setTimeout` is important to ensure `update()` is called after the DOM elements in the slide are updated after `isSlideActive` is changed.
8272
update();
8373
});
8474
});
8575

76+
watch(
77+
elem,
78+
(newVal) => {
79+
if (newVal) {
80+
const observer = new MutationObserver(update);
81+
observer.observe(newVal, { attributes: true });
82+
83+
onWatcherCleanup(() => {
84+
observer.disconnect();
85+
});
86+
}
87+
},
88+
{ immediate: true },
89+
);
90+
8691
onMounted(() => {
8792
update();
8893

0 commit comments

Comments
 (0)