Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: useClickOutside #415

Merged
merged 5 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ npm i --save @prefecthq/vue-compositions

- [useBoolean](https://github.com/prefecthq/vue-compositions/tree/main/src/useBoolean)
- [useChildrenAreWrapped](https://github.com/prefecthq/vue-compositions/tree/main/src/useChildrenAreWrapped)
- [useClickOutside](https://github.com/prefecthq/vue-compositions/tree/main/src/useClickOutside)
- [useComputedStyle](https://github.com/prefecthq/vue-compositions/tree/main/src/useComputedStyle)
- [useDebouncedRef](https://github.com/prefecthq/vue-compositions/tree/main/src/useDebouncedRef)
- [useElementRect](https://github.com/prefecthq/vue-compositions/tree/main/src/useElementRect)
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './useBoolean'
export * from './useChildrenAreWrapped'
export * from './useClickOutside'
export * from './useComputedStyle'
export * from './useDebouncedRef'
export * from './useElementRect'
Expand Down
35 changes: 35 additions & 0 deletions src/useClickOutside/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# useClickOutside
The `useClickOutside` composition is designed to detect and react to clicks outside a specified element. It takes an element and a callback function as arguments. The composition returns `on` and `off` functions, allowing for manual control over the event listener if needed, although automatic cleanup is handled as part of the component's lifecycle.

## Example
```vue
<template>
<div ref="el" />
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { useClickOutside } from '@prefecthq/vue-compositions'

function handleClickOutside(): void {
// Do something, like close a modal
}

const el = ref()
const { on, off } = useClickOutside(el, handleClickOutside)
// Don't need to do anything with on/off unless your handler needs to be paused for some reason
</script>
```

## Arguments
| Name | Type |
|----------|-----------------------------------|
| element | `MaybeRefOrGetter<Element> |
| callback | `() => void` |

## Returns

| Name | Type | Description |
|--------|-------------|---------------------------------------------------|
| on | `() => void` | Manually turn on the event listener (note: this isn't needed unless `off` has been called) |
| off | `() => void` | Manually turn off the event listener (note: this isn't needed for cleanup - the event listener will be removed automatically as part of the component lifecycle) |
1 change: 1 addition & 0 deletions src/useClickOutside/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useClickOutside'
74 changes: 74 additions & 0 deletions src/useClickOutside/useClickOutside.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { onScopeDispose } from 'vue'
import { useGlobalEventListener } from '..'
import { MaybeRefOrGetter } from '@/types/maybe'
import { toValue } from '@/utilities/vue'

type ClickOutsideEntry = {
element: MaybeRefOrGetter<Element>,
callback: () => void,
}

const callbacks = new Map<symbol, ClickOutsideEntry>()

function handleClick(event: MouseEvent): void {
for (const { element, callback } of callbacks.values()) {
const elementValue = toValue(element)

if (!elementValue.contains(event.target as Node)) {
callback()
}
}
}

const { add, remove } = useGlobalEventListener('click', handleClick, { capture: true })

function tryTeardownEventListener(): void {
if (callbacks.size > 0) {
return
}

remove()
}

function tryAddEventListener(): void {
if (callbacks.size > 0) {
return
}

add()
}

export type UseClickOutsideCallbackFunction = () => void

export type UseClickOutside = {
off: () => void,
on: () => void,
}

export function useClickOutside(element: MaybeRefOrGetter<Element>, callback: UseClickOutsideCallbackFunction): UseClickOutside {
const id = Symbol('useClickOutside')

callbacks.set(id, { element, callback })

tryAddEventListener()

function off(): void {
callbacks.delete(id)
tryTeardownEventListener()
}

function on(): void {
callbacks.set(id, { element, callback })
tryAddEventListener()
}

onScopeDispose(() => {
off()
tryTeardownEventListener()
})

return {
off,
on,
}
}
Loading