-
Notifications
You must be signed in to change notification settings - Fork 8
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 maths calc function to post-process-css #378
Draft
druhill
wants to merge
2
commits into
beta
Choose a base branch
from
maths-calc
base: beta
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
228 changes: 228 additions & 0 deletions
228
scripts/post-process-css/formatters/maths-calc/maths-calc.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
import { expect } from "chai"; | ||
import { BrandTokens } from "../../brand-tokens"; | ||
import { ScreenSizeTokens } from "../../screen-size-tokens"; | ||
import { MathsCalc } from "./maths-calc"; | ||
|
||
describe("MathsCalc", () => { | ||
const mathsCalc = new MathsCalc(); | ||
|
||
describe("mathsCalc", () => { | ||
it("should format tokens as expected", () => { | ||
const tokens = new BrandTokens([ | ||
new ScreenSizeTokens( | ||
[ | ||
{ name: "--global-prop1", value: "value1" }, | ||
{ name: "--global-prop2", value: "value1 value2" }, | ||
{ name: "--global-prop3", value: "value1 * value2" }, | ||
{ name: "--global-prop4", value: "value1 / value2" }, | ||
{ name: "--global-prop5", value: "value1 + value2" }, | ||
{ name: "--global-prop6", value: "value1 - value2" }, | ||
{ name: "--global-prop7", value: "value1 * value2 + value3" }, | ||
{ name: "--global-prop8", value: "value1 * value2 value3" }, | ||
{ name: "--global-prop9", value: "(value1 + value2) * value3" }, | ||
{ name: "--global-prop10", value: "(value1 + value2) * (value3 - value4)" }, | ||
{ name: "--global-prop11", value: "(value1 + value2) * (value3 - value4) value5 - value6" }, | ||
{ name: "--global-typography1", value: "900 clamp(2.1588rem, 1.7083rem + 2.2525vw, 4.4113rem)/1.25 'Sage Headline'"}, | ||
{ name: "--global-typography2", value: "400 20px/1 sage-icons"}, | ||
], | ||
[], | ||
[], | ||
{ | ||
badge: [ | ||
{ | ||
name: "--badge-prop1", | ||
value: "var(--global-prop1)", | ||
}, | ||
{ | ||
name: "--badge-prop2", | ||
value: "var(--global-prop1) * 2", | ||
}, | ||
{ | ||
name: "--badge-prop3", | ||
value: "var(--global-prop1) / 2", | ||
}, | ||
{ | ||
name: "--badge-prop4", | ||
value: "var(--global-prop1) + 2", | ||
}, | ||
{ | ||
name: "--badge-prop5", | ||
value: "var(--global-prop1) - 2", | ||
}, | ||
{ | ||
name: "--badge-prop6", | ||
value: "var(--global-prop1) - var(--global-prop2)", | ||
}, | ||
{ | ||
name: "--badge-prop7", | ||
value: "(var(--global-prop1) - var(--global-prop2)) * var(--global-prop3)", | ||
}, | ||
], | ||
} | ||
), | ||
new ScreenSizeTokens( | ||
[ | ||
{ name: "--global-size-breakpoint-min-width", value: "1024px" }, | ||
{ name: "--global-prop1", value: "value1" }, | ||
{ name: "--global-prop2", value: "value1 value2" }, | ||
{ name: "--global-prop3", value: "value1 * value2" }, | ||
{ name: "--global-prop4", value: "value1 / value2" }, | ||
{ name: "--global-prop5", value: "value1 + value2" }, | ||
{ name: "--global-prop6", value: "value1 - value2" }, | ||
{ name: "--global-prop7", value: "value1 * value2 + value3" }, | ||
{ name: "--global-prop8", value: "value1 * value2 value3" }, | ||
{ name: "--global-prop9", value: "(value1 + value2) * value3" }, | ||
{ name: "--global-prop10", value: "(value1 + value2) * (value3 - value4)" }, | ||
{ name: "--global-prop11", value: "(value1 + value2) * (value3 - value4) value5 - value6" }, | ||
{ name: "--global-typography1", value: "900 clamp(2.1588rem, 1.7083rem + 2.2525vw, 4.4113rem)/1.25 'Sage Headline'"}, | ||
{ name: "--global-typography2", value: "400 20px/1 sage-icons"}, | ||
], | ||
[], | ||
[], | ||
{ | ||
badge: [ | ||
{ | ||
name: "--badge-prop1", | ||
value: "var(--global-prop1)", | ||
}, | ||
{ | ||
name: "--badge-prop2", | ||
value: "var(--global-prop1) * 2", | ||
}, | ||
{ | ||
name: "--badge-prop3", | ||
value: "var(--global-prop1) / 2", | ||
}, | ||
{ | ||
name: "--badge-prop4", | ||
value: "var(--global-prop1) + 2", | ||
}, | ||
{ | ||
name: "--badge-prop5", | ||
value: "var(--global-prop1) - 2", | ||
}, | ||
{ | ||
name: "--badge-prop6", | ||
value: "var(--global-prop1) - var(--global-prop2)", | ||
}, | ||
{ | ||
name: "--badge-prop7", | ||
value: "(var(--global-prop1) - var(--global-prop2)) * var(--global-prop3)", | ||
}, | ||
], | ||
} | ||
), | ||
]); | ||
|
||
const result = mathsCalc.formatTokens(tokens); | ||
expect(result).to.deep.equal({ | ||
screenSizes: [ | ||
{ | ||
global: [ | ||
{ name: "--global-prop1", value: "value1" }, | ||
{ name: "--global-prop2", value: "value1 value2" }, | ||
{ name: "--global-prop3", value: "calc(value1 * value2)" }, | ||
{ name: "--global-prop4", value: "calc(value1 / value2)" }, | ||
{ name: "--global-prop5", value: "calc(value1 + value2)" }, | ||
{ name: "--global-prop6", value: "calc(value1 - value2)" }, | ||
{ name: "--global-prop7", value: "calc(value1 * value2 + value3)" }, | ||
{ name: "--global-prop8", value: "calc(value1 * value2) value3" }, | ||
{ name: "--global-prop9", value: "calc((value1 + value2) * value3)" }, | ||
{ name: "--global-prop10", value: "calc((value1 + value2) * (value3 - value4))" }, | ||
{ name: "--global-prop11", value: "calc((value1 + value2) * (value3 - value4)) calc(value5 - value6)" }, | ||
{ name: "--global-typography1", value: "900 clamp(2.1588rem, 1.7083rem + 2.2525vw, 4.4113rem)/1.25 'Sage Headline'"}, | ||
{ name: "--global-typography2", value: "400 20px/1 sage-icons"}, | ||
], | ||
light: [], | ||
dark: [], | ||
components: { | ||
badge: [ | ||
{ | ||
name: "--badge-prop1", | ||
value: "var(--global-prop1)", | ||
}, | ||
{ | ||
name: "--badge-prop2", | ||
value: "calc(var(--global-prop1) * 2)", | ||
}, | ||
{ | ||
name: "--badge-prop3", | ||
value: "calc(var(--global-prop1) / 2)", | ||
}, | ||
{ | ||
name: "--badge-prop4", | ||
value: "calc(var(--global-prop1) + 2)", | ||
}, | ||
{ | ||
name: "--badge-prop5", | ||
value: "calc(var(--global-prop1) - 2)", | ||
}, | ||
{ | ||
name: "--badge-prop6", | ||
value: "calc(var(--global-prop1) - var(--global-prop2))", | ||
}, | ||
{ | ||
name: "--badge-prop7", | ||
value: "calc((var(--global-prop1) - var(--global-prop2)) * var(--global-prop3))", | ||
}, | ||
], | ||
}, | ||
minBreakpoint: 0, | ||
}, | ||
{ | ||
global: [ | ||
{ name: "--global-prop1", value: "value1" }, | ||
{ name: "--global-prop2", value: "value1 value2" }, | ||
{ name: "--global-prop3", value: "calc(value1 * value2)" }, | ||
{ name: "--global-prop4", value: "calc(value1 / value2)" }, | ||
{ name: "--global-prop5", value: "calc(value1 + value2)" }, | ||
{ name: "--global-prop6", value: "calc(value1 - value2)" }, | ||
{ name: "--global-prop7", value: "calc(value1 * value2 + value3)" }, | ||
{ name: "--global-prop8", value: "calc(value1 * value2) value3" }, | ||
{ name: "--global-prop9", value: "calc((value1 + value2) * value3)" }, | ||
{ name: "--global-prop10", value: "calc((value1 + value2) * (value3 - value4))" }, | ||
{ name: "--global-prop11", value: "calc((value1 + value2) * (value3 - value4)) calc(value5 - value6)" }, | ||
{ name: "--global-typography1", value: "900 clamp(2.1588rem, 1.7083rem + 2.2525vw, 4.4113rem)/1.25 'Sage Headline'"}, | ||
{ name: "--global-typography2", value: "400 20px/1 sage-icons"}, | ||
], | ||
light: [], | ||
dark: [], | ||
components: { | ||
badge: [ | ||
{ | ||
name: "--badge-prop1", | ||
value: "var(--global-prop1)", | ||
}, | ||
{ | ||
name: "--badge-prop2", | ||
value: "calc(var(--global-prop1) * 2)", | ||
}, | ||
{ | ||
name: "--badge-prop3", | ||
value: "calc(var(--global-prop1) / 2)", | ||
}, | ||
{ | ||
name: "--badge-prop4", | ||
value: "calc(var(--global-prop1) + 2)", | ||
}, | ||
{ | ||
name: "--badge-prop5", | ||
value: "calc(var(--global-prop1) - 2)", | ||
}, | ||
{ | ||
name: "--badge-prop6", | ||
value: "calc(var(--global-prop1) - var(--global-prop2))", | ||
}, | ||
{ | ||
name: "--badge-prop7", | ||
value: "calc((var(--global-prop1) - var(--global-prop2)) * var(--global-prop3))", | ||
}, | ||
], | ||
}, | ||
minBreakpoint: 1024, | ||
}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
74 changes: 74 additions & 0 deletions
74
scripts/post-process-css/formatters/maths-calc/maths-calc.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { BrandTokens } from "../../brand-tokens"; | ||
import { Decorator } from "../decorator"; | ||
|
||
interface IPartConditions { | ||
isLeft: boolean | ||
isMath: boolean | ||
isMiddle: boolean | ||
isRight: boolean | ||
part: string | ||
} | ||
|
||
export class MathsCalc extends Decorator { | ||
private readonly mathChars = ['+', '-', '*', '/']; | ||
/** | ||
* | ||
* @param tokens Tokens to format. | ||
* @returns Formatted tokens. | ||
*/ | ||
public override formatTokens(tokens: BrandTokens) { | ||
|
||
tokens.screenSizes.forEach((screenSize) => { | ||
|
||
screenSize.global.forEach((token) => token.value = this.formatTokenValue(token.value)); | ||
screenSize.light.forEach((token) => token.value = this.formatTokenValue(token.value)); | ||
screenSize.dark.forEach((token) => token.value = this.formatTokenValue(token.value)); | ||
|
||
Object.keys(screenSize.components).forEach((component) => { | ||
if (screenSize.components[component]) { | ||
screenSize.components[component].forEach((token) => token.value = this.formatTokenValue(token.value)); | ||
} | ||
}); | ||
}) | ||
|
||
return super.formatTokens(tokens); | ||
} | ||
|
||
private formatTokenValue(value: string): string { | ||
const hasClamp = /clamp\s*\(.*\)/ | ||
|
||
if (hasClamp.test(value)) return value | ||
|
||
const valueParts = value.split(' '); | ||
|
||
const valueConditions = this.checkConditions(valueParts) | ||
|
||
valueConditions.forEach((item, i) => { | ||
if (item.isLeft && !item.isMiddle) valueParts[i] = `calc(${item.part}` | ||
if (item.isRight && !item.isMiddle) valueParts[i] = `${item.part})` | ||
}) | ||
|
||
return valueParts.join(' ') | ||
} | ||
|
||
private checkConditions(valueParts: string[]): IPartConditions[] { | ||
const partConditions: IPartConditions[] = [] | ||
|
||
valueParts.forEach((part, i) => { | ||
const left = i > 0 ? valueParts[i - 1] : ''; | ||
const right = valueParts[i + 1] ?? ''; | ||
|
||
const conditions = { | ||
part, | ||
isMath: this.mathChars.includes(part), | ||
isLeft: this.mathChars.includes(right), | ||
isRight: this.mathChars.includes(left), | ||
isMiddle: this.mathChars.includes(right) && this.mathChars.includes(left), | ||
} | ||
|
||
partConditions.push(conditions) | ||
}); | ||
|
||
return partConditions | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As discussed - change will need to be made to all CSS files in dist directory rather than only the all.css file.