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 css @property #1092

Merged
merged 19 commits into from
Dec 20, 2024
Merged

Conversation

z4o4z
Copy link
Contributor

@z4o4z z4o4z commented May 16, 2023

This PR adds support for CSS @property feature.

  • createVar - overloaded to allow a @property definition, returns property var format var(--propertyName)
  • createGlobalVar - same as createVar but with a global (non-scoped) name

example:

import { style, createVar, keyframes, fallbackVar } from '@vanilla-extract/css';

const color = createVar();
const angle = createVar({
  syntax: '<angle>',
  inherits: false,
  initialValue: '0deg',
});

const angleKeyframes = keyframes({
  '100%': {
    vars: {
      [getVarName(angle)]: '360deg',
    }
  },
});

export const root = style({
  background: 'pink',
  color: 'blue',
  padding: '16px',
  transition: `opacity .1s ease, color .1s ease`, // Testing autoprefixer
  backgroundImage: `linear-gradient(${angle}, rgba(153, 70, 198, 0.35) 0%, rgba(28, 56, 240, 0.46) 100%)`,
  animation: `${angleKeyframes} 7s infinite ease-in-out both`,


  ':hover': {
    opacity: 0.8,
    color: color,
  },
  
  vars: {
    [color]: '#fef',
    [angle]: fallbackVar(angle, '138deg'),
  }
});

@changeset-bot
Copy link

changeset-bot bot commented May 16, 2023

🦋 Changeset detected

Latest commit: f7d017a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 10 packages
Name Type
@vanilla-extract/babel-plugin-debug-ids Minor
@vanilla-extract/css Minor
@vanilla-extract/integration Patch
@vanilla-extract/rollup-plugin Patch
@vanilla-extract/esbuild-plugin Patch
@vanilla-extract/jest-transform Patch
@vanilla-extract/parcel-transformer Patch
@vanilla-extract/vite-plugin Patch
@vanilla-extract/webpack-plugin Patch
@vanilla-extract/next-plugin Patch

Not sure what this means? Click here to learn what changesets are.

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

@markdalgleish
Copy link
Contributor

Thanks for the PR! This looks really great.

To me this is just an extension of our current createVar API. The fact that it's natively called @property definitely challenges our current naming, but I think for now VE needs to be consistent in calling them "vars". If we rename them to properties, we'd do this across the whole library in a future major version. I think it's okay that internals refer to it as "property" though since this future-proofs the integration package against API changes.

So I think we'd need to make the following changes for now:

  • Overload createVar so the API is createVar({ syntax: '<color>', inherits: false, initialValue: '#fff' }).
  • Rename createGlobalProperty to createGlobalVar.
  • Replace getPropertyName with getVarName (which already exists in our private package). This would need to be exposed from css and dynamic packages since you'd want this at both build time and run time.

@z4o4z
Copy link
Contributor Author

z4o4z commented May 31, 2023

Thanks for the PR! This looks really great.

To me this is just an extension of our current createVar API. The fact that it's natively called @property definitely challenges our current naming, but I think for now VE needs to be consistent in calling them "vars". If we rename them to properties, we'd do this across the whole library in a future major version. I think it's okay that internals refer to it as "property" though since this future-proofs the integration package against API changes.

So I think we'd need to make the following changes for now:

  • Overload createVar so the API is createVar({ syntax: '<color>', inherits: false, initialValue: '#fff' }).
  • Rename createGlobalProperty to createGlobalVar.
  • Replace getPropertyName with getVarName (which already exists in our private package). This would need to be exposed from css and dynamic packages since you'd want this at both build time and run time.

hey @markdalgleish, thanks for your feedback! Updated the code, pls let me know if I need to change something else

@z4o4z
Copy link
Contributor Author

z4o4z commented Jun 16, 2023

@markdalgleish thanks for cleaning this up! is there something I should address before merging?

@z4o4z
Copy link
Contributor Author

z4o4z commented Jul 19, 2023

@markdalgleish any updates here, i'm really waiting for this feature?

@erwinsmit
Copy link

@markdalgleish any updates here, i'm really waiting for this feature?

This would be a nice addition. Currently I can only use properties using globalStyle with a @ts-ignore. That feels a bit dirty

globalStyle(`@property --headerBackground1`, {
  // @ts-ignore
  syntax: `'<color>'`,
  initialValue: colorPrimitives.neutrals.whites.W100,
  inherits: false,
});

@8bit-echo
Copy link

I'd like to bump this and see it land! 🙏 This would be a great feature for our design system to use.

@mikaalnaik
Copy link

Need this as well, any update to this PRs future @markdalgleish?

@lukelarsen
Copy link

I am also needing this in our current library. Any update?

Copy link
Contributor

@askoufis askoufis left a comment

Choose a reason for hiding this comment

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

Hey, I'm keen to help get this PR through the door. For me, there's a few things still missing.

Testing @property CSS output in a fixture

Your example in examples/next is somewhat complex, and isn't actually being tested anywhere. We snapshot screenshots and CSS output of each of the fixture packages, so maybe adding a simple property in the themed fixture would be enough.

Unit testing CSS output

This is done in transformCss.test.ts. You probably just need to add a single test case that uses an object with type: 'property'.

Docs

We tend to be a bit particular about docs, so that can be left to a separate PR. However, you're more than welcome try write them yourself if you want.

@askoufis
Copy link
Contributor

askoufis commented Sep 22, 2024

@z4o4z Hey, are you still interested in getting this PR over the line? If not, I may look at picking it up myself in the near future.

@z4o4z
Copy link
Contributor Author

z4o4z commented Sep 24, 2024

hey @askoufis, i've added tests and updated docs. let me know if that looks good for you :) Thanks for helping with the PR!

Copy link
Contributor

@askoufis askoufis left a comment

Choose a reason for hiding this comment

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

Thanks for picking this up again @z4o4z, it's most of the way there. For me the last missing bit is docs for createGlobalVar, which should be fairly straightforward. I've also made a comment about keyframes below.

Once the last bits are ready there's also some playwright snapshots that need updating.

site/docs/api/create-var.md Outdated Show resolved Hide resolved
@z4o4z z4o4z requested a review from askoufis September 28, 2024 18:35
Copy link
Contributor

@askoufis askoufis left a comment

Choose a reason for hiding this comment

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

Thanks for the quick updates. Just some more minor comments from me.

Also realised there's no changesets for the features. We'll need one for createVar and createGlobalVar, as well as a separate one for adding vars support to keyframes. There's actually a few more changes that need changeesets. I'll add them.

site/docs/global-api/create-global-var.md Show resolved Hide resolved
site/docs/global-api/create-global-var.md Outdated Show resolved Hide resolved
packages/css/src/vars.ts Outdated Show resolved Hide resolved
packages/css/src/vars.ts Outdated Show resolved Hide resolved
packages/css/src/vars.ts Outdated Show resolved Hide resolved
@askoufis
Copy link
Contributor

@markdalgleish Would you mind giving this another review when you've got some time?

@askoufis
Copy link
Contributor

Looks like linting is also failing, specifically prettier.

@z4o4z
Copy link
Contributor Author

z4o4z commented Sep 29, 2024

@askoufis thanks for the feedback! addressed everything

@z4o4z z4o4z requested a review from askoufis September 29, 2024 11:04
@z4o4z
Copy link
Contributor Author

z4o4z commented Oct 4, 2024

hey @askoufis @markdalgleish any chance we can merge it soon?

@Vishtar
Copy link

Vishtar commented Nov 5, 2024

Bump.

@NoyCoca
Copy link

NoyCoca commented Dec 10, 2024

Bump

@ShayDavidson
Copy link

bump! I just need this one as well ;)

Copy link
Contributor

@askoufis askoufis left a comment

Choose a reason for hiding this comment

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

I had a small handful of tweaks I wanted to make. Was going to push them to your fork but I don't have permission to do so (I think because it's the default branch of a fork). I've instead made suggestions for the changes and will commit them.

.changeset/poor-mirrors-care.md Outdated Show resolved Hide resolved
.changeset/poor-mirrors-care.md Show resolved Hide resolved
site/docs/api/keyframes.md Outdated Show resolved Hide resolved
packages/css/src/types.ts Outdated Show resolved Hide resolved
.changeset/angry-rules-tease.md Outdated Show resolved Hide resolved
site/docs/api/create-var.md Outdated Show resolved Hide resolved
site/docs/global-api/create-global-var.md Outdated Show resolved Hide resolved
site/docs/global-api/create-global-var.md Outdated Show resolved Hide resolved
site/docs/global-api/create-global-var.md Show resolved Hide resolved
site/docs/api/keyframes.md Outdated Show resolved Hide resolved
Copy link
Contributor

@askoufis askoufis left a comment

Choose a reason for hiding this comment

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

Seems like I can't run CI on your master fork. I will merge this and fix up anything that breaks in a subsequent PR. @z4o4z Really appreciate your work and everyone's patience on this feature. Thank you!

@askoufis askoufis merged commit fd673f6 into vanilla-extract-css:master Dec 20, 2024
5 checks passed
@askoufis
Copy link
Contributor

This has been released in @vanilla-extract/[email protected].

@ShayDavidson
Copy link

yay!

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.

10 participants