UNPKG

svelte

Version:

Cybernetically enhanced web apps

52 lines (45 loc) 1.63 kB
import { to_class } from '../../../shared/attributes.js'; import { hydrating } from '../hydration.js'; /** * @param {Element} dom * @param {boolean | number} is_html * @param {string | null} value * @param {string} [hash] * @param {Record<string, any>} [prev_classes] * @param {Record<string, any>} [next_classes] * @returns {Record<string, boolean> | undefined} */ export function set_class(dom, is_html, value, hash, prev_classes, next_classes) { // @ts-expect-error need to add __className to patched prototype var prev = dom.__className; if ( hydrating || prev !== value || prev === undefined // for edge case of `class={undefined}` ) { var next_class_name = to_class(value, hash, next_classes); if (!hydrating || next_class_name !== dom.getAttribute('class')) { // Removing the attribute when the value is only an empty string causes // performance issues vs simply making the className an empty string. So // we should only remove the class if the the value is nullish // and there no hash/directives : if (next_class_name == null) { dom.removeAttribute('class'); } else if (is_html) { dom.className = next_class_name; } else { dom.setAttribute('class', next_class_name); } } // @ts-expect-error need to add __className to patched prototype dom.__className = value; } else if (next_classes && prev_classes !== next_classes) { for (var key in next_classes) { var is_present = !!next_classes[key]; if (prev_classes == null || is_present !== !!prev_classes[key]) { dom.classList.toggle(key, is_present); } } } return next_classes; }