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

Allow vars to be passed as values to all style properties #50

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dry-chairs-sit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': patch
---

Allow vars to be passed as values to all style properties
17 changes: 14 additions & 3 deletions packages/css/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import type { Contract, MapLeafNodes } from '@vanilla-extract/private';
import type { PropertiesFallback, AtRule } from 'csstype';
import type { PropertiesFallback, AtRule, Properties } from 'csstype';

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

type BasicCSSProperties = PropertiesFallback<string | number>;
export type CSSVarFunction =
| `var(--${string})`
| `var(--${string}, ${string | number})`;

type CSSTypeProperties = PropertiesFallback<string | number>;

type BasicCSSProperties = {
[Property in keyof CSSTypeProperties]:
| CSSTypeProperties[Property]
| CSSVarFunction
| Array<CSSVarFunction | Properties[Property]>;
};

export interface CSSKeyframes {
[time: string]: BasicCSSProperties;
Expand Down Expand Up @@ -99,5 +110,5 @@ export type Tokens = {

export type ThemeVars<ThemeContract extends Contract> = MapLeafNodes<
ThemeContract,
string
CSSVarFunction
>;
18 changes: 8 additions & 10 deletions packages/css/src/vars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,10 @@ import {
import hash from '@emotion/hash';
import cssesc from 'cssesc';

import { CSSVarFunction, ThemeVars } from './types';
import { getAndIncrementRefCounter, getFileScope } from './fileScope';

type ThemeVars<ThemeContract extends Contract> = MapLeafNodes<
ThemeContract,
string
>;

export function createVar(debugId?: string) {
export function createVar(debugId?: string): CSSVarFunction {
// Convert ref count to base 36 for optimal hash lengths
const refCount = getAndIncrementRefCounter().toString(36);
const { filePath, packageName } = getFileScope();
Expand All @@ -34,10 +30,12 @@ export function createVar(debugId?: string) {
.replace(/([A-Z])/g, '-$1')
.toLowerCase();

return `var(--${cssVarName})`;
return `var(--${cssVarName})` as const;
}

export function fallbackVar(...values: [...Array<string>, string]) {
export function fallbackVar(
...values: [...Array<string>, string]
): CSSVarFunction {
let finalValue = '';

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

return finalValue;
return finalValue as CSSVarFunction;
}

export function assignVars<VarContract extends Contract>(
varContract: VarContract,
tokens: MapLeafNodes<VarContract, string>,
): Record<string, string> {
): Record<CSSVarFunction, string> {
const varSetters: { [cssVarName: string]: string } = {};

/* TODO
Expand Down