blob: be6c75da476c1f26a20de73b04bdd9fee46483c2 [file] [log] [blame]
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -06001<template>
2 <b-button
3 id="scrollToTopBtn"
4 class="btn-top btn-icon-only"
Derick Montague86e11ad2021-01-04 11:19:08 -06005 :class="{ 'show-btn': showButton }"
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -06006 variant="secondary"
7 :title="$t('global.ariaLabel.scrollToTop')"
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -06008 @click="scrollToTop"
9 >
10 <icon-up-to-top />
jason westoverd36ac8a2025-11-03 20:58:59 -060011 <span class="visually-hidden-focusable">
12 {{ $t('global.ariaLabel.scrollToTop') }}
13 </span>
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060014 </b-button>
15</template>
16
17<script>
18import UpToTop24 from '@carbon/icons-vue/es/up-to-top/24';
19
20import { debounce } from 'lodash';
Ed Tanous883a0d52024-03-23 14:56:34 -070021import { useI18n } from 'vue-i18n';
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060022
23export default {
24 name: 'BackToTop',
25 components: { IconUpToTop: UpToTop24 },
26 data() {
27 return {
Ed Tanous883a0d52024-03-23 14:56:34 -070028 $t: useI18n().t,
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060029 showButton: false,
30 };
31 },
32 created() {
33 window.addEventListener('scroll', debounce(this.handleScroll, 200));
34 },
35 methods: {
36 handleScroll() {
37 document.documentElement.scrollTop > 500
38 ? (this.showButton = true)
39 : (this.showButton = false);
40 },
41 scrollToTop() {
42 document.documentElement.scrollTo({
43 top: 0,
44 behavior: 'smooth',
45 });
46 },
47 },
48};
49</script>
50
51<style lang="scss" scoped>
52.btn-top {
53 position: fixed;
54 bottom: 24px;
55 right: 24px;
Derick Montague86e11ad2021-01-04 11:19:08 -060056
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060057 box-shadow: $box-shadow;
Derick Montague86e11ad2021-01-04 11:19:08 -060058 visibility: hidden;
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060059 opacity: 0;
60 transition: $transition-base;
Derick Montague86e11ad2021-01-04 11:19:08 -060061 z-index: $zindex-fixed;
62
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060063 @media (min-width: 1600px) {
64 left: 1485px;
65 right: auto;
66 }
67}
Derick Montague86e11ad2021-01-04 11:19:08 -060068.show-btn {
69 visibility: visible;
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060070 opacity: 1;
Dixsie Wolmers970ea7d2020-11-16 16:46:01 -060071}
72</style>