/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ /** @internal */ export function normalizeClassNames( ...classNames: Array ): Array { const rval = []; for (const className of classNames) { if (className && typeof className === 'string') { for (const [s] of className.matchAll(/\S+/g)) { rval.push(s); } } } return rval; } /** * Takes an HTML element and adds the classNames passed within an array, * ignoring any non-string types. A space can be used to add multiple classes * eg. addClassNamesToElement(element, ['element-inner active', true, null]) * will add both 'element-inner' and 'active' as classes to that element. * @param element - The element in which the classes are added * @param classNames - An array defining the class names to add to the element */ export function addClassNamesToElement( element: HTMLElement, ...classNames: Array ): void { const classesToAdd = normalizeClassNames(...classNames); if (classesToAdd.length > 0) { element.classList.add(...classesToAdd); } } /** * Takes an HTML element and removes the classNames passed within an array, * ignoring any non-string types. A space can be used to remove multiple classes * eg. removeClassNamesFromElement(element, ['active small', true, null]) * will remove both the 'active' and 'small' classes from that element. * @param element - The element in which the classes are removed * @param classNames - An array defining the class names to remove from the element */ export function removeClassNamesFromElement( element: HTMLElement, ...classNames: Array ): void { const classesToRemove = normalizeClassNames(...classNames); if (classesToRemove.length > 0) { element.classList.remove(...classesToRemove); } }