Last active
June 12, 2023 04:55
-
-
Save guiseek/b6d3b6b5e466983b98b8160128769329 to your computer and use it in GitHub Desktop.
HTML & SVG QuerySelector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function query<K extends keyof SVGElementTagNameMap>( | |
name: K | `${K}.${string}` | `${K}#${string}` | `${K}[${string}]`, | |
parent?: Element | |
): SVGElementTagNameMap[K] | |
export function query<K extends keyof HTMLElementTagNameMap>( | |
name: K | `${K}.${string}` | `${K}#${string}` | `${K}[${string}]`, | |
parent?: Element | |
): HTMLElementTagNameMap[K] | |
export function query< | |
K extends keyof (SVGElementTagNameMap | HTMLElementTagNameMap) | |
>( | |
name: K | `${K}.${string}` | `${K}#${string}` | `${K}[${string}]`, | |
parent: Element = document.body | |
) { | |
return parent.querySelector(name) as K extends keyof SVGElementTagNameMap | |
? SVGElementTagNameMap[K] | |
: HTMLElementTagNameMap[K] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment