npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

clickout-lite

v0.0.1

Published

A lightweight utility to detect outside clicks on elements — compatible with Vue, React, and vanilla JavaScript.

Readme

Click Outside Lite

A lightweight utility to detect clicks outside a specified element, compatible with any JavaScript framework such as React, Vue, Svelte, or vanilla JavaScript. Supports TypeScript, iOS compatibility, and customizable options like ignoring elements, iframe detection, and event capture.

Features

  • Framework Agnostic: Works with React, Vue, Svelte, or vanilla JavaScript.
  • TypeScript Support: Fully typed for a better developer experience.
  • iOS Compatibility: Handles iOS-specific click event quirks.
  • Customizable Options: Ignore specific elements, enable iframe detection, and configure event capture.
  • Lightweight: Minimal footprint with no external dependencies.
  • Performance Optimized: Efficient event handling with cleanup support.

Installation

# npm
npm install clickout-lite

# yarn
yarn add clickout-lite

# pnpm
pnpm add clickout-lite

Usage

Below are examples demonstrating how to use clickout-lite in different frameworks.

Vue Example

Detect clicks outside a specific element in a Vue 3 application using the Composition API.

<script setup>
import { ref, onMounted, onUnmounted } from 'vue'
import { onClickOutside } from 'clickout-lite'

const containerRef = ref(null)

onMounted(() => {
	onClickOutside(containerRef, () => {
		console.log('Clicked outside the container!')
	})
})
</script>

<template>
	<div
		ref="containerRef"
		style="width: 200px; height: 200px; background: lightblue;">
		Click outside this box
	</div>
</template>

React Example

Detect clicks outside a specific element in a React application using hooks.

import React, { useRef, useEffect } from 'react'
import { onClickOutside } from 'clickout-lite'

function App() {
	const containerRef = useRef(null)

	useEffect(() => {
		const stop = onClickOutside(containerRef.current, () => {
			console.log('Clicked outside the container!')
		})
		return () => stop() // Cleanup on unmount
	}, [])

	return (
		<div
			ref={containerRef}
			style={{
				width: '200px',
				height: '200px',
				background: 'lightblue',
			}}>
			Click outside this box
		</div>
	)
}

export default App

Vanilla JavaScript Example

Use clickout-lite in a plain JavaScript project.

import { onClickOutside } from 'clickout-lite'

const container = document.querySelector('#my-container')

const stop = onClickOutside(container, () => {
	console.log('Clicked outside the container!')
})

// Cleanup when done
// stop()

Svelte Example

Detect clicks outside an element in a Svelte application.

<script>
  import { onMount, onDestroy } from 'svelte'
  import { onClickOutside } from 'clickout-lite'

  let container

  onMount(() => {
    const stop = onClickOutside(container, () => {
      console.log('Clicked outside the container!')
    })
    return () => stop() // Cleanup on destroy
  })
</script>

<div bind:this={container} style="width: 200px; height: 200px; background: lightblue;">
  Click outside this box
</div>

Advanced Usage with Options

Ignore specific elements or enable iframe detection.

import { onClickOutside } from 'clickout-lite'

const container = document.querySelector('#my-container')
const ignoreElement = document.querySelector('#ignore-this')

onClickOutside(
	container,
	() => {
		console.log('Clicked outside the container!')
	},
	{
		ignore: [ignoreElement, '.ignore-class'], // Ignore specific elements or selectors
		detectIframe: true, // Trigger on iframe focus
		capture: false, // Use non-capturing event listener
	}
)

API

onClickOutside(target, handler, options?)

Parameters

| Parameter | Type | Description | | --------- | ----------------------------------- | --------------------------------------------------------------------------------------------- | | target | MaybeRefOrGetter<MaybeElementRef> | The element or ref to monitor for outside clicks. Supports direct elements, refs, or getters. | | handler | OnClickOutsideHandler | Callback function triggered when a click occurs outside the target. | | options | OnClickOutsideOptions | Optional configuration object. |

Options

| Option | Type | Default | Description | | -------------- | ------------------------------------------------- | ------------------- | ---------------------------------------------------------------------------- | | ignore | MaybeRefOrGetter<(MaybeElementRef \| string)[]> | [] | Elements or selectors to ignore for click detection. | | capture | boolean | true | Whether to use capturing event listeners. | | detectIframe | boolean | false | Trigger handler on iframe focus events. | | controls | boolean | false | Return control methods (stop, cancel, trigger) instead of just stop. | | window | Window | globalThis.window | Custom window object for testing or special environments. |

Returns

  • If options.controls is false: Returns a stop function to remove event listeners.
  • If options.controls is true: Returns an object with stop, cancel, and trigger methods.

Author

safdar-azeem