I am trying to use React Emotion to set up a horizontal rule with text in the middle. It would look like this:
----------------------SOME TEXT----------------------------------
I found examples on StackOverflow on how to do this with CSS, but it's not working for me.
Here is my DIV that I'm using, but it's not working.
Is this possible to set up with react Emotion? What am I missing?
<div
css={{
display: 'flex',
lineHeight:'1em',
color:'gray',
'&:before, &:after': {
content:'',
display:'inline-block',
flexGrow:'1px',
marginTop:'0.5em',
backgroundColor:'gray',
height:'1px',
marginRight:'10px',
marginLeft:'10px'
}
}}
>
SOME TEXT
</div>
Your code is right about the ::before and ::after syntax,
However, when trying to replicate I found two errors:
The flexGrow shouldn't have a px unit, just 1: flexGrow:'1
The content should have double quotes content:'""'
<div
css={{
display: 'flex',
lineHeight:'1em',
color:'gray',
'&:before, &:after': {
content:'""',
display:'inline-block',
flexGrow:'1',
marginTop:'0.5em',
backgroundColor:'gray',
height:'1px',
marginRight:'10px',
marginLeft:'10px'
}
}}
>
You need pass style object in css function from emotion/css or emotion/react.
You forgot to wrap your styled object in a function from emotion.
If you use an object and not a string, then wrap content in double quotes
// First way, use css from #emotion/css, pass as object
import { css } from '#emotion/css'
<div
className={css({
display: 'flex',
lineHeight: '1em',
color: 'gray',
':before, :after': {
content: '""',
flexGrow: '1',
marginTop: '0.5em',
backgroundColor: 'gray',
height: '1px',
marginRight: '10px',
marginLeft: '10px'
}
})}
>
SOME TEXT
</div>
// Second way, use css from #emotion/css, pass as string
import { css } from '#emotion/css'
<div
className={css`
display: flex;
line-height: 1em;
color: gray;
:before, :after: {
content: '';
flex-grow: 1;
margin-top: 0.5em;
background-color: gray;
height: 1px;
margin-right: 10px;
margin-left: 10px;
}
`}
>
SOME TEXT
</div>
// Third way, use css from #emotion/react
import { css } from '#emotion/react'
<div
css={css`
display: flex;
line-height: 1em;
color: gray;
:before, :after: {
content: '';
flex-grow: 1;
margin-top: 0.5em;
background-color: gray;
height: 1px;
margin-right: 10px;
margin-left: 10px;
}
`}
>
SOME TEXT
</div>
For string interpolation passed for css function you can use it like
let highlight = css`
border: 1px solid grey;
&:after {
position: absolute;
}`;
Related
I'm making a website with Next.js and wanting to embed vimeo videos and wondering what is the most common practice to do it.
I've tried regular iframe, react-player, #u-wave/react-vimeo.
I realized #u-wave/react-vimeo gives me 410 error sometimes when I leave the page open long and react-player when it loads it flickers a bit (small first and then it gets bigger to its size) :(
410 error I get on #u-wave/react-vimeo:
Thank you
This is how I have implemented Vimeo player:
import React, { useRef, useState} from "react";
import styled from "styled-components";
import { Play } from "phosphor-react";
import ReactPlayer from "react-player/lazy";
import styles from "./styles.module.scss";
export default function VideoPlayer() {
const playerRef = useRef()
return (
<div className={styles.playerWrapper}>
<ReactPlayer
id="MyId"
className={styles.reactPlayer}
width="100%"
height="100%"
controls
playing
light="img/main-page/image.webp"
playIcon={
<Btn
className={styles.btnSmall}
bgColor="var(--ifm-btn-primary)"
textColor="var(--ifm-text-black)"
borderRadius={40}
fontSize={20}
px={25}
py={25}
style={{
maxWidth: 361,
width: "100%",
display: "flex",
justifyContent: "center",
alignItems: "center",
gap: 10
}}
>
<Play weight="fill" color="--ifm-text-black" size={24} />
</Btn>
}
url="https://vimeo.com/yourid"
></ReactPlayer>
</div>
);
}
//styles.module.css
.playerWrapper {
position: relative;
padding-top: 56.25%;
/* Player ratio: 100 / (1280 / 720) */
box-shadow: 0px 0px 50px 0px rgba(0, 0, 0, 0.7);
border-radius: 20px;
margin-top: 92px;
margin-bottom: 21px;
}
.reactPlayer {
position: absolute;
top: 0;
left: 0;
border-radius: 20px;
overflow: hidden;
}
.btnSs {
max-width: 361px;
width: 100%;
display: flex;
justify-content: center;
align-items: center;
gap: 10;
}
#media(max-width: 768px) {
.btnSmall {
width: 150px !important;
font-size: 13px !important;
padding: 4px 10px !important;
gap: 0px !important;
}
}
#media(max-width: 451px){
.playerWrapper {
margin-bottom: 0px;
}
}
Relatively new to React, and am wanting to recreate the design below
enter image description here
I have the base formatting down, but as you will notice, there are lines separating the logo blocks, from the login and signup blocks, with the signup and login buttons pushed to the bottom.
Below is my current code
CSS:
width: 100vw;
height: 100vh;
}
body {
margin: 0;
padding: 0;
}
.Sidebar {
height: 100%;
width: 20%;
background-color: white;
border-right: 1px solid #F0F4FB;
padding-left: 15px;
box-sizing: border-box;
}
.SidebarList {
height: auto;
width: 100%;
padding-left: 15px;
font-size: 18px;
border: 2px #FD954E;
box-sizing: border-box
}
.SidebarList .row {
width: 100%;
height: 50px;
background-color: white;
list-style-type: none;
margin: 0%;
padding-bottom: 15px;
display: flex;
color: #A7ACB6;
justify-content: center;
align-items: center;
font-family: Arial, Helvetica, sans-serif;
}
.SidebarList .row:hover {
cursor: pointer;
background-color: #E7E7E7 ;
}
.SidebarList #active {
background-color: white;
color: #FD954E
}
.SidebarList .Login {
background-color: white;
color: #FD954E;
width: 279px;
height: 39px;
right: 1596px;
top: 958px;
border: 1px solid #FD954E;
box-sizing: border-box;
border-radius: 19.5px;
}
.SidebarList .SignUp {
width: 279px;
height: 39px;
right: 1596px;
top: 1011px;
background: #FD954E;
border-radius: 19.5px;
border: none;
}
.row #icon {
flex: 30%;
display: grid;
place-items: center;
transform: scale(1.2)
}
.row #title {
flex: 70%;
}
.Logo {
padding-left: 25px;
padding-top: 25px;
padding-bottom: 30px;
border-bottom: 1px solid #F0F4FB;
width: 55%;
}
Sidebar.js
import React from "react";
import "../App.css";
import { SidebarData } from './SidebarData'
import Logo from './Logo.svg'
function Sidebar() {
return (
<div className="Sidebar">
<div>
<img src = {Logo} alt='Logo’ className=‘Logo’ />
</div>
<ul className="SidebarList">
{SidebarData.map((val, key) => {
return (
<li
key={key}
className="row"
id={window.location.pathname == val.link ? "active" : ""}
onClick={() => {
window.location.pathname = val.link;
}}
>
<div id="icon">{val.icon}</div> <div id="title">{val.title}</div>
</li>
);
})}
</ul>
<div className= "SidebarList">
<button className="Login">
Login
</button>
</div>
<div className= "SidebarList">
<button className="SignUp">
Sign Up
</button>
</div>
</div>
);
}
export default Sidebar;
How should I structure my code in order to acheive my desired result? Ex: with the logo at the top with the seperator, the list of navigation elements, and then the login and signup buttons at the bottom with the seperator?
Currently, my sidebar looks as follows, with the seperator not full width between the logo and navigation elements, and the buttons extending beyond the sidebar.
enter image description here
It would be easier to simplify the problem with just HTML and CSS as that's much easier to troubleshoot. Part of your problem is that you are defining the width of the sidebar as a percentage of the screen width but elements within the sidebar are defined with a width in pixels. When the browser window is too small, your buttons will appear outside the full width of the sidebar. You could either code all your values as percentages or in pixels. Alternatively, you could use a mix and just set a min-width for the sidebar so that you don't end up with elements out of place.
The reason that your line break is not the full width of your sidebar is because you are defining it with the border-bottom property of the logo. Your logo is not 100% the width of the sidebar so your line break will only be the width of the logo. A better solution would be to define a div that is set to width: 100%. This way, you will have more control.
Here is a simplified solution to your sidebar problem using pixels to define the widths.
HTML:
<div class="sidebar">
<div class="header">
<div class="logo">Logo</div>
</div>
<div class="line-break" />
<div class="content">
<ul class="nav">
<li>Home</li>
<li>Blog</li>
</ul>
</div>
<div class="line-break" />
<div class="footer">
<button class="login">Login</button>
<button class="sign-up">Sign up</button>
</div>
</div>
CSS:
html, body {
height: 100%;
margin: 0;
padding: 0;
}
.sidebar {
width: 300px;
height: 100%;
border-right: 1px solid grey;
}
.line-break {
height: 1px;
width: 100%;
background-color: grey;
}
.header .logo {
height: 40px;
width: 200px;
background-color: grey;
margin: 20px;
}
ul.nav {
list-style-type: none;
padding: 20px 0 0 40px;
}
ul.nav li {
margin-bottom: 14px;
}
ul.nav li:last-child {
margin-bottom: 0;
}
.footer {
display: flex;
flex-direction: column;
align-items: center;
padding: 20px 0;
}
.footer button {
padding: 6px 0;
width: 80%;
margin-bottom: 14px;
}
.footer button:last-child {
margin-bottom: 0;
}
And here is a link to a CodePen
where you can see this in action.
so I'm using slick slider on my ReactJS app, by the config I set custom class from my modular CSS, slick slider dots active class (".slick-active) is automatically toggled
but my CSS wont applied, how is the proper way to style the active state of the dots
thanks in advance,
import style from "./MainSlider.module.scss";
const settings = {
dots: true,
infinite: true,
autoplaySpeed: 5000,
speed: 1500,
autoplay: true,
dotsClass: style.slickDots,
slidesToShow: 1,
slidesToScroll: 1
};
<Slider {...settings}>
{
sliderItems.map((sliderItem, idx) => (
<div key={idx} className={style.sliderItem}>
<div className={style.image}>
<img src={sliderItem.image} alt="" />
</div>
<div className={style.text}>
<p>{sliderItem.title}</p>
<Link to="/service" className={style.more}>
<img src={ViewMore} alt=""/>
</Link>
</div>
</div>
))
}
</Slider>
this is my MainSlider.module.scss
.slider {
width: 70vw;
height: 70vh;
.slickDots {
position: absolute;
bottom: -50px;
display: block;
width: 100%;
padding: 0;
margin: 0;
list-style: none;
li{
position: relative;
display: inline-block;
margin-right: 30px;
padding: 0;
cursor: pointer;
&.slick-active {
background: magenta !important;
button {
&::before {
background: red;
}
}
}
button {
font-size: 0;
line-height: 0;
display: block;
width: 15px;
height: 15px;
border-radius: 50%;
border:2px solid white;
cursor: pointer;
outline: none;
background: transparent;
&::before {
position: absolute;
top: 0;
left: 0;
width: 15px;
height: 15px;
border-radius: 50%;
content: ' ';
color: black;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
}
}
}
}
I add image instead of code so it will be more readable and can help you understand more about my problem
code
For applying your styles to global styles, like slick-active in css-modules you should use this approach:
:global(.slick-active) {
background-color: red;
}
i'm trying to se an element active with a different color, but its not working.Someone could help me ? And if it works, probably every element will change the color, how could i do it with something like "this", or any other.
Thats my element and my state:
//-> my state: I used true as default because i was trying to do it works, but i failed
export default function VertNavigation() {
const [active, setActive] = useState(true);
console.tron.log(active);
return (
<Container>
<button type="button">DEPOSITAR</button>
<QuickNavi>
<VerticalNavigation>
<Title>ACESSO RÁPIDO</Title>
<li>
<Icon.MdMonetizationOn size={20} />
<p>Emitir Cobrança</p>
</li>
<li>
<Icon.GoGraph size={20} active={active} />
<p>Gestão de cobrança</p>
</li>
<li>
<Icon.MdCompareArrows size={20} />
<p>Tranferencia</p>
</li>
<li>
<Icon.FaBarcode size={20} />
<p>Pagamentos</p>
</li>
// -> my styled component -- its always in "else", i don't know why
export const VerticalNavigation = styled.ul`
width: 100%;
height: 100%;
li:nth-child(4)::after {
content: 'novo';
right: 0;
margin-left: 6px;
text-align: center;
font-size: 12px;
top: 10px;
width: 45px;
height: 15px;
background: #47b248;
color: #fff;
border-radius: 10px 10px 10px 10px;
}
li {
width: 100%;
height: 45px;
display: flex;
align-items: center;
cursor: pointer;
&:hover svg {
transform: translateY(-5px);
color: #47b248;
}
svg {
transition: 0.3s ease;
color: ${props => (props.active ? '#47B248' : '#939393')};
}
p {
padding-left: 15px;
font-size: 15px;
color: ${darken(0.5, '#A6A6A5')};
}
}
`;
Thank you in advance.
The problem was my Icon like the guys said. I had to create a li component and pass my prop for him, then it works.
Thank you everyone for help me.
->index.js
<Item active={active}>
<Icon.GoGraph size={20} />
<p>Gestão de cobrança</p>
</Item>
->styles
export const Item = styled.li`
width: 100%;
height: 45px;
display: flex;
align-items: center;
cursor: pointer;
svg {
transition: 0.3s ease;
color: ${props => (props.active ? '#47B248' : '#939393')};
}
I am new to styled-component. I want to convert custom CSS to Styled-Component. I have already used className in React File.
App.js
<div className="left-menus">
{menus.map(item => {
return (
<Link to={item.name} name={item.name} key={item.name}
className={this.state.activeMenu === item.name ? 'menu active' : 'menu' }
onClick={() => this.setState({ activeMenu: item.name })}
>
<Icon name={item.icon} size="large"/>
<span>{item.name}</span>
</Link>
)
})}
</div>
App.css
.left-menus {
position: fixed;
width: 200px;
padding-right: 0 !important;
background-color: #fff;
height: 100%;
margin-top: 20px;
top: 47px;
font-size: 12px !important;
border-right: 2px solid #e8e8ec; }
.left-menus .menu {
color: #4a4a4a;
width: 100%;
display: block;
cursor: pointer;
text-transform: capitalize !important;
padding: 15px 10px 15px 18px; }
.left-menus .menu .angle.down.icon,
.left-menus .menu .angle.up.icon {
right: 10px;
position: absolute; }
.left-menus .menu .icon:first-child {
margin-right: 10px; }
.left-menus .menu.active {
border-right: 4px solid #3cbfc8;
background-color: #f8f8f8; }
.left-menus .sub-menu-container .sub-menu {
display: none;
height: 0;
transition: 0.3s all ease; }
.left-menus .sub-menu-container.active .sub-menu {
display: block;
height: 100%;
text-transform: capitalize;
transition: 0.3s all ease; }
.left-menus .sub-menu-container.active .sub-menu a {
color: #4a4a4a; }
.left-menus .sub-menu-container.active .sub-menu .icon {
margin: 10px 10px 0 10px; }
CSS with a class selector will only match elements that are members of a class.
To give a styled component that class, you need to do it when you instantiate it.
For example:
const MyStyledComponent = styled.div`
background: red;
`;
const myFunctionComponent = () => {
const myClass = "left-menus";
return(
<div>
Here is MyStyledComponent
<MyStyledComponent className={myClass}>
Example
</MyStyledComponent>
</div>
);
}
If you have an existing stylesheet you want to apply, it is likely that styled components are not the right tool for the job.