2020-10-10 23:32:27 +00:00
|
|
|
// These utility functions mainly exist so it's easier to work with TypeScript's
|
|
|
|
// typing and so we don't have to write `document.query...` all the time.
|
|
|
|
|
2023-06-25 10:00:43 +00:00
|
|
|
/**
|
|
|
|
* Get the first element found that matches the selector. Only use this when you
|
|
|
|
* know for certain that the target element exists.
|
|
|
|
*/
|
2020-10-10 23:32:27 +00:00
|
|
|
export function querySelector<T extends Element>(selector: string): T {
|
2022-02-23 13:52:06 +00:00
|
|
|
return document.querySelector(selector)!;
|
2020-10-10 23:32:27 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 10:00:43 +00:00
|
|
|
/**
|
|
|
|
* Get all elements found from all the given selectors.
|
|
|
|
*/
|
2020-10-10 23:32:27 +00:00
|
|
|
export function querySelectorAll<T extends Element>(
|
|
|
|
...selectors: string[]
|
|
|
|
): T[] {
|
|
|
|
return selectors.flatMap((selector) =>
|
2022-02-23 13:52:06 +00:00
|
|
|
Array.from(document.querySelectorAll(selector)),
|
2020-10-10 23:32:27 +00:00
|
|
|
);
|
|
|
|
}
|