Converting regular bootstrap code into React Bootstrap styled component - reactjs

I am following a tutorial where a person is teaching me how to write Bootstrap code. They created a top bar that goes across the entire browser width with the following code
CSS
.top-bar {
background: #7aaec4;
height: 2.8rem;
padding: .5rem 0;
}
.topbar a {
color: #fff;
text-decoration: none;
font-size: 1.1rem;
}
HTML Code
<div class="top-bar">
<div class="container">
<div class="col-12 text-right">
<p>
Call us at +1 (888) 555-5555
</p>
</div>
</div>
</div>
The code above created the top bar which runs across the browser width and has a specific height as well.
Now I am trying to get the same behavior but with react-bootstrap and styled-components. So I wrote this code
import React from 'react';
import {Container, Row, Col} from 'react-bootstrap';
import styled from 'styled-components';
const Styles = styled.div`
background: #7aaec4;
height: 2.8rem;
padding: .5rem 0;
text-align: right;
a {
color: #fff;
text-decoration: none;
font-size: 1.1rem;
};
`;
export const TopBar = () => {return (
<Container>
<Styles>
<Row>
<Col xs={12} sm={12} md={12} lg={12} xl={12}>
Call us at +1 (888) 555-5555
</Col>
</Row>
</Styles>
</Container>
)}
And then finally use my component as
<React.Fragment>
<GlobalStyles />
<TopBar />
<div>Hello World</div>
</React.Fragment>
I see that the topbar is not running across the browser screen. it looks like this

Related

Centering containers with React Bootstrap

I have an old react website that I'm starting to work on again. I made no changes to the code except updating packages to their current versions. npm start starts up with no issues. The problem is, nothing is centered the way it is supposed to be.
My assumption is that bootstrap updated and containers are handled differently. Below is my home.js
import React from 'react'
import Header from '../Header/Header';
import Footer from "../Footer";
import MachineLearningCard from './Card/Card';
import HowWeUse from './HowWeUse/HowWeUse';
import './home.css';
import { useNavigate } from 'react-router-dom';
import { Container, Row, Col, Image, Button } from 'react-bootstrap';
import { Hidden } from '#material-ui/core';
const Home = () => {
const history = useNavigate()
return (
<div>
<header className="home__HeaderContainer">
<Header />
<Container fluid>
<Row>
<Col md={7} sm={12}>
<div className="home__leftSideContainer">
<h1 className="home__headerHeroTextTitle">
RandomText <span className="home__headerHeroTextSpan">RandomText</span> Some more RandomText
</h1>
<p className="home__headerHeroTextDescription">
Even more RandomText.
</p>
<Button
bsPrefix="home__getStarted"
onClick={() => history("/register")}
>Get Started</Button>
</div>
</Col>
<Hidden xsDown>
<Col md={5}>
<Image
src={HeaderHeroImage}
alt="home-hero-image"
fluid
style={{ width: 600, marginTop: 50 }}
/>
</Col>
</Hidden>
</Row>
</Container>
<Hidden smUp>
<img
src={HomeChevron}
alt="Chevron Down"
className="header__chavron_down"
/>
</Hidden>
</header>
className="home__headerHeroTextTitle and className="home__headerHeroTextDescription" are centered. However, the home-hero-image should be the the right of the RandomText, but it is below and to the left of it. Below is the css
.home__HeaderContainer {
background-image: url("../../assets/images/home-bg.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
min-height: 800px;
}
.home__HeaderContainer {
background-image: url("../../assets/images/Hero bg-mobile.png");
background-position: center;
background-repeat: no-repeat;
background-size: cover;
border-radius: 0px 0px 0px 100px;
min-height: 500px;
}
I tried researching what changed in bootstrap but I could not find anything. The only logical explanation I can come up with is containers are handled different because everything is not centered.
I had to do the following and it fixed my issue
npm i bootstrap
and
add import 'bootstrap/dist/css/bootstrap.min.css' to index.js

Gatsby: How to insert data from frontmatter (Mdx file) into a component

I have a series of .mdx files that contain the same information but on different sites (ex: title="aswan"; title="rome" etc). These files are located in a folder called "metadata".
I need to insert mdx information into a component (OurSites.js) that goes to form the HomePage of the site (index.js).
This is the content of the OurSites.js component:
import React from "react";
import styled from "styled-components";
import Card from "react-bootstrap/Card";
import { Container, Row, Col } from "react-bootstrap";
import Button from "react-bootstrap/Button";
import TitleR from "../Title/TitleR";
import Card1 from "../../metadata/1-home-page/images/1-card.jpg";
import Card2 from "../../metadata/1-home-page/images/2-card.jpg";
const Sites = () => {
return (
<Wrapper>
<section className="section bottom-slant-gray">
<Container>
<TitleR title="our sites" />
<Row xs={1} md={2}>
<Col>
<Card>
<Card.Img variant="top" fluid={true} src={Card1} />
<Card.Body>
<Card.Title>First Step</Card.Title>
<Button variant="primary">
<a href="https://docs.paths-erc.eu/data/">
go to paths repository
</a>
</Button>
<Card.Text>
Collection and georeferencing on GIS platform of most of the
historical cartography of Egypt and plans of the main
temple/church/basilica complexes
</Card.Text>
</Card.Body>
</Card>
</Col>
<Col>
<Card>
<Card.Img variant="top" src={Card2} />
<Card.Body>
<Card.Title>Second Step</Card.Title>
<Button variant="primary">
<a href="https://docs.paths-erc.eu/data/svp">
go to spv protocol
</a>
</Button>
<Card.Text>
Vectorization of geo-referenced plants on GIS platform using
a protocol (SPV) developed by PAThs team members
</Card.Text>
</Card.Body>
</Card>
</Col>
</Row>
<Row xs={1} md={2}>
<Col>
<Card>
<Card.Img variant="top" fluid={true} src={Card1} />
<Card.Body>
<Card.Title>First Step</Card.Title>
<Button variant="primary">
<a href="https://docs.paths-erc.eu/data/">
go to paths repository
</a>
</Button>
<Card.Text>
Collection and georeferencing on GIS platform of most of the
historical cartography of Egypt and plans of the main
temple/church/basilica complexes
</Card.Text>
</Card.Body>
</Card>
</Col>
<Col>
<Card>
<Card.Img variant="top" src={Card2} />
<Card.Body>
<Card.Title>Second Step</Card.Title>
<Button variant="primary">
<a href="https://docs.paths-erc.eu/data/svp">
go to spv protocol
</a>
</Button>
<Card.Text>
Vectorization of geo-referenced plants on GIS platform using
a protocol (SPV) developed by PAThs team members
</Card.Text>
</Card.Body>
</Card>
</Col>
</Row>
<Row xs={1} md={2}>
<Col>
<Card>
<Card.Img variant="top" fluid={true} src={Card1} />
<Card.Body>
<Card.Title>First Step</Card.Title>
<Button variant="primary">
<a href="https://docs.paths-erc.eu/data/">
go to paths repository
</a>
</Button>
<Card.Text>
Collection and georeferencing on GIS platform of most of the
historical cartography of Egypt and plans of the main
temple/church/basilica complexes
</Card.Text>
</Card.Body>
</Card>
</Col>
<Col>
<Card>
<Card.Img variant="top" src={Card2} />
<Card.Body>
<Card.Title>Second Step</Card.Title>
<Button variant="primary">
<a href="https://docs.paths-erc.eu/data/svp">
go to spv protocol
</a>
</Button>
<Card.Text>
Vectorization of geo-referenced plants on GIS platform using
a protocol (SPV) developed by PAThs team members
</Card.Text>
</Card.Body>
</Card>
</Col>
</Row>
</Container>
</section>
</Wrapper>
);
};
const Wrapper = styled.section`
a {
color: rgb(130, 36, 51);
font-family: "Raleway", sans-serif;
font-weight: bolder;
font-size: 0.8rem;
line-height: 1rem;
text-transform: uppercase;
display: swap;
text-decoration: none;
}
a:hover {
color: rgb(130, 36, 51);
font-family: "Raleway", sans-serif;
font-weight: bolder;
font-size: 0.8rem;
line-height: 1rem;
text-transform: uppercase;
display: swap;
text-decoration: none;
}
button {
text-align: right;
margin: 3% 0 3% 0;
background: rgb(130, 36, 51, 0.2);
border-color: rgb(130, 36, 51, 0.3);
}
button:hover {
background: none;
border-color: transparent;
}
section {
padding: 4em 5;
position: relative;
z-index: 2;
}
.card {
border: none;
}
.card-body {
text-align: right;
font-family: "Raleway", sans-serif;
font-size: 1rem;
line-height: 1.7rem;
display: swap;
}
.card-title {
font-weight: bolder;
text-transform: uppercase;
}
.container {
padding-bottom: 5%;
}
section {
padding: 0;
position: relative;
z-index: 2;
}
.bottom-slant-gray {
position: relative;
padding-bottom: 10%;
background-color: #f8f9fa;
}
.bottom-slant-gray:before {
content: "";
width: 100%;
height: 240px;
background: #f8f9fa;
z-index: -1;
top: 0px;
background-color: #f8f9fa;
left: 0;
position: absolute;
-webkit-transform: skewY(-6deg);
-ms-transform: skewY(-6deg);
transform: skewY(-6deg);
-webkit-transform-origin: top left;
-ms-transform-origin: top left;
transform-origin: top left;
}
.row {
padding-top: 3%;
}
.sites-img {
border-top-left-radius: var(--radius);
border-top-right-radius: var(--radius);
height: 19rem;
z-index: 1;
}
.sites-img::after {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: linear-gradient(to bottom right, var(--clr-primary-5), #222);
opacity: 0.85;
transition: var(--transition);
}
`;
export default Sites;
Where there is text, such as in CardText, I would like to insert the contents of mdx files.
Querying the data on Graphql gave this result:
export const query = graphql`
{
allMdx(limit: 8, sort: { fields: frontmatter___title, order: ASC }) {
nodes {
id
frontmatter {
title
status
typology
image {
childImageSharp {
gatsbyImageData
}
}
}
excerpt
}
}
}
`;
This is the content of the index.js page inside which is present also the OurSite.js component:
import * as React from "react";
import Layout from "../components/Layout";
import Slider from "../components/Slider";
import About from "../components/Home/About";
import Methods from "../components/Home/Methods";
import OurSites from "../components/Home/OurSites";
import { graphql } from "gatsby";
// markup
const IndexPage = () => {
return (
<Layout>
<Slider />
<About />
<Methods />
<OurSites />
</Layout>
);
};
export const query = graphql`
{
allMdx(limit: 8, sort: { fields: frontmatter___title, order: ASC }) {
nodes {
id
frontmatter {
title
status
typology
image {
childImageSharp {
gatsbyImageData
}
}
}
excerpt
}
}
}
`;
export default IndexPage;
This is what the component looks like:
You have two options:
Using page queries
Using static queries (useStaticQuery hook)
With the first option, your queries must go on a page (hence the name) and you need to drill down the props containing your data to each desired component. Because of you already have the query set, you just need to:
import * as React from "react";
import Layout from "../components/Layout";
import Slider from "../components/Slider";
import About from "../components/Home/About";
import Methods from "../components/Home/Methods";
import OurSites from "../components/Home/OurSites";
import { graphql } from "gatsby";
// markup
const IndexPage = ({ data }) => {
console.log("your props.data is inside data: " data);
return (
<Layout>
<Slider />
<About />
<Methods />
<OurSites sites={data.allMdx.nodes}/>
</Layout>
);
};
export const query = graphql`
{
allMdx(limit: 8, sort: { fields: frontmatter___title, order: ASC }) {
nodes {
id
frontmatter {
title
status
typology
image {
childImageSharp {
gatsbyImageData
}
}
}
excerpt
}
}
}
`;
export default IndexPage;
Your queried data (via GraphQL) is inside props.data (destructured as data). You can drill down the site's information by data.allMdx.nodes (which is an array). So in your OurSites component, you only need to get the props (props.sites) and loop through them to display dynamically your data.
With the useStaticQuery the approach is exactly the same but the data can be stores somewhere else (in a custom hook or in the inner OurSites component).

React Not Loading Image when running code

If someone can let me know if I'm using an improper format in order to load an image on react that would be amazing! I'm not an expert and any advice is greatly appreciated -- thank you in advance. The image that populates when I run the code just shows a broken img icon and when I try to run through the terminal it says that the image is called but not loaded.
import React, { Component } from "react";
import PropTypes from "prop-types";
import Logo from "/Users/mauromartineziii/aqua-financial/src/logo.png";
import "./App.css";
import Photo from "/Users/mauromartineziii/aqua-financial/src/pexels-photo-3560168.jpeg";
class App extends Component {
render() {
return (
<div>
<section>
<header>
<img
src="/Users/mauromartineziii/aqua-financial/src/logo.png"
width="333"
alt="Aqua Financial"
/>
<div>
<ul>
<li>Financial Services</li>
<li>Liquid Capital Asset</li>
<li>Retirement</li>
</ul>
</div>
</header>
</section>
<section id="main">
<img src="Photo"></img>
<div className="main-text">
<span>Learn What An LCA™ Can </span> <br /> Do For You <br />
</div>
<div className="right-text">
<span>Innovation Driven Banking</span>
</div>
<div>
{" "}
<button>Learn More</button>
</div>
</section>
</div>
);
}
}
export default App;
body {
margin: 0;
padding: 0;
font-family: "Poppins", sans-serif;
}
section {
margin: 0 120px;
}
header {
display: grid;
grid-template-columns: 1fr 1fr;
margin-top: 50px;
}
header div {
margin-left: auto;
font-size: 20px;
}
header ul li {
display: inline-block;
margin: 0 20px;
}
#main {
margin: 0 120px;
margin-top: 200px;
display: grid;
grid-template-columns: 1fr 1fr;
}
#main .main-text {
font-size: 45px;
font-weight: 700;
line-height: 1.5;
}
`
It's a good idea to place the images you use in your project in the project directory and import them with a relative path, e.g:
import Logo from "./logo.png";
You also want to use the the resulting variables for your img tags.
<img
src={Logo}
width="333"
alt="Aqua Financial"
/>
// ...
<img src={Photo} />

Gap in right margin of page won't go away

I'm building a new project using reactJS, nodeJS, and react-bootstrap. When setting up my landing page. There is a gap in the margin on the right side of the page that won't go away, no matter what I try.
.I've tried including the meyer's reset css file in the index.html.
.I've tried setting margins, borders, and padding on the Body element to zero.
.I've tried setting my Container to width: 100%.
.I've tried resizing the background image.
.I've tried including the bootstrap 4 cdn in my index.html.
App.js
import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import LandingPage from './components/landingpage';
import Header from './components/header';
import Footer from './components/footer';
import './App.css';
class App extends Component {
render() {
return (
<Router>
<Header/>
<Switch>
<Route exact path='/' component={LandingPage}/>
</Switch>
<Footer/>
</Router>
);
}
}
export default App;
App.css
/* {
box-sizing: border-box;
}*/
body {
background-image: url('./images/background.jpg');
background-repeat: no-repeat;
background-position: center center;
/*background-size: cover;*/
font-family: 'Courgette', cursive;
color: beige;
font-size: 28px;
text-align: center;
max-width: 100%;
margin: 0;
border: 0;
padding: 0;
}
.container {
margin: 0;
padding: 0;
width: 100%;
}
.courgette {
font-family: 'Courgette', cursive;
}
.lobster {
font-family: 'Lobster', cursive;
}
header.js
import React from "react";
import './header.css';
import { Container, Row, Col } from 'react-bootstrap';
import { Link } from "react-router-dom";
const Header = () => {
return(
<Container className='header'>
<Row>
<Col>
<Link href="#">
<img className='hamburgermenu' src={require('./../../images/navbaricon.png')} alt='Hamburger Menu Icon'/>
</Link>
</Col>
<Col>
<h1>MDNight</h1>
</Col>
<Col>
<p>Hello!</p>
</Col>
</Row>
</Container>
)
}
export default Header;
header.css
.header{
background-color: maroon;
margin: 0;
}
.hamburgermenu {
height: 80px;
width: auto;
}
The header is supposed to extend to the edge of the page, but it does not.
There is any margin gap in the application.

How to create Accordion using Card Component

I am using react-bootstrap 1.0.0-beta.3, which is build for supporting bootstrap 4 update.
Before this I was using react-bootstrap 0.32.1 and created Accordion using Panels and Panel group.
But after bootstrap upgrade it was suggested to Card component. I tried to achieve the same behavior like this:
<CardGroup>
<Card eventKey={this.state.eventKey} className="border-0">
<Card.Header>
<div className="row">
<div className="col-xs-9 col-sm-9 col-md-9 col-lg-9">
<Card.Title>
This is test
</Card.Title>
</div>
<div className="col-xs-3 col-sm-3 col-md-3 col-lg-3">
Test Text 123
</div>
</div>
</Card.Header>
<Card.Body>
Test Text 456
</Card.Body>
</Card>
</CardGroup>
I am facing couple of issues here:
How to make one card to take the full width.
How to make this structure behave like accordion.
Something like this:
You'll need to create custom components and css classNames.
Working example: https://codesandbox.io/s/8zkrw9jw50
components/Accordian.js
import React from "react";
import Card from "../../components/Card";
const panels = [
"Add Edit Menus",
"Resource Management",
"Asset Management",
"User Management",
"Account Management"
];
export default () => (
<div className="app-container">
<div className="accordion-container">
{panels.map(title => (
<Card key={title} title={title} />
))}
</div>
</div>
);
components/Card.js
import React, { Component } from "react";
import PropTypes from "prop-types";
import { Row, Col, Card } from "react-bootstrap";
import Collapse from "../Collapse";
import Button from "../Button";
const { Body, Header, Title } = Card;
class CardComponent extends Component {
state = { isActive: false };
toggleVisibility = () =>
this.setState(prevState => ({ isActive: !this.state.isActive }));
render = () => (
<div className={`${this.state.isActive ? "active" : "inactive"}`}>
<Card>
<Header style={{ padding: 0 }}>
<Row>
<Col xs={9}>
<Button onClick={this.toggleVisibility}>
{!this.state.isActive ? "+" : "-"}
</Button>
<Title style={{ display: "inline-block" }}>
{this.props.title}
</Title>
</Col>
<Col style={{ paddingTop: 7 }} xs={3}>
Test Text 123
</Col>
</Row>
</Header>
<Collapse>
<Body style={{ padding: 10 }}>Test Text 456</Body>
</Collapse>
</Card>
</div>
);
}
export default CardComponent;
CardComponent.propTypes = {
title: PropTypes.string.isRequired
};
components/Button.js
import styled from "styled-components";
const StyledButton = styled.button`
color: #909090;
background-color: transparent;
font-weight: bold;
outline: none;
border: 0;
cursor: pointer;
font-size: 22px;
transition: all 0.3s ease-in-out;
margin: 0 15px;
width: 25px;
&:hover {
color: #333333;
}
&:focus {
outline: none;
}
`;
export default StyledButton;
components/Collapse.js
import React from "react";
import PropTypes from "prop-types";
const Collapse = ({ children }) => (
<span className="folding-pannel">{children}</span>
);
export default Collapse;
Collapse.propTypes = {
children: PropTypes.node.isRequired
};
styles.css
.accordion-container {
width: 100%;
}
.app-container {
margin: 20px;
}
.active,
.inactive {
margin-bottom: 5px;
}
.active .folding-pannel {
transition: all 0.3s ease-in-out;
height: 42px;
}
.inactive .folding-pannel {
transform: perspective(0px) rotateX(90deg);
transition: all 0.3s ease-in-out;
height: 0;
}

Resources