Appending a class with Click messes up the whole css - css

I don't really know why transforming the X position while appending an additional class to make the slide effect isn't working as expected in React, as it does while using vanilla javascript.
This is the code example
.inputModal {
position: absolute;
width: 500px;
height: 150px;
display: inline-flex;
justify-content: center;
align-items: center;
padding: 10px 20px;
background-color: rgba(25, 26, 26, 0.74);
left: -8px;
top: 30%;
box-shadow: 10px 15px 20px rgba(0, 0, 0, 0.5);
transform: translateX(-93%);
transition: all 0.3s ease-in-out;
}
.InputModal.show {
transform: translateX(0px);
}
And the simple Component which should append the class show on button click
function InputModal() {
const [toggleBtn, setToggleBtn] = useState(true);
return (
<div className={toggleBtn ? "inputModal" : "InputModal show"}>
<Input />
<div
className="toggleBtn"
onClick={() => {
setToggleBtn(!toggleBtn);
}}
>
<i className="fas fa-align-justify"></i>
</div>
</div>
);
}
Once I click the toggle button(div) the show class is appended on to the div, but the div is losing all the previous set stylings and gets messed up

You should append instead of change the classes, just like:
function InputModal() {
const [toggleBtn, setToggleBtn] = useState(true);
return (
<div className={`inputModal ${toggleBtn ? "" : "InputModal show"}`}>
<Input />
<div
className="toggleBtn"
onClick={() => {
setToggleBtn(!toggleBtn);
}}
>
<i className="fas fa-align-justify"></i>
</div>
</div>
);
}

Related

Regarding applying animation to an image using dynamic CSS styles in React

I'm new to React. I have this image ("c-tile-img") inside a div ("c-tile-holder") and what I want to do is when I hover over the div or the image element I want to translate on Y-axis slightly like a hover effect. I used React useState hook to achieve this but when I run it nothing is triggered. Here is what I've so far...
**
Tile.jsx
**
const Tile = () => {
const[isMousedOver, setMouseOver] = useState(false);
function handleMouseOver() {
setMouseOver(true);
}
function handleMouseOut() {
setMouseOver(false);
}
return (
<React.Fragment>
<div className="c-tiles">
<a href="/work/help-scout/" className="c-tile c-tile-hs" aria-label="Help Scout Case Study"
style={{translate: isMousedOver ? '50': '0'}}
onMouseOut={handleMouseOut}
onMouseOver={handleMouseOver}>
<span className="c-tile-category">Mobile</span>
<h2 className="c-tile-title">Help Scout</h2>
<div className="c-tile-holder">
<img className="c-tile-img c-tile-img-hs lazy loaded" alt="Help Scout" aria-label="Help Scout image" data-ll-status="loaded"
src={headshot}/>
</div>
</a>
My CSS File
Tile.css
.c-tile-holder {
position: relative;
max-width: 100%;
margin: 0 auto;
text-align: center;
}
.c-tile-img-hs {
max-width: 80%;
}
.c-tile-img {
position: relative;
transition: all .4s ease-in-out;
max-width: 100%;
display: inline-block;
}
img {
vertical-align: middle;
}
img {
border-style: none;
}

How to give animation on width change with different classname but for same component

what is the best practice to show/hide the side menu with animation? The way I implement works fine but I can't implement the animation. I also shared images so everyone can have some idea.
The answer might be easy but I couldn't fully understand the toggle class efficiently. If I would I should be able to implement transition just by changing the width from the same classname.
Also I am getting this error on console: Received false for a non-boolean attribute className.
React code ;
const handleSize = () => {
setOpen(!open); }; return (
<div
className={"sideBar" + `${open ? " sideBar__show" : " sideBar__hide"}`}
>
<div className="sideBar__header">
<div className="menu_buttons" onClick={handleSize}>
<MenuIcon className="sideBar_icon" />
<p className={!open && "sideBar_hide__content"}>Menu</p>
</div>
</div>
<hr className="sideBar__divider" />
<div className="sideBar__content">
{sideBarContent.map((item) => {
return (
<div className="menu_buttons" key={item.name}>
<item.icon />
<p className={!open && "sideBar_hide__content"}>{item.name}</p>
</div>
);
})}
</div>
<hr className="sideBar__divider" />
<div className="sideBar__footer">
<Avatar>{firstLetter}</Avatar>
<p className={!open && "sideBar_hide__content"}>{adminName}</p>
</div>
</div> ); };
CSS;
.sideBar {
position: fixed;
left: var(--sidebar-left-margin);
height: max-content;
top: calc(var(--header-height) + 10px);
bottom: 10px;
border-radius: 10px;
background-color: var(--second-color);
display: flex;
flex-direction: column;
align-items: center;
padding: 10px 5px;
border: 1px solid var(--main-color);
/* transition: all ease-in 1s; */
}
.sideBar__show {
width: var(--sidebarOpen-width);
}
.sideBar_hide {
width: var(--sidebarClose-width);
}
.sideBar_hide__content {
display: none;
}
Not sure what is best practice, this is my currently approach:
const handleSize = () => setOpen(!open);
const sideBarClasses = open ? "sideBar sideBar_show : "sideBar sideBar_hide;
<div className="sideBar__header">
<div className={sideBarClasses}>

overlay navbar with transition in React

I'm having issues with an overlay navbar which is supposed to slide in from top.
Not sure why, but if i click the hamburger menu, the proper css class is applied, but the transition doesn't occur (it acts like a show/hidden).
However, if i change the top property value from the dev tool, it works as expected.
what am i missing?
Here's the react component
import React, { useState } from "react";
import NavMenuIcon from "../../public/svg/nav.svg";
import styles from '../../styles/Navbar.module.scss'
import Link from 'next/link'
function Navbar() {
const [menu, setToggle] = useState(false);
const toggleMenu = () => setToggle(!menu);
const Menu = props => (
<div className={ props.toggle ? `${styles.menu__container} ${styles.toggle}` : `${styles.menu__container}` }>
<div className={styles.menu__content}>
<ul className={styles.menu__list}>
<li className={styles.menu__item} onClick={toggleMenu}> <Link href="/"><span>Home</span></Link></li>
<li className={styles.menu__item} onClick={toggleMenu}> <Link href="/services"><span>Services</span></Link></li>
<li className={styles.menu__item} onClick={toggleMenu}> <Link href="/services#contact"><span>Contact</span></Link></li>
</ul>
</div>
</div>
);
return (
<div className={styles.navbar}>
<i>Meuartelie</i>
<NavMenuIcon className={styles.hamburger} onClick={toggleMenu} />
<Menu toggle={menu}></Menu>
</div>
);
}
export default Navbar;
and here's the css
#import './variables.scss';
.navbar {
box-sizing: border-box;
position: fixed;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
padding: 5%;
z-index: 5;
}
.menu__container {
font-family: 'Jokerman Std';
position: absolute;
top: -300px;
left: 0;
width: 100vw;
height: 300px;
z-index: -1;
background-image: url("../public/png/welcome_mobile.png");
background-color: black;
background-size: cover;
overflow: hidden;
transition: 850ms;
}
.menu__content {
font-size: 2rem;
display: flex;
align-items: center;
justify-content: center;
margin-top: 20%;
}
.menu__container.toggle {
top: 0;
transition: all 1s ease-in;
}
.menu__list {
width: 80%;
text-align: center;
list-style: none;
padding: 0px;
line-height: 2;
margin: 0px;
margin-bottom: 20px;
}
.menu__item {
transition: padding-left .7s ease, color .7s ease;
/* underline css*/
text-decoration: none;
position: relative;
}
.menu__item:hover {
color: $base-color;
padding-left: 20px;
cursor: pointer;
}
.menu__item:after {
content: '';
height: 2px;
/* adjust this to move up and down. you may have to adjust the line height of the paragraph if you move it down a lot. */
bottom: 0px;
/* center - (optional) use with adjusting width */
margin: 0 auto;
left: 0;
right: 0;
width: 40%;
background: #fff;
/* optional animation */
-o-transition:.5s;
-ms-transition:.5s;
-moz-transition:.5s;
-webkit-transition:.5s;
transition: .5s;
}
/* optional hover classes used with anmiation */
.menu__item:hover:after {
background: $base-color;
}
.hamburger {
z-index: 1000;
cursor: pointer;
}
This has to do with how react determines what to rerender.
Whenever react determines a new reference in the render tree, it automatically rerenders that element.
CSS transitions on the other hand will transform from a starting state, to the end state.
If the start and end are the same, no transition is applied.
So what's actually happening:
The button gets clicked.
The render tree is modified. specifically const Menu is assigned a new reference.
React removed any old elements from the dom, in this case <div class="menu__container">
React adds the new elements to the dom, in this case <div class="menu_container toggle">
the browser renders the element and applies the initial css rules. the element receives the top:0 as the initial state. there is no transition since the old element was removed.
How to fix it:
Make sure you React knows we're still rendering the same element, so it will update the existing dom element, instead of removing and adding a new element.
several ways to do this, the recommended way is to extract the element from the render function. The reference becomes static then
//extracted from render function:
const Menu = (props) => (
<div key='1' className={ props.toggle ? `menu__container toggle` : `menu__container` }>
<div className={'menu__content'}>
<ul className={'menu__list'}>
<li className={'menu__item'} onClick={props.toggleMenu}> <Link href="/"><span>Home</span></Link></li>
<li className={'menu__item'} onClick={props.toggleMenu}> <Link href="/services"><span>Services</span></Link></li>
<li className={'menu__item'} onClick={props.toggleMenu}> <Link href="/services#contact"><span>Contact</span></Link></li>
</ul>
</div>
</div>
);
function Navbar() {
const [menu, setToggle] = useState(false);
const toggleMenu = () => setToggle(!menu);
return (
<div className={'navbar'}>
<i>Meuartelie</i>
<button className={'hamburger'} onClick={toggleMenu} >☰</button>
<Menu toggle={menu} toggleMenu={toggleMenu}></Menu>
</div>
);
}
Sometimes its inconvenient to extract the component from the render function, for example If the components is dynamically created/determined.
You can then use a the hook useMemo to tell react to store the reference, and only recalulate the value if a prop in the dependency-array changes.
function NavbarAndMenuMemoized() {
const [menu, setToggle] = useState(false);
const toggleMenu = () => setToggle(!menu);
// useMemo will not recalculate Menu each render, instead it will keep the same reference for Menu.
const Menu = useMemo(()=>{
return(props) => (
<div key='1' className={ props.toggle ? `menu__container toggle` : `menu__container` }>
<div className={'menu__content'}>
<ul className={'menu__list'}>
<li className={'menu__item'} onClick={toggleMenu}> <Link href="/"><span>Home</span></Link></li>
<li className={'menu__item'} onClick={toggleMenu}> <Link href="/services"><span>Services</span></Link></li>
<li className={'menu__item'} onClick={toggleMenu}> <Link href="/services#contact"><span>Contact</span></Link></li>
</ul>
</div>
</div>
);
}, [])
return (
<div className={'navbar'}>
<i>Meuartelie</i>
<button className={'hamburger'} onClick={toggleMenu} >☰</button>
<Menu toggle={menu} ></Menu>
</div>
);
}
live demo Example

When rendering modal/component i want background to have a dark color

Hello so i use a portal Component that looks like this that is used to create "modals"
export const ModalWindow = (props: RenderableProps<Props>) => {
if (!props.display) {
return null;
}
return (
<Portal parentNode={props.parentNode} renderOnMount={true}>
<div id='modal' className={props.largeModal ? 'modal-large' : 'modal-small'}>
{props.children}
</div>
</Portal>
)
}
Then i have this component that i then render inside that modal
export const ShowInformation = (props: RenderableProps<Props>) => {
return (
<div className='blur-out-menu'>
<div className='information-content'>
<span className='information-title'>Show Information?</span>
<span className='information-text'>Click check button to show information</span>
<div className='button-section'>
<div className='decline-button' onClick={() => props.onShow(false)}>
<CancelButton />
</div>
<div className='check-button' onClick={() => props.onShow(true)}>
<CheckButton />
</div>
</div>
</div>
</div>
);
};
what is important here is the blur-out-menu that i just want to use to blur out everything except the modal but i cant figure it out either it just covers the same area as the modal or if i put position fixed it messes it up totally and nothing inside the modal keeps in place. is there any nice way of doing this?
here is the css for the blur-out-menu
.blur-out-menu {
background-color: rgba(0, 0, 0, 0.25);
height: 100%;
width: 100%;
position: absolute;
top: 0;
left: 0;
z-index: 4;
}
the modal got z-index 5 so it is above this blur out
here is the modal css as well
.modal-small {
background-color: #1E2933;
top: 25%;
left: 5%;
width: 90%;
height: 30%;
border-radius: 10px;
z-index: 5;
}
In .blur-out-menu change height property to 100vh and width property to 100vw
This should to the trick.

position in reactjs with css

everyone, I am working on Reactjs project, I want to add submit button inside the input field, but I am not able to add, can anyone help me to place submit button inside input field, thanks in advance, my demo code, expected and actual outcome is attached
Reactjs code
<div className={styles.subscribeInputContainer}>
{I18n.getComponent(
(formattedValue) =>
<input
id="subscribeInput"
required={true}
value={this.state.value}
className={styles.subscribeInput}
placeholder={formattedValue}
onKeyPress={(e) => this.onKeyPress(e)}
onChange={(e) => this.onEmailChange(e.target.value)}
/>,
'footer.your-email-address',
{},
'Enter Your email address'
)}
<p className={styles.subscribeButton} id="btnId">
<Button variant="primary" className={styles.subscribeIcon} onClick={() => this.onSubscribe()}>Button</Button>
</p>
</div>
CSS code
.inputContainer {
position: relative;
}
.subscribeIcon {
position: absolute;
right: 10px;
}
.subscribeInputContainer {
display: flex;
flex-flow: row;
flex: 1;
font-size: $fontSize-lg;
color: $color-body;
}
Actual outcome
Expected Outcome
First, don't use a p as a wrapper, use a div
When you use position: absolute you take that element out of flow, and as such its parent will not take it into account when sizing itself.
If you remove position: absolute from the .subscribeIcon and add margin-left: auto to the .subscribeButton, your button should be properly right aligned within its parent, the .subscribeInputContainer.
.subscribeInputContainer {
display: flex;
flex-flow: row;
flex: 1;
border: 1px solid gray;
padding: 5px;
}
.subscribeButton {
margin-left: auto;
}
.subscribeIcon {}
<div class='subscribeInputContainer'>
<input id="subscribeInput" required={true} value={this.state.value} className={styles.subscribeInput} placeholder={formattedValue} onKeyPress={(e) />
<div class='subscribeButton' id="btnId">
<Button variant="primary" class='subscribeIcon' onClick={()=> this.onSubscribe()}>Button</Button>
</div>
</div>

Resources