my apps flex capabilities have changed since adding some code.
In my MovieContainer ideally I want the flex-direction to be row (it's currently set to column) and only the width of the page and then to start again on the next column ideally 6 across.
But when set to row it goes outside the scope of my page.
Also since adding this new code the text-overflow on my MovieName is now exceeding its width and overflow is no longer hidden.
I feel like I've effected these styled components somehow although when I change the font-size it works fine.
Styled Components:
const MovieContainer = styled.div`
display: flex;
flex-direction: column;
padding: 10px;
width: 280px;
box-shadow: 0 3px 10px 0 #aaa;
cursor: pointer;
`;
const CoverImage = styled.img`
height: 362px;
`;
const MovieName = styled.span`
font-size: 18px;
font-weight: 600;
color: black;
margin: 15px 0;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
`;
const InfoColumn = styled.div`
display: flex;
flex-direction: row;
justify-content: space-between;
`;
const MovieInfo = styled.span`
font-size: 12px;
font-weight: 500;
color: black;
text-transform: capitalize;
`;
Rest of the code:
const MovieComponent = () => {
const [filmWorld, setFilmWorld] = useState([]);
const [cinemaWorld, setCinemaWorld] = useState([]);
useEffect(() => {
const theaters = ["cinema", "film"];
theaters.forEach((theater) => {
async function fetchCinema() {
const data = await api.getFilm(theater);
if (data.Provider === "Film World") {
console.log("FILM WORLD: ", data.Provider);
console.log(`Data ${theater}World: `, data);
setFilmWorld(data);
} else {
console.log("CINEMAWORLD: ", data.Provider);
console.log(`Data ${theater}World: `, data);
setCinemaWorld(data);
}
}
fetchCinema();
});
}, []);
const movies = useMemo(() => {
if (!filmWorld.Provider) return [];
return filmWorld.Movies.map((movie, index) => ({
...movie,
cinemaWorldPrice:
cinemaWorld.Provider && cinemaWorld.Movies[index]?.Price,
}));
}, [filmWorld, cinemaWorld]);
Since making changes to the below code the styling seems to go out of wack. This is where the styled components are called:
return (
<MovieContainer>
{movies.map((movie, index) => (
<div className="movies" key={index}>
<MovieName>{movie.Title}</MovieName>
<CoverImage src={movie.Poster} alt={movie.Title} />
<InfoColumn>
<MovieInfo>Filmworld Price: ${movie.Price}</MovieInfo>
<MovieInfo>Cinemaworld Price: ${movie.cinemaWorldPrice}</MovieInfo>
</InfoColumn>
</div>
))}
</MovieContainer>
);
};
export default MovieComponent;
If I understood you correctly this is what you trying to achieve:
If that is the case all you have to do is add this line:
const MovieContainer = styled.div`
.
.
.
flex-direction: row;
flex-wrap: wrap;
`;
Related
i've been tasked with renaming a Button.jsx file for my react web app into 'v2_button.jsx' however when I do so it makes the entire web app unable to compile as there are many times where 'Button' is called but not 'v2_button'. My question is how do I make it so that I can update the name of the jsx file and update every instance where the original Button.jsx is called into 'v2_button'?
Button.jsx file:
import styled from 'styled-components';
const Button = styled.Button`
display: flex;
align-items: center;
justify-content: center;
height: 50px;
width: 300px;
background: ${(props) => {
let bg;
if (props.disabled) {
bg = props.theme.colors.disabled.background;
} else if (props.color === 'secondary') {
bg = props.theme.colors.primary.main;
} else if (props.color === 'danger') {
bg = props.theme.colors.danger.main;
} else {
bg = props.theme.colors.white;
}
return bg;
}};
font-family: ${(props) => props.theme.font.font3.new};
font-size: 18px;
font-weight: ${(props) => props.theme.fontWeight.heavy_900};
letter-spacing: 0.105em;
color: ${(props) => {
let fontColor;
if (props.color === 'secondary') {
fontColor = props.theme.colors.textPrimary.light;
} else {
fontColor = props.theme.colors.textPrimary.purple.dark;
}
return fontColor;
}};
padding: ${(props) => {
let padding;
if (props.size !== 's') {
padding = '0 39px';
}
return padding;
}};
border-radius: 40px;
border: ${(props) =>
props.disabled
? `1px solid ${props.theme.colors.disabled.border}`
: 'none'};
white-space: nowrap;
transition: background ${(props) => props.theme.animations.short} linear;
:hover {
background: ${(props) => {
let bg;
if (props.disabled) {
bg = props.theme.colors.disabled.background;
} else if (props.color === 'secondary') {
bg = props.theme.colors.primary.light;
} else if (props.color === 'danger') {
bg = props.theme.colors.danger.light;
} else {
bg = props.theme.colors.secondary.light;
}
return bg;
}};
cursor: pointer;
}
`;
export default Button;
Example where Button is called:
import styled from 'styled-components';
import Button from "../../Button";
export const Wrapper = styled.div`
display: flex;
flex-direction: column;
`;
export const Container = styled.div`
display: flex;
flex-direction: column;
`;
export const NameContainer = styled.div`
width: 100%;
display: flex;
align-content: space-between;
flex-direction: row;
font-style: normal;
font-weight: ${(props) => props.theme.fontWeight.med_500};
font-size: ${(props) => props.theme.fontSize.med_16};
`;
export const List = styled.div`
display: flex;
`;
export const FieldLabel = styled.label`
height: 36px;
color: ${(props) => props.theme.colors.white};
font-family: ${(props) => props.theme.font.font1.demi};
margin: 0 0 25px 0;
text-align: center;
font-style: normal;
font-weight: ${(props) => props.theme.fontWeight.heavy_800};
font-size: ${(props) => props.theme.fontSize.large_28};
line-height: 50px;
`;
export const Selected = styled.label`
width: 158px;
height: 40px;
margin: 0 10px 20px 10px;
padding-top: 10px;
padding-left: 20px;
font-size: ${(props) => props.theme.fontSize.med_20};
color: ${(props) => props.theme.colors.white};
font-family: ${(props) => props.theme.font.font1};
caret-color: ${(props) => props.theme.colors.textSecondary.main};
:focus {
outline: none;
}
:focus-within {
box-shadow: inset 0px 0px 2px 1px
${(props) => props.theme.colors.textSecondary.main};
}
::placeholder {
color: ${(props) => props.theme.colors.textPrimary.light};
}
background: ${(props) => props.theme.colors.primary.main};
border: ${(props) => props.theme.borders.textSecondary};
border-radius: 4px;
`;
export const AddFieldButton = styled(Button)`
margin: 30px;
font-size: ${(props) => props.theme.fontSize.med_20};
color: ${(props) => props.theme.colors.textSecondary.main};
background: none;
width: 1px;
text-align: center;
`;
Compiling error I receive after changing Button.jsx to v2_button.jsx:
https://imgur.com/a/kC0AdmG
The best thing is to renamed all the imports.
But a 'workaround' to renaming all the imports could be:
Keep the Button.jsx
Instead of exporting the default implementation, import the implementation from v2_button.jsx file and re-export it from the Button.jsx file.
Thus, every import of Button.tsx will actually refer to the implementation of v2_button.tsx file.
Example:
Button_v1.tsx
const Button_v1 = () => (
<>Button v1</>
);
export default Button_v1;
Button_v2.tsx
const Button_v2 = () => (
<>Button v2</>
);
export default Button_v2;
Since all other files reference Button_v1 and you don't want OR can't update the imports, you could do this:
Button_v1.tsx (after)
import Button_v2 from "./Button_v2"; // <-- ADDED THIS
const Button_v1 = () => (
<>Button v1</>
);
// export default Button_v1; // <-- COMMENTED THIS
export default Button_v2; // <-- ADDED THIS
This is my code, below it you will find the question
import { printIntrospectionSchema } from 'graphql';
import React, { useContext, useState } from 'react';
import styled from 'styled-components';
import { Context } from '../../../Context/Context';
// DATA
function DurationIntervalSelection() {
const tabsData = [
{
text: 'Duration',
},
{
text: 'Interval',
},
];
// STATE
const [selectedIndex, setselectedIndex] = useState(0);
const { selected } = useContext(Context);
console.log(selected.color, 'selected color');
// FUNCTION
const handleIndexSelection = (index) => {
setselectedIndex(index);
};
return (
<Container>
<TabsContainer backgroundColor={selected.backgroundColor}>
<Indicator
backgroundColor={selected.color}
style={{
left: `${selectedIndex * 50}%`,
}}
/>
{tabsData.map((tab, index) => {
return (
<Tab
style={{
color: selectedIndex === index ? 'white' : 'black',
}}
onClick={() => handleIndexSelection(index)}
key={tab.text}
>
<p style={{ zIndex: 100 }}>{tab.text}</p>
</Tab>
);
})}
</TabsContainer>
</Container>
);
}
const Container = styled.div`
margin-top: 10px;
border: 1px solid #f2f2f7;
border-radius: 10px;
box-sizing: border-box;
padding: 10px 0;
`;
const Indicator = styled.div`
width: 50%;
position: absolute;
/* z-index: -1; */
height: 100%;
border-radius: 20px;
cursor: pointer;
transition: all 0.3s;
background-color: ${(props) => props.backgroundColor};
`;
const TabsContainer = styled.div`
display: flex;
width: 100%;
justify-content: space-between;
position: relative;
box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.2);
border-radius: 20px;
overflow: hidden;
background-color: ${(props) => props.backgroundColor};
`;
const Tab = styled.div`
display: flex;
width: 50%;
justify-content: center;
cursor: pointer;
align-items: center;
padding: 10px;
`;
export default DurationIntervalSelection;
As you can see from the above code i pass backgroundColor as a prop that takes some color from a React state.
<TabsContainer backgroundColor={selected.backgroundColor}>
And in the styled component I do:
background-color: ${(props) => props.backgroundColor};
But my question is, since i need to follow a design made by a colleague, how do i add opacity to the above background color without affecting the opacity of the whole div: TabsContainer. I mean, if i add on the TabsContainer -> opacity: 0.3 - for instance the whole TabsContainer div will be affected by the opacity, including the Tab div, while what I want is just to change opacity on the color itself so i do not affect any child. I hope it makes sense. Is there a way?
Okay if your hex value comes from your backend, you can either create your own javascript function that converts hex to rgba or you can use a library like polished. They have a function named rgba that converts a hex string and adds an alpha value to it
import { rgba } from 'polished'
const div = styled.div`
background: ${(props) => rgba(props.backgroundColor, 0.3)};
`
Or you can apply it directly to the prop
<TabsContainer backgroundColor={rgba(selected.backgroundColor, 0.3)}>
Example CodeSandbox.
Since the data from the backend where right but I could not find a solution I have realised that I could have added to
<TabsContainer backgroundColor="rgba(255,255,255, 0.5)">
A background color on the white notes with opacity so to emphasise the color behind it in a lighter way
I am just getting started learning React with Function Component and Styled-Components. I created Modal Component and when I clicked the modal it shows up and I cannot scroll down/up(Background) but when I close the modal, I cannot scroll. I expect it should be work when I close the modal. Could you help me with what part something wrong?
const [showModal, setShowModal] = useState(false);
const [active, setActive] = useState(true);
const openModal = active => {
setShowModal(prev => !prev);
if (active) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'auto';
}
};
<ShowAllSizes>
<AllSize>모든 사이즈</AllSize>
<FaRegArrowAltCircleDown // isActive={active} onClick={()=> openModal}
className="FaIconModal"
/>
<Modal showModal={showModal} setShowModal={setShowModal} />
</ShowAllSizes>
I will leave the whole code just in case
import React, { useState, useEffect } from 'react';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { FaRegArrowAltCircleDown } from 'react-icons/fa';
import { Modal } from './components/Modal';
export default function ColumnTop() {
const [showModal, setShowModal] = useState(false);
const [active, setActive] = useState(true);
const openModal = active => {
setShowModal(prev => !prev);
if (active) {
document.body.style.overflow = 'hidden';
} else {
document.body.style.overflow = 'auto';
}
};
return (
<RightSideSection>
<ColumnTopSection>
<TitleBox>
<BrandName>Jordan</BrandName>
<EngName>Jordan 1 Retro High OG Bordeaux</EngName>
<KrName>조던 1 레트로 하이 OG 보르도</KrName>
</TitleBox>
<FigureWrap>
<DetailSize>
<SizeInKorean>사이즈</SizeInKorean>
<ShowAllSizes>
<AllSize>모든 사이즈</AllSize>
<FaRegArrowAltCircleDown
// isActive={active}
onClick={() => openModal}
className="FaIconModal"
/>
<Modal showModal={showModal} setShowModal={setShowModal} />
</ShowAllSizes>
</DetailSize>
<DetailPrice>
<TransactionPriceKorean>최근 거래가</TransactionPriceKorean>
<PriceInfo>
<CurrentPrice>248,000</CurrentPrice>
<PricePunctuation>6000원(-2.4%)</PricePunctuation>
</PriceInfo>
</DetailPrice>
<ButtonWrapper>
<Button>
<ButtonDivision>
<LeftSide>
<BtnTitle to="/order">구매</BtnTitle>
</LeftSide>
<RightSide>
<OrderPrice>23500원</OrderPrice>
<OrderStatusName>즉시 구매가</OrderStatusName>
</RightSide>
</ButtonDivision>
</Button>
<Button primary>
<ButtonDivision>
<LeftSide>
<BtnTitle to="/order">판매</BtnTitle>
</LeftSide>
<RightSide>
<OrderPrice>11500원</OrderPrice>
<OrderStatusName>즉시 판매가</OrderStatusName>
</RightSide>
</ButtonDivision>
</Button>
</ButtonWrapper>
</FigureWrap>
</ColumnTopSection>
</RightSideSection>
);
}
const ShowAllSizes = styled.div``;
const RightSideSection = styled.div`
padding-left: 40px;
`;
const ColumnTopSection = styled.div`
display: flex;
flex-direction: column;
`;
const TitleBox = styled.div`
display: flex;
flex-direction: column;
`;
const BrandName = styled.span`
display: inline-block;
vertical-align: top;
line-height: 19px;
padding-top: 1px;
margin-bottom: 9px;
font-size: 18px;
font-weight: 800;
border-bottom: 2px solid #222;
width: 61px;
height: 21px;
`;
const EngName = styled.span`
margin-bottom: 4px;
font-size: 18px;
font-weight: 400;
`;
const KrName = styled.span`
line-height: 17px;
font-size: 14px;
color: rgba(34, 34, 34, 0.5);
`;
const FigureWrap = styled.div`
width: 500px;
`;
const DetailSize = styled.div`
padding-top: 19px;
padding-bottom: 12px;
border-bottom: 1px solid #ebebeb;
`;
const SizeInKorean = styled.span`
padding-top: 4px;
display: inline-block;
line-height: 16px;
font-size: 13px;
color: rgba(34, 34, 34, 0.8);
`;
const AllSize = styled.span`
display: inline-block;
margin-right: 5px;
margin-left: 392px;
font-size: 18px;
`;
const FaIconModal = styled.button``;
const DetailPrice = styled.div`
display: flex;
margin-top: 4px;
`;
const TransactionPriceKorean = styled.span`
position: absolute;
width: 60px;
padding-top: 5px;
height: 7px;
font-size: 13px;
color: rgba(34, 34, 34, 0.8);
`;
const PriceInfo = styled.div`
display: flex;
flex-direction: column;
margin-left: 330px;
`;
const CurrentPrice = styled.span`
margin-left: 100px;
margin-top: 4px;
`;
const PricePunctuation = styled.span`
color: #31b46e;
margin-left: 110px;
`;
const ButtonWrapper = styled.div`
margin-top: 15px;
`;
const ButtonDivision = styled.div`
display: flex;
`;
const Button = styled.button`
background: ${props => (props.primary ? '#41B979' : '#EF6253')};
width: 235px;
height: 40px;
margin-right: 15px;
padding: 0 10px;
border: none;
border-radius: 5px;
`;
const LeftSide = styled.div`
margin-top: 3px;
left: 5px;
`;
const BtnTitle = styled(Link)`
font-weight: bold;
color: #fff;
text-decoration: none;
`;
const RightSide = styled.div`
margin-left: 10px;
border-left: 1px solid silver;
color: #fff;
`;
const OrderPrice = styled.div`
margin-left: 10px;
`;
const OrderStatusName = styled.div`
margin-left: 20px;
`;
This is my Modal Component
import React, { useRef, useState, useEffect, useCallback } from 'react';
import styled from 'styled-components';
import { MdClose } from 'react-icons/md';
export const Modal = ({ showModal, setShowModal }) => {
const modalRef = useRef();
const [detailPageData, setDetailPageData] = useState([]);
useEffect(() => {
fetch(`/data/detailPageData.json`)
.then(res => res.json())
.then(data => setDetailPageData(data.result.price_by_size));
}, []);
const closeModal = e => {
if (modalRef.current === e.target) {
setShowModal(false);
}
};
const keyPress = useCallback(
e => {
if (e.key === 'Escape' && showModal) {
setShowModal(false);
}
},
[setShowModal, showModal]
);
useEffect(() => {
document.addEventListener('keydown', keyPress);
return () => document.removeEventListener('keydown', keyPress);
}, [keyPress]);
// console.log(setDetailPageData);
return (
<div>
{showModal ? (
<Background onClick={closeModal} ref={modalRef}>
<ModalWrapper showModal={showModal}>
<SizeModalContent>
<Title>사이즈</Title>
<ButtonWrapper>
{detailPageData.map(({ id, title, price }) => {
return (
<Button key={id}>
<Size>{title}</Size>
<Price>{price}</Price>
</Button>
);
})}
</ButtonWrapper>
</SizeModalContent>
<CloseModalButton
aria-label="Close modal"
onClick={() => setShowModal(prev => !prev)}
/>
</ModalWrapper>
</Background>
) : null}
</div>
);
};
const Background = styled.div`
position: fixed;
display: flex;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
left: -0px;
top: -0px;
background: rgba(0, 0, 0, 0.8);
z-index: 10;
`;
const ModalWrapper = styled.div`
width: 400px;
height: 400px;
background: #fff;
color: #000;
border-radius: 10px;
`;
const SizeModalContent = styled.div`
margin-left: 20px;
margin-right: 20px;
`;
const Title = styled.div`
margin: 15px;
font-weight: 700;
color: #000;
text-align: center;
`;
const ButtonWrapper = styled.div`
margin: 10px 0px;
display: grid;
grid-template-columns: repeat(3, 33%);
`;
const Button = styled.button`
width: 100px;
height: 35px;
margin-left: 5px;
margin-bottom: 5px;
border: 1px solid #d3d3d3;
border-radius: 10px;
background-color: #fff;
`;
const Size = styled.span`
display: block;
font-weight: 700px;
`;
const Price = styled.span`
color: red;
`;
const CloseModalButton = styled(MdClose)`
cursor: pointer;
position: absolute;
top: 350px;
right: 800px;
font-size: 20px;
/* top: -50px;
right: -360px;
width: 32px;
height: 32px; */
`;
Edit1: Copy and Past my Modal.js
I am trying to create a Styled Component which can receive a prop for the type of border. The Section can either have border-top or border-bottom based on the prop passed to it.
Here is the code
type TSectionBorder = 'top' | 'bottom';
<Section edge="top">
{children}
</Section>
How can I achieve this in styled components, something along these lines -
const Section = styled.section<{edge: TSectionBorder}>`
border-${edge}: 1px solid black;
`;
const Section = styled.section<{edge: TSectionBorder}>`
${({ edge }) => `border-${edge}: 1px solid black`};
`;
I deconstructed the props just to keep your syntax, however, this is one way to do so. They also have a css helper to look into.
Documentation:
https://styled-components.com/docs/basics#passed-props
https://styled-components.com/docs/api#css
First you will import:
import styled from 'styled-components';
Here is the function ButtonBlock:
function ButtonBlock(props) {
return (
<BtnBlock type="primary" {...props}>
{props.children}
</BtnBlock>
);
}
const handleColorType = (color) => {
switch (color) {
case 'grey':
return '#D4D5D6';
default:
return '#6633CC';
}
};
const hoverColorType = (color) => {
switch (color) {
case 'grey':
return '#6a7581';
default:
return '#99CC00';
}
};
Styled Component:
const BtnBlock = styled(Button)`
&& {
max-width: none;
width: 100%;
text-align: center;
display: block;
font-size: 14px;
font-weight: 600;
line-height: 50px;
height: 50px;
text-transform: uppercase;
border-radius: 0;
border: 0px;
padding: 0;
background: ${({ color }) => handleColorType(color)};
&[disabled] {
background-color: #c6c6c6;
}
:hover {
background-color: ${({ color }) => hoverColorType(color)};
}
&[disabled]:hover {
background-color: #c6c6c6;
}
}
`;
I'm trying to style my component which was styled already. But styles in the new rules are not applied in output CSS.
Can I style component that I already styled?
Thanks you in advance for your help.
EDIT: Add rest of LanugageChooser definition
// COMPONENT THAT I'M TRYING TO STYLE
const LanguageChooser = () => {
const Container = styled.div`
display: flex;
align-items: center;
height: 36px;
& > div:not(:last-child) {
margin-right: 5px;
}
`;
return (
<Container>
<Flag { ...languages.pl }/>
<Flag { ...languages.en }/>
</Container>
)
}
const Flag = ({ flag, language }) => {
const { i18n } = useTranslation();
const Button = styled.div`
cursor: pointer;
font-size: 24px;
transition: .2s all;
&:hover {
font-size: 36px;
}
`;
return (
<Button onClick={ () => i18n.changeLanguage(language) }>{ flag }</Button>
)
}
// TRYING ADD MARGIN LEFT, BUT THERE IS NO RESULT.
// ANY OTHER CSS PROPERTY ARE NOT APPLYING
const Navbar = ({ color }) => {
...
const StyledLanguageChooser = styled(LanguageChooser)`
margin-left: auto;
`;
const Nav = styled.nav`
display: flex;
align-content:center;
background: ${ color };
padding: 2px 3px;
`;
return (
<Nav className="navbar">
<StyledNavLink to="/home">...</StyledNavLink>
<StyledNavLink to="/maps">...</StyledNavLink>
<StyledNavLink to="/charts">...</StyledNavLink>
<StyledLanguageChooser/>
</Nav>
)
}
First, move the styled component outside of function scope or your style will reset on every render and you will get heavy performance issues.
Secondly, in order to apply styles, you need to pass className property.
See Styling normal React components
If you use the styled(MyComponent) notation and MyComponent does not render the passed-in className prop, then no styles will be applied.
const Container = styled.div`
display: flex;
align-items: center;
height: 36px;
& > div:not(:last-child) {
margin-right: 5px;
}
`;
const LanguageChooser = ({ className }) => {
return (
<Container className={className}>
<Flag {...languages.pl} />
<Flag {...languages.en} />
</Container>
);
};
const Button = styled.div`
cursor: pointer;
font-size: 24px;
transition: 0.2s all;
&:hover {
font-size: 36px;
}
`;
const Flag = ({ flag, language }) => {
const { i18n } = useTranslation();
return <Button onClick={() => i18n.changeLanguage(language)}>{flag}</Button>;
};