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

Support use of css tagged template inside style objects (v6) #3497

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions packages/styled-components/src/constructors/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@ import interleave from '../utils/interleave';
import isFunction from '../utils/isFunction';
import isPlainObject from '../utils/isPlainObject';

/**
* Used when flattening object styles to determine if we should
* expand an array of styles.
*/
const addTag = (arg: ReturnType<typeof flatten> & { isCss?: boolean }) => {
if (Array.isArray(arg)) {
// eslint-disable-next-line no-param-reassign
arg.isCss = true;
}
return arg;
Comment on lines +13 to +17
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this mutation required? or is it possible to:

Suggested change
if (Array.isArray(arg)) {
// eslint-disable-next-line no-param-reassign
arg.isCss = true;
}
return arg;
return Array.isArray(arg) ? { ...arg, isCss: true} : arg;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I think the OP's code is probably a little more performant since it isn't introducing additional objects into a hot path

};

export default function css(
styles: Styles,
...interpolations: Array<Interpolation>
): FlattenerResult {
if (isFunction(styles) || isPlainObject(styles)) {
const styleFunctionOrObject = styles as Function | ExtensibleObject;

return flatten(interleave(EMPTY_ARRAY as string[], [styleFunctionOrObject, ...interpolations]));
return addTag(
flatten(interleave(EMPTY_ARRAY as string[], [styleFunctionOrObject, ...interpolations]))
);
}

const styleStringArray = styles as string[];
Expand All @@ -25,5 +39,5 @@ export default function css(
return styleStringArray;
}

return flatten(interleave(styleStringArray, interpolations));
return addTag(flatten(interleave(styleStringArray, interpolations)));
}
106 changes: 106 additions & 0 deletions packages/styled-components/src/constructors/test/keyframes.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,112 @@ describe('keyframes', () => {
`);
});

it('should insert the correct styles for objects', () => {
const rules = `
0% {
opacity: 0;
}
100% {
opacity: 1;
}
`;

const animation = keyframes`${rules}`;
const name = animation.getName();

getRenderedCSS('');

const Comp = styled.div({
animation: css`
${animation} 2s linear infinite
`,
});

TestRenderer.create(<Comp />);

getRenderedCSS(`
.a {
-webkit-animation: ${name} 2s linear infinite;
animation: ${name} 2s linear infinite;
}
@-webkit-keyframes ${name} {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
@keyframes ${name} {
0% {
opacity:0;
}
100% {
opacity:1;
}
}
`);
});

it('should insert the correct styles for objects with nesting', () => {
const rules = `
0% {
opacity: 0;
}
100% {
opacity: 1;
}
`;

const animation = keyframes`${rules}`;

getRenderedCSS('');

const Comp = styled.div({
'@media(max-width: 700px)': {
animation: css`
${animation} 2s linear infinite
`,
':hover': {
animation: css`
${animation} 10s linear infinite
`,
},
},
});

TestRenderer.create(<Comp />);

getRenderedCSS(`
@media(max-width: 700px) {
.a {
-webkit-animation: jgzmJZ 2s linear infinite;
animation: jgzmJZ 2s linear infinite;
}
}
.a:hover {
-webkit-animation: jgzmJZ 10s linear infinite;
animation: jgzmJZ 10s linear infinite;
}
@-webkit-keyframes jgzmJZ {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
@keyframes jgzmJZ {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
`);
});

it('should insert the correct styles when keyframes in props', () => {
const rules = `
0% {
Expand Down
6 changes: 3 additions & 3 deletions packages/styled-components/src/utils/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export const objToCssArray = (obj: ExtensibleObject, prevKey?: string): string[]
for (const key in obj) {
if (!obj.hasOwnProperty(key) || isFalsish(obj[key])) continue;

if (isPlainObject(obj[key])) {
rules.push(...objToCssArray(obj[key], key));
} else if (isFunction(obj[key])) {
if ((Array.isArray(obj[key]) && obj[key].isCss) || isFunction(obj[key])) {
rules.push(`${hyphenate(key)}:`, obj[key], ';');
} else if (isPlainObject(obj[key])) {
rules.push(...objToCssArray(obj[key], key));
} else {
rules.push(`${hyphenate(key)}: ${addUnitIfNeeded(key, obj[key])};`);
}
Expand Down