React js styled component input animation not working - css

I am trying to create an form with input fields.I am trying to set up the floating labels for the inputs. The floating label works for the second field and not the first field. Please help me understand what am I missing on..
Here is code sandbox link :Please refer to productDetails component..
https://codesandbox.io/s/pensive-water-pojf8?file=/src/StyledComponents/FormInputStyles/index.js:199-1554
Form components
<InputWrapper>
<TextBox />
<LabelText>Product Name</LabelText>
</InputWrapper>
<InputWrapper>
<TextParagraph />
<LabelText1 for="description">Product Name</LabelText1>
</InputWrapper>
styled components: Where I am trying to make the input label floating
export const TextBox = styled.input`
display: flex;
width: 100%;
height: 2em;
border-radius: 5px;
border: none;
background: ${styles.backgroundGradient};
margin-top: ${props => props.marginTop || "1.5em"};
&:focus {
outline: none;
border: 2px solid #6e5dcc;
}
`;
export const LabelText = styled.label`
position: absolute;
top: 1.5em;
left: 0;
font-size: 18px;
line-height: 16px;
pointer-events: none;
transition: 0.5s;
color: ${styles.formContentColor};
${TextBox}:focus ~ &,
${TextBox}:not([value=""]) ~ & {
top: 0;
left: 0;
color: ${styles.formContentColor};
font-size: 16px;
}
`;
export const TextParagraph = styled.input`
display: flex;
width: 100%;
height: 7em;
border-radius: 5px;
border: none;
text-align: left;
appearance: none;
background: ${styles.backgroundGradient};
margin-top: ${props => props.marginTop || "1.5em"};
&:focus {
outline: none;
border: 2px solid #6e5dcc;
}
`;
export const LabelText1 = styled.label`
position: absolute;
top: 1.5em;
left: 0;
font-size: 18px;
line-height: 16px;
pointer-events: none;
transition: 0.5s;
color: ${styles.formContentColor};
${TextParagraph}:focus ~ &,
${TextParagraph}:not([value=""]) ~ & {
top: 0;
left: 0;
color: ${styles.formContentColor};
font-size: 16px;
}
`;

Related

different background color after implementing css entity code in content

I am trying to implement the CSS entity code in content in react SCSS file I am getting the background colour green with black border and not able to set the background colour as black.
here is my code
input[type=checkbox] {
width: 29px;
height: 25px;
margin-right: -18px;
cursor: pointer;
font-size: 17px;
visibility: hidden;
margin-top: -1.5px;
}
input[type=checkbox]:checked{
margin-top: -6px;
}
input[type=checkbox]:after,
input[type=checkbox]::after {
content: " ";
display: inline-block;
margin-left: 2px;
padding-bottom: 5px;
width: 12px;
height: 12px;
visibility: visible;
border: none;
padding-left: 3px;
border-radius: 4px;
}
input[type=checkbox]:checked:after,
input[type=checkbox]:checked::after {
content: "\2714";
padding: -5px;
font-weight: bold;
font-size: 7px;
padding: 1px;
}
Using your CSS, and simplified it a little, here are the results.
The green and black styles must coming from another CSS source, as my example using your CSS does not show this?
input[type=checkbox] {
transform: scale(1000%) translate(50%, 50%);
}
input[type=checkbox] {
visibility: hidden;
}
input[type=checkbox]:after {
content: " ";
background-color: #775273;
display: inline-block;
color: #775273;
width: 12px;
height: 12px;
visibility: visible;
border: none;
border-radius: 4px;
position: relative;
padding: 2px;
text-align: center;
}
input[type=checkbox]:checked:after {
content: "\2714";
background-color: #ffba03;
line-height: 1;
}
<input type="checkbox" />

How to make close effect after clicking on close in Styled-components?

I'm trying to make an in and out animation for my comment -
import {
Popup,
Overlay,
NavBarPopupHeader,
NavBarPopupContainer,
NavBarPopupImg,
NavBarPopupClose,
NavBarPopupContent,
NavBarPopupTitle,
NavBarPopupInfo,
NavBarPopupContentImg,
NavBarPopupFooter,
NavBarPopupSignOut,
} from "./styled.js";
function NavBarPopup(props) {
const { isPopupOpen } = props;
return (
<Overlay>
<Popup active={false}>
<NavBarPopupHeader>
<NavBarPopupClose
src="./popup_close.svg"
alt="close"
onClick={() => isPopupOpen(false)}
active={true}
/>
<NavBarPopupContainer>
<NavBarPopupImg />
</NavBarPopupContainer>
</NavBarPopupHeader>
<NavBarPopupContent>
<NavBarPopupTitle>You are logged as</NavBarPopupTitle>
<NavBarPopupInfo>'UserName'</NavBarPopupInfo>
<NavBarPopupInfo>'Email'</NavBarPopupInfo>
<NavBarPopupContentImg src="./popup_img.png" />
</NavBarPopupContent>
<NavBarPopupFooter>
<NavBarPopupSignOut>Sign Out...</NavBarPopupSignOut>
</NavBarPopupFooter>
</Popup>
</Overlay>
);
}
export default NavBarPopup;
I am using styled-components :
import styled, { keyframes, css } from "styled-components";
import { fadeIn, bounceInRight, bounceInLeft } from 'react-animations';
const fader = keyframes`${fadeIn}`;
const bounceIn = keyframes`${bounceInRight}`;
const bounceOut = keyframes`${bounceInLeft}`;
const Popup = styled.div`
position: fixed;
top: 0;
right: 0;
height: 100vh;
width: 400px;
background-color: #ffffff;
transition: ${(active) =>
active
? css`
${bounceIn} 0.5s linear
`
: css`${bounceOut} 0.5s`};
`;
const Overlay = styled.div`
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 0.8);
animation: 0.2s ${fader};
`;
const NavBarPopupHeader = styled.div`
width: 100%;
height: 15%;
background-color: #000000;
`;
const NavBarPopupContainer = styled.p`
text-align: center;
margin: 0;
padding: 30px 0 0 0;
`;
const NavBarPopupImg = styled.img`
width: 80px;
height: 80px;
border-radius: 50px;
border: none;
background-color: white;
color: black;
font-size: 25px;
`;
const NavBarPopupClose = styled.img`
position: absolute;
top: 5px;
left: 5px;
cursor: pointer;
`;
const NavBarPopupContent = styled.div`
height: 75%;
`;
const NavBarPopupTitle = styled.h3`
margin:0;
padding: 10px 0 10px 0;
text-align: center;
font-size: 30px;
`
const NavBarPopupInfo = styled.h4`
width: 80%;
margin: 10px auto;
padding:5px;
text-align: center;
font-size: 25px;
background-color: black;
color:white;
border-radius: 15px;
`
const NavBarPopupContentImg = styled.img`
width:100%;
margin-top:90px;
`
const NavBarPopupFooter = styled.div`
width: 100%;
height: 10%;
background-color: #000000;
`;
const NavBarPopupSignOut = styled.p`
width:200px;
text-align: center;
margin: 0 auto;
padding: 15px 0 0 0;
font-weight: 700;
font-size: 32px;
color: white;
text-decoration: underline;
cursor: pointer;
`;
export {
Popup,
Overlay,
NavBarPopupHeader,
NavBarPopupContainer,
NavBarPopupImg,
NavBarPopupClose,
NavBarPopupContent,
NavBarPopupTitle,
NavBarPopupInfo,
NavBarPopupContentImg,
NavBarPopupFooter,
NavBarPopupSignOut
};
I want my popup to be animated with $bounceIn when it appears, and disappear with $bounceOut when it closes. I tried to do it by passing an attribute to styled-components but now it only works when my Popup appears. Perhaps I chose the wrong approach, in which case please guide me in the right direction.

Using CSS attr() with styled components in react Hooks

I am trying to use attribute of SkillPer component pers={80} in css content attr function like so:
content: attr(pers) "%";
But content: attr() cannot get the attributes of <SkillPer pers={80}></SkillPer>
Am I missing something there? please help!
<SkillBar className="skill-bar">
<SkillPer
className="skill-per"
pers={80}
style={{ maxWidth: "80%" }}
></SkillPer>
</SkillBar>
export const SkillPer = styled.div`
height: 12px;
border-radius: 8px;
background-color: var(--color-green);
animation: ${fillBars} 2.5s 1;
position: relative;
&::before {
content: attr(pers) "%"; // here
position: absolute;
padding: 4px 6px;
background-color: #333;
color: var(--color-white);
font-size: 15px;
right: 0;
transform: translateX(200%);
}
`;
pers is a prop, so you can access the props in the style body. I believe this should work for you.
export const SkillPer = styled.div`
height: 12px;
border-radius: 8px;
background-color: var(--color-green);
animation: ${fillBars} 2.5s 1;
position: relative;
&::before {
content: ${({ pers }) => `"${pers} %"`};
position: absolute;
padding: 4px 6px;
background-color: #333;
color: var(--color-white);
font-size: 15px;
right: 0;
transform: translateX(200%);
}
`;

How can two classes share the same style for reactjs #emotion/styled

For my regular html code, I have this:
<div class="tooltip">Hover over me
<span class="tooltiptext">Tooltip text</span>
</div>
For the regular CSS, I have this:
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
/* Position the tooltip */
position: absolute;
z-index: 1;
top: 100%;
left: 50%;
margin-left: -60px; /* Use half of the width (120/2 = 60), to center the tooltip */
}
In my react app using #emotion/styled, I have the code
const TooltipContainer = styled('div')`
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
`
for the .tooltip class but how can I implement the .tooltip .tooltiptext part?
Based from the tutorial https://www.w3schools.com/css/css_tooltip.asp
You can use Inheritance of Styled component like below.
// The Button from the last section without the interpolations
const Button = styled.button`
color: palevioletred;
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid palevioletred;
border-radius: 3px;
`;
// A new component based on Button, but with some override styles
const TomatoButton = styled(Button)`
color: tomato;
border-color: tomato;
`;
try
'.tooltip, .tooltiptext'
or
'.tooltip .tooltiptext '

Button not laid out against top of relatively positioned container in Firefox

I'm building a toggle switch and the head (button) isn't positioned all the way to the top as I'd expect it to be in Firefox. Works fine in Chrome, so maybe has something to do with default browser styles?
Edit: I know how to make it work, I want to know why it happens.
codepen https://codepen.io/warhammered_cat/pen/qBZYZVy
const toggleSwitch = document.querySelector('.toggle-switch')
const toggleSwitchHead = document.querySelector('.head')
function handleToggle(e) {
toggleSwitch.classList.toggle('active')
}
toggleSwitchHead.addEventListener('click', handleToggle)
* {
box-sizing: border-box;
}
document, body {
margin: 0;
padding: 0;
}
.head {
width: 1.25rem;
height: 1.25rem;
border: 0.125rem solid gray;
border-radius: 50%;
background-color: white;
cursor: pointer;
transition: all 0.4s;
outline: none;
box-shadow: none;
}
.rail {
height: 0.75rem;
width: 100%;
background-color: gray;
border: 0.125rem solid gray;
position: absolute;
top: 0.25rem;
z-index: -1;
border-radius: 0.3rem;
transition: all 0.4s;
}
.toggle-switch {
position: relative;
height: 1.25rem;
width: 2rem;
}
.toggle-switch.active > .head {
background-color: #F7941E;
border-color: #F7841E;
transform: translateX(1rem);
}
.toggle-switch.active > .rail {
border: 0.125rem solid #F7941E;
background-color: white;
}
<div class='toggle-switch'>
<button class='head'></button>
<div class='rail'></div>
</div>
The <button> element is what is specifically being rendered differently in Firefox and Chrome.
If you change the <button> to a <div> the problem fixes itself.
The reason is that the button element is not a block element by default. You have to explicitly set the button to be a block element to get the desired behavior.
.head {
...
display: block;
...
}
try this instead,
Add display:inline-flex; to head class
.head{
display:inline-flex;
}
Click below to see codepen demo
Result :
const toggleSwitch = document.querySelector('.toggle-switch')
const toggleSwitchHead = document.querySelector('.head')
function handleToggle(e) {
toggleSwitch.classList.toggle('active')
}
toggleSwitchHead.addEventListener('click', handleToggle)
* {
box-sizing: border-box;
}
document, body {
margin: 0;
padding: 0;
}
.head {
width: 1.25rem;
height: 1.25rem;
border: 0.125rem solid gray;
border-radius: 50%;
background-color: white;
cursor: pointer;
transition: all 0.4s;
outline: none;
box-shadow: none;
display:inline-flex;
}
.rail {
height: 0.75rem;
width: 100%;
background-color: gray;
border: 0.125rem solid gray;
position: absolute;
top: 0.25rem;
z-index: -1;
border-radius: 0.3rem;
transition: all 0.4s;
}
.toggle-switch {
position: relative;
height: 1.25rem;
width: 2rem;
}
.toggle-switch.active > .head {
background-color: #F7941E;
border-color: #F7841E;
transform: translateX(1rem);
}
.toggle-switch.active > .rail {
border: 0.125rem solid #F7941E;
background-color: white;
}
<div class='toggle-switch'>
<button class='head'></button>
<div class='rail'></div>
</div>

Resources