Skip to content

Commit

Permalink
Allow passing of null contract tokens in createThemeContract (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcompiles authored Jul 18, 2021
1 parent c62b53c commit 1ee9ba2
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-boxes-return.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@vanilla-extract/css': patch
---

Allow passing of null contract tokens in `createThemeContract`
6 changes: 5 additions & 1 deletion packages/css/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,15 @@ export interface Adapter {
onEndFileScope: (fileScope: FileScope) => void;
}

export type NullableTokens = {
[key: string]: string | Tokens | null;
};

export type Tokens = {
[key: string]: string | Tokens;
};

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

import { ThemeVars, Tokens } from './types';
import { NullableTokens, ThemeVars } from './types';
import { getAndIncrementRefCounter, getFileScope } from './fileScope';
import { validateContract } from './validateContract';

Expand Down Expand Up @@ -69,7 +69,7 @@ export function assignVars<VarContract extends Contract>(
return varSetters;
}

export function createThemeContract<ThemeTokens extends Tokens>(
export function createThemeContract<ThemeTokens extends NullableTokens>(
tokens: ThemeTokens,
): ThemeVars<ThemeTokens> {
return walkObject(tokens, (_value, path) => {
Expand Down
20 changes: 20 additions & 0 deletions tests/types/type-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
createTheme,
createThemeContract,
createGlobalTheme,
} from '@vanilla-extract/css';

const vars = createThemeContract({
shouldSupportNull: null,
shouldSupportString: '',
});

createGlobalTheme(':root', vars, {
shouldSupportNull: 'some-value',
shouldSupportString: 'some-value',
});

createTheme(vars, {
shouldSupportNull: 'some-value',
shouldSupportString: 'some-value',
});

0 comments on commit 1ee9ba2

Please sign in to comment.