Skip to content

Commit 8ad642a

Browse files
committed
feat(market): add first item autofocus
1 parent 6545440 commit 8ad642a

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed

src/components/Controls.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ defineProps<{
1414

1515
<style lang="scss" scoped>
1616
.controls {
17-
margin-top: 1rem;
17+
margin: 1rem 0;
1818
}
1919
</style>

src/views/Market/Autofocus.vue

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<script setup lang="ts">
2+
import { Checkbox } from "@/components"
3+
import { useMarketStore } from "./store"
4+
import { storeToRefs } from "pinia"
5+
import { watchEffect } from "vue"
6+
import { useSwalObserver } from "@/hooks"
7+
8+
const { shouldAutoFocusFirstItem } = storeToRefs(useMarketStore())
9+
10+
function selectFirstButton() {
11+
const firstButton = document.querySelector<HTMLButtonElement>("tbody button")
12+
firstButton?.focus()
13+
}
14+
15+
useSwalObserver({
16+
toggle: shouldAutoFocusFirstItem,
17+
onReject(el) {
18+
if (el.innerText.includes("sold")) window.location.reload()
19+
},
20+
onClose() {
21+
selectFirstButton()
22+
},
23+
})
24+
25+
watchEffect(() => {
26+
if (shouldAutoFocusFirstItem.value) selectFirstButton()
27+
})
28+
</script>
29+
30+
<template>
31+
<Checkbox v-model="shouldAutoFocusFirstItem">
32+
<template #default> Autofocus first item </template>
33+
<template #subtitle> Press space them bulk purchases </template>
34+
</Checkbox>
35+
</template>

src/views/Market/_Market.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<script setup lang="ts">
2+
import Autofocus from "./Autofocus.vue"
23
import { Controls } from "@/components"
34
import { onBeforeMount } from "vue"
45
@@ -17,6 +18,6 @@ onBeforeMount(() => {
1718

1819
<template>
1920
<Controls :to="`#${marketControlsId}`">
20-
<div style="color: white">web-app-container</div>
21+
<Autofocus />
2122
</Controls>
2223
</template>

src/views/Market/store.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { defineStore } from "pinia"
2+
import { ref } from "vue"
23

34
export const useMarketStore = defineStore(
45
"market",
56
() => {
6-
return {}
7+
const shouldAutoFocusFirstItem = ref(false)
8+
9+
return {
10+
shouldAutoFocusFirstItem,
11+
}
712
},
813
{
914
persist: true,

0 commit comments

Comments
 (0)