-
-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Documentation for Using Hooks with TypeScript
- Loading branch information
1 parent
8638d99
commit a238ff6
Showing
2 changed files
with
178 additions
and
1 deletion.
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
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,80 @@ | ||
import React from 'react' | ||
import {createUseStyles, useTheme, ThemeProvider} from 'react-jss' | ||
|
||
/* -------------------- EXPLICIT EXAMPLE -------------------- */ | ||
// Define Component | ||
type RuleNames = 'myButton' | 'myLabel' | ||
|
||
interface ButtonProps { | ||
children?: React.ReactNode | ||
spacing?: number | ||
fontWeight?: string | ||
labelColor?: string | ||
} | ||
|
||
interface CustomTheme { | ||
background: string | ||
} | ||
|
||
const useStyles = createUseStyles<RuleNames, ButtonProps, CustomTheme>({ | ||
myButton: { | ||
padding: ({spacing}) => spacing || 10 | ||
}, | ||
myLabel: ({theme, ...props}) => ({ | ||
display: 'block', | ||
color: props.labelColor || 'red', | ||
fontWeight: props.fontWeight || 'bold', | ||
backgroundColor: theme.background || 'gray' | ||
}) | ||
}) | ||
|
||
function Button({children, ...props}: ButtonProps): React.ReactElement { | ||
const theme = useTheme<CustomTheme>() | ||
const classes = useStyles({...props, theme}) | ||
|
||
return ( | ||
<button className={classes.myButton}> | ||
<span className={classes.myLabel}>{children}</span> | ||
</button> | ||
) | ||
} | ||
|
||
// Create App | ||
const theme = {background: 'black'} | ||
|
||
function App(): React.ReactElement { | ||
return ( | ||
<ThemeProvider theme={theme}> | ||
<Button fontWeight="bold">Submit</Button> | ||
</ThemeProvider> | ||
) | ||
} | ||
|
||
/* -------------------- IMPLICIT EXAMPLES -------------------- */ | ||
// Note: `Theme` must be typed by the `theme` argument. And the | ||
// first occurence of a data function must provide `Props`. | ||
const useStylesImplicitFunc = createUseStyles((theme: CustomTheme) => ({ | ||
myButton: { | ||
padding: (props: ButtonProps) => props.spacing || 10 | ||
}, | ||
myLabel: props => ({ | ||
display: 'block', | ||
color: props.labelColor || 'red', | ||
fontWeight: props.fontWeight || 'bold', | ||
backgroundColor: theme.background || 'gray' | ||
}) | ||
})) | ||
|
||
// Note: First occurence of data function must provide `Props` and `Theme`. | ||
// It must also be defined at the `RuleNames` level. | ||
const useStylesImplicitObj = createUseStyles({ | ||
myLabel: ({theme, ...props}: ButtonProps & {theme: CustomTheme}) => ({ | ||
display: 'block', | ||
color: props.labelColor || 'red', | ||
fontWeight: props.fontWeight || 'bold', | ||
backgroundColor: theme.background || 'gray' | ||
}), | ||
myButton: { | ||
padding: props => props.spacing || 10 | ||
} | ||
}) |