Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinMalfait committed Mar 22, 2021
1 parent 8ab81fd commit 3c69bf1
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ describe('Keyboard interactions', () => {
})
)

it(
it.only(
'should close the Popover menu once we Tab out of the Popover.Group',
suppressConsoleLogs(async () => {
render(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<template>
<div class="flex justify-center w-screen h-full p-12 bg-gray-50">
<div class="w-full max-w-xs mx-auto">
<FocusTrap>
<button>Trigger</button>
</FocusTrap>
</div>
</div>
</template>

<script>
import { defineComponent, h, ref, onMounted, watchEffect, watch } from 'vue'
import {
FocusTrap
} from '@headlessui/vue'
function classNames(...classes) {
return classes.filter(Boolean).join(' ')
}
export default {
components: { Listbox, ListboxLabel, ListboxButton, ListboxOptions, ListboxOption },
setup(props, context) {
return {
}
},
}
</script>
11 changes: 11 additions & 0 deletions packages/@headlessui-vue/examples/src/routes.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,16 @@
"component": "./components/switch/switch.vue"
}
]
},
{
"name": "Focus Trap",
"path": "/focus-trap",
"children": [
{
"name": "FocusTrap (basic)",
"path": "/focus-trap/focus-trap",
"component": "./components/focus-trap/focus-trap.vue"
}
]
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,16 @@ function renderTemplate(input: string | Partial<Parameters<typeof defineComponen
}

it.only('should focus the first focusable element inside the FocusTrap', () => {
renderTemplate(
let { debug } = renderTemplate(
`
<FocusTrap>
<button>Trigger</button>
</FocusTrap>
`
)

debug()

console.log('In test:', document.activeElement?.outerHTML)

assertActiveElement(getByText('Trigger'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
// Types
PropType,
Ref,
onUnmounted,
} from 'vue'
import { render } from '../../utils/render'
import { useFocusTrap } from '../../hooks/use-focus-trap'
Expand Down Expand Up @@ -43,6 +44,10 @@ export let FocusTrap = defineComponent({
console.log('After:', document.activeElement?.outerHTML)
})

onUnmounted(() => {
console.log('Unmount:', document.activeElement?.outerHTML)
})

return { el: container }
},
})
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,7 @@ describe('Keyboard interactions', () => {

// Verify the next link is now focused
assertActiveElement(getByText('Next'))
await new Promise<void>(nextTick)

// Verify the popover is closed
assertPopoverButton({ state: PopoverState.InvisibleUnmounted })
Expand Down Expand Up @@ -1216,10 +1217,10 @@ describe('Keyboard interactions', () => {
})
)

it(
it.only(
'should focus the PopoverButton when pressing Shift+Tab when we focus inside the PopoverPanel',
suppressConsoleLogs(async () => {
renderTemplate(
let { debug } = renderTemplate(
`
<Popover>
<PopoverButton>Trigger 1</PopoverButton>
Expand All @@ -1242,6 +1243,9 @@ describe('Keyboard interactions', () => {

// Tab out of the Panel
await press(shift(Keys.Tab))
console.log(document.activeElement?.outerHTML)

debug()

// Ensure the PopoverButton is focused again
assertActiveElement(getPopoverButton())
Expand Down
48 changes: 36 additions & 12 deletions packages/@headlessui-vue/src/components/popover/popover.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import {
defineComponent,
inject,
onMounted,
onUnmounted,
onBeforeUnmount,
provide,
ref,
watchEffect,
Expand Down Expand Up @@ -135,14 +138,18 @@ export let Popover = defineComponent({
watchEffect(() => registerPopover?.(registerBag))

// Handle focus out
useWindowEvent('focus', () => {
if (popoverState.value !== PopoverStates.Open) return
if (isFocusWithinPopoverGroup()) return
if (!button) return
if (!panel) return
useWindowEvent(
'focus',
() => {
if (popoverState.value !== PopoverStates.Open) return
if (isFocusWithinPopoverGroup()) return
if (!button) return
if (!panel) return

api.closePopover()
})
api.closePopover()
},
true
)

// Handle outside click
useWindowEvent('mousedown', (event: MouseEvent) => {
Expand Down Expand Up @@ -222,6 +229,7 @@ export let PopoverButton = defineComponent({
useWindowEvent(
'focus',
() => {
console.log('Ding dong', document.activeElement?.outerHTML)
previousActiveElementRef.value = activeElementRef.value
activeElementRef.value = document.activeElement
},
Expand Down Expand Up @@ -390,7 +398,7 @@ export let PopoverPanel = defineComponent({
let propsWeControl = {
ref: 'el',
id: this.id,
onKeyDown: this.handleKeyDown,
onKeydown: this.handleKeyDown,
}

return render({
Expand All @@ -408,11 +416,15 @@ export let PopoverPanel = defineComponent({

provide(PopoverPanelContext, api.panelId)

onUnmounted(() => {
api.panel.value = null
})

// Move focus within panel
watchEffect(() => {
if (!focus) return
if (api.popoverState.value !== PopoverStates.Open) return
if (!dom(api.panel)) return
if (!api.panel) return

let activeElement = document.activeElement as HTMLElement
if (dom(api.panel)?.contains(activeElement)) return // Already focused within Dialog
Expand All @@ -438,7 +450,9 @@ export let PopoverPanel = defineComponent({
let result = focusIn(dom(api.panel)!, event.shiftKey ? Focus.Previous : Focus.Next)

if (result === FocusResult.Underflow) {
return dom(api.button)?.focus()
dom(api.button)?.focus()
console.log(document.activeElement?.outerHTML)
return
} else if (result === FocusResult.Overflow) {
if (!dom(api.button)) return

Expand Down Expand Up @@ -486,7 +500,17 @@ export let PopoverGroup = defineComponent({
props: {
as: { type: [Object, String], default: 'div' },
},
setup(props, { slots, attrs }) {
render() {
let propsWeControl = { ref: 'el' }

return render({
props: { ...this.$props, ...propsWeControl },
slot: {},
attrs: this.$attrs,
slots: this.$slots,
})
},
setup() {
let groupRef = ref<HTMLElement | null>(null)
let popovers = ref<PopoverRegisterBag[]>([])

Expand Down Expand Up @@ -529,6 +553,6 @@ export let PopoverGroup = defineComponent({
closeOthers,
})

return () => render({ props, slot: {}, attrs, slots })
return { el: groupRef }
},
})

0 comments on commit 3c69bf1

Please sign in to comment.