From f305def148758da35ad32444fbfdfcdc76f4f2d1 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Thu, 29 Jun 2023 13:25:01 +0200 Subject: [PATCH] feat: add include and exclude filters --- src/filters.ts | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/filters.ts b/src/filters.ts index 9544edc..6f816f1 100644 --- a/src/filters.ts +++ b/src/filters.ts @@ -1,5 +1,30 @@ // common filters for global events -export function excludeElements() { - // TODO: implement +/** + * Creates a filter than can be passed to `` to exclude events from elements with the given tag names. + * + * @param tagNames - array of tag names to exclude + */ +export function excludeElements(tagNames: Array>) { + return (event: Event) => { + const target = event.target as HTMLElement + return !(tagNames as string[]).includes(target.tagName) + } } + +/** + * Creates a filter than can be passed to `` to include events from elements with the given tag names. + * + * @see excludeElements + * + * @param tagNames - array of tag names to include + */ +export function includeElements(tagNames: Array>) { + return (event: Event) => { + const target = event.target as HTMLElement + return (tagNames as string[]).includes(target.tagName) + } +} + +// @internal +export type _HTMLElementNames = keyof HTMLElementTagNameMap