Flex spacing for horizontal components in React JS - css

I have a footer which has three elements, I want to distribute them horizontally. I tried to space them horizontally using flex and space-between. Here is my react Component:
import React from "react";
export default function Footer()
{
const Element1 = "GZB Automation";
const today = new Date();
const DayFormat = today.toString().split(" ").slice(0,4);
const Element3 = DayFormat.filter(Boolean).join(" ");
console.log(Element3);
const Element2 = "Copyright © "+today.getFullYear().toString()+". All rights reserved.";
return(
<div className="FooterAligner">
<div className="FooterElement">{Element1}</div>
<div className="FooterElement">{Element2}</div>
<div className="FooterElement">{Element3}</div>
</div>
);
}
CSS of the FooterAligner and FooterElement is as shown:
.FooterAligner
{
display: flex;
justify-content: space-between;
bottom: 2%;
position:fixed;
}
.FooterElement
{
width: calc(100%/3);
}
Here is how they appear:
And this is how I want them to appear:
P.S. Ignore the font-styling, text part, and slight background variations, I just want to know about spacing.
HomePage Script (app.js):
import React from 'react';
import StarterScreen from './components/screens/StarterScreen.jsx';
import LoginScreen from './components/screens/LoginScreen.jsx';
import RegisterScreen from './components/screens/RegisterScreen.jsx';
import DisplayScreen from './components/screens/DisplayScreen.jsx';
import 'bootstrap/dist/css/bootstrap.min.css';
import Footer from './components/junk/Footer.jsx';
//import LineChart from './components/coreComponents/LineChart.js';
import ProtectedRoute from './components/security/PrivateRoute.jsx';
import './App.css';
import {BrowserRouter,Route,Switch} from 'react-router-dom';
function App(){
return (
<div className="App">
<BrowserRouter>
<Switch>
<Route component={StarterScreen} exact path="/"></Route>
<Route component={RegisterScreen} exact path="/register"></Route>
<Route component={LoginScreen} exact path="/login"></Route>
<ProtectedRoute component={DisplayScreen} exact path="/login-props-test" />
</Switch>
</BrowserRouter>
<Footer />
</div>
);
}
export default App;
For reproduction: https://codesandbox.io/s/i8r3u

Since you're using Bootstrap in your project, why not leverage the flexbox utility classes it comes with?
In this particular example I added the d-flex class to the footer container. This converts it to a flex container, so then you can add justify-content-between, which would distribute the available space between elements:
function Footer() {
const element1 = "GZB Automation";
const today = new Date();
const dayFormat = today.toString().split(" ").slice(0, 4);
const element3 = dayFormat.filter(Boolean).join(" ");
const element2 = `Copyright © ${today
.getFullYear()
.toString()}. All rights reserved.`;
return (
<footer className="d-flex justify-content-between">
<div className="p-2">{element1}</div>
<div className="p-2">{element2}</div>
<div className="p-2"> {element3} </div>
</footer>
);
}
class App extends React.Component {
render() {
return (
<div>
Rest of the app goes here..
<Footer />
</div>
);
}
}
ReactDOM.render(<App />, document.getElementById("root"));
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="root"></div>
Note: The class p-2 is a Bootstrap utility class that adds padding to each child inside the flex container

Related

Outlet component shifts my footer from visible page | React-router-dom with Tailwind CSS

Please help me to solve my problem.
When I use
h-screen
to every element in Output, it makes scrolls on a single page which I don't need. My footer is not on the bottom of visible page, it's under Outler. How to make an Outlet not to shift my footer outside the visible page?
this is my main.jsx
import ReactDOM from 'react-dom/client';
import { createBrowserRouter, Outlet, RouterProvider } from 'react-router-dom';
import Index from './pages/Index';
import './index.css';
import ErrorPage from './pages/ErrorPage';
import Personnel from './pages/Personnel';
import NewNavbar from './components/NewNavbar';
import Footer from './components/Footer';
import Contacts from './pages/Contacts';
import About from './pages/About';
const AppLayout = () => {
return (
<div className=' flex justify-between flex-col bg-gradient-to-r from-zinc-900 via-zinc-700 to-zinc-900'>
<NewNavbar />
<Outlet />
<Footer />
</div>
);
};
const router = createBrowserRouter([
{
element: <AppLayout />,
path: '/',
errorElement: <ErrorPage />,
children: [
{
index: true,
element: <Index />,
},
{
path: '/personnel',
element: <Personnel />,
},
{
path: '/contacts',
element: <Contacts />,
},
{
path: '/about',
element: <About />,
},
],
},
]);
ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
Index, About and Contacts are similar
import React from 'react';
const Index = () => {
return (
<main className='bg-red-500 '>This is main page
</main>
);
};
export default Index;
As I said before Personnel.jsx works as I expected, but not sure that is right.
import React from 'react';
import Administration from '../components/Administration';
import Teachers from '../components/Teachers';
const Personnel = () => {
return (
<div className='flex w-full flex-col'>
<div className='w-full px-[8%] lg:mx-auto lg:max-w-7xl text-white '>
<Administration />
<Teachers />
</div>
</div>
);
};
export default Personnel;
Each of these two elements has:
<div className='flex min-h-screen flex-col justify-center'>
h-screen will set an element with height: 100vh, meaning it will take the span of the entire viewport.
If I understand your problem correctly, you should set your AppLayout component with min-h-screen, so all the components inside it (NewNavbar, Outlet and Footer) will take at least the height of the viewport:
const AppLayout = () => {
return (
<div
className='min-h-screen flex justify-between flex-col bg-gradient-to-r from-zinc-900 via-zinc-700 to-zinc-900'
>
<NewNavbar />
<Outlet />
<Footer />
</div>
);
};

React sidebar took full width

so I'm trying to create a dashboard page which will show sidebar after user login, But I don't know why my sidebar is taking full width that make my Dashboard component on Routes drop into bottom.
This is the image link of the sidebar https://imgur.com/a/UnSPyaA
My code on App.js :
import Login from './Component/Login';
import Register from './Component/Register.jsx'
import Home from './Component/Home'
import {
BrowserRouter as Router,
Routes,
Route,
} from "react-router-dom";
function App() {
return (
<div className="App">
<Router>
<Routes>
<Route exact path="/" element={<Login/>}/>
<Route path="/register" element={<Register/>}/>
<Route path="/dashboard" element={<Home/>}/>
</Routes>
</Router>
</div>
);
}
export default App;
my code for Home.js:
import React from "react";
import Sidebar from './Sidebar'
import Dashboard from "./Dashboard";
import {Routes, Route} from 'react-router-dom'
import './Home.css'
function Home() {
return (
<>
<Sidebar/>
<Routes>
<Route index element={<Dashboard/>}/>
</Routes>
</>
);
}
export default Home;
The Dashboard Component is just 80 of Lorem text.
This is the code in Sidebar.js and Sidebar.css
import React from 'react'
import './Sidebar.css'
import {Link} from 'react-router-dom'
function Sidebar() {
return (
<div>
<nav className="sidebar">
<Link to="/dashboard">DashBoard</Link>
</nav>
</div>
)
}
export default Sidebar
the Sidebar.css :
.sidebar{
display:flex;
width:200px;
border: 2px solid black;
height:100vh;
background-color:#FFD302;
}
I don't know if this the problem with the CSS or the Router dom. Thanks for the help.
The Home component controls the layout of the HTML/JSX it is rendering. I suggest using a grid layout with 2 columns, one for the sidebar and the other for the descendent routes.
Example:
.home {
display: grid;
grid-template-columns: 1fr 1fr;
}
...
function Home() {
return (
<div className="home"> // <-- parent element
<Sidebar />
<Routes>
<Route index element={<Dashboard />} />
</Routes>
</div>
);
}
try to write your css in sperate file and then attach it by importing i think that is a right way to do work.

How do I override Material-UI with css modules in create-react-app and Typescript?

For example I want to add flex my h1 with class pageTitle
import AppBar from '#material-ui/core/AppBar';
const styles = require('./Header.module.css');
<div className={styles.header}>
<AppBar>
<Typography>
<h1 className={styles.pageTitle}>HELLO</h1>
</Typography>
</AppBar>
</div>
In my Header.module.css file
.pageTitle {
display: flex;
border-top: 10px;
}
Is this possible to do? Because its not changing anything on my end, again using Typescript, but following the same process as the docs that Material UI shows for css modules
Probably material-ui selectors are "stronger".
Material-ui supply several ways to override their styles.
For example, use classes. In your case:
import React from "react";
import ReactDOM from "react-dom";
import { makeStyles } from '#material-ui/core/styles';
import AppBar from "#material-ui/core/AppBar";
import Typography from "#material-ui/core/Typography";
const useStyles = makeStyles({
root: {
display: 'flex',
borderTop: '10px solid orange'
}
});
function App() {
const classes = useStyles();
return (
<AppBar classes={classes}>
<Typography>
<h1>HELLO</h1>
</Typography>
</AppBar>
);
}
ReactDOM.render(<App />, document.querySelector("#app"));
https://codesandbox.io/s/material-ui-override-styles-classes-v8nhl
BTW, <AppBar /> already has display: flex and you set borderTop in a css file which is invalid (it should be border-top) so maybe if it was valid, it was working.

How to convert CSS with properties to MaterialUI Styles in ReactJS

I have the following CSS:
[contentEditable=true]:empty:not(:focus):before{
content:attr(data-text)
}
which allows to show a placeholder inside a content-editable div when it has no content. I am using Material-UI Styles, so I need something like:
const styles = theme => ({
div[contentEditable=true]:empty:not(:focus):before: {
content:attr(data-text)
}
});
How could I achieve this? Any idea?
Thank you.
Below are a couple syntax options depending on whether you want to specify the class directly on the div (editableDiv) or on an ancestor element (container). The only difference between the two is the space after & when targeting descendants.
import React from "react";
import ReactDOM from "react-dom";
import { makeStyles } from "#material-ui/core/styles";
const useStyles = makeStyles({
container: {
"& [contentEditable=true]:empty:not(:focus):before": {
content: "attr(data-text)"
}
},
editableDiv: {
"&[contentEditable=true]:empty:not(:focus):before": {
content: "attr(data-text)"
}
}
});
function App() {
const classes = useStyles();
return (
<div>
<div className={classes.container}>
<div contentEditable data-text="Click here to edit div 1" />
<div contentEditable data-text="Click here to edit div 2" />
</div>
<div
className={classes.editableDiv}
contentEditable
data-text="Click here to edit div 3"
/>
</div>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Related documentation: https://cssinjs.org/jss-plugin-nested?v=v10.0.0#use--to-reference-selector-of-the-parent-rule

How to customize react-popper arrow

How to set arrow style the same as the popper
import React from "react";
import ReactDOM from "react-dom";
import { Manager, Reference, Popper } from "react-popper";
function App() {
return (
<Manager>
<Reference>
{({ ref }) => (
<button type="button" ref={ref}>
Reference element
</button>
)}
</Reference>
<Popper placement="right">
{({ ref, style, placement, arrowProps }) => (
<div
ref={ref}
style={style}
className={`popover show bs-popover-${"right"}`}
>
<div className="popover-inner bg-primary">Popper element</div>
<div
className="arrow bg-primary"
ref={arrowProps.ref}
style={arrowProps.style}
/>
</div>
)}
</Popper>
</Manager>
);
}
ReactDOM.render(<App />, document.getElementById("root"));
Code Sandbox: https://codesandbox.io/s/bold-shape-lomrm
First, add this CSS:
.bs-popover-auto[x-placement^=right] .arrow::after, .bs-popover-right .arrow::after {
border-right-color: #007bff;
}
Get rid of bg-primary class from the .arrow and give this style:
style="top: -2px;"
Preview
Demo: https://codesandbox.io/s/eager-wu-h337q

Resources