๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

CSS

styled-components ๊ตฌ๊ธ€๋ง

728x90

# 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.

Mystery buttonWhat do I do?
728x90

'CSS' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

CSS ๋ฐ˜์‘ํ˜• vw, vh, vmin, vmax์™€ Flex ๊ทธ๋ฆฌ๊ณ  inline, block  (0) 2023.09.01