Skip to content

Commit

Permalink
fix: preserve bound media query styles when converting builder to mit…
Browse files Browse the repository at this point in the history
…osis (#1644)
  • Loading branch information
liamdebeasi authored Nov 22, 2024
1 parent 73a55a3 commit 10a168d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/violet-kangaroos-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/mitosis': patch
---

[Builder] preserve bound media query styles when converting to Mitosis
50 changes: 50 additions & 0 deletions packages/core/src/__tests__/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,56 @@ describe('Builder', () => {
'world',
);
});

test('preserve bound media query styles when converting to mitosis', () => {
const content = {
data: {
blocks: [
{
'@type': '@builder.io/sdk:Element' as const,
bindings: {
'responsiveStyles.small.left': 'state.left',
'responsiveStyles.small.top': 'state.top',
'responsiveStyles.large.color': 'state.color',
'style.fontSize': 'state.fontSize',
},
},
],
},
};

const mitosis = builderContentToMitosisComponent(content);
expect(mitosis.children[0].bindings).toMatchInlineSnapshot(`
{
"style": {
"bindingType": "expression",
"code": "{ fontSize: state.fontSize, \\"@media (max-width: 640px)\\": {\\"left\\":\\"state.left\\",\\"top\\":\\"state.top\\"}, \\"@media (max-width: 1200px)\\": {\\"color\\":\\"state.color\\"}, }",
"type": "single",
},
}
`);

const jsx = componentToMitosis()({ component: mitosis });
expect(jsx).toMatchInlineSnapshot(`
"export default function MyComponent(props) {
return (
<div
style={{
fontSize: state.fontSize,
\\"@media (max-width: 640px)\\": {
left: \\"state.left\\",
top: \\"state.top\\",
},
\\"@media (max-width: 1200px)\\": {
color: \\"state.color\\",
},
}}
/>
);
}
"
`);
});
});

const bindingJson = {
Expand Down
35 changes: 34 additions & 1 deletion packages/core/src/parsers/builder/builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,50 @@ const getActionBindingsFromBlock = (

const getStyleStringFromBlock = (block: BuilderElement, options: BuilderToMitosisOptions) => {
const styleBindings: any = {};
const responsiveStyles: Record<string, Record<string, string>> = {};
let styleString = '';

if (block.bindings) {
for (const key in block.bindings) {
if (key.includes('style') && key.includes('.')) {
if (!key.includes('.')) {
continue;
}
if (key.includes('style')) {
const styleProperty = key.split('.')[1];
styleBindings[styleProperty] = convertExportDefaultToReturn(
block.code?.bindings?.[key] || block.bindings[key],
);
/**
* responsiveStyles that are bound need to be merged into media queries.
* Example:
* responsiveStyles.large.color: "state.color"
* responsiveStyles.large.background: "state.background"
* Should get mapped to:
* @media (max-width: 1200px): {
* color: "state.color",
* background: "state.background"
* }
*/
} else if (key.includes('responsiveStyles')) {
const [_, size, prop] = key.split('.');
const mediaKey = `@media (max-width: ${sizes[size as Size].max}px)`;

/**
* The media query key has spaces/special characters so we need to ensure
* that the key is always a string otherwise there will be runtime errors.
*/
const objKey = `"${mediaKey}"`;
responsiveStyles[objKey] = {
...responsiveStyles[objKey],
[prop]: block.bindings[key],
};
}
}

// All binding values are strings, so stringify media query objects
for (const key in responsiveStyles) {
styleBindings[key] = JSON.stringify(responsiveStyles[key]);
}
}

const styleKeys = Object.keys(styleBindings);
Expand Down

0 comments on commit 10a168d

Please sign in to comment.