Skip to content

Commit

Permalink
Complex expression fix (#68)
Browse files Browse the repository at this point in the history
* Fix for highly complex expressions.

* add changeset
  • Loading branch information
nsaunders authored Aug 16, 2024
1 parent 1475160 commit 1c5e06a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
6 changes: 6 additions & 0 deletions .changeset/friendly-snakes-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@embellish/react": patch
"@embellish/core": patch
---

Fix for highly complex expressions not working properly in Chrome.
30 changes: 24 additions & 6 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,23 +266,41 @@ export function createInlineConditions<
condition: Condition<HookId>,
valueIfTrue: string,
valueIfFalse: string,
): string {
): [string, Record<string, string>] {
if (typeof condition === "string") {
return `var(--${condition}-1, ${valueIfTrue}) var(--${condition}-0, ${valueIfFalse})`;
let valTrue = valueIfTrue,
valFalse = valueIfFalse;
const extraDecls: Record<string, string> = {};
if (valTrue.length > 32) {
const hash = createHash(valTrue);
extraDecls[`--${hash}`] = valTrue;
valTrue = `var(--${hash})`;
}
if (valFalse.length > 32) {
const hash = createHash(valFalse);
extraDecls[`--${hash}`] = valFalse;
valFalse = `var(--${hash})`;
}
return [
`var(--${condition}-1, ${valTrue}) var(--${condition}-0, ${valFalse})`,
extraDecls,
];
}
if (condition.and) {
const [head, ...tail] = condition.and;
if (!head) {
return valueIfTrue;
return [valueIfTrue, {}];
}
if (tail.length === 0) {
return buildExpression(head, valueIfTrue, valueIfFalse);
}
return buildExpression(
head,
buildExpression({ and: tail }, valueIfTrue, valueIfFalse),
const [tailExpr, tailDecls] = buildExpression(
{ and: tail },
valueIfTrue,
valueIfFalse,
);
const [expr, decls] = buildExpression(head, tailExpr, valueIfFalse);
return [expr, { ...decls, ...tailDecls }];
}
if (condition.or) {
return buildExpression(
Expand Down
13 changes: 8 additions & 5 deletions packages/react/src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,11 +168,14 @@ export function createComponent({
continue;
}
delete style[resolvedProperty];
style[resolvedProperty] = conditions.conditionalDeclarationValue(
stylePrefix,
condValue,
fallback,
);
let extraDecls;
[style[resolvedProperty], extraDecls] =
conditions.conditionalDeclarationValue(
stylePrefix,
condValue,
fallback,
);
Object.assign(style, extraDecls);
}
}

Expand Down

0 comments on commit 1c5e06a

Please sign in to comment.