forked from vueuse/vueuse
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(UseFocusTrap): component (vueuse#820)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
1 parent
223bfce
commit bdea8ea
Showing
6 changed files
with
126 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { h, ref, defineComponent, watch, onUnmounted } from 'vue-demi' | ||
import { createFocusTrap } from 'focus-trap' | ||
import type { FocusTrap } from 'focus-trap' | ||
import { unrefElement } from '@vueuse/core' | ||
import { RenderableComponent } from '@vueuse/core/types' | ||
|
||
export const UseFocusTrap = defineComponent<RenderableComponent>({ | ||
name: 'UseFocusTrap', | ||
props: ['as'] as unknown as undefined, | ||
setup(props, { slots }) { | ||
let trap: undefined | FocusTrap | ||
const target = ref() | ||
|
||
const activate = () => trap && trap.activate() | ||
const deactivate = () => trap && trap.deactivate() | ||
watch( | ||
() => unrefElement(target), | ||
(el) => { | ||
if (!el) | ||
return | ||
trap = createFocusTrap(el, {}) | ||
activate() | ||
}, { flush: 'post' }) | ||
|
||
// Cleanup on unmount | ||
onUnmounted(() => deactivate()) | ||
return () => { | ||
if (slots.default) | ||
return h(props.as || 'div', { ref: target }, slots.default()) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters