Skip to content

Commit

Permalink
Better class removing
Browse files Browse the repository at this point in the history
  • Loading branch information
ulyssear committed Jan 11, 2023
1 parent 0027ecf commit efe0829
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ulyssear/css-in-js-in-html",
"version": "0.1.1",
"version": "0.1.2",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
18 changes: 15 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,17 @@ function apply_custom_class(element, className) {
value: class_value.substring(1, class_value.length - 1),
});
}
element.className = element.className.replace(className, '').trim();
const original_class = className;
// element.className = element.className.replace(className, '').trim();
// console.log({classes_to_apply})

// console.log({ selectors, classes_to_apply, events, media_query });

do_apply(element, selectors, classes_to_apply, events, media_query);
do_apply(element, selectors, classes_to_apply, events, media_query, original_class);
}
}

function do_apply(element, selectors, classes, events, media_query) {
function do_apply(element, selectors, classes, events, media_query, original_class) {
if (media_query) {
if ('(' !== media_query[0]) {
media_query = `(${media_query})`;
Expand Down Expand Up @@ -353,14 +354,25 @@ function do_apply(element, selectors, classes, events, media_query) {
return;
}

let to_remove_class = false;

for (let j = 0; j < elements_to_apply.length; j++) {
const element_to_apply = elements_to_apply[j];
if (!element_to_apply) continue;
for (let k = 0; k < classes.length; k++) {
const { name, value } = classes[k];
if (!name) continue;
element_to_apply.style.setProperty(name, value);
if (to_remove_class) continue;
if (element.className.includes(original_class)) {
to_remove_class = true;
}
}
}

if (to_remove_class) {
element.className = element.className.replace(original_class, '');
}
}

function init(document, event = undefined) {
Expand Down

0 comments on commit efe0829

Please sign in to comment.