Skip to content

Commit ad63d52

Browse files
ealmloffjkelleyrtp
andauthored
Merge static and dynamic styles (#3950)
* Merge static and dynamic styles --------- Co-authored-by: Jonathan Kelley <[email protected]>
1 parent 68c7b90 commit ad63d52

File tree

6 files changed

+47
-3
lines changed

6 files changed

+47
-3
lines changed

packages/interpreter/src/js/common.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/interpreter/src/js/core.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[6449103750905854967, 17669692872757955279, 13069001215487072322, 11420464406527728232, 3770103091118609057, 5444526391971481782, 15100726369461302769, 5052021921702764563, 10988859153374944111, 16153602427306015669]
1+
[6449103750905854967, 17669692872757955279, 13069001215487072322, 11420464406527728232, 3770103091118609057, 5444526391971481782, 15100726369461302769, 5052021921702764563, 10988859153374944111, 8086017882241405700]

packages/interpreter/src/ts/set_attribute.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ export function setAttributeInner(
5858
node.innerHTML = value;
5959
break;
6060

61+
case "style":
62+
// Save the existing styles
63+
const existingStyles: Record<string, string> = {};
64+
65+
for (let i = 0; i < node.style.length; i++) {
66+
const prop = node.style[i];
67+
existingStyles[prop] = node.style.getPropertyValue(prop);
68+
}
69+
// Override all styles
70+
node.setAttribute(field, value);
71+
// Restore the old styles
72+
for (const prop in existingStyles) {
73+
// If it wasn't overridden, restore it
74+
if (!node.style.getPropertyValue(prop)) {
75+
node.style.setProperty(prop, existingStyles[prop]);
76+
}
77+
}
78+
break;
79+
6180
case "multiple":
6281
setAttributeDefault(node, field, value);
6382
// reset the selected value whenever multiple changes

packages/playwright-tests/web.spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,16 @@ test("document elements", async ({ page }) => {
156156
await expect(main).toHaveCSS("font-family", "Roboto");
157157
});
158158

159+
test("merge styles", async ({ page }) => {
160+
await page.goto("http://localhost:9999");
161+
// wait until the div is mounted
162+
const div = page.locator("div#merge-styles-div");
163+
await div.waitFor({ state: "attached" });
164+
await expect(div).toHaveCSS("background-color", "rgb(255, 0, 0)");
165+
await expect(div).toHaveCSS("width", "100px");
166+
await expect(div).toHaveCSS("height", "100px");
167+
});
168+
159169
test("select multiple", async ({ page }) => {
160170
await page.goto("http://localhost:9999");
161171
// wait until the select element is mounted

packages/playwright-tests/web/src/main.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ fn app() -> Element {
6363
OnMounted {}
6464
WebSysClosure {}
6565
DocumentElements {}
66+
MergeStyles {}
6667
SelectMultiple {}
6768
}
6869
}
@@ -156,6 +157,20 @@ fn DocumentElements() -> Element {
156157
}
157158
}
158159

160+
// Regression test for https://github.com/DioxusLabs/dioxus/issues/3887
161+
#[component]
162+
fn MergeStyles() -> Element {
163+
let px = 100;
164+
165+
rsx! {
166+
div {
167+
id: "merge-styles-div",
168+
style: "width: {px}px; height: {px}px",
169+
background_color: "red",
170+
}
171+
}
172+
}
173+
159174
// Select elements have odd default behavior when you set the multiple attribute after mounting the element
160175
// Regression test for https://github.com/DioxusLabs/dioxus/issues/3185
161176
#[component]

0 commit comments

Comments
 (0)