--- category: Sensors --- # onClickOutside Listen for clicks outside of an element. Useful for modal or dropdown. ## Usage ```vue ``` If you need more control over triggering the handler, you can use the `controls` option. ```ts const { cancel, trigger } = onClickOutside( modalRef, (event) => { modal.value = false }, { controls: true }, ) useEventListener('pointermove', (e) => { cancel() // or trigger(e) }) ``` If you want to ignore certain elements, you can use the `ignore` option. Provide the elements to ignore as an array of Refs or CSS Selectors. ```ts const ignoreElRef = useTemplateRef('ignoreEl') const ignoreElSelector = '.ignore-el' onClickOutside( target, event => console.log(event), { ignore: [ignoreElRef, ignoreElSelector] }, ) ``` ## Component Usage ```vue ``` ## Directive Usage ```vue ``` You can also set the handler as an array to set the configuration items of the instruction. ```vue ``` ## Type Declarations ```ts export interface OnClickOutsideOptions extends ConfigurableWindow { /** * List of elements that should not trigger the event, * provided as Refs or CSS Selectors. */ ignore?: MaybeRefOrGetter<(MaybeElementRef | string)[]> /** * Use capturing phase for internal event listener. * @default true */ capture?: boolean /** * Run handler function if focus moves to an iframe. * @default false */ detectIframe?: boolean /** * Use controls to cancel/trigger listener. * @default false */ controls?: Controls } export type OnClickOutsideHandler< T extends OnClickOutsideOptions = OnClickOutsideOptions, > = ( event: | (T["detectIframe"] extends true ? FocusEvent : never) | (T["controls"] extends true ? Event : never) | PointerEvent, ) => void interface OnClickOutsideControlsReturn { stop: Fn cancel: Fn trigger: (event: Event) => void } /** * Listen for clicks outside of an element. * * @see https://vueuse.org/onClickOutside * @param target * @param handler * @param options */ export declare function onClickOutside( target: MaybeComputedElementRef, handler: OnClickOutsideHandler, options?: T, ): Fn export declare function onClickOutside>( target: MaybeComputedElementRef, handler: OnClickOutsideHandler, options: T, ): OnClickOutsideControlsReturn ```