tickle/modules/utils/getElementByCSSSelector.mjs

19 lines
605 B
JavaScript
Raw Normal View History

2023-05-13 15:26:48 +00:00
//@ts-check
import { isLocalHost } from "./isLocalHost.mjs";
/**
* Gets an element by a valid selector if the element exists, otherwise throws, but only if running in localhost environments.
* Use this in the initial setup to verify all elements exist
* @param {string} selector
* @return {HTMLElement}
*/
export const getElementByCSSSelector = (selector) => {
const element = document && document.querySelector(selector);
if (isLocalHost && !element) {
throw new Error(`Element "#${selector}" was not found`);
}
// @ts-ignore
return element;
};
export default getElementByCSSSelector