// forked from https://github.com/component/classes function isRegExp(val: string | RegExp): val is RegExp { return '[object RegExp]' === toString.call(val) } /** * Whitespace regexp. */ const re = /\s+/ class Classes { el: Element list: Element['classList'] constructor(el: Element) { if (!el || !el.nodeType) { throw new Error('A DOM element reference is required') } this.el = el this.list = el.classList } /** * Add class `name` if not already present. */ add(name: string) { // classList if (this.list) { this.list.add(name) return this } // fallback const arr = this.array() const i = arr.indexOf(name) if (!~i) { arr.push(name) } this.el.className = arr.join(' ') return this } /** * Remove class `name` when present, or * pass a regular expression to remove * any which match. */ remove(name: string | RegExp) { if (isRegExp(name)) { return this.removeMatching(name) } // classList if (this.list) { this.list.remove(name) return this } // fallback const arr = this.array() const i = arr.indexOf(name) if (~i) { arr.splice(i, 1) } this.el.className = arr.join(' ') return this } /** * Toggle class `name`, can force state via `force`. * * For browsers that support classList, but do not support `force` yet, * the mistake will be detected and corrected. */ toggle(name: string, force?: boolean) { // classList if (this.list) { if ('undefined' !== typeof force) { this.list.toggle(name, force) } else { this.list.toggle(name) } return this } // fallback if ('undefined' !== typeof force) { if (!force) { this.remove(name) } else { this.add(name) } } else if (this.has(name)) { this.remove(name) } else { this.add(name) } return this } /** * Return an array of classes. */ array() { const className = this.el.getAttribute('class') || '' const str = className.replace(/^\s+|\s+$/g, '') const arr = str.split(re) if ('' === arr[0]) { arr.shift() } return arr } /** * Check if class `name` is present. */ contains(name: string) { return this.list ? this.list.contains(name) : !!~this.array().indexOf(name) } /** * aka contains */ has(name: string) { return this.contains(name) } /** * Remove all classes matching `re`. */ private removeMatching(re: RegExp) { const arr = this.array() for (const cls of arr) { if (re.test(cls)) { this.remove(cls) } } return this } } /** * Wrap `el` in a `ClassList`. */ export function classes(el: Element) { return new Classes(el) } export function hasClass(el: Element | null | undefined, className: string) { if (!el) { console.warn('param `elem` hasClass(elem, className) should be an element') return false } if (el.classList) { return el.classList.contains(className) } else { // className can also be an instance of SVGAnimatedString if the element is an SVGElement // https://developer.mozilla.org/en-US/docs/Web/API/Element/className return typeof el.className === 'string' && !!(new RegExp('(\\s|^)' + className + '(\\s|$)').exec(el.className)) } } export function addClass(el: Element | null | undefined, className: string) { if (!el) { console.warn('param `elem` hasClass(elem, className) should be an element') return } else if (hasClass(el, className)) { return } if (el.classList) { el.classList.add(className) } else { el.className += ' ' + className } } export function removeClass(el: Element | null | undefined, className: string) { if (!el) { console.warn('param `elem` hasClass(elem, className) should be an element') return } else if (!hasClass(el, className)) { return } if (el.classList) { el.classList.remove(className) } else { const reg = new RegExp('(\\s|^)' + className + '(\\s|$)') el.className = el.className.replace(reg, ' ') } }