Skip to content

Refactor css utils calc classes#1419

Closed
rowan-gud wants to merge 6 commits intovanilla-extract-css:masterfrom
rowan-gud:refactor-css-utils-calc-classes
Closed

Refactor css utils calc classes#1419
rowan-gud wants to merge 6 commits intovanilla-extract-css:masterfrom
rowan-gud:refactor-css-utils-calc-classes

Conversation

@rowan-gud
Copy link

@rowan-gud rowan-gud commented Jun 5, 2024

CSS-utils

Motivation

I have found myself wanting more CSS utils. I have refactored the calc util to use classes to do the chaining in hopes that it will be a little bit of an easier structure to follow. I have a couple more PRs that can be opened if people are interested in this idea! I realize that this is not linked to an issue but I just thought it would be cool and started working on it.

Changes

The calc function now returns a class with the same methods as before but now has context on what operator the sub expressions are using and so can try to group the expression in a logical way. The actual changes in functionality here are very minimal but this class based implementation gives a structure to follow for the other changes I mention later.

Example

Current implementation:

expect(calc(2).add(3).multiply(4).add(3).toString()).toBe('calc(((2 + 3) * 4) + 3)')

This implementation:

expect(calc(2).add(3).multiply(4).add(3).toString()).toBe('calc((2 + 3) * 4 + 3)')

Note that extra parentheses are not added in the above expression

Future Changes

The main reason for doing this is to add more functions to the css-utils packages. Some that I was thinking of are:

  • transition
const root = style({
  transition: transition('background-color')
    .duration('1s')
    .easing('ease-in')
    .build()
})
  • animation
const rotate = keyframes({
  '0%': { transform: 'rotate(0deg)' },
  '100%': { transform: 'rotate(360deg)' }
});

const root = style({
  animation: animation(rotate)
    .duration('1s')
    .build()
});
  • px, rem (super simple)
const root = style({
  height: px(400),
  fontSize: rem(0.875),
})
  • transform - This one I'm really excited about! My lines are getting reaaally long and multiline strings are not the prettiest thing to look at
const root = style({
  transform: transform
    .rotate('45deg')
    .translate(0, 1, 2)
    .translateX('50%')
    .build()
})

Closing Thoughts

I would mostly just like feedback on this PR. I'm sure there're things that I've missed / overlooked. Also, let me know if you would like this transitioned into an issue I just wanted to provide some code as context!

EDIT: I started a discussion #1420

@changeset-bot
Copy link

changeset-bot bot commented Jun 5, 2024

⚠️ No Changeset found

Latest commit: fdadf68

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@rowan-gud rowan-gud closed this Jun 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant