Skip to content

Commit 1ee9ba2

Browse files
authored
Allow passing of null contract tokens in createThemeContract (#238)
1 parent c62b53c commit 1ee9ba2

File tree

4 files changed

+32
-3
lines changed

4 files changed

+32
-3
lines changed

.changeset/lemon-boxes-return.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 passing of null contract tokens in `createThemeContract`

packages/css/src/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,15 @@ export interface Adapter {
104104
onEndFileScope: (fileScope: FileScope) => void;
105105
}
106106

107+
export type NullableTokens = {
108+
[key: string]: string | Tokens | null;
109+
};
110+
107111
export type Tokens = {
108112
[key: string]: string | Tokens;
109113
};
110114

111-
export type ThemeVars<ThemeContract extends Tokens> = MapLeafNodes<
115+
export type ThemeVars<ThemeContract extends NullableTokens> = MapLeafNodes<
112116
ThemeContract,
113117
CSSVarFunction
114118
>;

packages/css/src/vars.ts

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

11-
import { ThemeVars, Tokens } from './types';
11+
import { NullableTokens, ThemeVars } from './types';
1212
import { getAndIncrementRefCounter, getFileScope } from './fileScope';
1313
import { validateContract } from './validateContract';
1414

@@ -69,7 +69,7 @@ export function assignVars<VarContract extends Contract>(
6969
return varSetters;
7070
}
7171

72-
export function createThemeContract<ThemeTokens extends Tokens>(
72+
export function createThemeContract<ThemeTokens extends NullableTokens>(
7373
tokens: ThemeTokens,
7474
): ThemeVars<ThemeTokens> {
7575
return walkObject(tokens, (_value, path) => {

tests/types/type-tests.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import {
2+
createTheme,
3+
createThemeContract,
4+
createGlobalTheme,
5+
} from '@vanilla-extract/css';
6+
7+
const vars = createThemeContract({
8+
shouldSupportNull: null,
9+
shouldSupportString: '',
10+
});
11+
12+
createGlobalTheme(':root', vars, {
13+
shouldSupportNull: 'some-value',
14+
shouldSupportString: 'some-value',
15+
});
16+
17+
createTheme(vars, {
18+
shouldSupportNull: 'some-value',
19+
shouldSupportString: 'some-value',
20+
});

0 commit comments

Comments
 (0)