Skip to content

Commit 6aacc67

Browse files
committed
refactor(vue): use template ref different from useTemplateRef constant
1 parent 2e3eebb commit 6aacc67

File tree

7 files changed

+30
-26
lines changed

7 files changed

+30
-26
lines changed

src/components/icon-loader.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<icon-refresh ref="refreshIcon" />
2+
<icon-refresh ref="refreshIconElement" />
33
</template>
44
<script setup lang="ts">
55
import { useTemplateRef, watch } from "vue";
@@ -10,7 +10,7 @@ const props = defineProps<{ modelValue: boolean }>();
1010
const emit = defineEmits<{ "update:modelValue": [value: boolean] }>();
1111
const active = useVModel(props, "modelValue", emit);
1212
13-
const refreshIcon = useTemplateRef<SVGElement>("refreshIcon");
14-
const { play, finish } = useAnimate(refreshIcon, { transform: "rotate(360deg)" }, 1000);
13+
const refreshIconRef = useTemplateRef<SVGElement>("refreshIconElement");
14+
const { play, finish } = useAnimate(refreshIconRef, { transform: "rotate(360deg)" }, 1000);
1515
watch(active, (isActive) => { isActive ? play() : finish(); });
1616
</script>

src/components/modals/about-modal.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
About
55
</button>
66
<teleport to="body">
7-
<dialog ref="dialogRef" class="about">
7+
<dialog ref="dialogElement" class="about">
88
<header>
99
About
1010
<button type="button" name="close" class="icon" @click="close">
@@ -26,11 +26,13 @@
2626
</teleport>
2727
</template>
2828
<script setup lang="ts">
29+
import { useTemplateRef } from "vue";
2930
import { IconBrandGithub, IconTilde, IconX } from "@tabler/icons-vue";
3031
import dayjs from "dayjs";
3132
import { useDialog } from "@/composable/useDialog";
3233
33-
const { element: dialogRef, open, close } = useDialog();
34+
const dialogRef = useTemplateRef("dialogElement");
35+
const { open, close } = useDialog(dialogRef);
3436
const commitDate = dayjs(import.meta.env.VITE_GIT_COMMIT_DATE).format("DD.MM.YY");
3537
</script>
3638
<style lang="scss">

src/components/modals/add-repo.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Add GitHub repo
55
</button>
66
<teleport to="body">
7-
<dialog ref="dialogRef" class="add-repo" :class="{ invalid: hasError }">
7+
<dialog ref="dialogElement" class="add-repo" :class="{ invalid: hasError }">
88
<header>
99
Add GitHub repo
1010
<button type="button" name="close" class="icon" @click="close">
@@ -25,7 +25,7 @@
2525
</teleport>
2626
</template>
2727
<script setup lang="ts">
28-
import { reactive, ref, watch } from "vue";
28+
import { reactive, ref, useTemplateRef, watch } from "vue";
2929
import { IconPlus, IconX } from "@tabler/icons-vue";
3030
import { useDialog } from "@/composable/useDialog";
3131
import { deepCopy } from "@/helpers/object";
@@ -38,7 +38,8 @@ import UserRepos from "../header/user-repos.vue";
3838
defineOptions({ inheritAttrs: false });
3939
4040
const { settings } = useSettingsStore();
41-
const { element: dialogRef, open, close } = useDialog();
41+
const dialogRef = useTemplateRef("dialogElement");
42+
const { open, close } = useDialog(dialogRef);
4243
4344
// Form
4445
const tab = ref<"url" | "token">("url");

src/components/modals/edit-repo.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<icon-pencil title="edit repo" />
44
</button>
55
<teleport to="body">
6-
<dialog v-if="form" ref="dialogRef" :class="{ invalid: hasError }">
6+
<dialog v-if="form" ref="dialogElement" :class="{ invalid: hasError }">
77
<header>
88
Edit &laquo;{{ form.name }}&raquo;
99
<button type="button" name="close" class="icon" @click="close">
@@ -15,7 +15,7 @@
1515
</teleport>
1616
</template>
1717
<script setup lang="ts">
18-
import { ref, watch } from "vue";
18+
import { ref, useTemplateRef, watch } from "vue";
1919
import { IconPencil, IconX } from "@tabler/icons-vue";
2020
import { useDialog } from "@/composable/useDialog";
2121
import { deepCopy } from "@/helpers/object";
@@ -25,7 +25,8 @@ import RepoForm from "../header/repo-form.vue";
2525
2626
const props = defineProps<{ repo: Pick<Repository, "name" | "full_name" | "integrations"> }>();
2727
28-
const { element: dialogRef, open, close } = useDialog();
28+
const dialogRef = useTemplateRef("dialogElement");
29+
const { open, close } = useDialog(dialogRef);
2930
3031
// Form
3132
const form = ref(deepCopy(props.repo));

src/components/modals/settings-modal.vue

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Settings
55
</button>
66
<teleport to="body">
7-
<dialog ref="dialogRef" class="settings">
7+
<dialog ref="dialogElement" class="settings">
88
<header>
99
Settings
1010
<button type="button" name="close" class="icon" @click="close">
@@ -65,7 +65,7 @@
6565
</teleport>
6666
</template>
6767
<script setup lang="ts">
68-
import { reactive, watch } from "vue";
68+
import { reactive, useTemplateRef, watch } from "vue";
6969
import { IconSettings, IconX } from "@tabler/icons-vue";
7070
import { useDialog } from "@/composable/useDialog";
7171
import { deepCopy } from "@/helpers/object";
@@ -82,7 +82,8 @@ const themes = [
8282
] as const;
8383
8484
const { settings, needRefresh, updateServiceWorker } = useSettingsStore();
85-
const { element: dialogRef, open, close } = useDialog();
85+
const dialogRef = useTemplateRef("dialogElement");
86+
const { open, close } = useDialog(dialogRef);
8687
const form = reactive(deepCopy(settings.value));
8788
8889
async function getUsername(): Promise<void> {

src/composable/useDialog.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
import { useTemplateRef, type ShallowRef } from "vue";
1+
import type { ShallowRef } from "vue";
22
import { useEventListener } from "@vueuse/core";
33

4-
export function useDialog(elementRef?: Readonly<ShallowRef<HTMLDialogElement | null>>) {
5-
const element = elementRef ?? useTemplateRef("dialogRef");
4+
export function useDialog(elementRef: Readonly<ShallowRef<HTMLDialogElement | null>>) {
65
function open(): void {
7-
if (!element.value) return;
8-
element.value.showModal();
6+
if (!elementRef.value) return;
7+
elementRef.value.showModal();
98
document.body.classList.add("disable-scroll");
109
}
1110
function close(): void {
12-
if (!element.value) return;
13-
element.value.close();
11+
if (!elementRef.value) return;
12+
elementRef.value.close();
1413
document.body.classList.remove("disable-scroll");
1514
}
1615
// Close on click outside
17-
useEventListener(element, "mousedown", (event) => {
18-
if (event.target === element.value) close();
16+
useEventListener(elementRef, "mousedown", (event) => {
17+
if (event.target === elementRef.value) close();
1918
});
2019

21-
return { element, open, close };
20+
return { open, close };
2221
}

src/views/RepositoriesView.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</button>
1919
</div>
2020
</div>
21-
<ul ref="reposRef" class="repo-grid__list">
21+
<ul ref="reposElement" class="repo-grid__list">
2222
<repo-item
2323
v-for="repo in filteredItems"
2424
:key="repo.id"
@@ -52,7 +52,7 @@ const filteredItems = computed(() => {
5252
});
5353
});
5454
55-
const reposRef = useTemplateRef("reposRef");
55+
const reposRef = useTemplateRef("reposElement");
5656
useSortable(reposRef, repositories, { handle: ".repo__header-actions-handler" });
5757
5858
function sort(option: "alphabetic" | "stars" | "forks" | "language"): void {

0 commit comments

Comments
 (0)