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(tailwind): perf optimisations #19

Merged
merged 2 commits into from
Oct 2, 2023
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
23 changes: 11 additions & 12 deletions packages/tailwind/src/tailwind.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ function processElement(
const classNames = element.props.className.split(' ');

const customClassNames = classNames.filter((className: string) => {
if (twi(className, { ignoreMediaQueries: true })) {
convertedStyles.push(twi(className, { ignoreMediaQueries: true }));
const tailwindClassNames = twi(className, { ignoreMediaQueries: true });
if (tailwindClassNames) {
convertedStyles.push(tailwindClassNames);
return false;
} else if (twi(className, { ignoreMediaQueries: false })) {
responsiveStyles.push(className);
Expand All @@ -40,15 +41,13 @@ function processElement(

headStyles.push(convertedResponsiveStyles.replace(/^\n+/, '').replace(/\n+$/, ''));

const newProps = {
// eslint-disable-next-line no-param-reassign
element = React.cloneElement(element, {
...element.props,
// eslint-disable-next-line no-undefined
className: customClassNames.length ? customClassNames.join(' ') : undefined,
style: { ...element.props.style, ...cssToJsxStyle(convertedStyles.join(' ')) }
};

// eslint-disable-next-line no-param-reassign
element = React.cloneElement(element, newProps);
});
}

if (element.props.children) {
Expand All @@ -75,6 +74,7 @@ function processHead(child: React.ReactElement, responsiveStyles: string[]): Rea
const headChildren = React.Children.toArray(child.props.children);
headChildren.push(styleElement);

// eslint-disable-next-line no-param-reassign
child = React.cloneElement(child, child.props, ...headChildren);
}
if (child.props.children) {
Expand All @@ -86,6 +86,7 @@ function processHead(child: React.ReactElement, responsiveStyles: string[]): Rea
return child;
});

// eslint-disable-next-line no-param-reassign
child = React.cloneElement(child, child.props, ...processedChildren);
}

Expand All @@ -109,10 +110,10 @@ export const Tailwind = ({ children, config }: React.PropsWithChildren<TailwindP
const hasResponsiveStyles = /@media[^{]+\{(?<content>[\s\S]+?)\}\s*\}/gm.test(
headStyles.join(' ')
);
const hasHTML = /<html[^>]*>/gm.test(fullHTML);
const hasHead = /<head[^>]*>/gm.test(fullHTML);

if (hasResponsiveStyles && (!hasHTML || !hasHead)) {
const hasHTMLAndHead = /<html[^>]*>(?=[\s\S]*<head[^>]*>)/gm.test(fullHTML);

if (hasResponsiveStyles && !hasHTMLAndHead) {
throw new Error(
'Tailwind: To use responsive styles you must have a <html> and <head> element in your template.'
);
Expand All @@ -130,5 +131,3 @@ export const Tailwind = ({ children, config }: React.PropsWithChildren<TailwindP

return <>{childrenWithInlineAndResponsiveStyles}</>;
};

Tailwind.displayName = 'Tailwind';
2 changes: 0 additions & 2 deletions packages/tailwind/test/.snapshots/tailwind.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,3 @@ exports[`Responsive styles > should persist exsisting <head/> elements 1`] = `"<
exports[`Tailwind component > should be able to use background image 1`] = `"<div style=\\"background-image:url(https://example.com/image.png)\\"></div>"`;

exports[`Tailwind component > should override inline styles with Tailwind styles 1`] = `"<div style=\\"background-color:rgb(0,0,0);font-size:16px\\"></div>"`;

exports[`should preserve mso styles 1`] = `"<html data-id=\\"@jsx-email/html\\" lang=\\"en\\" dir=\\"ltr\\"><head data-id=\\"@jsx-email/head\\"><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=UTF-8\\"/><style><style>@media(min-width:640px){.sm\\\\:text-sm{font-size:0.875rem;line-height:1.25rem}.sm\\\\:bg-red-50{background-color:rgb(254,242,242)}}@media(min-width:768px){.md\\\\:text-lg{font-size:1.125rem;line-height:1.75rem}}</style></style></head><span><!--[if mso]><i style=\\"letter-spacing: 10px;mso-font-width:-100%;\\" hidden>&nbsp;</i><![endif]--></span><div class=\\"custom-class\\" style=\\"background-color:rgb(255,255,255)\\"></div></html>"`;
50 changes: 48 additions & 2 deletions packages/tailwind/test/tailwind.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ describe('Custom plugins config', () => {
});
});

describe('', () => {
describe('<Tailwind> component', () => {
it('should preserve mso styles', () => {
const actualOutput = render(
<Tailwind>
Expand All @@ -305,6 +305,52 @@ describe('', () => {
</Tailwind>
);

expect(actualOutput).toMatchSnapshot();
expect(actualOutput).toMatchInlineSnapshot(
'"<html data-id=\\"@jsx-email/html\\" lang=\\"en\\" dir=\\"ltr\\"><head data-id=\\"@jsx-email/head\\"><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=UTF-8\\"/><style><style>@media(min-width:640px){.sm\\\\:text-sm{font-size:0.875rem;line-height:1.25rem}.sm\\\\:bg-red-50{background-color:rgb(254,242,242)}}@media(min-width:768px){.md\\\\:text-lg{font-size:1.125rem;line-height:1.75rem}}</style></style></head><span><!--[if mso]><i style=\\"letter-spacing: 10px;mso-font-width:-100%;\\" hidden>&nbsp;</i><![endif]--></span><div class=\\"custom-class\\" style=\\"background-color:rgb(255,255,255)\\"></div></html>"'
);
});

it('should recognize custom resopnsive screen', () => {
const config: TailwindConfig = {
theme: {
screens: {
sm: { min: '640px' },
// eslint-disable-next-line sort-keys
md: { min: '768px' },
// eslint-disable-next-line sort-keys
lg: { min: '1024px' },
xl: { min: '1280px' },
// eslint-disable-next-line sort-keys
'2xl': { min: '1536px' }
}
}
};
const actualOutput = render(
<Tailwind config={config}>
<Html>
<Head />
<div className="xl:bg-green-500">Test</div>
<div className="2xl:bg-blue-500">Test</div>
</Html>
</Tailwind>
);

expect(actualOutput).toMatchInlineSnapshot(
'"<html data-id=\\"@jsx-email/html\\" lang=\\"en\\" dir=\\"ltr\\"><head data-id=\\"@jsx-email/head\\"><meta http-equiv=\\"Content-Type\\" content=\\"text/html; charset=UTF-8\\"/><style><style>@media(min-width:1280px){.xl\\\\:bg-green-500{background-color:rgb(34,197,94)}}@media(min-width:1536px){.\\\\32xl\\\\:bg-blue-500{background-color:undefined}.\\\\32xl\\\\:bg-blue-500{background-color:rgb(59,130,246)}}</style><style>@media(min-width:1280px){.xl\\\\:bg-green-500{background-color:rgb(34,197,94)}}@media(min-width:1536px){.\\\\32xl\\\\:bg-blue-500{background-color:undefined}.\\\\32xl\\\\:bg-blue-500{background-color:rgb(59,130,246)}}</style></style></head><div><div>Test</div></div><div><div>Test</div></div></html>"'
);
});

it('should work with calc() with + sign', () => {
const actualOutput = render(
<Tailwind>
<div className="max-h-[calc(50px+3rem)] bg-red-100">
<div className="h-[200px]">something tall</div>
</div>
</Tailwind>
);

expect(actualOutput).toMatchInlineSnapshot(
'"<div style=\\"max-height:calc(50px + 3rem);background-color:rgb(254,226,226)\\"><div style=\\"height:200px\\"><div style=\\"height:200px\\">something tall</div></div></div>"'
);
});
});
Loading