React / Material UI - <CardHeader> css - css

Using React & Material UI I'm trying to layout 3 divs within a <Card> <CardHeader/> such that it has a left, center and right alignment respectively as shown below.
The change is trivial, I need to remove the padding and change to display: inherit but it seems this <div> exists between the exposed style & titleStyle for <CardHeader> and <CardHeader title={someElement}/>
The hierarchy looks like:
...<div><div.myCardHeader><div><span><myTitleElement>...
Being so new to React and styles, I'm unsure how to get to it.
Some representative code follows.
Thanks for help.
// #flow
import React, { Component } from 'react';
import Paper from 'material-ui/Paper';
import { Card, CardActions, CardHeader, CardMedia, CardTitle, CardText } from 'material-ui/Card';
const style = {
paper: {
height: 250,
width: 200,
margin: 20,
},
card: {
header: {
container: {
display: 'flex', /* establish flex container */
justifyContent: 'space-between',
backgroundColor: 'lightblue'
},
padding: 1,
height: 26
}
}
};
const POCardTitle = () => (
<div className="myContainer" style={style.card.header.container}>
<div style={{ width: 25, height: 26, border: '2px dashed red' }}> - </div>
<div style={{ width: 25, height: 26, border: '2px dashed blue' }}> - </div>
<div style={{ width: 25, height: 26, border: '2px dashed green' }}> - </div>
</div>
);
export default class POCard extends Component {
render() {
return (
<div>
<Paper style={style.paper} zDepth={2} >
<Card >
<CardHeader className="myCardHeader"
style={style.card.header}
titleStyle={{ paddingRight: 0, display: 'inline' }}
title={<POCardTitle />}
/>
</Card>
</Paper>
</div>
);
}
}

I managed to get there courtesy of https://www.styled-components.com
and the following:
const StyledHeader = styled(CardHeader) `
padding: 0px !important;
height: 26px !important;
> div {
display: inherit !important;
padding-right: 0px !important;
}
`;
I could find no other way to get to the "first div" after the component through regular CSS...

Related

Vertical Fixed Box at bottom of screen using MUI

So far I have tried this:
import "./styles.css";
import { Box, Typography } from "#mui/material";
import styled from "#emotion/styled";
export default function App() {
const MainStyle = styled("div")(({ theme }) => ({
position: "fixed",
zIndex: 99999,
right: 0,
bottom: 0
}));
return (
<MainStyle>
<Box sx={{ transform: "rotate(-90deg)", backgroundColor: "blue" }}>
<Typography sx={{ color: "white" }}>CHAT WITH US!</Typography>
</Box>
</MainStyle>
);
}
The problem with this is that half the box is out of the screen and is not all the way to the right.
My goal is to have it all the way in the right corner but also show the entire box like up against the side like https://proxy-seller.com/ have their "chat with us, we are online" just I want it on the right side.
writing-mode: vertical-rl; you can the mentioned css property.
Simple html example for writing-mode: vertical-rl, you can lear more about it from this.
.main {
position: relative;
}
.text {
writing-mode: vertical-rl;
border: 2px solid #000;
position: absolute;
top: 15px;
right: 15px;
height: 120px;
}
<main class="main">
<h1 class="text">Test text</h1>
</main>

Make a panel under a button that overlays another element

I have an editor and several buttons above it on the right. I would like to have a panel just under Button2 that overlays the editor. Then, clicking on Button2 will expand and collapse the panel (which will be easy to implement).
I have written the following code: https://codesandbox.io/s/fervent-mclaren-3mrtyj?file=/src/App.js. At the moment, the panel is NOT under Button2 and does NOT overlay the editor.
Does anyone know how to amend the CSS?
import React from "react";
import { Stack } from "#fluentui/react";
export default class App extends React.Component {
render() {
return (
<div>
<Stack horizontal horizontalAlign="space-between">
<div>Title</div>
<div>Button1 Button2 Button3</div>
</Stack>
<div
style={{
backgroundColor: "yellow",
width: "350px",
height: "50px"
}}
>
A floating panel which is supposed to be always under "Button2" and
overlays the editor.
</div>
<div
style={{
backgroundColor: "gray",
width: "100%",
height: "300px"
}}
>
An editor
</div>
</div>
);
}
}
You need to use position:absolute on floating pane and add it in the editor div which will have position:relative.You can see the result it works fine
On clicking button 2 the floating panel hides/shows alternatively
This will work.
var btn=document.querySelector('.drop_btn');
btn.onclick=function()
{
document.querySelector('.dropdown').classList.toggle('block');
}
*
{
font-family: 'arial';
margin: 0px;
padding: 0px;
}
.menu_pane
{
display: flex;
background: #151515;
color: white;
padding:5px 10px;
align-items: center;
border-bottom: 1px solid rgba(255,255,255,0.2);
}
.menu_pane h3
{
font-weight: normal;
font-size: 18px;
flex-grow: 1;
}
.menu_pane .btn button
{
position: relative;
background: #0971F1;
color: white;
border-radius: 5px;
border:none;
cursor: pointer;
padding: 8px 20px;
margin-right: 10px;
}
.dropdown
{
display: none;
height: 100%;
width: 100%;
top: 0;
left: 0;
color: white !important;
position: absolute;
background: #242424;
border-radius: 0 0 10px 10px;
}
.menu_pane .btn .dropdown p
{
font-size: 14px;
}
.editor_pane
{
position: relative;
background:#151515;
color: white;
min-height: 50vh;
border-radius: 0 0 10px 10px;
padding: 10px;
color: #512DA8;
}
.block
{
display: block;
}
<div class="container">
<div class="menu_pane">
<h3>Title</h3>
<div class="btn">
<button>Button-1</button>
</div>
<div class="btn">
<button class="drop_btn">Button-2</button>
</div>
<div class="btn">
<button>Button-3</button>
</div>
</div>
<div class="editor_pane">
<p>An editor</p>
<div class="dropdown">
<p>A floating panel which is supposed to be always under "Button2" and overlays the editor.</p>
</div>
</div>
</div>
How does this look?
https://codesandbox.io/s/hidden-platform-q3v4kc?file=/src/App.js
I moved the floating pane into the button, made the button position relative, and made the floating pane position absolute.
Note: notice there's no top property on the floating pane. One neat thing about position absolute is if you don't set top, left, bottom, right those positions will be where that box would be if it wasn't position absolute.
Update
I noticed that the overlay needed to cover the "editor" area only and have the example updated with hopefully the right placement of it.
Updated live example: codesandbox
import React from "react";
import { Stack } from "#fluentui/react";
export default class App extends React.Component {
state = { showOverlay: true };
handleToggle = () =>
this.setState((prev) => ({ showOverlay: !prev.showOverlay }));
render() {
return (
<div>
<Stack
horizontal
horizontalAlign="space-between"
style={{ padding: "12px" }}
>
<div>Title</div>
<Stack horizontal>
<button>Button1</button>
<button onClick={this.handleToggle}>
{`Button2 (${this.state.showOverlay ? "Hide" : "Show"} Overlay)`}
</button>
<button>Button3</button>
</Stack>
</Stack>
<div
style={{
backgroundColor: "gray",
width: "100%",
height: "300px",
position: "relative"
}}
>
An editor
<div
style={{
position: "absolute",
inset: "0",
backgroundColor: "rgba(0, 0, 0, 0.70)",
display: this.state.showOverlay ? "flex" : "none",
justifyContent: "center",
alignItems: "center",
color: "#fff"
}}
>
A floating panel which is supposed to be always under "Button2" and
overlays the editor.
</div>
</div>
</div>
);
}
}
Keeping the original example (it had overlay on the whole component except for "Button 2") just in case if it might be useful.
Original
Not sure if I fully understand the desired result, but here is the component implemented with a toggle overlay controlled by Button2.
The overlay is currently set on top of and blocking all child elements except for Button2, so that it works as a "start editing" button, but it can be further adjusted to specify which element it covers to better suit the use case.
Quick demo of the example: codesandbox
import React from "react";
import { Stack } from "#fluentui/react";
export default class App extends React.Component {
state = { showOverlay: true };
handleToggle = () =>
this.setState((prev) => ({ showOverlay: !prev.showOverlay }));
render() {
return (
<div style={{ position: "relative", zIndex: "1" }}>
<Stack
horizontal
horizontalAlign="space-between"
style={{ padding: "12px" }}
>
<div>Title</div>
<Stack horizontal>
<button>Button1</button>
<button style={{ zIndex: "75" }} onClick={this.handleToggle}>
{`Button2 (${this.state.showOverlay ? "Hide" : "Show"} Overlay)`}
</button>
<button>Button3</button>
</Stack>
</Stack>
<div
style={{
position: "absolute",
inset: "0",
backgroundColor: "rgba(0, 0, 0, 0.70)",
zIndex: "50",
display: this.state.showOverlay ? "flex" : "none",
justifyContent: "center",
alignItems: "center",
color: "#fff",
}}
>
A floating panel which is supposed to be always under "Button2" and
overlays the editor.
</div>
<div
style={{
backgroundColor: "gray",
width: "100%",
height: "300px",
}}
>
An editor
</div>
</div>
);
}
}

Why is flex container wrapper the flex items despite exceeding 100%?

I want to make a 3 x 6 matrix. I used flexbox and all the flexItems have a flex-basis of 1/6.
At full screen, its how I want it. However, if you make it a smaller screen it starts to wrap. I don't want to break the 3 x 6
https://codesandbox.io/s/vibrant-spence-gski65
import "./styles.css";
import React, { useState } from "react";
const gridValues = new Array(18).fill(0);
export default function App() {
const [grid, setGrid] = useState<number[]>(gridValues);
return (
<div
style={{
width: "90%",
height: "100%"
}}
>
<div
style={{
display: "flex",
flexWrap: "wrap",
padding: "16px"
}}
>
{grid.map((num, i) => {
return (
<span
key={i}
style={{
display: "flex",
flexBasis: "16.666%",
cursor: "pointer",
justifyContent: "center",
padding: "16px",
border: "1px solid black",
flexShrink: 0
}}
>
<span style={{ opacity: 0 }}>{num}</span>
</span>
);
})}
</div>
</div>
);
}
https://codesandbox.io/s/vibrant-spence-gski65?file=/src/App.tsx
Ended up using CSS grid instead after listening to comment. Definitely much easier
import "./styles.css";
import React, { useState } from "react";
const gridValues = new Array(18).fill(0);
export default function App() {
const [grid, setGrid] = useState<number[]>(gridValues);
return (
<div
style={{
width: "90%",
height: "100%"
}}
>
<div
style={{
display: "grid",
gridTemplateColumns: "1fr 1fr 1fr 1fr 1fr 1fr",
gridTemplateRows: "1fr 1fr 1fr"
}}
>
{grid.map((num, i) => {
return (
<span
key={i}
style={{
display: "flex",
cursor: "pointer",
justifyContent: "center",
padding: "16px",
border: "1px solid black"
}}
>
<span style={{ opacity: 0 }}>{num}</span>
</span>
);
})}
</div>
</div>
);
}

How to style HTML element tags in MUI? [duplicate]

This question already has an answer here:
how to apply global backgroundColor using MuiCssBaseline and styleOverrides
(1 answer)
Closed 10 months ago.
I'm using the latest MUI version (v5) and CssBaseline from #mui/materials and given how I normally do this in CSS:
body, html, #root {
width: 100%;
height: 100%;
min-height: 100%;
margin: 0;
padding: 0;
font-size: 62.5%; /* makes 1.2rem === 12px, 1.6rem === 16px, ect... */
text-align: left;
}
MUI Body
(add the following to my theme)
components: {
MuiCssBaseline: {
styleOverrides: {
body: {
width: '100%',
height: '100%',
minHeight: '100%',
margin: 0,
padding: 0,
fontSize: '62.5%', // makes 1.2rem === 12px, 1.6rem === 16px, ect...
textAlign: 'left'
}
}
}
}
MUI Root
(add the following to my sxStyle e.g: sx={{...sxLayout, ...sxLayout.root}})
const sxLayout = {
flexDirection: 'column',
root: {
width: '100%',
height: '100%',
minHeight: '100%',
margin: 0,
padding: 0,
fontSize: '62.5%', // makes 1.2rem === 12px, 1.6rem === 16px, ect...
textAlign: 'left',
'&': {
width: '100%',
height: '100%',
minHeight: '100%',
margin: 0,
padding: 0,
fontSize: '62.5%', // makes 1.2rem === 12px, 1.6rem === 16px, ect...
textAlign: 'left'
},
'&.MuiListItemButton-root': {
width: '100%',
height: '100%',
minHeight: '100%',
margin: 0,
padding: 0,
fontSize: '62.5%', // makes 1.2rem === 12px, 1.6rem === 16px, ect...
textAlign: 'left'
}
}
}
MUI Html
(????)
????
There are a few ways to get components styled. The last one I describe is specific to MUI and is probably likeliest to be what you are looking for.
Some of the css classes belong to specific MUI components, so you just use the MUI component and if your <ThemeProvider /> is used correctly, you shouldn't have to add any classes.
You have the option of defining regular CSS or SASS in a separate file and importing it in your component, then using the class via the className property of a component.
Another option is CSS-in-JS. You can use the style property to use the custom css and you define the CSS in your JS file for the component:
... //inside your component
const mystyle = {
color: "white",
backgroundColor: "DodgerBlue",
padding: "10px",
fontFamily: "Arial"
};
...
return (
<div>
<h1 style={mystyle}>Hello Style!</h1>
<p>Add a little style!</p>
</div>
);
...
Above example adapted from https://www.w3schools.com/react/react_css.asp
The above work in react without MUI as well. The MUI specific way to create a custom style for a component is to use the makeStyles() hook. This will give you variables to use as input to the className property but when you inspect and look at the html, the classes applied will have names that are generated by MUI.
import React from 'react';
import { makeStyles } from '#material-ui/core/styles';
import Button from '#material-ui/core/Button';
const useStyles = makeStyles({
root: {
background: 'linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)',
border: 0,
borderRadius: 3,
boxShadow: '0 3px 5px 2px rgba(255, 105, 135, .3)',
color: 'white',
height: 48,
padding: '0 30px',
},
});
export default function MyComponent() {
const classes = useStyles();
return <Button className={classes.root}>Hook</Button>;
}
Above example copied from https://react.school/material-ui/styling
To learn more about the makeStyles() hook, see the MUI documentation for it: https://mui.com/system/styles/api/#makestyles-styles-options-hook
If you scroll on that same page, you can see info about other style related hooks too.

CSS Margin not working in React typescript project

CSS margin not working in my react typescript project
My code:
const Home: React.FC = () => {
return (
<div style={{ width: "100vw", background: "skyblue" }}>
<div
style={{
background: "red",
height: "100px",
width: "100px",
marginLeft: "100px",
}}
></div>
</div>
);
};
Codesandbox link for this project
Your margin is overridden by margin: 0px !important which is declared in your App.css file

Resources