Screen Change Orientation Listener ReactJS - reactjs

I am trying to make a different button position for both portrait and landscape versions of the React application I’m working on. I tried learning about event listeners and referred to these two posts, and inspected the console for a message while trying to rotate using an iPhoneX view on Google Chrome. However, I don’t seem to see a message of 'changed orientation'. Does anyone know the problem with my code? Feel free to ask for more clarification.
Welcome.js
import React, { Component} from "react";
import { Link } from "react-router-dom";
import "../style/App.css";
import { Typography, Button, Card, Grid } from "#material-ui/core";
import homepage2 from "../images/homepage2.png";
export default class WelcomeComponent extends Component {
onOrientationChange = (event) => {
console.log('changed orientation');
}
render() {
return (
<div onOrientationChange={this.onOrientationChange}
style={{
minHeight: "100vh",
backgroundImage: `url(${homepage2})`,
backgroundPosition: "center",
backgroundSize: "cover"
}}
>
<div
style={{
textAlign: "center",
verticalAlign: "bottom",
bottom: "0px"
}}
>
<Link to="/Language">
<Button
variant="contained"
color="secondary"
size="large"
style={{
position: "absolute",
top: "50%",
left: "50%",
transform: "translate(-50%,-50%)"
}}
>
CONTINUE
</Button>
</Link>
</div>
</div>
);
}
}

Related

React Create a Horizontal Divider with Text In between

I need to create a React component that is a Horizontal Divider with a content like text In between. All the resources I have online is unable to help me get this done. I tried a material-ui divider by creating a Divider component and placing my text in-between like the example below:
<Divider>Or</Divider>
But I get the error:
hr is a void element tag and must neither have `children` nor use `dangerouslySetInnerHTML`.
I need to achieve this in the image below:
Any help will be appreciated.
These are my codes below:
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import List from '#material-ui/core/List';
import Divider from '#material-ui/core/Divider';
const useStyles = makeStyles((theme) => ({
root: {
width: '100%',
maxWidth: 360,
backgroundColor: theme.palette.background.paper,
},
}));
export default function ListDividers() {
const classes = useStyles();
return (
<List component="nav" className={classes.root} aria-label="mailbox
folders">
<Divider>Or</Divider>
</List>
);
}
Using Material UI.
import React from "react";
import { makeStyles } from "#material-ui/core";
const useStyles = makeStyles(theme => ({
container: {
display: "flex",
alignItems: "center"
},
border: {
borderBottom: "2px solid lightgray",
width: "100%"
},
content: {
paddingTop: theme.spacing(0.5),
paddingBottom: theme.spacing(0.5),
paddingRight: theme.spacing(2),
paddingLeft: theme.spacing(2),
fontWeight: 500,
fontSize: 22,
color: "lightgray"
}
}));
const DividerWithText = ({ children }) => {
const classes = useStyles();
return (
<div className={classes.container}>
<div className={classes.border} />
<span className={classes.content}>{children}</span>
<div className={classes.border} />
</div>
);
};
export default DividerWithText;
You can import and use it like the below
<DividerWithText>Or</DividerWithText>
Result
Here a custom example of what you could do : repro on stackblitz
import React, { Component } from "react";
import { render } from "react-dom";
import Hello from "./Hello";
import "./style.css";
const App = () => {
return <Divider>Or</Divider>;
};
const Divider = ({ children }) => {
return (
<div className="container">
<div className="border" />
<span className="content">
{children}
</span>
<div className="border" />
</div>
);
};
render(<App />, document.getElementById("root"));
And the CSS:
.container{
display: flex;
align-items: center;
}
.border{
border-bottom: 1px solid black;
width: 100%;
}
.content {
padding: 0 10px 0 10px;
}
Update 29/03/2022
That's now possible with Material UI 🔥
https://mui.com/components/dividers/#dividers-with-text
You may want different spacing sometime
<Divider spacing={1}>Hello World</Divider>
Or
<Divider spacing={2}>Hello World</Divider>
For a configurable spacing here a Github Gist
Or a playground in codesandbox if you prefer
The current answer causes any text with spaces in-between to wrap:
If that happens, changing width: 100% to flexGrow: 1 should solve it:
border: {
borderBottom: "2px solid lightgray",
flexGrow: 1,
}
Unfortunately for now, having Divider with text on it in MUI is only available in v5, which is still in alpha stage. If you would like to try, you can download the alpha package, but be warned that it is still highly unstable and a lot of changes might be needed to migrate to v5, which is not very worth it.
GitHub discussion: https://github.com/mui-org/material-ui/issues/24036

Hook API Styles aren't loading?

The project is being built with Next.js and material-ui. I've used the Hook API method to style my components but the styles aren't always loading. When I run next dev to test it, my styles don't load. If I edit anything in the page currently being rendered, it reloads, but this time with the styles. If I click reload, however, the styles fail to load again. I've been searching for help with this but I can't find anything that seems to solve the issue. I saw a few things about using material-ui/core with material-ui/styles but it's all in reference to v3 and it doesn't solve the issue I have.
Here's one of the pages for example:
import Nav from '../components/nav';
import Footer from '../components/footer';
import Product from '../components/product';
import { makeStyles, ThemeProvider } from '#material-ui/core/styles';
import theme from '../components/customTheme';
import {
Typography,
Button
} from '#material-ui/core';
const useStyles = makeStyles({
body: {
margin: '15vh 0',
backgroundColor: ' white',
textAlign: 'left'
},
earnWrapper: {
display: 'block',
textAlign: 'left',
backgroundColor: 'white',
width: '80%',
margin: '5% 2vh',
padding: '5%',
borderRadius: '25px',
transition: '0.3s',
boxShadow: '0px 5px 20px #dedede'
},
earnContent: {
textAlign: 'left',
display: 'inline-block'
},
earntCaption: {
color: 'grey',
},
earntAmount: {
margin: '1vh 0 2vh'
},
shareInfo: {
margin: '5% 2vh',
textAlign: 'left'
},
products: {
textAlign: 'center ',
width: '100%'
}
});
export default function Home(props) {
const styles = useStyles();
return (
<ThemeProvider theme={theme}>
<DefaultHead
title="Oorigin | Home"
/>
<Nav isLoggedIn={true} />
<div className={styles.body}>
<div className={styles.earnWrapper}>
<div className={styles.earnContent}>
<Typography className={styles.earntCaption} variant="caption">You've earned</Typography>
<Typography className={styles.earntAmount} variant="h4">S$18.50</Typography>
<Button disableElevation variant="contained" color="primary">Withdraw</Button>
</div>
</div>
<div className={styles.shareInfo}>
<Typography><b>Shop, Share, Earn</b></Typography>
<Typography><br/>Shop products you like, share products you love, and earn up to 10% commission on every qualifying sale you refer</Typography>
</div>
<div className={styles.products}>
<Product
imgURL="../TestItem1.svg"
imgAlt="Test Product"
title="Disinfecting Air Purifying Solution"
price={(22.80).toFixed(2)}
link="/products/staticProduct"
/>
<Product
imgURL="../TestItem2.svg"
imgAlt="Test Product"
title="Disinfecting Air Purifying Solution"
price={(22.80).toFixed(2)}
/>
<Product
imgURL="../TestItem3.svg"
imgAlt="Test Product"
title="Disinfecting Air Purifying Solution"
price={(22.80).toFixed(2)}
/>
<Product
imgURL="../TestItem4.svg"
imgAlt="Test Product"
title="Disinfecting Air Purifying Solution"
price={(22.80).toFixed(2)}
/>
</div>
</div>
<Footer/>
</ThemeProvider>
);
}

Unable to use style when convert html to image and download in react js

I am trying to convert HTML to Image and download using this library, but when I apply style in HTML then it don't print anything in image, without style are generates proper image.
import React, { Component } from "react";
import htmlToImage from "html-to-image";
import download from "downloadjs";
export default class MyPage extends Component {
downloadAsImage = () => {
htmlToImage
.toPng(document.getElementById("test"))
.then(function (dataUrl) {
download(dataUrl, "test-img.png");
})
.catch(function (error) {
console.error("oops, something went wrong!", error);
});
}
render() {
return (
<span><div
id="test"
style={{
margin: "0 auto",
position: "relative",
height: "480px",
width: "310px",
backgroundRepeat: "no-repeat",
backgroundPosition: "center top",
}}
>
Test
</div>
<button
onClick={() => {
this.downloadAsImage();
}}
>
Download {" "}
<img src={require("../../assets/icons/background.svg")} />
</button>
</span>
);
}
}

Pass multiple props to component in react-materialui

I'm trying to pass the styles and text to the reactjs component using PropTypes, but I'm getting the error that TypeError: Cannot read property 'header' of undefined and its point the error to {typo.header}
Unable to pass 2 different props into react component. I'm sure I'm not passing the props correctly, I'm learning reactjs and not sure how to do this correctly. Can some guide me how to do this.
App.js:
import React from 'react';
import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles';
import Card, { CardActions, CardContent } from 'material-ui/Card';
import Typography from 'material-ui/Typography';
import './App.css';
import { AppStyle } from "./AppStyle";
import { Typos } from "./AppTypo";
function SurveyApp(props) {
const { classes, typo } = props;
return (
<div className={classes.wrapper}>
<div className={classes._header}>
</div>
<div className={classes._body}>
<div className={classes.__formContainer}>
<Card className={classes.card}>
<CardContent>
<Typography>
{typo.header}
</Typography>
<Typography>
</Typography>
</CardContent>
</Card>
</div>
</div>
</div>
);
}
SurveyApp.propTypes = {
classes: PropTypes.object.isRequired,
typo: PropTypes.object.isRequired
}
export default withStyles(AppStyle, Typos)(SurveyApp);
AppStyle.js:
export const AppStyle = {
wrapper: {
width: '100%',
height: '100%',
position: 'relative'
},
_header: {
width: '100%',
height: 100,
backgroundColor: '#295e94',
position: 'relative'
},
_body: {
width: '100%;',
height: 'auto',
minHeight: 'calc(100% - 100px)',
position: 'relative',
display: 'flex',
flexDirection: 'row',
alignContent: 'center',
alignItems: 'start',
justifyContent: 'center'
},
__formContainer: {
width: 750,
position: 'absolute',
top: -30
},
'#media (max-width: 480px)': {
__formContainer: {
width: '100%',
position: 'absolute',
top: -30
}
}
}
AppTypo.js:
export const Typos = {
header: "User Experience",
subHeader: "This is intended to improve your experience."
}
You're not providing the props correctly.
export default withStyles(AppStyle, Typos)(SurveyApp);
Does not do what you think it does with Typos.
Besides that, you have two different definitions for header: header and _header. The style you pass via withStyles is _header, from AppStyle. That's the one you should use.
You should not try to pass non-style information via withStyles, which is what it looks like you're trying to do with Typos. Those need to come in from the parent component, or since this is the parent, just use the imported Typos.header.
I recommend you read more on props in general to get a better idea of what they are for an how to use them.
In the future, you may find using React's Context is better for handling styles.

Put a PNG image over a background image

I'm trying to replicate http://www.milkbardigital.com.au/ as a learning project in react.
Currently I'm struggling to get the PNG image over the top of the background image.
Here's my code,
import React from 'react';
import {Image} from 'react-bootstrap';
export default React.createClass ( {
render() {
var background = {backgroundSize : 'cover'};
var textStyle = {
position: 'absolute',
top: '50%',
left: '50%'
};
return (
<div style={{width: 'auto'}}>
<Image
style={background} responsive
src="http://www.milkbardigital.com.au/wp-content/uploads/2015/11/Milkbar-Home-Background.jpg">
</Image>
<h1 style={textStyle}>Agile Media Content</h1>
</div>
);
}
});
<Image src="http://www.milkbardigital.com.au/wp-content/uploads/2015/11/Milkbar-Digital-Media.png" responsive/>
This "Image src" I've tried to place inside the above code but it won't work.
Anyone able to work this out?
It's with React Bootstrap!
Thanks,
Edit - - -
I've worked out how to get it slightly on top, now trying to get it put in the right position.
This is the code, ignore the opacity part,
/**
* Created by Hewlbern on 21-Jan-17.
*/
import React from 'react';
import {Image} from 'react-bootstrap';
export default React.createClass ( {
render() {
var background = {backgroundSize : 'cover',
opacity: '0.5',
};
var textStyle = {
position: 'absolute',
top: '50%',
left: '50%'
};
var ontop = {
display: 'block',
margin: 'auto',
width: '50%',
};
return (
<div style={{width: 'auto'}}>
<Image
style={background} responsive
src="http://www.milkbardigital.com.au/wp-content/uploads/2015/11/Milkbar-Home-Background.jpg">
</Image>
<h1 style={textStyle}>Agile Media Content</h1>
<Image style={textStyle} responsive src="http://www.milkbardigital.com.au/wp-content/uploads/2015/11/Milkbar-Digital-Media.png" />
</div>
);
}
});

Resources