Trying to achieve expand and collapse animation on a card - ReactJS - css

I'm trying to achieve expand and collapse animation on a card without using bootstrap or anything and for this, I tried attaching a transition property but for some reason it's not working on button click.
Here is the component:
Card.jsx
import React, { useState } from 'react';
import './Card.scss';
const Card = (props) => {
const [collapse, toggleCollapse] = useState(true);
return (<div className="card">
<div className="card-header">
<h4 className="card-title">Card Actions</h4>
<div className="heading-elements">
<button onClick={() => toggleCollapse(!collapse)}>Collapse</button>
</div>
</div>
<div className={`card-content ${!collapse ? 'collapse show' : 'collapsing'}`}>
<div className="card-body">
<div className="row">
Hi there, this content needs to shown on button click
</div>
</div>
</div>
</div>);
}
export default Card;
Here is the scss file of the card:
Card.scss
.card {
position: relative;
display: flex;
flex-direction: column;
min-width: 0;
word-wrap: break-word;
background-color: #fff;
background-clip: border-box;
border-radius: 0.428rem;
border: none;
margin-bottom: 2rem;
box-shadow: 0 4px 24px 0 rgba(34, 41, 47, 0.10);
transition: all 0.3s ease-in-out, background 0s, color 0s, border-color 0s;
}
.card .card-header {
position: relative;
display: flex;
align-items: center;
flex-wrap: wrap;
justify-content: space-between;
border-bottom: none;
padding: 1.5rem;
background-color: transparent;
}
.card-header:first-child {
border-radius: calc(0.428rem - 1px) calc(0.428rem - 1px) 0 0;
}
.collapse:not(.show) {
display: none;
}
.collapsing {
position: relative;
height: 0;
overflow: hidden;
transition: height 2s ease;
}

The problem is, when you want to collapse the card you remove the show class therefore the .collapse:not(show) selector activates where you wrote display: none;. display: none; cannot be used with a transition, take a look here for a list of properties that can be animated. Just adjusting the height should be enough.

Related

Small circle div - CSS [duplicate]

This question already has answers here:
How to use CSS to surround a number with a circle?
(20 answers)
Closed 7 months ago.
I'm trying to make the divs round as much as possible. however it should be "small", without having to increase width and height to 100px.
image of my calcualtor
here is the code:
.contas div {
color: white;
display: inline-block;
border: 0;
width: 40px;
border-radius: 100%;
text-align: center;
padding: 10px;
margin: 20px 4px 10px 1px;
cursor: pointer;
background-color: #202020;
transition: border-color .2s ease-in-out, background-color .2s, box-shadow .2s;
}
If you really want to get a circle, you need an equal amount of height and width on your div.
That way you get a square.
Then you add a border-radius of 50% for the perfect circle.
You're less likely to get a perfect circle if the height and width of the div are not the same.
Use for buttons display:inline-flex; and border-radius: 50%;
here is code HTML:
<aricle class="contas">
<!-- row 0 -->
<input class="contas__head" placeholder="0" type="text" />
<!-- row 1 -->
<button class="contas__btn">C</button>
<button class="contas__btn">()</button>
<button class="contas__btn">%</button>
<button class="contas__btn">/</button>
<!-- row 2 -->
<button class="contas__btn">7</button>
<button class="contas__btn">8</button>
<button class="contas__btn">9</button>
<button class="contas__btn">*</button>
<!-- row 3 -->
<button class="contas__btn">4</button>
<button class="contas__btn">5</button>
<button class="contas__btn">6</button>
<button class="contas__btn">-</button>
<!-- row 4 -->
<button class="contas__btn">1</button>
<button class="contas__btn">2</button>
<button class="contas__btn">3</button>
<button class="contas__btn">+</button>
<!-- row 5 -->
<button class="contas__btn">+/-</button>
<button class="contas__btn">0</button>
<button class="contas__btn">.</button>
<button class="contas__btn">=</button>
</aricle>
here is code SCSS:
$contas-color: #f2f2f2;
$contas-fill: #000000;
$contas-fill-light:#202020;
$contas-radius: 1rem;
$contas-space: 0.75rem;
$contas-field-size: 4rem;
$contas-btn-size: var(--contas-btn-size, 2.5rem);
$contas-btn-hover-fill: #ff810f;
body {
display: flex;
align-items: center;
justify-content: center;
min-height: 100vh;
padding: 1.5rem;
background-color: $contas-color;
}
.contas {
position: relative;
display: grid;
grid-gap: 0.8rem 0.5rem;
grid-template-columns: repeat(4, $contas-btn-size);
justify-content: center;
align-items: center;
width: 12.5rem;
padding: calc(#{ $contas-field-size + ($contas-space* 1.5)}) $contas-space ($contas-space* 1.5) ;
background-color: $contas-fill;
border-radius: $contas-radius;
overflow: hidden;
&__head {
position: absolute;
inset: 0 0 auto;
min-height: $contas-field-size;
padding: 0 ($contas-space*1.5);
font-size: 1.5rem;
font-weight: 500;
text-align:right;
color:$contas-color;
border: unset;
user-select: none;
background: $contas-fill-light;
pointer-events:none;
&:focus, &:active, &:hover {
color:$contas-color;
outline: none;
box-shadow: none;
}
}
&__btn {
display: inline-flex;
align-items: center;
justify-content: center;
color: white;
width: $contas-btn-size;
aspect-ratio: 1 / 1;
text-align: center;
border-radius: 50%;
border: unset;
background-color: $contas-fill-light;
transition: .3s lianer;
transition-property: background-color, box-shadow, transform;
cursor: pointer;
&:hover, &:active{
background-color: $contas-btn-hover-fill;
}
&:active{
transform: scale(1.15);
}
}
}
see https://jsfiddle.net/d4htsLgb/2/
You can fix it using pixels instead of percents:
.contas .div {
border-radius: 99999px;
}

How to transform: translatex(0%) when click event using react

I am new to react and tring to build mobile responsive navbar. in mobile view i want to slide the navbar and I added click event to change transform : translateX(0%) but css doesn't appear to work. here's how my code looks like.
App.js
import Navbar from "./components/Navbar";
function App() {
return (
<div className="App">
<Navbar />
</div>
);
}
export default App;
Navbar.jsx
import '../css/navbar.css'
import {useState} from 'react';
function Navbar() {
const [isShowNavLinks, setIsShowNavLinks] = useState(false);
const handleBurgerClick = () => {
setIsShowNavLinks(!isShowNavLinks)
console.log(isShowNavLinks)
}
return (
<nav className="nav">
<h2 className='logo'>LOGO</h2>
<div className="burger" onClick={handleBurgerClick}>
<div className="line1"></div>
<div className="line2"></div>
<div className="line3"></div>
</div>
<ul className={isShowNavLinks? 'nav-active' : ''}>
<li>Home</li>
<li>About</li>
<li>Contact</li>
<li>Blog</li>
</ul>
</nav>
)
}
export default Navbar;
navbar.css
.nav{
background-color: #16003B;
color: #EEEEEE;
display: flex;
justify-content: space-between;
align-items: center;
height: 10vh;
}
.logo {
padding: 1%;
}
.nav ul {
width: 60vw;
display: flex;
justify-content: space-around;
list-style: none;
}
ul li {
text-decoration: none;
}
.burger {
display: none;
}
.burger>div {
width: 25px;
height: 2px;
background-color:white;
margin: 4px;
}
#media screen and (max-width: 500px) {
body{
overflow-x: hidden;
}
.burger {
display: block;
cursor: pointer;
}
.nav ul{
position:absolute;
top:10vh;
right:0px;
width:50%;
height:90vh;
background-color: #16003B;
flex-direction: column;
transform: translateX(100%);
justify-content: space-evenly;
align-items: center;
transition: 0.5s ease-in;
}
.nav-active {
transform: translateX(0);
}
}
when click on .burger in dom it shows nav-active class but doesn't apply the its css
DOM when click burger
just simply add the !important property on the nav-active class because it will give high priority to this class.
To read out more about it go to this link about important property in css
After adding important property it will work fine for me. Go to checkout
.nav-active{
transform:translateX(0) !important;
}

Hover effect not being applied to SVG Nested in NavLink

How do I get the nested SVG to have the same hover effect as the anchor tag? When I hover over one it should change both, but right now they are not being seen as the same entity.
I am trying to apply a style to an SVG on hover. The SVG is nested inside of a NavLink component. The CSS that is class-specific gets applied to the SVG, but the hover for the a tag that the SVG is nested in only applies to the a tag's text. I have tried using just the Link component from react-router-dom. I have noticed that when I removed the text-decoration: none from my CSS, the underline only applies to the text. In the rendered HTML the a tag DOES nest the svg tag.
I have tried wrapping the anchor tag and SVG in a span and giving the span the hover effect, it did not work.
component
import {Link, NavLink} from "react-router-dom";
import logo from '../../assets/LogoOnly_Square_Transparent.png';
import { ReactComponent as CalculatorIcon } from '../../assets/calculator.svg';
import './header.styles.scss';
export const Header = () => {
return (
<nav className="header">
<div className='options'>
<Link className='logo-container' to='/'>
<img src={logo} alt="Logo" className='logo'/>
</Link>
<NavLink to='/calculator' className='header-option'>CALCULATOR<CalculatorIcon className='link-icon'/></NavLink>
</div>
<div className='options-right'>
<Link to='/sign-in-sign-up' className='header-option'>SIGN IN</Link>
</div>
</nav>
)
}
CSS
border-bottom: 1px solid #D1D3D4;
height: 70px;
width: 100%;
display: flex;
justify-content: space-between;
margin-bottom: 25px;
padding: 5px;
.options {
height: 100%;
display: flex;
align-items: center;
justify-content: flex-start;
.logo-container {
width: 70px;
margin: 5px 0;
height: 100%;
.logo {
height: 100%;
}
}
.header-option {
padding: 10px 15px;
font-size: 16px;
.link-icon {
height: 13px;
fill: #58595B;
margin: auto 5px;
}
&:hover {
color: #4FB47C;
fill: #4FB47C;
}
}
}
a {
text-decoration: none;
color: #58595B;
&:hover {
color: #4FB47C;
fill: #4FB47C;
}
}
.options-right {
height: 100%;
display: flex;
align-items: center;
justify-content: flex-end;
.header-option {
padding: 10px 15px;
}
}
}

V - slot, can't style or access it

I add the v-flip on my nuxt webpage. So my template looks like this:
<div class="about__cards">
<vue-flip class="about__single-card" active-click width = "350px" height="450px">
<template v-slot:front>
<p class="about__title">Stacks</p>
</template>
<template v-slot:back>
<h4>My stacks</h4>
<ul>
<li>Javascript</li>
<li>Css</li>
<li>HTML</li>
<li>Vue</li>
<li>Nuxt</li>
</ul>
</template>
</vue-flip>
...
This was my styling:
.about__cards{
display:flex;
justify-content: space-evenly;
/* background-color: transparent; */
}
.about__single-card{
border:1px red solid;
border-radius: 16px;
overflow: hidden;
box-shadow: 0 5px 18px rgba(0, 0, 0, 0.6);
cursor: pointer;
transition: 0.5s;
position: relative;
text-align: center;
transition: transform 0.6s;
transform-style: preserve-3d;
}
.front{
background: red;
width: 100%;
height: 100%;
/* line-height: 448px; */
text-align: center;
vertical-align: middle;
/* display: flex;
flex-direction: column;
align-content: flex-end; */
}
I put front because when inspecting the element I saw that was given that class. All all seems working. Then I realized that I haven't scoped the style so was messing with other pages. I did, restart the server and now it's not working anymore the styling of the v-slot. But If I go to inspect the element and I go to the "front"class that was pre-given I can change it there... I read that you can style v-slots, but before I did it so I am kind of confused. What I am missing here?
Thank you
It's because scoped styles are "scoped" by using component unique id as part of the selector.
If you inspect your app, you will see things like this (1d328d7a is the uid)
<div
data-v-1d328d7a=""
class="field-input-wrapper"
...
.field-input-wrapper[data-v-1d328d7a] {
...
In order to bypass this, you can use deep selector like this
.about__single-card >>> .front{
background: red;
width: 100%;
height: 100%;
text-align: center;
vertical-align: middle;
}

CSS: How do I change .ClassA when .ClassB is in focus [duplicate]

This question already has answers here:
Change CSS element that comes after another
(2 answers)
Make :focus change css of another class
(3 answers)
Closed 4 years ago.
Is it possible to change one class when another is in focus with CSS? For example, .ClassA when .ClassB is in focus
I'm trying to achieve something similar to the apple.com search feature where the menu hides and the search input field expands when selected.
Try this out, it may give you a scent of hope O:-)
However it is very unlikely, that you will get a better solution with pure CSS, it truly is usually made with JavaScript, while each HTML element has list of classes and you may add a class or remove a class quite easily.
<html>
<head>
<style>
#spawner {
display: none;
}
#spawner ~ div {
display: none;
}
#spawner:checked ~ div {
display: block;
}
</style>
</head>
<body>
<label for="spawner">
<div>Click me to show content!</div>
</label>
<input type="checkbox" id="spawner">
<div>
Was that a JavaScript? I doubt it!
</div>
</body>
</html>
Maybe something like this?
input { width: 35%; -webkit-transition: width .35s ease-in-out; transition: width .35s ease-in-out;}
input:focus { width: 100%; }
const menu = document.querySelector('.menu');
const search = document.querySelector('.search');
search.addEventListener('focus', () => {
console.log('Focuse');
search.classList.add('search--active');
menu.classList.add('menu--hidden');
});
search.addEventListener('blur', () => {
search.classList.remove('search--active');
menu.classList.remove('menu--hidden');
});
html, body {
padding: 0;
margin: 0;
}
.menu {
display: flex;
flex-direction: row;
}
.menu > a {
padding: 10px;
color: white;
text-decoration: none;
transition: background-color 0.3s;
}
.menu > a:hover {
background-color: rgba(0,0,0,0.5);
}
.wrapper {
display: flex;
flex-direction: row;
background-color: #eae;
box-shadow: 0 0 5px #888;
align-items: center;
justify-content: space-between;
padding-right: 10px;
}
.search {
width: 200px;
border-radius: 10px;
transition: width 0.5s;
}
.search--active {
width: 500px;
margin-left: auto;
}
.menu--hidden {
display: none;
}
<div class='wrapper'>
<div class='menu'>
<a href='#'>Home</a>
<a href='#'>About</a>
<a href='#'>Contact Us</a>
<a href='#'>Support</a>
<a href='#'>Page</a>
</div>
<input class='search' type='search' name='search' />
</div>

Resources