Repository navigation
Add types and an RNTester example for conditional style values - #60126
intergalacticspacehighway wants to merge 1 commit into
Conversation
eeb8789 to
1e6965a
Compare
1e6965a to
df201fd
Compare
df201fd to
b7a0b53
Compare
b7a0b53 to
0605966
Compare
| // Components convert these style props in JavaScript, so they do not support | ||
| // conditional values yet. | ||
| type ____StylePropsWithoutConditionalValues_Internal = | ||
| 'objectFit' | 'resizeMode' | 'tintColor' | 'userSelect' | 'verticalAlign'; |
There was a problem hiding this comment.
TouchableOpacity also reads opacity in JS (_getChildStyleOpacityWithDefault).
There was a problem hiding this comment.
Good catch! But adding it to this list would block opacity on View too. This probably needs to be handled natively. For now I'll add it as a known issue, wyt?
There was a problem hiding this comment.
Yes, it's probably better (and touchables are likely to be removed at some point)
| */ | ||
| export type ConditionalStyleValue<out T> = Readonly<{ | ||
| default: T, | ||
| [mediaQuery: string]: T, |
There was a problem hiding this comment.
[mediaQuery: string]: T accepts any key, so foo or '@media (prefers-color-scheme: drak)' type checks. We could list the handled queries instead (also, better for autocomplete):
export type ConditionalStyleValue<out T> = Readonly<{
default: T,
'@media (prefers-color-scheme: light)'?: T,
'@media (prefers-color-scheme: dark)'?: T,
}>;There was a problem hiding this comment.
Makes sense, done. Thought about it before, but felt it would get tricky with more media queries. Template literal types might help there later, fine for now.
0605966 to
f2c055d
Compare
f2c055d to
e7b8082
Compare
6bc12b3 to
db3bfad
Compare
db3bfad to
f0ec683
Compare
Summary:
This is PR 3 of 3 (RFC). Stack: #60124 (core) → #60125 (platforms) → this PR.
This PR adds Flow and TypeScript types for conditional style values, and an RNTester example.
StyleSheet.createandStylePropaccept a conditional value for a style prop:{default: value, '@media (prefers-color-scheme: dark)': value}. The new types areConditionalStyleValue<T>andWithConditionalStyleValues<T>.objectFit,resizeMode,tintColor,userSelectandverticalAligndo not accept conditional values yet, because components convert them in JS.StyleSheet.flatten, a style value can be aConditionalStyleValue. Code that reads it must handle the object (for example,.default) or cast.width.yarn flow-checkofAnimatedExports.jstakes about 12 s instead of 9.5 s.Changelog:
[GENERAL] [BREAKING] - A style value from
StyleSheet.flattencan be aConditionalStyleValue(types only).Test Plan:
stylesheet-conditional-values.tsx(correct usage and expected errors).yarn build-types(no diff inReactNativeApi.d.ts),yarn test-generated-typescript,yarn flow-check, ESLint and Prettier: pass.gh pr checkout 60126, build RNTester, and open Media Queries. The screenshots are in PR 2.