Skip to content

Commit e02cb07

Browse files
committed
webapp/utils.ts: scrollToTop utils function
1 parent b585b8a commit e02cb07

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

webapp/src/components/progress_bars/TestingButtons.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<script setup lang="ts">
2222
import { computed } from "vue";
2323
24+
import { scrollToTop } from "@/utils";
2425
import { useValidationStore } from "@/store";
2526
import CustomButton from "@/components/simple/CustomButton.vue";
2627
@@ -42,7 +43,6 @@ function setStep(index: number): void {
4243
throw new Error("step out of range");
4344
}
4445
45-
const appElement = document.getElementById("base-container");
46-
if (appElement !== null) appElement.scrollTop = 0;
46+
scrollToTop();
4747
}
4848
</script>

webapp/src/components/progress_bars/TrainingButtons.vue

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,22 @@
2121
<script lang="ts" setup>
2222
import { useRouter, useRoute } from 'vue-router'
2323
24+
import { scrollToTop } from "@/utils";
2425
import { useTrainingStore } from "@/store";
2526
import CustomButton from '@/components/simple/CustomButton.vue'
2627
2728
const router = useRouter()
2829
const trainingStore = useTrainingStore()
2930
const route = useRoute()
3031
31-
function scrollToTop() {
32-
const appElement = document.getElementById('base-container');
33-
if (appElement !== null) appElement.scrollTop = 0;
34-
}
35-
3632
async function prevStepOrList(): Promise<void> {
37-
if (trainingStore.step === 1) {
38-
await router.push({ path: '/list' });
39-
} else {
40-
trainingStore.prevStep();
41-
scrollToTop(); // scroll manually
42-
}
33+
if (trainingStore.step === 1) {
34+
await router.push({ path: '/list' });
35+
trainingStore.prevStep();
36+
} else {
37+
trainingStore.prevStep();
38+
scrollToTop(); // scroll manually
39+
}
4340
}
4441
4542
function nextStep() {

webapp/src/router/router.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import createDebug from "debug";
22
import { createRouter, createWebHashHistory } from 'vue-router'
3+
import { scrollToTop } from "@/utils";
34

45
import TrainingBar from '@/components/progress_bars/TrainingBar.vue'
56
import TestingBar from '@/components/progress_bars/TestingBar.vue'
@@ -19,8 +20,7 @@ const router = createRouter({
1920
// Always scroll to top when navigating to a new page
2021
// Because router is wrapped in a BaseLayout, returning { top: 0 } doesn't do anything
2122
// https://github.com/vuejs/vue-router/issues/3451#issuecomment-975637797
22-
const containerId = document.getElementById('base-container');
23-
if (containerId) containerId.scrollTop = 0;
23+
scrollToTop();
2424
return { top: 0 }
2525
},
2626
routes: [

webapp/src/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Instantly scrolls to the top of the page without animation.
3+
* Because router is wrapped in a BaseLayout, window.scrollTo({ top: 0 }) doesn't work.
4+
*/
5+
export function scrollToTop() {
6+
const appElement = document.getElementById('base-container');
7+
if (appElement !== null) {
8+
appElement.scrollTop = 0;
9+
}
10+
}

0 commit comments

Comments
 (0)