how change background color in different pages - reactjs

i am two page in reactjs
pageOne.js:
import React from "react";
import { Link } from "react-router-dom";
import "./pageOne.css";
const PageOne = () => {
return (
<div>
one
<br />
<Link to="/pageTwo">Two Page</Link>
</div>
);
};
export default PageOne;
pageTwo.js:
import React from "react";
import { Link } from "react-router-dom";
import "./pageTwo.css";
const PageTwo = () => {
return (
<div>
two
<br />
<Link to="/">One Page</Link>
</div>
);
};
export default PageTwo;
i am define two css files for change background color when page loaded.
pageOne.css
body {
background-color: whitesmoke !important;
}
pageTwo.css
body {
background-color: crimson !important;
}
it's problem.in pageOne background color is crimson and in pageTwo background color is crimson.
sample

As I said earlier, there is only one body tag in the DOM tree by default. So when you try to style it whatever comes last will override the previous ones and in your case, the page two style will override the page one style.
To solve this, you got several options, but I will go with the easiest one. You can make a container for each of your pages and then assign a colour to that container to make the whole page background as you desired (You can simply make a layout component then wrap each of the components within it and with similar approach make it reusable). So, for example, you can create your first page like this:
<div className="crimson">
two
<br />
<Link to="/">one Page</Link>
</div>
and style it like this:
.crimson {
background-color: crimson;
min-height: 100vh; /* minimum height of page would be equal to available view-port height */
}
This goes the same for your other page. But you need to consider you have to remove the default margins from the body itself to prevent any disorder.
Working Demo:

I would solve this with Layout component:
const Layout = ({ backgroundColor = '#fff', children }) => (
<div style={{ backgroundColor }} className="layout">
{children}
</div>
)
then remove your css(and try not to use important in your css)
<Layout backgroundColor="#fff"><PageOne /></Layout>
and
<Layout backgroundColor="#f00"><PageTwo /></Layout>

Related

How to extract a property value of a child node in a styled component and pass it as props

I am using multiple instances of the 'Icons' component. I was trying to access and use width of the 'a' tag inside the 'Icons' styled component(Here 'a' tag is inside 'Icons' component). Since the content of 'a' tag changes its width also changes. That is why I want to use the width of the 'a' tag inside each 'Icons' component.
import React from "react";
import styled from "styled-components";
import home from "../images/home-icon.svg";
import search from "../images/search-icon.svg";
const Header = () => {
return (
<Navbar>
<Icons imgUrl={home}>
<div className="special-underline"></div>
<a className="text-container" href="/">HOME</a>
</Icons>
<Icons imgUrl={search}>
<div className="special-underline"></div>
<a className="text-container" href="/">SEARCH</a>
</Icons>
</Navbar>
);
};
const Icons=styled.div`
//css styling
//here I want to access and use width of the anchor component of that Icons component
`
I tried using useRef hook and referencing the 'a' tag but it didn't work. I just could not figure out how to do it.
You can style Icons width dynamically adding width: fit-content.
I wrote a similar example to your code, replacing Icons component with a div with class Icons, but the general idea applies to styled components.
*Edit: the width of the div with class special-underline will also adjust accordingly and be the same as of the a tag. I edited the snippet below to show this.
.Icons {
width: fit-content; /* <- Here */
background-color: red;
margin: 10px;
}
.special-underline {
background-color: blue;
height: 2px;
}
<div class="Icons">
<div class="special-underline"></div>
<a>HOME</a>
</div>
<div class="Icons">
<div class="special-underline"></div>
<a>SEARCH</a>
</div>

Tailwind, Twin-macro, Styled Components className override priority

I'm creating a custom Section component using styled-components, twin-macro, tailwind css. The default styling is stored in StyledSection constant.
import tw, { styled } from "twin.macro";
const Section = (props) => {
return (
<StyledSection
className={props.className}
style={{ backgroundImage: `url(${props.backgroundImage})` }}
>
{props.children}
</StyledSection>
);
};
const StyledSection = styled.section(tw`py-20 bg-no-repeat bg-cover bg-center`);
export default Section;
However, I want to override the styling if I pass something on the component, for it to more reusable.
Example of passing override class in the Section component:
<Section className="my-override-class" />
However, the styled-components classes is being priotized. Please refer to the picture below for the render output.
Image reference for the render output

NextJS huge icon on initial page load after build project

I have some problem with icons and style were used in NextJs. I use material-ui in my project, developed time everything is ok but when i build project, the icons first be large then it gets normal. I try used other icon sets but result isn't change. Is it a NextJs bug?.
Before :
After :
This seem to help me with MUI icons
Before:
<WebAssetOffIcon sx={{maxWidth: 60, maxHeight: 60 }} />
After:
<WebAssetOffIcon style={{ maxWidth: 60, maxHeight: 60 }} />
Somone mentioned:
"The cause is the fact that the icon is being rendered before the CSS is loaded."
You can use the width property to set the icon default size, so when the CSS completely loads, they change to the icon's size.
<Icon width="16" />
add below code to your app.js/ts file it should fix your problem
import { config } from '#fortawesome/fontawesome-svg-core' // 👈
import '#fortawesome/fontawesome-svg-core/styles.css' // 👈
config.autoAddCss = false // 👈
credit goes https://github.com/FortAwesome/react-fontawesome/issues/234
Please add this code to "_document.js"
_document.js
import Document, {Html, Head, NextScript, Main} from 'next/document'
import {ServerStyleSheet} from 'styled-components';
export default class MyDocument extends Document {
static getInitialProps({renderPage}) {
const sheet = new ServerStyleSheet();
const page = renderPage(App => props => sheet.collectStyles(<App {...props}/>));
const styleTags = sheet.getStyleElement();
return {...page, styleTags};
}
render() {
return (
<Html lang="en">
<Head/>
<body>
<Main />
<NextScript/>
</body>
</Html>
)
}
}
~ Storm In Talent

Change Gatsby Link Behaviour - Stop Scroll Animation on Page Change

I'm sure this is going to be a very straight forward answer, but I can't for the life of me work out how to get my Gatsby/React site to start at the top of a new page without a scroll transition effect when I click on an internal link.
Example:
On my home page I scroll to the bottom, then click on a link to take me to an 'About Me' page. The About Me Page loads with the browser scroll position still at the bottom of the page and then the window scrolls to the top of the page. This only takes a few milliseconds but when I click on a link I want to start at the top of the page without any transition.
The links are all standard Gatsby Links:
<Link className="" to="/aboutme/">
About Me
</Link>
Thanks!
EDIT
Adding in my layout wrapper, with useScrollRestoration hooks added:
import React from 'react'
import PropTypes from 'prop-types'
import useScrollRestoration from 'gatsby'
import NavBar from './NavBar/NavBar'
import Footer from './Footer/Footer'
import './layout.scss'
const Layout = ({ children }) => {
const aboutMeScrollRestoration = useScrollRestoration(`page-component-main`)
return (
<>
<NavBar />
<main className="bumpdown" {...aboutMeScrollRestoration}>
{children}
</main>
<Footer />
</>
)
}
Layout.propTypes = {
children: PropTypes.node.isRequired,
}
export default Layout
I managed to work this out in the end after a lot of Googling. It appears not to be a scrollRestoration issue but a Bootstrap issue. In bootstrap/scss/_reboot.scss there's the following code:
:root {
font-size: $font-size-root;
#if $enable-smooth-scroll {
#media (prefers-reduced-motion: no-preference) {
scroll-behavior: smooth;
}
}
}
This can be turned off either using $enable-reduced-motion: true; or $enable-smooth-scroll: false; in bootstrap overrides which stops the scrolling behaviour when a new page opens.
I'm using the enable-smooth-scrolling:false option as the other option may have further knock on effects.
I created a custom.scss file and imported it into the gatsby-browser.js file. The contents of the scss file is overriding the default value of the scroll-behaviour in bootstrap:
$enable-smooth-scroll: false;
:root {
scroll-behavior: auto !important;
}
#import "node_modules/bootstrap/scss/bootstrap";
This issue is known as Scroll Restoration. #reach/router (from where Gatsby extends its routing) handles it automatically. However, in some cases, it fails, especially when rendering containers that have their own scroll values. To bypass this "issue", Gatsby exposes a useScrollRestoration hook to restore the scroll position. For example:
import { useScrollRestoration } from "gatsby"
import countryList from "../utils/country-list"
export default function PageComponent() {
const ulScrollRestoration = useScrollRestoration(`page-component-ul-list`)
return (
<ul style={{ height: 200, overflow: `auto` }} {...ulScrollRestoration}>
{countryList.map(country => (
<li>{country}</li>
))}
</ul>
)
}
So, in your destination component (/aboutme/ page), use the useScrollRestoration hook in your outer wrapper to restore the scroll position.

How to override prime-react component CSS styling?

I am using prime-react to style my React page. But I want a more compact website with very few padding and minimum styling. For this purpose, I want to override a few CSS properties for the prime-react components.
For eg, I am trying to reduce the padding for the MenuBar -
HomePage.js
import {React, Component } from 'react';
import { Menubar } from 'primereact/menubar';
import 'primereact/resources/themes/saga-blue/theme.css';
import 'primereact/resources/primereact.min.css';
import 'primeicons/primeicons.css';
import styled from "styled-components";
export default class HomeMenuBar extends Component {
// menu code ...
render() {
return (
<div>
<div className="card">
<Menubar model={this.items} className={this.props.className} />
</div>
</div>
);
}
}
const ComponentView = styled(HomeMenuBar)`
.p-menubar .p-menubar-root-list > .p-menuitem > .p-menuitem-link {
padding: 0.1rem 1rem !important;
}
`;
The above code makes no difference to the original styling.
I am trying to make use of this component.
However, particularly using these styled-components I don't like it. I am new to react and would like to know if there are better alternatives like, storing the CSS properties in another file and then importing it in the required file. I tried this part but it also didn't work out.
I work with react over a year and have seen lot of different ways to customise components and so far, I think that styled-components is the most convenient way to customize components if you cook them right.
I love to put all customized components with styled to a separate file near the index.js called styled.js of Component.js and Componnet.styled.js (in the separate folder of course MyComponent/index.js);
In styled.js you export all components like this:
export const Container = styled.div`
.p-menubar .p-menubar-root-list > .p-menuitem > .p-menuitem-link {
padding: 0.1rem 1rem !important;
}
`
In index.js file you inport them like this:
import {Container} from './styled'
// or import * as Styled from './styled' (if you have a lot of customized components);
export default class HomeMenuBar extends Component {
// menu code ...
render() {
return (
<Container>
<div className="card">
<Menubar model={this.items} className={this.props.className} />
</div>
</Container>
);
}
}
If you want to try something more like classic css try to look at css-modules.
This article can help https://www.triplet.fi/blog/practical-guide-to-react-and-css-modules/
You can also try patch-styles, a more declarative way to apply CSS/SCSS modules to your code. Also, check out the StackBlitz example.

Resources