Skip to content

Commit

Permalink
feat: add include and exclude filters
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jun 29, 2023
1 parent b2000c3 commit f305def
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/filters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
// common filters for global events

export function excludeElements() {
// TODO: implement
/**
* Creates a filter than can be passed to `<GlobalEvents />` to exclude events from elements with the given tag names.
*
* @param tagNames - array of tag names to exclude
*/
export function excludeElements(tagNames: Array<Uppercase<_HTMLElementNames>>) {
return (event: Event) => {
const target = event.target as HTMLElement
return !(tagNames as string[]).includes(target.tagName)
}
}

/**
* Creates a filter than can be passed to `<GlobalEvents />` 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<Uppercase<_HTMLElementNames>>) {
return (event: Event) => {
const target = event.target as HTMLElement
return (tagNames as string[]).includes(target.tagName)
}
}

// @internal
export type _HTMLElementNames = keyof HTMLElementTagNameMap

0 comments on commit f305def

Please # to comment.