React + TypeScript 환경에서 Styled-components를 활용한 다양한 스타일의 버튼 컴포넌트 만들기

2023. 7. 27. 21:29·TypeScript

React + TypeScript 환경에서 Styled-components를 사용하여 다양한 스타일의 버튼을 만들어봤다. 

import React from 'react'
import styled, { css, keyframes }  from 'styled-components'

// ButtonProps 타입 정의
type ButtonProps = {
  children: string,
  sm?: boolean, // sm: boolean | undefined 로도 사용 가능 
  me?: boolean,
  lg?: boolean,
  xlg?: boolean,
}

// Button 컴포넌트 정의
export const Button = ({ children, ...props }:ButtonProps) => {
  return  <BtnStyle {...props}> {children}</BtnStyle>
 
}


// 아래에서 위로 등장하는 듯한 애니메이션 
const toUp = keyframes`
0% {
  opacity: 0;
  transform: translateY(50%);
}
100% {
  opacity: 1;
  transform: translateY(0);
}
`;

const BtnStyle = styled.button<ButtonProps>`
  width: 90%;
  height: 3.5rem;
  border: none;
  outline: none;
  cursor: pointer;
  border-radius: 0.25rem;
  ${props => props.theme.DisFlexCenter};
  background-color: ${props => props.theme.colorTheme.orange};
  animation: ${toUp} 0.25s ease-in-out;
  // sm 속성이 true일 때, 버튼 크기를 변경
  ${(props) =>
    props.sm &&
    css`   
        width: 4.6875rem;
        height: 1.875rem;
    `};
    ${(props) =>
    props.me &&
    css`   
        width: 9.375rem;
        height: 2.5rem;
    `}; 
    ${(props) =>
    props.lg &&
    css`   
        width: 12.5rem;
        height: 2.8125rem;
    `}; 
    ${(props) =>
    props.xlg &&
    css`   
        width: 4.6875rem;
        height: 1.875rem;
    `}; 
`;

'TypeScript' 카테고리의 다른 글

타입스크립트_02 기본타입  (0) 2023.06.06
타입스크립트_01 타입스크립트란 ?  (0) 2023.06.06
'TypeScript' 카테고리의 다른 글
  • 타입스크립트_02 기본타입
  • 타입스크립트_01 타입스크립트란 ?
jihun_BS
jihun_BS
  • jihun_BS
    Harry 개발블로그
    jihun_BS
  • 전체
    오늘
    어제
    • 분류 전체보기 (507)
      • React Native (2)
      • React (7)
      • 알고리즘 (37)
      • JavaScript (11)
      • 항해99 (12)
        • 항해 WIL (13)
      • TIL (405)
      • TypeScript (3)
      • 트러블 슈팅 (5)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • Github
  • 공지사항

  • 인기 글

  • 태그

    pxemrem차이
    virtualizedlists should never be nested inside plain scrollviews with the same orientation because it can break windowing and other functionality - use another virtualizedlist-backed container instead. [component stack]
    React-hook-form 등록 버튼 활성화
    항해99
    vite 환경에서 svg 컴포넌트
    n의 배수
    다양한 스타일의 버튼 컴포넌트
    react-native-dropdown-picker
    vite svg
    n의 배수 자바스크립트
    잔디 기부 캠페인
    pxemrem
    프로그래머스
    React-hook-form watch
    typescript 환경에서 다양한 버튼 컴포넌트
    자바스크립트
    react-native-dropdown-picker 오류
    Received true` for a non-boolean attribute`mode`Warning
    til
    잔디 기부
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
jihun_BS
React + TypeScript 환경에서 Styled-components를 활용한 다양한 스타일의 버튼 컴포넌트 만들기
상단으로

티스토리툴바