onClick() not working on several viewports - css

I am trying to make a portfolio for my student organization and am using the package react-flip-card for profiles of the team members in that organization. Interestingly enough, when I viewed in on my iPad Pro, on landscape view (width > 1024px), I was able to trigger the flip animation by pressing the button. But when I was on portrait view (width: 1024px), suddenly the flip card will not trigger.
You can access my website and try it:
https://ibsa-display-sub.herokuapp.com/about
Here is my code:
PeopleCard.js
import React, {Component} from 'react'
import styled from 'styled-components'
import {mediaQueries, colors} from '../../shared/config'
import ReactCardFlip from 'react-card-flip'
import overlay from '../../images/overlay.png'
// width change for officers in about us
const FrontContainer = styled.div`
box-shadow: 15px 15px 5px;
background-color: white;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
${mediaQueries.tablet} {
box-shadow: 7.5px 7.5px 3.725px;
}
`
const BackContainer = styled.div`
box-shadow: 10px 10px 5px;
border-radius:1em;
height: ${props => `${props.cardHeight}px`};
background-image: url(${props => props.overlay}), url(${props => props.imgURL});
background-position: center top;
background-size: cover;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
${mediaQueries.tablet} {
box-shadow: 7.5px 7.5px 3.725px;
}
`
const Image = styled.img`
/* border-top-left-radius: 1em;
border-top-right-radius: 1em;
border-bottom: 1px solid black; */
`;
const Description = styled.div`
text-align: center;
margin: 1em 0.5em;
`;
const Name = styled.h2`
font-weight: 800;
font-size: 2em;
margin-bottom: 0;
`;
const Title = styled.h4`
font-weight: 300;
`
const Button = styled.button`
border-radius:1em;
margin-bottom: ${props => props.side === "front" ? "1em" : "0"};
margin-top: ${props => props.side === "back" ? "1em" : "0"};
cursor: pointer;
height: 3em;
background-color: ${colors.blue};
${mediaQueries.iphone7} {
padding: 0.5em 2em;
}
`;
const Summary = styled.p`
margin: 1em 2.5em;
font-weight: 400;
text-align: justify;
color: ${colors.white};
font-weight: 400;
`;
export default class PeopleCard extends Component {
constructor() {
super();
this.imgref = React.createRef();
this.frontContainer = React.createRef();;
this.state = {
isFlipped: false,
cardHeight : 0,
};
this.handleClick = this.handleClick.bind(this);
this.updateHeight = this.updateHeight.bind(this);
}
/**
* handles the flip card animation for people card
*/
handleClick(e) {
e.preventDefault();
this.setState(prevState => ({ isFlipped: !prevState.isFlipped }));
}
componentDidMount() {
this.updateHeight();
window.addEventListener("resize", this.updateHeight);
}
componentWillUnmount() {
window.removeEventListener("resize", this.updateHeight);
}
/**
* updateHeight() updates the height of the container for the front side
* of the car whenever it's changed. The height is then used for BackContainer
* to have reference to (passed in as a prop), so that size is consistent
*/
updateHeight() {
this.setState({ cardHeight: this.frontContainer.current.clientHeight })
}
render() {
const {imgURL, name, title, bio, instalink, lilink} = this.props;
const {cardHeight} = this.state;
return (
<ReactCardFlip
className="card-container"
isFlipped={this.state.isFlipped}
flipDirection="horizontal"
infinite="true"
>
<FrontContainer ref={this.frontContainer}
id="get-height">
<Image
src={imgURL}
ref={this.imgref}
onLoad={() => {
if (cardHeight < 400) {
this.setState({
cardHeight: cardHeight + this.imgref.current.clientHeight
})
}
}}
className="img-fluid"
/>
<Description >
<Name className="heading"> {name} </Name>
<Title> {title} </Title>
</Description>
<Button side="front" onClick={this.handleClick}> Learn more </Button>
</FrontContainer>
<BackContainer
cardHeight={cardHeight}
overlay={overlay}
imgURL={imgURL}
>
<Summary>
{bio}
</Summary>
<Button side="back" onClick={this.handleClick}>Back</Button>
</BackContainer>
</ReactCardFlip>
)
}
}
config.js
export const colors = {
blue : "#1F8AD0",
white: "#ffffff",
lightblue: "#A0CAEA",
}
export const mediaQueries = {
iphone7: "#media screen and (max-width: 480px)",
notIphone7: "#media screen and (min-width: 481px)",
beforeMobile : "#media screen and (min-width: 481px) and (max-width: 700px)",
mobile: "#media screen and (max-width: 700px)",
notMobile: "#media screen and (min-width: 701px)",
notTablet: "#media screen and (min-width: 1024px)",
tablet: "#media screen and (max-width: 1024px)",
};
EDIT: SOLVED! it turns out it has something to do with a z-index that made the buttons and forms unclickable.

Related

Styled component not being respected between files?

For whatever reason, the buttons (defined by AboutItem) are displaying light blue backgrounds when I hover over them. I want to make it #282828.
In a separate file I define some styled components for Buttons:
export const Button = styled.button`
display: inline-block;
font-size: 1em;
margin: 1em;
padding: 0em 1em;
`;
export const InfoButton = styled(Button)`
color: grey;
&:hover {
background-color: lightblue;
}
`;
which I then use:
interface AboutNavProps {
selected: boolean;
}
const AboutItem = styled(InfoButton) <AboutNavProps>`
color: grey;
background-color: ${props => props.selected ? "#282828" : "transparent"};
text-align: center;
&:hover {
background-color: #282828;
}
`;
export function AboutNavbar() {
const router = useRouter()
useEffect(() => {
router.prefetch("/method");
}, [])
return (
<AboutNav aria-label="Navbar">
<AboutItem as="a" href="method" selected={router.pathname == "/method"}>How It Works</AboutItem>
<AboutItem as="a" href="about" selected={router.pathname == "/about"}>About</AboutItem>
<Logo />
</AboutNav>
);
}
Is there a reason for this? I don't understand why the light blue overrides the color I define for the hover.

How to change CSS of a child component from parent component

I have a react component called TeamBanner like so
import styles from "./teamBanner.module.css";
import { useNavigate } from "react-router-dom";
import { TeamBannerProps } from "../ProjectTypes.types";
export const TeamBanner = ({
team,
onBannerClick,
showName,
}: TeamBannerProps) => {
let navigate = useNavigate();
const handleOnClick = () => {
navigate(`/${team}`);
onBannerClick();
};
return (
<div
className={`${styles.container} flex theme-light ${
showName ? "" : styles["no-name"]
}`}
title={team}
onClick={handleOnClick}
>
<div>
<img
src={require(`../logos/${team}.svg`)}
alt="logo"
className={`${styles.logo} ${showName ? "" : styles["only-logo"]}`}
/>
</div>
{showName ? team : ""}
</div>
);
};
The teamBanner.module.css file is like so
.container {
margin: 0.25em;
align-items: center;
cursor: pointer;
top: 0;
z-index: 2;
background-color: hsl(var(--color-background));
padding: 0.1em 0.25em;
border-radius: 0.5em;
box-shadow: 0.05rem 0.05rem 0.2rem rgb(0 0 0 / 50%);
font-family: var(--ff-serif);
font-size: var(--fs-200);
}
.logo {
display: inline-block;
vertical-align: middle;
width: 3vmax;
height: 3vmax;
}
.container:hover {
outline: 0.15em solid hsl(240, 90%, 67%);
}
.no-name {
border-radius: 0.25em;
}
.only-logo {
width: 5vmax;
height: 5vmax;
}
I am using it to create a list of team of a page & this works just fine.
However, I want to use it in another place also. In the place, the to now want the .container:hover effect & I want the font properties of the .contaner and width & height properties of the .logo changed.
How can I accomplish this? only changing these items of the module.css? Can any one help? Thanks

How to center Items in react

Im crating a navbar for a cite and currently formatting the page Im pretty new to react so im trying to start with the basics. I am stuck on how to center my NavBarLinks, I have used text-center and position to make the links be in the center of the navbar but if I make the web browser smaller it wont stay in the center. My question is what is the right to center items in react.
import React, { useState } from "react";
import {
NavbarContainer,
TopContainer,
BottomContainer,
NavbarExtendedContainer,
NavbarInnerContainer,
NavbarLinkContainer,
NavbarLink,
Logo,
OpenLinksButton,
NavbarLinkExtended,
} from "../styles/Navbar.style";
import LogoImg from "../assets/logo.png";
function Navbar() {
const [extendNavbar, setExtendNavbar] = useState(false);
return (
<NavbarContainer extendNavbar={extendNavbar}>
<NavbarInnerContainer>
<TopContainer>
<NavbarLinkContainer>
<OpenLinksButton
onClick={() => {
setExtendNavbar((curr) => !curr);
}}
>
{extendNavbar ? <>✕</> : <> ≡</>}
</OpenLinksButton>
<Logo src={LogoImg}></Logo>
</NavbarLinkContainer>
</TopContainer>
<BottomContainer>
<NavbarLinkContainer>
<NavbarLink to="/"> Home</NavbarLink>
<NavbarLink to="/products"> Products</NavbarLink>
<NavbarLink to="/contact"> Contact Us</NavbarLink>
<NavbarLink to="/about"> About Us</NavbarLink>
</NavbarLinkContainer>
</BottomContainer>
</NavbarInnerContainer>
{extendNavbar && (
<NavbarExtendedContainer>
<NavbarLinkExtended to="/"> Home</NavbarLinkExtended>
<NavbarLinkExtended to="/products"> Products</NavbarLinkExtended>
<NavbarLinkExtended to="/contact"> Contact Us</NavbarLinkExtended>
<NavbarLinkExtended to="/about"> About Us</NavbarLinkExtended>
</NavbarExtendedContainer>
)}
</NavbarContainer>
);
}
export default Navbar;
Style page
import styled from "styled-components";
import { Link } from "react-router-dom";
export const NavbarContainer = styled.nav`
width: 100%;
background-color: black;
#media (min-width: 700px) {
height: 80px;
}
`;
export const TopContainer = styled.div`
padding-left: 5%;
`;
export const BottomContainer = styled.div`
padding-right: 50px;
background-color:salmon;
`;
export const NavbarInnerContainer = styled.div`
width: 100%;
height: 80px;
`;
export const NavbarLinkContainer = styled.div`
display: flex;
`;
export const NavbarLink = styled(Link)`
color: white;
font-size: x-large;
font-family: Arial, Helvetica, sans-serif;
position: relative;
left: 43%;
top:10%;
text-decoration: none;
margin: 10px;
height: 30px;
text-align: center;
#media (max-width: 700px) {
display: none;
}
`;
export const NavbarLinkExtended = styled(Link)`
color: white;
font-size: x-large;
font-family: Arial, Helvetica, sans-serif;
text-decoration: none;
margin: 10px;
`;
export const Logo = styled.img`
#media (min-width: 700px) {
margin: auto;
}
margin: 10px;
max-width: 180px;
height: auto;
`;
I really wouldn't recommend using a whole different page for css instead just add your code to index.css, from there you can access your css from any component in your react app.
And to center the NavbarLink you can use flexbox.
Create some css code in index.css:
.navbar__link {
width: 100%
display: flex;
justify-content: center;
}
And add that class in your component element:
<NavbarLinkContainer className='navbar__link'>
<NavbarLink to="/"> Home</NavbarLink>
<NavbarLink to="/products"> Products</NavbarLink>
<NavbarLink to="/contact"> Contact Us</NavbarLink>
<NavbarLink to="/about"> About Us</NavbarLink>
</NavbarLinkContainer>

Styled-Components mobile menu

I'm trying to achieve the following result. I have a responsive menu, however when the screen is less than 768px should hide a few elements (the ItemsMenu and LanguageItems) from the horizontal menu that it is for the desktop and display in the burger menu for mobile.
I'm having an issue because if I use display: none; it'll hide in both displays, for desktop and for mobile. How would be a better approach to achieve this result:
Component:
function Languages() {
const [language, setLanguage] = useState("en");
const handleLanguage = (language) => (event) => {
setLanguage(language);
};
return (
<>
<Lang>
<Logo>
<h1>Flávio</h1>
</Logo>
<ItemsMenu>
<li>{languages[language].about}</li>
<li>{languages[language].project}</li>
<li>{languages[language].contact}</li>
</ItemsMenu>
<LanguageIcon>
<img src={English} alt="EN" onClick={handleLanguage("en")} />
<img src={Japanese} alt="JP" onClick={handleLanguage("jp")} />
<img src={Russian} alt="RU" onClick={handleLanguage("ru")} />
<img src={Portuguese} alt="PT-BR" onClick={handleLanguage("ptbr")} />
</LanguageIcon>
</Lang>
</>
);
}
Styled-component:
import styled from "styled-components";
export const Lang = styled.div`
display: flex;
justify-content: space-between;
margin: auto;
width: 100%;
li:hover {
transition: all 0.2s ease-in-out;
transform: scale(1.5);
}
`;
export const Logo = styled.div`
color: #2e186a;
cursor: pointer;
`;
export const ItemsMenu = styled.ul`
display: flex;
justify-content: center;
cursor: pointer;
color: #2e186a;
margin: auto;
li {
display: inline-block;
color: #2e186a;
cursor: pointer;
padding-left: 1.5rem;
}
#media (max-width: 768px) {
/* display: none; */
}
`;
export const LanguageIcon = styled.div`
#media (max-width: 768px) {
/* display: none; */
}
img {
height: 2em;
margin-left: 1em;
cursor: pointer;
}
img:hover {
transition: all 0.2s ease-in-out;
transform: scale(1.5);
}
`;

css React onclick show component

I am learning css and react and a bit confused how to display:block for a component when a user clicks a button myButton. And to hide the component when the user clicks it again.
I am using mobile and desktop views and want the button to not show in desktop view, it will only show in Mobile view.
In Mobile view once the user clicks the button toggle component needs to show. That is where I dont know how onclick I can say for Toggle css to display:block to show entire component. Toggle shows in desktop view.
Here is code map.tsx
<div className={styles.controlsContainer}>
<Toggle />
</div>
<div className={styles.button_about}>
<AboutPopup />
<Button id=myButton
type="primary"
icon={<FilterOutlined />}
className={styles.filterbutton} onClick== --- How to say to enable toggle css to block??
></Button>
</div>
Here is how my map.module.scss looks like
.toggle {
width: 244px;
background: #ffffff;
border: 1px solid #d9d9d9;
box-sizing: border-box;
border-radius: 4px;
display: flex;
align-items: center;
float: left;
}
#media screen and (max-width: $mobileMax) {
.toggle {
display: none;
}
}
.button {
&_about {
#include for-desktop {
position: absolute;
bottom: 2em;
right: 2em;
}
#include for-mobile {
position: absolute;
top: 2em;
z-index: 1;
right: 2em;
z-index: 1;
}
}
}
.filterbutton {
width: 40px;
height: 40px;
left: calc(50% - 40px / 2);
top: calc(50% - 40px / 2);
background: $color-blue;
border-radius: 100px;
color: #83888c;
#include for-desktop {
display: none;
}
#include for-mobile {
position: absolute;
top: 4em;
right: 2em;
}
}
----- Update 2
So I did something like this
const [isShow, setIsShow] = React.useState(true);
const handleClick = () => {
setIsShow((prev) => !prev);
console.log(isShow);
};
return
(
<div className={styles.controlsContainer}>
<Toggle
className= {isShow ? {styles.toggle_show} : {styles.toggle_hide}}
/>
)
But I am getting a parsing error on styles.toggle_show Parsing error: ',' expected.eslt
maybe this could help
import React from "react";
import "./styles.css";
export default function App() {
const [isShow, setIsShow] = React.useState(false);
const handleClick = () => setIsShow((prev) => !prev);
return (
<>
<div className={isShow ? "show" : "hide"}>my toggling component</div>
<button onClick={handleClick}>toggle show</button>
</>
);
}
css:
.show {
display: block;
}
.hide {
display: none;
}

Resources