Last active
February 10, 2021 02:34
-
-
Save Oaphi/01cecebeab6c69e822e351be2c098f8b to your computer and use it in GitHub Desktop.
Common basic DOM helpers
This file contains 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 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