Skip to content
On this page

useOutsidePress

Execute callback when click is outside of element

Parameters

typescript
import { useOutsidePress } from '@elonehoo/pistachio'

useOutsidePress(element, callback)
import { useOutsidePress } from '@elonehoo/pistachio'

useOutsidePress(element, callback)
ParametersTypeRequiredDefaultDescription
elementRef\<Element>trueElement to keep track if clicked outside
callback(e: MouseEvent) => voidtrueCallback when clicked outside

Methods

The useOutsidePress function exposes the following methods:

typescript
import { useOutsidePress } from '@elonehoo/pistachio'

const remove = useOutsidePress()
import { useOutsidePress } from '@elonehoo/pistachio'

const remove = useOutsidePress()
SignatureDescription
removeManually removes the event listener

Example

Click Outside or inside

check the console

vue
<script setup lang="ts">
import { ref } from 'vue'
import { useOutsidePress } from '@elonehoo/pistachio'

const elref = ref(null)

useOutsidePress(elref, () => console.log('clicked outside'))
</script>

<template>
  <div>
    <div ref="elref" style="background: #99CC99;width:100px">
      Click Outside or inside
    </div>
    <p>check the console</p>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { useOutsidePress } from '@elonehoo/pistachio'

const elref = ref(null)

useOutsidePress(elref, () => console.log('clicked outside'))
</script>

<template>
  <div>
    <div ref="elref" style="background: #99CC99;width:100px">
      Click Outside or inside
    </div>
    <p>check the console</p>
  </div>
</template>

Released under the MIT License.