Sun's Blog

node-sass 사용해보기 본문

ETC

node-sass 사용해보기

버스는그만 2024. 2. 18. 20:40

Syntactically Awesome Style Sheets

  • Css의 pre-processor
  • 확장자는 .scss와 .sass가 있으며 아래와 같은 문법적 차이가 있다

sass

$font-stack:    Helvetica, sans-serif
$primary-color: #333

body
  font: 100% $font-stack
  color: $primary-color

scss

$font-stack:    Helvetica, sans-serif;
$primary-color: #333;

body {
  font: 100% $font-stack;
  color: $primary-color;
}

 

이번에는 scss를 간단하게 공부해본다.

설치

npm i node-sass

Button.scss

$blue: #228be6; // 주석 선언

.Button {
  display: inline-flex;
  color: white;
  font-weight: bold;
  outline: none;
  border-radius: 4px;
  border: none;
  cursor: pointer;

  height: 2.25rem;
  padding-left: 1rem;
  padding-right: 1rem;
  font-size: 1rem;

  background: $blue; // 주석 사용
  &:hover {
    background: lighten($blue, 10%);
  }

  &:active {
    background: darken($blue, 10%);
  }
}

코드 처음에 주석을 선언 시 변수처럼 사용 가능하며 lighten과 darken은 함수를 사용할 수 있다. 이제 버튼의 여러 사이즈를 추가해본다.

Button.scss(사이즈 및 간격 추가)

$blue: #228be6;

.Button {
  display: inline-flex;
  color: white;
  font-weight: bold;
  outline: none;
  border-radius: 4px;
  border: none;
  cursor: pointer;

  // 사이즈 관리
  &.large {
    height: 3rem;
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1.25rem;
  }

  &.medium {
    height: 2.25rem;
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1rem;
  }

  &.small {
    height: 1.75rem;
    font-size: 0.875rem;
    padding-left: 1rem;
    padding-right: 1rem;
  }

  background: $blue;
  &:hover {
    background: lighten($blue, 10%);
  }

  &:active {
    background: darken($blue, 10%);
  }

  & + & {
    margin-left: 1rem;
  }
}

 

  • &.large의 의미는 .Button.large를 의미한다.
  • & + &의 의미는 .Button + .Button 이다.

Button.scss(색상 추가)

$blue: #228be6;
$gray: #495057;
$pink: #f06595;

.Button {
  display: inline-flex;
  color: white;
  font-weight: bold;
  outline: none;
  border-radius: 4px;
  border: none;
  cursor: pointer;

  // 사이즈 관리
  &.large {
    height: 3rem;
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1.25rem;
  }

  &.medium {
    height: 2.25rem;
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1rem;
  }

  &.small {
    height: 1.75rem;
    font-size: 0.875rem;
    padding-left: 1rem;
    padding-right: 1rem;
  }

  // 색상 관리
  &.blue {
    background: $blue;
    &:hover {
      background: lighten($blue, 10%);
    }

    &:active {
      background: darken($blue, 10%);
    }
  }

  &.gray {
    background: $gray;
    &:hover {
      background: lighten($gray, 10%);
    }

    &:active {
      background: darken($gray, 10%);
    }
  }

  &.pink {
    background: $pink;
    &:hover {
      background: lighten($pink, 10%);
    }

    &:active {
      background: darken($pink, 10%);
    }
  }

  & + & {
    margin-left: 1rem;
  }
}

 

gray, pink를 추가해준 것을 보면 각 반복적인 요소가 있는데 이것을 mixin이라는 기능을 사용하면 재사용할 수 있다.

$blue: #228be6;
$gray: #495057;
$pink: #f06595;

@mixin button-color($color) {
  background: $color;
  &:hover {
    background: lighten($color, 10%);
  }
  &:active {
    background: darken($color, 10%);
  }
}

.Button {
  display: inline-flex;
  color: white;
  font-weight: bold;
  outline: none;
  border-radius: 4px;
  border: none;
  cursor: pointer;

  // 사이즈 관리
  &.large {
    height: 3rem;
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1.25rem;
  }

  &.medium {
    height: 2.25rem;
    padding-left: 1rem;
    padding-right: 1rem;
    font-size: 1rem;
  }

  &.small {
    height: 1.75rem;
    font-size: 0.875rem;
    padding-left: 1rem;
    padding-right: 1rem;
  }

  // 색상 관리
  &.blue {
    @include button-color($blue);
  }

  &.gray {
    @include button-color($gray);
  }

  &.pink {
    @include button-color($pink);
  }

  & + & {
    margin-left: 1rem;
  }
}

번외

React에서 동적으로 클래스를 주기에는 아래와 같은 방법으로 줘야한다.

function Button({children, size, color}) {
    return <button className={['Button', size, color].join(' ')}>{children}</button>
}

허나 이러한 방법으로 문자열을 하나씩 주는 것보다는 classnames라는 라이브러리를 주면 훨씬 편하게 줄 수 있다.

의존성 추가

npm i classnames

소스코드 적용

import React from "react";
import './Button.scss';
import classNames from 'classnames';


function Button({children, size, color}) {
    return <button className={classNames('Button', size, color)}>{children}</button>
}

Button.defaultProps = {
    size: 'medium',
    color: 'blue'
}

export default Button;

className안에 classNames를 사용하여 문자열, 객체, 배열 등으로 클래스를 선언할 수 있다.

아래는 예시이다.

classNames('foo', 'bar'); // => 'foo bar'
classNames('foo', { bar: true }); // => 'foo bar'
classNames({ 'foo-bar': true }); // => 'foo-bar'
classNames({ 'foo-bar': false }); // => ''
classNames({ foo: true }, { bar: true }); // => 'foo bar'
classNames({ foo: true, bar: true }); // => 'foo bar'
classNames(['foo', 'bar']); // => 'foo bar'

// 동시에 여러개의 타입으로 받아올 수 도 있습니다.
classNames('foo', { bar: true, duck: false }, 'baz', { quux: true }); // => 'foo bar baz quux'

// false, null, 0, undefined 는 무시됩니다.
classNames(null, false, 'bar', undefined, 0, 1, { baz: null }, ''); // => 'bar 1'

'ETC' 카테고리의 다른 글

React Todo 리스트 컴포넌트편  (0) 2024.02.20
React CSS Module  (0) 2024.02.18
React 4일차: state  (1) 2024.02.04
Tiles  (0) 2024.02.04
리액트 3일차  (0) 2024.01.31