Skip to content

Commit

Permalink
fix: correct focus toggle handling
Browse files Browse the repository at this point in the history
  • Loading branch information
visualjerk committed Aug 9, 2020
1 parent e68998e commit 7042817
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
17 changes: 10 additions & 7 deletions examples/src/App.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
<template>
<div>
<TestButton @click="handleClick">
Button
</TestButton>
<TestButton disabled @click="handleClick">
Button
</TestButton>
<DialogDisclosure v-bind="dialog" :as="TestButton">Open Dialog</DialogDisclosure>
<Dialog v-bind="dialog">Hello Dialog</Dialog>
</div>
</template>

<script>
import { markRaw } from 'vue'
import { Dialog, DialogDisclosure, useDialogState } from 'vue-ari'
import TestButton from './MyButton.vue'
export default {
name: 'App',
components: {
TestButton,
Dialog,
DialogDisclosure,
},
setup() {
const dialog = useDialogState()
return { dialog, TestButton: markRaw(TestButton) }
},
methods: {
handleClick() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-ari",
"version": "0.0.0-alpha.4",
"version": "0.0.0-alpha.5",
"license": "MIT",
"repository": "https://github.com/visualjerk/ari",
"main": "lib/index.js",
Expand Down
13 changes: 12 additions & 1 deletion src/Dialog/Dialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,23 @@ function useHandleToggleFocus(props: DialogProps, ref: Ref<HTMLElement>) {
(visible) => {
if (visible) {
focusFirstFocusable(ref.value)
} else if (focusIsWithin(ref.value)) {
}
}
)
// Needs to be a 'sync' watcher, so we can check
// if focus was within the dialog before it is closed
watch(
() => props.visible,
(visible) => {
if (!visible && focusIsWithin(ref.value)) {
const disclosure: HTMLElement = document.querySelector(
`[aria-controls="${props.baseId}"]`
)
disclosure.focus()
}
},
{
flush: 'sync',
}
)
}
Expand Down

0 comments on commit 7042817

Please sign in to comment.