-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from facebook/better-theming-types
Fix types for theming with tests
- Loading branch information
Showing
3 changed files
with
220 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
*/ | ||
|
||
/* eslint-disable no-unused-vars */ | ||
|
||
import stylex from '@stylexjs/stylex'; | ||
import type { | ||
TokensFromVarGroup, | ||
StyleXVar, | ||
VarGroup, | ||
Theme, | ||
CompiledStyles, | ||
} from '@stylexjs/stylex/lib/StyleXTypes'; | ||
|
||
const DARK = '@media (prefers-color-scheme: dark)'; | ||
|
||
const buttonTokens = stylex.defineVars({ | ||
bgColor: { | ||
default: 'cyan', | ||
[DARK]: 'navy', | ||
}, | ||
textColor: { | ||
default: 'black', | ||
[DARK]: 'white', | ||
}, | ||
cornerRadius: '4px', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}); | ||
|
||
// DefineVars creates the right type. | ||
buttonTokens satisfies VarGroup< | ||
Readonly<{ | ||
bgColor: string; | ||
textColor: string; | ||
cornerRadius: string; | ||
paddingBlock: string; | ||
paddingInline: string; | ||
}>, | ||
symbol | ||
>; | ||
buttonTokens.bgColor satisfies StyleXVar<string>; | ||
|
||
type TokensType = TokensFromVarGroup<typeof buttonTokens>; | ||
({ | ||
bgColor: 'red', | ||
textColor: 'white', | ||
cornerRadius: '4px', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}) satisfies TokensType; | ||
|
||
({ | ||
bgColor: 'red', | ||
textColor: 'white', | ||
// @ts-expect-error - cornerRadius is a string. | ||
cornerRadius: 4, | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}) satisfies TokensType; | ||
|
||
({ | ||
bgColor: 'red', | ||
textColor: 'white', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
// @ts-expect-error - cornerRadius is missing. | ||
}) satisfies TokensType; | ||
|
||
const correctTheme = stylex.createTheme(buttonTokens, { | ||
bgColor: 'red', | ||
textColor: 'white', | ||
cornerRadius: '4px', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}); | ||
|
||
correctTheme satisfies Theme<typeof buttonTokens, symbol>; | ||
|
||
correctTheme satisfies CompiledStyles; | ||
|
||
const result: string = stylex(correctTheme); | ||
const result2: Readonly<{ | ||
className?: string; | ||
style?: Readonly<{ [key: string]: string | number }>; | ||
}> = stylex.props(correctTheme); | ||
|
||
const wrongTheme1 = stylex.createTheme(buttonTokens, { | ||
bgColor: 'red', | ||
textColor: 'white', | ||
// @ts-expect-error - cornerRadius is a string. | ||
cornerRadius: 1, | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}); | ||
|
||
const wrongTheme2 = stylex.createTheme( | ||
buttonTokens, | ||
// @ts-expect-error - cornerRadius is missing. | ||
{ | ||
bgColor: 'red', | ||
textColor: 'white', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow strict | ||
*/ | ||
|
||
/* eslint-disable no-unused-vars */ | ||
|
||
import stylex from '../src/stylex'; | ||
import type { StyleXVar, VarGroup, Theme } from '../src/StyleXTypes'; | ||
|
||
const DARK = '@media (prefers-color-scheme: dark)'; | ||
|
||
const buttonTokens = stylex.defineVars({ | ||
bgColor: { | ||
default: 'cyan', | ||
[DARK]: 'navy', | ||
}, | ||
textColor: { | ||
default: 'black', | ||
[DARK]: 'white', | ||
}, | ||
cornerRadius: '4px', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}); | ||
|
||
// DefineVars creates the right type. | ||
(buttonTokens: VarGroup< | ||
$ReadOnly<{ | ||
bgColor: string, | ||
textColor: string, | ||
cornerRadius: string, | ||
paddingBlock: string, | ||
paddingInline: string, | ||
}>, | ||
string, | ||
>); | ||
(buttonTokens.bgColor: StyleXVar<string>); | ||
|
||
const correctTheme = stylex.createTheme(buttonTokens, { | ||
bgColor: 'red', | ||
textColor: 'white', | ||
cornerRadius: '4px', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}); | ||
|
||
(correctTheme: Theme<typeof buttonTokens, string>); | ||
|
||
const result: string = stylex(correctTheme); | ||
const result2: $ReadOnly<{ | ||
className?: string, | ||
style?: $ReadOnly<{ [string]: string | number }>, | ||
}> = stylex.props(correctTheme); | ||
|
||
const wrongTheme1 = stylex.createTheme(buttonTokens, { | ||
bgColor: 'red', | ||
textColor: 'white', | ||
// $FlowExpectedError[incompatible-call] - cornerRadius must be a string | ||
cornerRadius: 1, | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}); | ||
|
||
const wrongTheme2 = stylex.createTheme( | ||
buttonTokens, | ||
// $FlowExpectedError[prop-missing] - cornerRadius missing | ||
{ | ||
bgColor: 'red', | ||
textColor: 'white', | ||
paddingBlock: '4px', | ||
paddingInline: '8px', | ||
}, | ||
); |