# styled-components์์ css ๋ณ์ ์ฌ์ฉํ๊ธฐ (var)
css ๋ณ์๋ฅผ ์ฌ์ฉํ๋ฉด ์์ฃผ ์ฐ๋ ์ปฌ๋ฌ ํ๋ ํธ๋ฅผ ๋ง๋ค์ด ์คํ ์์ด ์ฝ๋ฉ์ ํ ์ ์์ต๋๋ค. ์ด๋ฅผ ์กฐ๊ธ ๋ ํ์ฅํ๋ฉด ๋คํฌ ๋ชจ๋ ๊ฐ์ด state ํ๋๋ก ์ฑ, ์น์ ์ ์ฒด theme์ ๋ฐ๊พธ๋ ๊ธฐ๋ฅ๋ ์์ํ๊ฒ ๋ง๋ค ์ ์์ต๋๋ค.
์ด๋ฒ ๊ธ์์๋ nextJS์์ styled-components์ var ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์ ์ฝ๊ฒ ์คํ์ผ ๊ฐ์ ๋ณ์ํ ํ๋ ๋ฐฉ๋ฒ์ ๋ํด ์์๋ณด๊ฒ ์ต๋๋ค!
dependence ์ค์น
yarn add node-sass
yarn add styled-components
ํ๋ ํธ scss ์์ฑ
์คํ์ผ์ ๋ณ์ํ ํ scss ํ์ผ์ ๋ง๋ค์ด์ค๋๋ค.
// src/styles/common.scss
:root {
--primary-color: #2294e3;
--border-color: #9a9a9a;
}
_app.tsx์์ scss import
import "../styles/common.scss";
...
์์ฒ๋ผ next์์๋ _app.tsx, react์์๋ index.tsx์ ์ฌ์ฉํ๋ scss๋ฅผ Import ํด์ฃผ๋ฉด ์ฌ์ฉ ์๋ฃ์ ๋๋ค.
styled-components์์ ์ฌ์ฉํ๊ธฐ
import styled from "styled-components";
const Test = () => {
return <TestBlock>var test ํ๋ฉด</TestBlock>;
};
const TestBlock = styled.article`
background: var(--primary-color);
`;
์ฐธ๊ณ ๋ธ๋ก๊ทธ
styled-components์์ css ๋ณ์ ์ฌ์ฉํ๊ธฐ (var), react, redux
styled-components์์ css ๋ณ์ ์ฌ์ฉํ๊ธฐ (var), react, redux, ํ๊ฒฝ์ค์ , global theme, ๊ธ๋ก๋ฒ ์คํ์ผ, props, mixins
kyounghwan01.github.io
# Styled-Component๋ก ํ์ ์๋ฆฌ๋จผํธ css ์ ํ๊ธฐ
์ผ๋จ Toggle์ด๋ผ๋ ์ด๋ฆ์ styled-component๋ฅผ ๋ง๋ค์ด๋ณด์
const Toggle = Styled.div``
์ด์ ๋ฐฑํฑ ์ฌ์ด์ css ๋ฃ์ผ๋ฉด css ๊ฐ ์ง์ ๋ ์๋ฆฌ๋จผํธ๋ฅผ ์ฌ์ฉํ ์ ์์
const Toggle = Styled.div`
background:transparent;
border-color:transparent;
`
์ฌ๊ธฐ์ Toggle ์ปดํฌ๋ํธ ์์ ๋ค๋ฅธ ์๋ฆฌ๋จผํธ๋ค์ด ๋ค์ด๊ฐ ์์ ๋
<Toggle>
<input
type="checkbox"
id="nav-toggle"
className="nav-toggle"
/>
<label htmlFor="nav-toggle">
<span></span>
<span></span>
<span></span>
</label>
</Toggle>
styled-components๋ก ์์ ๋ค์ด ์๋ ์๋ฆฌ๋จผํธ๋ค์ ํ๋ฒ์ css ๋ฅผ ์ ํ ์ ์๋ค.
styled-component์์์๋ & ๋ผ๋ ๊ธฐํธ์ ์๋ฏธ๊ฐ ์ค์ค๋ก (class ์ปดํฌ๋ํธ์์ this ๊ฐ์ ๋๋) ๋ผ์ & ๋ค์ > ํ๋ก ์์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
const Toggle = Styled.div`
background:transparent;
border-color:transparent;
& > .nav-toggle{
display:none;
}
์ฐธ๊ณ ๋ธ๋ก๊ทธ
Styled-Component๋ก ํ์ ์๋ฆฌ๋จผํธ css ์ ํ๊ธฐ
default npm install styled-components๋์ด์๊ณ import Styled from 'styled-components' ๋ก ํ์ผ์์ ์ ์ธ์ด ๋์ด์์ด์ผ ํจ ์ผ๋จ Toggle์ด๋ผ๋ ์ด๋ฆ์ styled-component๋ฅผ ๋ง๋ค์ด๋ณด์ const Toggle = Styled.div`` ์ด์ ๋ฐฑํฑ ์ฌ์ด์
develop-famous.tistory.com
# styled-components์์ styleํ์ฅ(์ฌ์ฌ์ฉ)ํ๊ธฐ
import React from "react";
import styled from "styled-components";
const App = () => (
<Container>
<Apple />
<GreenApple />
<BlueApple />
</Container>
);
const Apple = ({ className }) => <div className={className}>Apple!</div>;
const Container = styled.div`
display: flex;
`;
const GreenApple = styled(Apple)`
background-color: green;
`;
const BlueApple = styled(Apple)`
background-color: blue;
`;
export default App;
์ฐธ๊ณ ๋ธ๋ก๊ทธ
[react package] styled-components์์ styleํ์ฅ(์ฌ์ฌ์ฉ)ํ๊ธฐ
styled-components๋ก ์์ฑํ ์ปดํฌ๋ํธ๋ ์ค๋ณต๋๋ ์คํ์ผ์ด ์์ ๋ ์ฌ์ฌ์ฉํ์ฌ ์ฝ๋์ ์์ ์ค์ผ ์ ์์ต๋๋ค. ํด๋์ค๋ฅผ ์์(extends)๋ฐ๋ ๊ฐ๋ ๊ณผ ๋์ผํฉ๋๋ค. ์คํ์ผ ์ปดํฌ๋ํธ ํ์ฅ Button์ ๋ง๋ค๊ณ Gr
codingbroker.tistory.com
&
const Thing = styled.div.attrs((/* props */) => ({ tabIndex: 0 }))` color: blue; &:hover { color: red; // <Thing> when hovered } & ~ & { background: tomato; // <Thing> as a sibling of <Thing>, but maybe not directly next to it } & + & { background: lime; // <Thing> next to <Thing> } &.something { background: orange; // <Thing> tagged with an additional CSS class ".something" } .something-else & { border: 1px solid; // <Thing> inside another element labeled ".something-else" } ` render( <React.Fragment> <Thing>Hello world!</Thing> <Thing>How ya doing?</Thing> <Thing className="something">The sun is shining...</Thing> <div>Pretty nice day today.</div> <Thing>Don't you think?</Thing> <div className="something-else"> <Thing>Splendid.</Thing> </div> </React.Fragment> ) |
Hello world!
How ya doing?
The sun is shining...
Pretty nice day today.
Don't you think?
Splendid.
|
const Input = styled.input.attrs({ type: "checkbox" })``; const Label = styled.label` align-items: center; display: flex; gap: 8px; margin-bottom: 8px; ` const LabelText = styled.span` ${(props) => { switch (props.$mode) { case "dark": return css` background-color: black; color: white; ${Input}:checked + && { color: blue; } `; default: return css` background-color: white; color: black; ${Input}:checked + && { color: red; } `; } }} `; render( <React.Fragment> <Label> <Input defaultChecked /> <LabelText>Foo</LabelText> </Label> <Label> <Input /> <LabelText $mode="dark">Foo</LabelText> </Label> <Label> <Input defaultChecked /> <LabelText>Foo</LabelText> </Label> <Label> <Input defaultChecked /> <LabelText $mode="dark">Foo</LabelText> </Label> </React.Fragment> ) |
FooFooFooFoo |
- && a double ampersand alone has a special behavior called a "precedence boost"; this can be useful if you are dealing with a mixed styled-components and vanilla CSS environment where there might be conflicting styles:
-
I'm blue, da ba dee da ba daa
If you put selectors in without the ampersand, they will refer to children of the component.
'CSS' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
CSS ๋ฐ์ํ vw, vh, vmin, vmax์ Flex ๊ทธ๋ฆฌ๊ณ inline, block (0) | 2023.09.01 |
---|