Skip to content

Instantly share code, notes, and snippets.

@kLabz
Created July 30, 2019 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kLabz/20f6e4dece9aa4c256af58514626b66f to your computer and use it in GitHub Desktop.
Save kLabz/20f6e4dece9aa4c256af58514626b66f to your computer and use it in GitHub Desktop.
Haxe MaterialUI: withStyles

Using Styles.withStyles to style your components

Component without public props

private typedef Props = {
	var classes:TClasses;
}

private typedef TClasses = Classes<[
	root
]>;

@:noPublicProps()
@:wrap(Styles.withStyles(styles))
class MyComponent extends ReactComponentOfProps<Props> {
	public static function styles(theme:MuiTheme):ClassesDef<TClasses> {
		return Styles.jss({
			root: {
				// Css styles here
			}
		});
	}

	override public function render() {
		return jsx('
			<div className=${props.classes.root} />
		')
	}
}

Component with public props

private typedef Props = {
	> PublicProps,
	var classes:TClasses;
}

private typedef PublicProps = {
	// Public props here
}

private typedef TClasses = Classes<[
	root
]>;

@:publicProps(PublicProps)
@:wrap(Styles.withStyles(styles))
class MyComponent extends ReactComponentOfProps<Props> {
	public static function styles(theme:MuiTheme):ClassesDef<TClasses> {
		return Styles.jss({
			root: {
				// Css styles here
			}
		});
	}

	override public function render() {
		return jsx('
			<div className=${props.classes.root} />
		')
	}
}

Component with public props and HOC

import react.router.ReactRouter;
import react.router.Route.RouteRenderProps;

private typedef Props = {
	> PublicProps,
	> RouteRenderProps,
	var classes:TClasses;
}

private typedef PublicProps = {
	// Public props here
}

private typedef TClasses = Classes<[
	root
]>;

@:publicProps(PublicProps)
@:wrap(ReactRouter.withRouter)
@:wrap(Styles.withStyles(styles))
class MyComponent extends ReactComponentOfProps<Props> {
	public static function styles(theme:MuiTheme):ClassesDef<TClasses> {
		return Styles.jss({
			root: {
				// Css styles here
			}
		});
	}

	override public function render() {
		return jsx('
			<div className=${props.classes.root} />
		')
	}
}

Combine props with classNames

	override public function render() {
		var classes = classNames({
			'${props.classes.root}': true,
			'${props.classes.conditionalClass}': props.condition
		});

		return jsx('
			<div className=${classes}>
				${props.children}
			</div>
		');
	}
@kLabz
Copy link
Author

kLabz commented Jul 30, 2019

Another example:

		return Styles.jss({
			root: {
				position: Absolute, // Some properties have enumerations, see css-types lib
				top: 42, // will be converted to pixels if a number type is used
				marginLeft: theme.spacing.unit, // I think this API changed in recent versions of MUI, though
				"& > div": {
					// more css here, with enums, etc.
				},
				"@media print": {
					// media queries work too
				}
			}
		});

@kLabz
Copy link
Author

kLabz commented Sep 6, 2021

Also, note that Styles is mui.core.styles.Styles

@sonygod
Copy link

sonygod commented Sep 7, 2021

@kLabz

how to export with withRouter?

from

export default withStyles(styles)(Topbar)

to

export default withRouter(withStyles(styles)(Topbar));

oh ,I got it

@:wrap(ReactRouter.withRouter)

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