|
6 | 6 | </div> |
7 | 7 | </template> |
8 | 8 |
|
9 | | -<script lang="ts"> |
10 | | -import { defineComponent } from 'vue'; |
| 9 | +<script setup lang="ts"> |
| 10 | +import { ref } from 'vue'; |
11 | 11 | import { CdxProgressBar } from "@wikimedia/codex"; |
12 | 12 |
|
13 | | -export default defineComponent({ |
14 | | - name: 'LoadingOverlay', |
15 | | - props: { |
16 | | - delay: { |
17 | | - type: Number, |
18 | | - default: 250, |
19 | | - }, |
20 | | - visible: { |
21 | | - type: Boolean, |
22 | | - default: false, |
23 | | - } |
24 | | - }, |
25 | | - components: { |
26 | | - CdxProgressBar |
27 | | - }, |
28 | | - data() { |
29 | | - return { |
30 | | - shown: this.visible, |
31 | | - cachedStyles: { |
32 | | - overflow: 'auto' |
33 | | - } |
34 | | - }; |
35 | | - }, |
36 | | - methods: { |
37 | | - show(): void { |
38 | | - if (this.shown) { |
39 | | - return; |
40 | | - } |
| 13 | +const props = withDefaults(defineProps<{ |
| 14 | + delay: number |
| 15 | + visible: boolean |
| 16 | +}>(), { |
| 17 | + delay: 250, |
| 18 | + visible: false |
| 19 | +}); |
41 | 20 |
|
42 | | - // Determine the current styles for body, in order to cache the overflow |
43 | | - const bodyStyles = window.getComputedStyle(document.body); |
44 | | - this.shown = true; |
| 21 | +const shown = ref(props.visible); |
| 22 | +const cachedStyles = ref({ overflow: 'auto' }); |
45 | 23 |
|
46 | | - this.cachedStyles.overflow = bodyStyles.overflow; |
47 | | - document.body.style.overflow = 'hidden'; |
48 | | - }, |
49 | | - hide(): Promise<void> { |
50 | | - return new Promise((resolve) => { |
51 | | - setTimeout(() => { |
52 | | - this.shown = false; |
53 | | - // Restore previous overflow value |
54 | | - document.body.style.overflow = this.cachedStyles.overflow; |
55 | | - resolve(); |
56 | | - }, this.delay); |
57 | | - }); |
58 | | - }, |
59 | | - }, |
60 | | -}); |
| 24 | +function show(){ |
| 25 | + if (shown.value) { |
| 26 | + return; |
| 27 | + } |
| 28 | +
|
| 29 | + // Determine the current styles for body, in order to cache the overflow |
| 30 | + const bodyStyles = window.getComputedStyle(document.body); |
| 31 | + shown.value = true; |
| 32 | +
|
| 33 | + cachedStyles.value.overflow = bodyStyles.overflow; |
| 34 | + document.body.style.overflow = 'hidden'; |
| 35 | +} |
| 36 | +
|
| 37 | +function hide(): Promise<void> { |
| 38 | + return new Promise((resolve) => { |
| 39 | + setTimeout(() => { |
| 40 | + shown.value = false; |
| 41 | + // Restore previous overflow value |
| 42 | + document.body.style.overflow = cachedStyles.value.overflow; |
| 43 | + resolve(); |
| 44 | + }, props.delay); |
| 45 | + }); |
| 46 | +} |
| 47 | +
|
| 48 | +defineExpose({show, hide}); |
61 | 49 | </script> |
62 | 50 |
|
63 | 51 | <style lang="scss"> |
|
0 commit comments