React JS: trying to have sentimental anlysis prediction link to background - reactjs

The goal of this current app is to have the "POSITIVE" mood prediction link to a sunny day background, and "NEGATIVE" mood prediction link to a moving cloudy day background. Below is the crux of my code. My question is more specifically about the logic. If somebody can explain how state would work in this situation would be the best. How to tie the "css based background in JS" element to the state and how to make a method to handle the changing background and how to return the method. Let me know if the question is unclear.
enter code here
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
theme: "",
};
}
handleBackground = () => {
if (!this.state.mood) {
return startBackground;
} else if (this.state.mood === "NEGATIVE") {
return cloudFromCss;
} else {
return sunFromCss;
}
};
render() {
const isLoading = this.state.isLoading;
const formData = this.state.formData;
const mood = this.state.mood;
const sunFromCss = {
".sun": {
position: "absolute",
margin: "auto",
borderRadius: "50%",
top: "0",
right: "0",
bottom: "80%",
left: "60%",
backgroundColor: "rgb(252, 174, 7)",
height: "50px",
width: "50px",
animationName: "beat",
animationDuration: "3s",
animationIterationCount: "infinite",
transform: "scale(2.5)",
opacity: "0.8",
},
"#keyframes backdiv": { "50%": { background: "#bde6f3" } },
"#keyframes beat": {
"0%": { transform: "scale(2.5) rotate(-45deg)" },
"50%": { transform: "scale(2) rotate(-45deg)" },
},
};
const startBackground = {
"#clouds": { padding: "100px 0", background: "#c9dbe9" },
};
const cloudFromCss = {
"#clouds": { padding: "100px 0", background: "#c9dbe9" },
".cloud": {
width: "200px",
height: "60px",
background: "#fff",
borderRadius: "200px",
animationName: "moveclouds",
animationIterationCount: "infinite",
position: "relative",
},
".cloud:before,\n.cloud:after": {
content: '""',
position: ["absolute", "absolute"],
background: "#fff",
width: "100px",
height: "80px",
top: "-15px",
left: "10px",
borderRadius: "100px",
transform: "rotate(30deg)",
},
".cloud:after": {
width: "120px",
height: "120px",
top: "-55px",
left: "auto",
right: "15px",
},
".x1": { opacity: "0.8", animationDuration: "15s" },
"#keyframes moveclouds": {
"0%": { marginLeft: "1000px" },
"100%": { marginLeft: "-1000px" },
},
};
return (
{() => this.handleBackground()}
)

Related

Passing props to classname using Emotion

My code looks like this:
export const MUISlider = styled(Slider)(({ theme, marks }) => ({
'& .MuiSlider-valueLabel': {
padding: [4, 12],
width: 33,
height: 30,
background: theme.palette.secondary.main,
},
'& .MuiSlider-markLabel': {
color: theme.palette.secondary.light,
'&[data-index="0"]': {
transform: 'none',
},
'&[data-index=`${({ marks }) => marks.length}`]': {
color: 'red',
},
},
'& .MuiSlider-markLabelActive': {
color: theme.palette.secondary.light,
},
}));
I want to pass a prop into the CSS here where it says '&[data-index="${({ marks }) => marks.length}"]': { . How would I format this correctly using #emotion?

White-labeling in reactjs with Materail-UI and Tailwind

I have a react setup in which I am using Tailwind and Material-UI. I have a component library in the Storybook which has MUI Theme in it to style the variants like button outline. I am trying to find a way of how can I use these two things to make the react app white-labeled. Basically, Which properties I can set in my theme that it can be directly applied to the components by the user/providers/companies. Currently, my theme looks something like this in the component library.
import { createTheme, ThemeOptions } from '#mui/material/styles';
import { Colours } from '../common';
export const MyThemeOptions: ThemeOptions = {
palette: {
primary: {
main: Colours.primary,
},
secondary: {
main: Colours.secondary,
},
error: {
main: Colours.error,
},
warning: {
main: Colours.warning,
},
info: {
main: Colours.info,
},
success: {
main: Colours.success,
},
},
components: {
MuiButton: {
styleOverrides: {
root: {
borderRadius: '4px',
},
contained: {
padding: '4px 12px',
},
outlined: {
border: '1px solid #bdbdbd',
textTransform: 'none',
minWidth: 0,
color: '#212121',
},
outlinedSizeSmall: {
padding: '2px',
},
outlinedSizeMedium: {
padding: '6px',
},
outlinedSizeLarge: {
padding: '10px'
},
},
},
MuiButtonGroup: {
styleOverrides: {
grouped: {
minWidth: 0,
},
},
},
MuiCard: {
styleOverrides: {
root: {
boxShadow: '0 3px 7px 0 rgba(98, 125, 152, 0.16), 0 0 2px 0 rgba(36, 59, 83, 0.04)',
},
},
},
MuiToggleButton: {
styleOverrides: {
root: {
border: '1px solid #bdbdbd',
borderRadius: '4px',
textTransform: 'none',
color: '#212121',
minWidth: 0,
},
sizeSmall: {
padding: '2px',
},
sizeMedium: {
padding: '6px',
},
sizeLarge: {
padding: '10px',
}
}
}
},
};
const MyTheme = createTheme(MyThemeOptions);
export default MyTheme;
I am not able to find an article or tutorial or something which could lead me in a direction of how can I achieve the white labeling? If you know any knowledge asset in this direction, please share with me. Thank you!

Change style when rotation the smartphone

I use the react-native-orientation-locker package and I would like to change the style when I rotate the smartphone
I use the state to initialize my style, but this solution I don't think is optimal
here is an example of my code
const [styles, setStyles] = useState(Styles(theme, layout))
const changeStyles = (o: OrientationType): void => {
layout = getDeviceVideoLayout(o) // return 'PORTRAIT or 'LANDSCAPE-LEFT' or 'LANDSCAPE-RIGHT'
// change the rotation of the phone
const { height, width } = Dimensions.get('window')
//I update width and height
theme.layout.width = width
theme.layout.height = height
// I update the status
setStyles(Styles(theme, layout))
}
useDeviceOrientationChange((o) => {
changeStyles(o)
})
I don't like this solution of the style in the state
an example of my style
const StyleCommon = (theme: Theme, bottomSpace: number): StyleGeneral => StyleSheet.create({
container: {
height: theme.layout.height,
width: '100%',
backgroundColor: theme.colors.black
},
actionButtonContainer: {
width: 50,
backgroundColor: 'rgba(242, 241, 240, 0.3)',
borderRadius: 12,
marginBottom: 20
}
})
const StyleLayoutPortrait = (theme: Theme, bottomSpace: number): StyleLayout => StyleSheet.create({
actionContainer: {
position: 'absolute',
bottom: theme.spacing.sm + theme.layout.insets.bottom + 40 + bottomSpace,
right: theme.spacing.sm
}
})
const StyleLayoutLandscapeLeft = (theme: Theme): StyleLayout => StyleSheet.create({
actionContainer: {
position: 'absolute',
top: theme.spacing.sm,
right: theme.spacing.sm
}
})
const StyleLayoutLandscapeRight = (theme: Theme): StyleLayout => StyleSheet.create({
actionContainer: {
position: 'absolute',
right: theme.spacing.sm + theme.layout.insets.top,
top: theme.spacing.sm
}
})
const Styles = (theme: Theme, layout: VideoLayoutType, bottomSpace: number): Style => {
if (layout === 'LANDSCAPE-RIGHT') {
return ({
...StyleCommon(theme, bottomSpace),
...StyleLayoutLandscapeRight(theme)
})
} else if (layout === 'LANDSCAPE-LEFT') {
return ({
...StyleCommon(theme, bottomSpace),
...StyleLayoutLandscapeLeft(theme)
})
} else {
return ({
...StyleCommon(theme, bottomSpace),
...StyleLayoutPortrait(theme, bottomSpace)
})
}
}
I do not think this solution is optimal, how could I do?
indicatorContainerProps:{
position: 'fixed',
right: '0px',
bottom: '0%',
marginBottom: '40%',
display:'flex',
justifyContent:'center',
flexDirection:'row',
alignItems:'center',
"#media (orientation:landscape)": {
marginBottom: '0px'
}
This example is for Jsx or ReactJs.
You just need to specify the some class name like text
text:{
position: 'fixed',
right: '0px',
bottom: '0%',
marginBottom: '40%',
display:'flex',
justifyContent:'center',
flexDirection:'row',
alignItems:'center',
"#media (orientation:landscape)": { //here you can change based on orientation
marginBottom: '0px'
}

Stepper vertical line detaches when label wraps over multiple lines?

The text inside my MaterialUI Stepper // StepLabel sometimes wraps over multiple lines.
I need to keep the vertical StepConnectors attached the StepIcons regardless of the number of lines of text in the label.
I've tried other solutions such as using CSS pseudo tags, but I hit a wall every time I try to work those changes into our existing solution.
Massive thanks in advance to anyone who can help.
Sandbox
https://codesandbox.io/s/practical-chebyshev-4hktl?file=/src/App.js
Current Screenshot
Existing ThemeOptions
import {
ThemeOptions,
createTheme,
ThemeProvider,
CssBaseline
} from "#material-ui/core";
export const themeOptions: ThemeOptions = {
overrides: {
MuiStepper: {
root: {
backgroundColor: "transparent" // remove set background
}
},
MuiStepConnector: {
vertical: {
padding: 0,
width: 5,
marginLeft: 8 // half icon
},
lineVertical: {
top: "calc(-50%)",
bottom: "calc(50%)",
borderLeftWidth: "2px",
marginLeft: "-1px", // center (1/2 width)
marginTop: "-6px", // add -ve margin to top and bottom ...
marginBottom: "-6px", // ... to hide gap due to smaller icon
borderColor: "lightgrey",
"$active &, $completed &": {
borderLeftWidth: "4px",
marginLeft: "-2px",
borderColor: "blue"
}
}
},
MuiStepLabel: {
label: {
textAlign: "left",
fontSize: "1.25rem",
"&$active": {
fontWeight: 400
},
"&$completed": {
fontWeight: 400
}
},
iconContainer: {
paddingRight: 12
}
},
MuiStepIcon: {
root: {
display: "block",
fontSize: "1rem",
color: "lightgrey",
"&$completed": {
color: "blue"
},
"&$active": {
color: "blue"
}
}
}
}
};
Just in case anyone finds this in the future, we compromised on the ​implementation to deliver the task.
Instead of having a variable height on the MuiStepLabel, it was given a fixed height to keep the StepIcons the same distance apart. If you imagine the below screenshot with a different font size + spacing, it ended up looking OK, but not ideal.
Before
// src/Theme/index.tsx
export const themeOptions: ThemeOptions = {
overrides: {
MuiStepConnector: {
marginTop: "-6px",
marginBottom: "-6px",
}
MuiStepLabel: {}
}
}
After
// src/Theme/index.tsx
export const themeOptions: ThemeOptions = {
overrides: {
MuiStepConnector: {
marginTop: "-2px",
marginBottom: "-4px",
minHeight: "calc(24px + 0.5rem)",
},
MuiStepLabel: {
height: "1.25rem",
lineHeight: "1.25rem",
}
}
}
Sandbox
https://codesandbox.io/s/epic-bohr-0p7fj?file=/src/Theme/index.ts

Google React Charts Placement & Legend Placement

I am having troubles placing the google react charts on the left of a full width container without the legend thinking it's being cut off.
If I leave it as a default it looks like this:
However I want it to be on the left side and the legend to be next to it.
When I change the position of the chart area:
chartArea: {
left: 0,
right: 400,
top: 0,
bottom: 0,
width: '100%',
height: '80%',
},
It then looks like this:
For whatever reason it thinks there isn't enough space and it cuts the legend text which is really annoying.
Here is the code I am using:
const pieOptions = {
title: '',
pieHole: 0.6,
slices: [
{
color: '#00A3E0',
},
{
color: '#F2A900',
},
{
color: '#43B02A',
},
{
color: '#DF4661',
},
{
color: '#003865',
},
{
color: '#A15A95',
},
{
color: '#FF6A13',
},
],
legend: {
position: 'right',
alignment: 'end',
textStyle: {
color: '#666',
fontSize: 12,
},
},
tooltip: {
showColorCode: true,
},
chartArea: {
left: 0,
right: 400,
top: 0,
bottom: 0,
width: '100%',
height: '80%',
},
fontName: 'inherit',
fontSize: 12,
}
Can anyone advise?
Also if this is not fixable, can you please recommend a chart which supports that and custom colours please?
Thank you in advance!

Resources