Skip to content

Commit

Permalink
Write style for once in rerender mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Oct 25, 2023
1 parent c4dcc73 commit 8a67a67
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
20 changes: 4 additions & 16 deletions fastn-js/js/dom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let fastn_dom = {};

fastn_dom.styleClasses = "";

fastn_dom.codeData = {
availableThemes: {},
addedCssFile: []
Expand Down Expand Up @@ -937,14 +939,7 @@ class Node2 {
if (!!className) {
if (!fastn_dom.classes[cssClass]) {
fastn_dom.classes[cssClass] = fastn_dom.classes[cssClass] || obj;
let styles = document.getElementById('styles');
let newClasses = getClassAsString(cssClass, obj);
let textNode = document.createTextNode(newClasses);
if (styles.styleSheet) {
styles.styleSheet.cssText = newClasses;
} else {
styles.appendChild(textNode);
}
fastn_utils.createStyle(cssClass, obj);
}
return cls;
}
Expand All @@ -958,14 +953,7 @@ class Node2 {
if (createClass) {
if (!fastn_dom.classes[cssClass]) {
fastn_dom.classes[cssClass] = fastn_dom.classes[cssClass] || obj;
let styles = document.getElementById('styles');
let newClasses = getClassAsString(cssClass, obj);
let textNode = document.createTextNode(newClasses);
if (styles.styleSheet) {
styles.styleSheet.cssText = newClasses;
} else {
styles.appendChild(textNode);
}
fastn_utils.createStyle(cssClass, obj);
}
this.#node.style.removeProperty(property);
this.#node.classList.add(cls);
Expand Down
14 changes: 14 additions & 0 deletions fastn-js/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,20 @@ let fastn_utils = {
}
return [node, css, attributes];
},
createStyle(cssClass, obj) {
if (rerender) {
fastn_dom.styleClasses = `${fastn_dom.styleClasses}${getClassAsString(cssClass, obj)}\n`;
} else {
let styles = document.getElementById('styles');
let newClasses = getClassAsString(cssClass, obj);
let textNode = document.createTextNode(newClasses);
if (styles.styleSheet) {
styles.styleSheet.cssText = newClasses;
} else {
styles.appendChild(textNode);
}
}
},
getStaticValue(obj) {
if (obj instanceof fastn.mutableClass) {
return this.getStaticValue(obj.get());
Expand Down
4 changes: 4 additions & 0 deletions fastn-js/js/virtual.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ let fastn_virtual = {}
let id_counter = 0;
let hydrating = false;
let ssr = false;
let rerender = false;

class ClassList {
#classes = [];
Expand Down Expand Up @@ -152,6 +153,7 @@ fastn_virtual.hydrate = function(main) {
let current_device = ftd.get_device();
let found_device = ftd.device.get();
if (current_device !== found_device) {
rerender = true
ftd.device = fastn.mutable(current_device);
let styles = document.getElementById("styles");
styles.innerText = "";
Expand All @@ -165,6 +167,8 @@ fastn_virtual.hydrate = function(main) {
}

main(document.body);
rerender = false;
styles.innerHTML = fastn_dom.styleClasses;
return;
}
hydrating = true;
Expand Down

0 comments on commit 8a67a67

Please sign in to comment.