Skip to content

Instantly share code, notes, and snippets.

@pbedat
Created March 27, 2018 05:31
Show Gist options
  • Select an option

  • Save pbedat/26eb9376ac11041e1106cad5f1e7606f to your computer and use it in GitHub Desktop.

Select an option

Save pbedat/26eb9376ac11041e1106cad5f1e7606f to your computer and use it in GitHub Desktop.
react-jss typescript
declare module 'react-jss' {
import * as React from 'react';
export interface CSSProperties extends React.CSSProperties {
composes?: string | string[]
}
export type StyleSheet<Props = {}>
= Record<
string,
CSSProperties
| ((props: Props) => React.CSSProperties)
>;
type StyleRules<ClassKey extends string = string, Props = {}>
= Record<ClassKey, CSSProperties
| ((props: Props) => React.CSSProperties)>;
export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>;
export interface WithStyles<ClassKey extends string = string> {
classes: ClassNameMap<ClassKey>
}
export interface StyledComponentProps<ClassKey extends string = string> {
classes?: Partial<ClassNameMap<ClassKey>>
}
function injectSheet<ClassKey extends string>(
style: StyleRules<ClassKey>,
options?: any,
): <P>(
component: React.ComponentType<P & WithStyles<ClassKey>>,
) => React.ComponentType<P & StyledComponentProps<ClassKey>>;
export default injectSheet;
export const jss: any
export const JssProvider: any
}
const styles: StyleSheet<Props> = { // you don't have to type `styles` explicitly, but it helps you with code completion
root: {
color: "red",
composes: ["btn", "btn-default"]
},
header: ({important}) => ({
fontWeight: important ? 'bold' : 'normal'
}),
"&:hover": {
color: "blue",
"& a": {
textDecoration: 'none'
} as React.CSSProperties
}
}
interface Props {
important: boolean
}
const Comp = injectSheet(styles)<Props>(({ classes, important }) => (
<div className={classes.root}>
<h1 className={classes.header}>Hello JSS</h1>
</div>
))
type StyledProps = WithStyles<keyof typeof styles> & Props;
const ClassComp = injectSheet(styles)(class extends React.Component<StyledProps> {
render() {
const {classes} = this.props;
return (
<div className={classes.root}>
<h1 className={classes.header}>Hello JSS</h1>
</div>
)
}
})
@JasonKaz
Copy link

JasonKaz commented Jan 1, 2019

Can confirm with Saturate, this one and the definitions in @types/react-jss do not support dynamic values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment