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.
|
|
|
|
|
|
|
|
// The first function should only ever be used when we know for certain that
|
|
|
|
// the target element is going to exist.
|
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
/** Returns the first element found that matches the selector. */
|
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
|
|
|
}
|
|
|
|
|
2022-02-23 13:52:06 +00:00
|
|
|
/** Returns all elements found from all the 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
|
|
|
);
|
|
|
|
}
|