Skip to content

Commit 48c4a78

Browse files
authored
Allow vars to be passed as values to all style properties (#50)
1 parent c80a862 commit 48c4a78

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

.changeset/dry-chairs-sit.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@vanilla-extract/css': patch
3+
---
4+
5+
Allow vars to be passed as values to all style properties

packages/css/src/types.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
import type { Contract, MapLeafNodes } from '@vanilla-extract/private';
2-
import type { PropertiesFallback, AtRule } from 'csstype';
2+
import type { PropertiesFallback, AtRule, Properties } from 'csstype';
33

44
import type { SimplePseudos } from './transformCss';
55

6-
type BasicCSSProperties = PropertiesFallback<string | number>;
6+
export type CSSVarFunction =
7+
| `var(--${string})`
8+
| `var(--${string}, ${string | number})`;
9+
10+
type CSSTypeProperties = PropertiesFallback<string | number>;
11+
12+
type BasicCSSProperties = {
13+
[Property in keyof CSSTypeProperties]:
14+
| CSSTypeProperties[Property]
15+
| CSSVarFunction
16+
| Array<CSSVarFunction | Properties[Property]>;
17+
};
718

819
export interface CSSKeyframes {
920
[time: string]: BasicCSSProperties;
@@ -99,5 +110,5 @@ export type Tokens = {
99110

100111
export type ThemeVars<ThemeContract extends Contract> = MapLeafNodes<
101112
ThemeContract,
102-
string
113+
CSSVarFunction
103114
>;

packages/css/src/vars.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,10 @@ import {
77
import hash from '@emotion/hash';
88
import cssesc from 'cssesc';
99

10+
import { CSSVarFunction, ThemeVars } from './types';
1011
import { getAndIncrementRefCounter, getFileScope } from './fileScope';
1112

12-
type ThemeVars<ThemeContract extends Contract> = MapLeafNodes<
13-
ThemeContract,
14-
string
15-
>;
16-
17-
export function createVar(debugId?: string) {
13+
export function createVar(debugId?: string): CSSVarFunction {
1814
// Convert ref count to base 36 for optimal hash lengths
1915
const refCount = getAndIncrementRefCounter().toString(36);
2016
const { filePath, packageName } = getFileScope();
@@ -34,10 +30,12 @@ export function createVar(debugId?: string) {
3430
.replace(/([A-Z])/g, '-$1')
3531
.toLowerCase();
3632

37-
return `var(--${cssVarName})`;
33+
return `var(--${cssVarName})` as const;
3834
}
3935

40-
export function fallbackVar(...values: [...Array<string>, string]) {
36+
export function fallbackVar(
37+
...values: [...Array<string>, string]
38+
): CSSVarFunction {
4139
let finalValue = '';
4240

4341
values.reverse().forEach((value) => {
@@ -52,13 +50,13 @@ export function fallbackVar(...values: [...Array<string>, string]) {
5250
}
5351
});
5452

55-
return finalValue;
53+
return finalValue as CSSVarFunction;
5654
}
5755

5856
export function assignVars<VarContract extends Contract>(
5957
varContract: VarContract,
6058
tokens: MapLeafNodes<VarContract, string>,
61-
): Record<string, string> {
59+
): Record<CSSVarFunction, string> {
6260
const varSetters: { [cssVarName: string]: string } = {};
6361

6462
/* TODO

0 commit comments

Comments
 (0)