React sidebar took full width - css

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.

Related

Why are my styles not getting applied to elements in nextjs?

I have added the following classes to elements as follows:
import Link from "next/link";
import React from "react";
const Page = () => {
return (
<div>
<h1 className="notes-header">Index Page</h1>
<Link href="./notes">
<a className="notes-home">Notes</a>
</Link>
</div>
);
};
export default Page;
Next, I added styling to my element as follows
.notes-header {
font-size: 100px;
}
.notes-home {
font-size: 28px;
}
Lastly, I created a _app.js file and imported the global css file as shown below:
import React from "react";
import "../styles/globals.css";
export default function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />;
}
The problem is my styles don't get applied to my selected elements. What I'm I getting wrong?

React / CSS : Nightmode button that dosnt changes my navbar background color

I'm new on React. I just added a theme color to my page.
Everything goes from black to white and vice versa when I click my button "Change Theme".
But .. the navbar that was initially lightblue colored, isnt changing !
That navbar is the only element that isnt changed by my button " Change Theme ".
I dont quite clearly understand, because the body color was supposed to be changed.
Im really bad at CSS.
App.js
import './App.css';
import React, { useState } from 'react';
import {BrowserRouter as Router, Switch, Route, Link} from 'react-router-dom';
import About from './Pages/About';
import Home from './Pages/Home';
import Works from './Pages/Works';
import PageNotFound from './Pages/PageNotFound';
import StudyCase from './Pages/StudyCase';
import styled, { ThemeProvider } from "styled-components";
import { lightTheme, darkTheme, GlobalStyles } from './Components/Themes';
const StyledApp = styled.div`
color: ${props => props.theme.fontColor}
`;
function App() {
const [theme, setTheme] = useState('light')
const themeToggler = () => {
theme === 'light' ? setTheme("dark") : setTheme("light");
}
return (
<ThemeProvider theme={theme === 'light' ? lightTheme : darkTheme} >
<GlobalStyles />
<StyledApp>
<Router>
// NAVBAR
<div style={{width: 100 + "vw", height: 80, backgroundColor: "lightblue"}}>
<button onClick={() => themeToggler()}>Change Theme</button>
<Link to="/" style={{margin: 50}}>Home</Link>
<Link to="/works" style={{margin: 50}}>Projets</Link>
<Link to="/about" >L'agence</Link>
</div>
// END OF NAVBAR
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" exact component={About} />
<Route path="/works" exact component={Works} />
<Route path="/works/:name-study-case" exact component={StudyCase} />
<Route path="*" exact component={PageNotFound} />
</Switch>
</Router>
</StyledApp>
</ThemeProvider>
)
}
export default App;
Themes.js
import {createGlobalStyle} from 'styled-components';
export const lightTheme = {
body: "#fff",
fontColor: "#000"
}
export const darkTheme = {
body: "#000",
fontColor: "#fff"
}
export const GlobalStyles = createGlobalStyle`
body {
background-color: ${props => props.theme.body}
}
`
You set the styling "manually" here:
style={{width: 100 + "vw", height: 80, backgroundColor: "lightblue"}
That will always override your theme stylesheet due to CSS precedence rules. Get rid of that and it should all work fine.
Is Pass Object in style?
let styles = {
backgoungColor: "red;
}
return(<div style={styles}></div>);

Flex spacing for horizontal components in React JS

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

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.

activeClassName in react router is highlighting two NavLinks at the same time

I'm learning React Router and following this tutorial
Everything is clear and working as instructed (thank you author) except an issue that highlights the active NavLink.
Take a look at the css file and note the .current class:
nav ul {
list-style: none;
display: flex;
background-color: black;
margin-bottom: 20px;
}
nav ul li {
padding: 20px;
}
nav ul li a {
color: white;
text-decoration: none;
}
.current {
border-bottom: 4px solid white;
}
When I load the app, Home link in the navi is highlighted with a the white bottom border line but it stays highlighted even when I click on the "about" link or the "Contact" link. I followed everything step by step but this one issue I can't figure out.
After reading the react documentaton about the NavLink and many tutorials online, still, don't know how to fix it.
Here is when I click on the about link
Here is when I click on Contact link
This is how the tutorial routes example
<nav>
<ul>
<li><NavLink exact activeClassName="current" to='/'>Home</NavLink></li>
<li><NavLink exact activeClassName="current" to='/about'>About</NavLink></li>
<li><NavLink exact activeClassName="current" to='/contact'>Contact</NavLink></li>
</ul>
</nav>
Notice how the Home link stays with the active white line under it despite the fact that it is not the active link anymore.
Adding activeClassName="current" suppose to highlight the current view with white underline but the home link stays highlighted with the white underline.
So, to all of you out there with years of experience, please help.
Here is My app.js
import React, { Component } from 'react';
import './App.css';
import Main from './components/main';
import Navigation from './components/navigation';
class App extends Component {
render() {
return (
<div>
<h1> React Router </h1>;
<h5> import 'BrowserRouter' from 'react-router-dom' in Appjs</h5>
<hr></hr>
<Navigation />
<Main />
</div>
);
}
}
export default App;
Here is my index.js
import React from 'react';
import ReactDOM from 'react-dom';
//Add browserrouter to use it in your app
import { BrowserRouter } from 'react-router-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
//this router example is from this site
//https://blog.pusher.com/getting-started-with-react-router-v4/
/*
You can only pass a single child element to a Router,
so it is necessary to create a root component
that renders the rest of your application and
then pass that in as the child of the Router.
*/
ReactDOM.render((
<BrowserRouter>
<App />
</BrowserRouter>
), document.getElementById('root'));
Here is my main.js
import React, { Component } from 'react';
import { NavLink, Switch, Route } from 'react-router-dom';
import Home from './home';
import About from './about'
import Contact from './contact'
class Main extends Component {
render() {
return (
<div>
<p> My main component </p>
<Switch>
<Route exact={true} path='/' component={Home}></Route>
<Route path='/about' component={About}></Route>
<Route path='/contact' component={Contact}></Route>
</Switch>
</div>
);
}
}
export default Main;
And finally here is my navigation.js
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<p> My Navigation component </p>
<nav>
<ul>
<li><NavLink to='/'> Home </NavLink> </li>
<li><NavLink to='/about'> About </NavLink> </li>
<li><NavLink to='/contact'> Contact </NavLink> </li>
</ul>
</nav>
</div>
);
}
}
export default Navigation;
The Navlink also needs an exact on it... Change to this:
import React, { Component } from 'react';
import { NavLink } from 'react-router-dom';
class Navigation extends Component {
render() {
return (
<div>
<p> My Navigation component </p>
<nav>
<ul>
<li><NavLink to='/' activeClassName="current" exact={true}> Home </NavLink> </li>
<li><NavLink to='/about' activeClassName="current"> About </NavLink> </li>
<li><NavLink to='/contact' activeClassName="current"> Contact </NavLink> </li>
</ul>
</nav>
</div>
);
}
}
export default Navigation;
You should add the exact property to your root("") Route, otherwise, it will match every route starting with "", React Router exact
eg:
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
Update:
Also changing the order will have same effect without exact property
<Route path="/about" component={About} />
<Route path="/topics" component={Topics} />
<Route path="/" component={Home} />
in side a router switch it will go to the first matching route, if we add a route with path / at the beginning it will always render the component in the path, it is better avoid exact fix with order
for v6, use end instead of exact
<NavLink end activeClassName="activeNavLink" to="/">
Remove activeClassName and activeStyle props from <NavLink />
As of v6.0.0-beta.3, the activeClassName and activeStyle props have been removed from NavLinkProps. Instead, you can pass a function to either style or className that will allow you to customize the inline styling or the class string based on the component's active state.
className
<NavLink
to="/"
className={({ isActive }) => "nav-link" + (isActive ? " activated" : "")}
>
Home
</NavLink>
style
<NavLink
to="/"
style={({ isActive }) => ({ color: isActive ? 'green' : 'blue' })}
>
Home
</NavLink>
source
exact prop has to be provided to the NavLink component.
It works something like this,
Home
Pictures
When the user clicks on Home, url='/me', it loads home component, when the user clicks on Pictures, url='/me/pictures', if exact is not provided in the NavLink, then both url contains '/me', which makes both of them active.
Its the usecase for exact prop of NavLinks.
You can use exact and strict asper your need in Navlinks.

Resources