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

Add support for keyframes in css calls in ternary operator #1683

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 27 additions & 9 deletions packages/babel-plugin/src/utils/css-builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,32 +387,50 @@ const extractConditionalExpression = (node: t.ConditionalExpression, meta: Metad
variables.push(...cssOutput.variables);

const mergedOutput = mergeSubsequentUnconditionalCssItems(cssOutput.css);
if (mergedOutput.length > 1) {
const sheetItems: SheetCssItem[] = [];
const nonSheetItems: Exclude<CssItem, SheetCssItem>[] = [];

for (const cssItem of mergedOutput) {
if (cssItem.type === 'sheet') {
sheetItems.push(cssItem);
} else {
nonSheetItems.push(cssItem);
}
}

if (nonSheetItems.length > 1) {
// Each branch should evaluate down to a single logical or unconditional CSS Item.
throw buildCodeFrameError(
'Conditional branch contains unexpected expression',
node,
meta.parentPath
);
}
return mergedOutput[0];

return { sheetItems, nonSheetItems: nonSheetItems[0] };
}

return undefined;
});

if (consequentCss && alternateCss) {
css.push({
if (consequentCss?.nonSheetItems && alternateCss?.nonSheetItems) {
css.push(...consequentCss.sheetItems, ...alternateCss.sheetItems, {
type: 'conditional',
test: node.test,
consequent: consequentCss,
alternate: alternateCss,
consequent: consequentCss.nonSheetItems,
alternate: alternateCss.nonSheetItems,
});
} else if (consequentCss) {
// convert single-sided conditional into logical statements
css.push(...getLogicalItemFromConditionalExpression([consequentCss], node, 'consequent'));
css.push(
...consequentCss.sheetItems,
// convert single-sided conditional into logical statements
...getLogicalItemFromConditionalExpression([consequentCss.nonSheetItems], node, 'consequent')
);
} else if (alternateCss) {
css.push(...getLogicalItemFromConditionalExpression([alternateCss], node, 'alternate'));
css.push(
...alternateCss.sheetItems,
...getLogicalItemFromConditionalExpression([alternateCss.nonSheetItems], node, 'alternate')
);
}

return { css, variables };
Expand Down
Loading