Skip to content

Commit 497f185

Browse files
authored
fix createUseStyles types (#1352)
* fix `createUseStyles` types * support falsy values for function rules and only a null for function values * allow null and undefined on function values * update changelog.md
1 parent 671c03f commit 497f185

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
Since you are interested in what happens next, in case, you work for a for-profit company that benefits from using the project, please consider supporting it on https://opencollective.com/jss.
44

5+
### Improvements
6+
7+
- [jss, react-jss] TS fixes, allow autocomplete for CSS rules [1352](https://github.com/cssinjs/jss/pull/1352)
8+
59
---
610

711
## 10.2.0 (2020-6-3)

packages/jss/src/index.d.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import * as css from 'csstype'
22

33
// TODO: Type data better, currently typed as any for allowing to override it
4-
type FnValue<R> = R | ((data: any) => R)
4+
type Func<R> = ((data: any) => R)
55

66
type NormalCssProperties = css.Properties<string | number>
7-
type CssProperties = {[K in keyof NormalCssProperties]: FnValue<NormalCssProperties[K] | JssValue>}
7+
type NormalCssValues<K> = K extends keyof NormalCssProperties
8+
? NormalCssProperties[K] | JssValue
9+
: JssValue
810

9-
// Jss Style definitions
10-
type JssStyleP = {
11-
[key: string]: FnValue<JssValue | JssStyleP>
11+
export type JssStyle = {
12+
[K in keyof NormalCssProperties | string]:
13+
| NormalCssValues<K>
14+
| JssStyle
15+
| Func<NormalCssValues<K> | JssStyle | undefined>
1216
}
1317

14-
export type JssStyle = CssProperties & JssStyleP
15-
16-
export type Styles<Name extends string | number | symbol = string> = Record<Name, JssStyle | string>
18+
export type Styles<Name extends string | number | symbol = string> = Record<
19+
Name,
20+
JssStyle | string | Func<JssStyle | string | null | undefined>
21+
>
1722
export type Classes<Name extends string | number | symbol = string> = Record<Name, string>
1823
export type Keyframes<Name extends string = string> = Record<Name, string>
1924

packages/jss/tests/types/Styles.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import {Styles} from '../../src'
2+
3+
interface Props {
4+
flag?: boolean
5+
}
6+
7+
const styles: Styles = {
8+
basic: {
9+
textAlign: 'center',
10+
display: 'flex',
11+
width: 500,
12+
justifyContent: 'center'
13+
},
14+
property: {
15+
textAlign: 'center',
16+
display: 'flex',
17+
width: '100%',
18+
justifyContent: (props: Props) => (props.flag ? 'center' : undefined)
19+
},
20+
inner: {
21+
textAlign: 'center',
22+
display: 'flex',
23+
width: '100%',
24+
25+
'&.foo': {
26+
fontSize: 12
27+
}
28+
},
29+
func: (props: Props) => ({
30+
display: 'flex',
31+
flexDirection: 'column',
32+
justifyContent: 'center',
33+
alignItems: 'center',
34+
width: '96px',
35+
cursor: 'pointer',
36+
position: 'relative',
37+
pointerEvents: props.flag ? 'none' : null
38+
}),
39+
funcNull: (props: Props) => null,
40+
funcWithTerm: (props: Props) => ({
41+
width: props.flag ? 377 : 272,
42+
height: props.flag ? 330 : 400,
43+
boxShadow: '0px 2px 20px rgba(0, 0, 0, 0.08)',
44+
borderRadius: '8px',
45+
position: 'relative',
46+
color: '#222222',
47+
background: '#fff',
48+
animation: '$fadeIn 300ms ease-in-out 300ms both',
49+
...(props.flag && {
50+
height: 288
51+
})
52+
}),
53+
'@keyframes fadeIn': {
54+
from: {opacity: 0},
55+
to: {opacity: 1}
56+
}
57+
}

packages/react-jss/src/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ interface CreateUseStylesOptions<Theme = DefaultTheme> extends BaseOptions<Theme
6262
}
6363

6464
declare function createUseStyles<Theme = DefaultTheme, C extends string = string>(
65-
styles: Record<C, any> | ((theme: Theme) => Record<C, any>),
65+
styles: Styles<C> | ((theme: Theme) => Styles<C>),
6666
options?: CreateUseStylesOptions<Theme>
6767
): (data?: unknown) => Classes<C>
6868

0 commit comments

Comments
 (0)