Reputation: 1760
I want to be able to change my layout when i hit a given screen size so i can change the way my site looks when it hits a mobile view.
Currently my react site scrolls horizontally using the package Horitzontal Scroll Package
I've tried implementing this with the react-responsive package which allows me to run media queries but there seems to be a conflict between the two packages.
So i want my site to scroll horizontal when in full screen (Desktop and table) and then scroll top to bottom in mobile view. How would i do this?
class Home extends React.Component{
constructor(props) {
super(props);
this.state = {};
}
componentWillMount() {
this.setState({
home: projectsData.filter( p => p.name === "homepage" ),
galleryImages: [
ImgOne,
ImgTwo,
ImgThree,
ImgFour,
ImgFive,
ImgSix
]
});
}
render(){
const test = { color: "black", position: "fixed" }
return(
<div className="row">
<Responsive minWidth={992}>
<HorizontalScroll reverseScroll={true}>
<Header />
<SplashScreen />
<CopyText />
</HorizontalScroll>
</Responsive>
</div>
);
}
}
NOTE: I know that the horizontal-scroll package doesn't support sizing with percentages.
Upvotes: 5
Views: 24026
Reputation: 16102
You can use Material UI <Hidden>
component.
Deprecated in Version5 of Material Ui. sx prop replaced it
import React from 'react';
import PropTypes from 'prop-types';
import compose from 'recompose/compose';
import { withStyles } from '@material-ui/core/styles';
import Paper from '@material-ui/core/Paper';
import Hidden from '@material-ui/core/Hidden';
import withWidth from '@material-ui/core/withWidth';
import Typography from '@material-ui/core/Typography';
const styles = theme => ({
root: {
flexGrow: 1,
},
container: {
display: 'flex',
},
paper: {
padding: theme.spacing.unit * 2,
textAlign: 'center',
color: theme.palette.text.secondary,
flex: '1 0 auto',
margin: theme.spacing.unit,
},
});
function BreakpointUp(props) {
const { classes } = props;
return (
<div className={classes.root}>
<Typography variant="subtitle1">Current width: {props.width}</Typography>
<div className={classes.container}>
<Hidden xsUp>
<Paper className={classes.paper}>xsUp</Paper>
</Hidden>
<Hidden smUp>
<Paper className={classes.paper}>smUp</Paper>
</Hidden>
<Hidden mdUp>
<Paper className={classes.paper}>mdUp</Paper>
</Hidden>
<Hidden lgUp>
<Paper className={classes.paper}>lgUp</Paper>
</Hidden>
<Hidden xlUp>
<Paper className={classes.paper}>xlUp</Paper>
</Hidden>
</div>
</div>
);
}
BreakpointUp.propTypes = {
classes: PropTypes.object.isRequired,
width: PropTypes.string.isRequired,
};
export default compose(
withStyles(styles),
withWidth(),
)(BreakpointUp);
Upvotes: 5
Reputation: 2236
I am not familiar with the HorizontalScroll component, but I can give you a code example of how to change layout based on screen size with React.
class WindowWidth extends React.Component {
constructor(props) {
super(props);
this.state = {};
this.onResize = this.onResize.bind(this);
}
render() {
return this.props.children({ width: this.state.width });
}
getWindowWidth() {
return Math.max(
document.documentElement.clientWidth,
window.innerWidth || 0
);
}
onResize() {
window.requestAnimationFrame(() => {
this.setState({
width: this.getWindowWidth()
});
});
}
componentWillMount() {
this.setState({
width: this.getWindowWidth()
});
}
componentDidMount() {
window.addEventListener("resize", this.onResize);
}
componentWillUnmount() {
window.removeEventListener("resize", this.onResize);
}
}
const Vertical = props => <div> Vertical component: {props.children}</div>;
const Horizontal = props => <div> Horizontal component: {props.children}</div>;
ReactDOM.render(
<WindowWidth>
{({ width }) => {
if (width >= 992) {
return <Horizontal>{width}px</Horizontal>;
}
return <Vertical>{width}px</Vertical>;
}}
</WindowWidth>,
document.querySelector("#react-app")
);
Upvotes: 1
Reputation: 108
I recommend looking into Media Queries. Here is a short video to show you the basics.
You can set a threshold that will trigger separate styles under those conditions. It can be very useful when you want your UI to do something different at different sizes.
@media only screen
and (*Your threshold criteria goes here* EXAMPLE: max-width : 1000px) {
//css goes here with whatever css you want to fire at this threshold
}
Upvotes: 0