Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bound media query styles are not converted to strings #1651

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-windows-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[Builder]: bound media query styles are not converted to strings
8 changes: 4 additions & 4 deletions packages/core/src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ describe('Builder', () => {
{
"style": {
"bindingType": "expression",
"code": "{ fontSize: state.fontSize, \\"@media (max-width: 640px)\\": {\\"left\\":\\"state.left\\",\\"top\\":\\"state.top\\"}, \\"@media (max-width: 1200px)\\": {\\"color\\":\\"state.color\\"}, }",
"code": "{ fontSize: state.fontSize, \\"@media (max-width: 640px)\\": { left: state.left, top: state.top }, \\"@media (max-width: 1200px)\\": { color: state.color }, }",
"type": "single",
},
}
Expand All @@ -712,11 +712,11 @@ describe('Builder', () => {
style={{
fontSize: state.fontSize,
\\"@media (max-width: 640px)\\": {
left: \\"state.left\\",
top: \\"state.top\\",
left: state.left,
top: state.top,
},
\\"@media (max-width: 1200px)\\": {
color: \\"state.color\\",
color: state.color,
},
}}
/>
Expand Down
15 changes: 11 additions & 4 deletions packages/core/src/parsers/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ const getStyleStringFromBlock = (block: BuilderElement, options: BuilderToMitosi
* responsiveStyles.large.background: "state.background"
* Should get mapped to:
* @media (max-width: 1200px): {
* color: "state.color",
* background: "state.background"
* color: state.color,
* background: state.background
* }
*/
} else if (key.includes('responsiveStyles')) {
Expand All @@ -148,9 +148,16 @@ const getStyleStringFromBlock = (block: BuilderElement, options: BuilderToMitosi
}
}

// All binding values are strings, so stringify media query objects
/**
* All binding values are strings, but we don't want to stringify the values
* within the style object otherwise the bindings will be evaluated as strings.
* As a result, do not use JSON.stringify here.
*/
for (const key in responsiveStyles) {
styleBindings[key] = JSON.stringify(responsiveStyles[key]);
const styles = Object.keys(responsiveStyles[key]);
const keyValues = styles.map((prop) => `${prop}: ${responsiveStyles[key][prop]}`);
const stringifiedObject = `{ ${keyValues.join(', ')} }`;
styleBindings[key] = stringifiedObject;
}
}

Expand Down
Loading