Skip to content

Instantly share code, notes, and snippets.

@Oaphi
Last active February 10, 2021 02:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Oaphi/01cecebeab6c69e822e351be2c098f8b to your computer and use it in GitHub Desktop.
Save Oaphi/01cecebeab6c69e822e351be2c098f8b to your computer and use it in GitHub Desktop.
Common basic DOM helpers
export const remClasses = (
{ classList }: { classList: DOMTokenList },
...classes: string[]
) => classList.remove(...classes);
export const addClasses = (
{ classList }: { classList: DOMTokenList },
...classes: string[]
) => classList.add(...classes);
export const clear = ({ children }: { children: HTMLCollection }) =>
Array.from(children).forEach((child) => child.remove());
export const sortChildren = (
element: Element,
comparator: (a: Element, b: Element) => number
) => {
const { children } = element;
const arr = Array.from(children);
comparator ? arr.sort(comparator) : arr.sort();
arr.forEach((child) => element.append(child));
};
export const $ = (selector: string, ctx = document) =>
ctx.querySelector(selector);
export const $$ = (selector: string, ctx = document) =>
ctx.querySelectorAll(selector);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment