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

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
6 changes: 4 additions & 2 deletions examples/next/components/HelloWorld.css.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { style, createVar, keyframes, getVarName, fallbackVar } from '@vanilla-extract/css';
import { style, createVar, keyframes, fallbackVar } from '@vanilla-extract/css';

const color = createVar();
const angle = createVar({
Expand All @@ -9,7 +9,9 @@ const angle = createVar({

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

Expand Down
1 change: 0 additions & 1 deletion fixtures/themed/src/styles.css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ globalStyle(`body ${iDunno}:after`, {
const blankVar1 = createVar({
syntax: '<number>',
inherits: false,
initialValue: '0',
});
const blankVar2 = createVar();

Expand Down
2 changes: 0 additions & 2 deletions packages/css/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export { getVarName } from '@vanilla-extract/private';

import './runtimeAdapter';

export type {
Expand Down
2 changes: 1 addition & 1 deletion packages/css/src/transformCss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class Stylesheet {
if (root.type === 'keyframes') {
root.rule = Object.fromEntries(
Object.entries(root.rule).map(([keyframe, rule]) => {
return [keyframe, this.transformProperties(rule)];
return [keyframe, this.transformVars(this.transformProperties(rule))];
z4o4z marked this conversation as resolved.
Show resolved Hide resolved
}),
);
this.keyframesRules.push(root);
Expand Down
2 changes: 1 addition & 1 deletion packages/css/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type CSSProperties = {
};

export interface CSSKeyframes {
[time: string]: CSSProperties;
[time: string]: CSSPropertiesWithVars;
}

export type CSSPropertiesWithVars = CSSProperties & {
Expand Down
2 changes: 1 addition & 1 deletion packages/css/src/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type VarDeclaration = {
const buildPropertyRule = ({ syntax, inherits, initialValue }: VarDeclaration): AtRule.Property => ({
syntax: `"${Array.isArray(syntax) ? syntax.join(' | ') : syntax}"`,
inherits: inherits ? 'true' : 'false',
initialValue,
...(initialValue != null && { initialValue }),
z4o4z marked this conversation as resolved.
Show resolved Hide resolved
z4o4z marked this conversation as resolved.
Show resolved Hide resolved
})

export function createVar(declaration: VarDeclaration, debugId?: string): CSSVarFunction
Expand Down
2 changes: 0 additions & 2 deletions packages/dynamic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
export { getVarName } from '@vanilla-extract/private';

export { assignInlineVars } from './assignInlineVars';
export { setElementVars } from './setElementVars';
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ body, button {
}",
],
[
"assets/src/styles.css.ts.vanilla-BFtGQ1T7.css",
"assets/src/styles.css.ts.vanilla-BPHcBqBf.css",
"@font-face {
src: local("Impact");
font-family: "styles_impact__jteyb10";
Expand All @@ -27,7 +27,6 @@ body, button {
@property --blankVar1__jteyb14 {
syntax: "<number>";
inherits: false;
initial-value: 0;
}
.styles_container__jteyb11 {
display: flex;
Expand Down Expand Up @@ -199,7 +198,7 @@ export { shadow };
"src/styles.css.js",
"import './../assets/src/shared.css.ts.vanilla-G_Gyt4-e.css';
import './../assets/src/themes.css.ts.vanilla-DMMXGwB8.css';
import './../assets/src/styles.css.ts.vanilla-BFtGQ1T7.css';
import './../assets/src/styles.css.ts.vanilla-BPHcBqBf.css';

var button = "styles_button__jteyb13 shared_shadow__4dtfen0 styles_iDunno__jteyb12";
var container = "styles_container__jteyb11";
Expand Down Expand Up @@ -390,7 +389,6 @@ export { button, container, opacity };
@property --blankVar1__jteyb14 {
syntax: "<number>";
inherits: false;
initial-value: 0;
}
.styles_container__jteyb11 {
display: flex;
Expand Down Expand Up @@ -521,7 +519,7 @@ exports[`rollup-plugin should build with sourcemaps 1`] = `
"",
],
[
"assets/src/styles.css.ts.vanilla-BFtGQ1T7.css",
"assets/src/styles.css.ts.vanilla-BPHcBqBf.css",
"",
],
[
Expand Down Expand Up @@ -586,7 +584,7 @@ body, button {
}",
],
[
"assets/src/styles.css.ts.vanilla-BFtGQ1T7.css",
"assets/src/styles.css.ts.vanilla-BPHcBqBf.css",
"@font-face {
src: local("Impact");
font-family: "styles_impact__jteyb10";
Expand All @@ -598,7 +596,6 @@ body, button {
@property --blankVar1__jteyb14 {
syntax: "<number>";
inherits: false;
initial-value: 0;
}
.styles_container__jteyb11 {
display: flex;
Expand Down Expand Up @@ -678,7 +675,7 @@ html .styles_opacity_1\\/4__jteyb17 {
"import { assignInlineVars, setElementVars } from '@vanilla-extract/dynamic';
import './assets/src/themes.css.ts.vanilla-DMMXGwB8.css';
import './assets/src/shared.css.ts.vanilla-G_Gyt4-e.css';
import './assets/src/styles.css.ts.vanilla-BFtGQ1T7.css';
import './assets/src/styles.css.ts.vanilla-BPHcBqBf.css';

var altTheme = "themes_altTheme__cvta176";
var responsiveTheme = "themes_responsiveTheme__cvta177";
Expand Down
32 changes: 2 additions & 30 deletions site/docs/api/create-var.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,34 +137,6 @@ export const pink = style({
[accentVar]: 'pink'
}
});
```
z4o4z marked this conversation as resolved.
Show resolved Hide resolved

The main benefit of using `createVar` to create typed css properties is that they can be animated via `keyframes`.

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

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

export const root = style({
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`,

vars: {
// This will fallback to 180deg if the @property is not supported by the browser
[angle]: fallbackVar(angle, '180deg'),
}
});
```

```
The main benefit of using `createVar` to create typed css properties is that they can be [animated via keyframes](/documentation/api/keyframes#animating-variables).
35 changes: 35 additions & 0 deletions site/docs/api/keyframes.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,38 @@ export const spinAgain = style({
animation: `${rotate} 3s`
});
```

## Animating variables

It is also possible to animate [typed CSS property](/documentation/api/create-var#css-property) using keyframes:

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

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

export const root = style({
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`,

vars: {
// This will fallback to 180deg if the @property is not supported by the browser
[angle]: fallbackVar(angle, '180deg')
}
});
```
27 changes: 27 additions & 0 deletions site/docs/global-api/create-global-var.md
z4o4z marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
title: createGlobalVar
parent: global-api
---

# createGlobalVar

Similar to `createVar`, `createGlobalVar` creates a single global CSS Variable reference.

```ts compiled
// vars.css.ts

import {
createGlobalVar,
style
} from '@vanilla-extract/css';

const opacityVar = createVar({
syntax: '<number>',
inherits: false,
initialValue: '0.5'
});
z4o4z marked this conversation as resolved.
Show resolved Hide resolved

export const content = style({
opacity: opacityVar
});
```
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --blankVar1__jteyb14 {
syntax: "<number>";
inherits: false;
}
.styles_container__jteyb11 {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --jteyb14 {
syntax: "<number>";
inherits: false;
}
.jteyb11 {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --blankVar1__jteyb14 {
syntax: "<number>";
inherits: false;
}
.styles_container__jteyb11 {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --jteyb14 {
syntax: "<number>";
inherits: false;
}
.jteyb11 {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --blankVar1__blj6yb4 {
syntax: "<number>";
inherits: false;
}
.styles_container__blj6yb1 {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --blj6yb4 {
syntax: "<number>";
inherits: false;
}
.blj6yb1 {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ button {
src: local("Comic Sans MS");
font-family: MyGlobalComicSans;
}
@property --jteyb14 {
syntax: "<number>";
inherits: false;
}
.jteyb11 {
display: flex;
flex-direction: column;
Expand Down