zoaseo
To Infinity And Beyond
zoaseo
전체 방문자
오늘
어제
  • 분류 전체보기 (763)
    • 개발이 좋아서 (381)
      • SAP가 좋아서 (3)
      • Java가 좋아서 (42)
      • Spring이 좋아서 (50)
      • JPA가 좋아서 (0)
      • QueryDSL이 좋아서 (26)
      • Docker가 좋아서 (7)
      • Redis가 좋아서 (7)
      • AWS가 좋아서 (5)
      • CI/CD가 좋아서 (6)
      • Troubleshooting이 좋아서 (4)
      • Kotlin이 좋아서 (7)
      • SQL이 좋아서 (6)
      • HTTP가 좋아서 (21)
      • JavaScript가 좋아서 (30)
      • TypeScript가 좋아서 (6)
      • Vue가 좋아서 (21)
      • Flutter가 좋아서 (61)
      • React가 좋아서 (20)
      • Redux(React)가 좋아서 (2)
      • Angular가 좋아서 (22)
      • HTML이 좋아서 (9)
      • CSS가 좋아서 (15)
      • PHP가 좋아서 (9)
      • Illustrator가 좋아서 (2)
    • 노력이 좋아서 (169)
    • 결과물이 좋아서 (14)
    • 코딩연습이 좋아서 (168)
      • 이론이 좋아서 (62)
      • SQL이 좋아서 (90)
    • 유용한 사이트가 좋아서 (28)
    • Github (2)

인기 글

티스토리

hELLO · Designed By 정상우.
zoaseo
노력이 좋아서

<step66>'react_styledcomponents'

<step66>'react_styledcomponents'
노력이 좋아서

<step66>'react_styledcomponents'

2022. 6. 23. 14:00

1)

App.js

import './App.css';
import styled from 'styled-components';
import Button from './components/Button.js';
import Dialog from './components/Dialog.js';
import { useState } from 'react';
import Box from './components/Box';

const AppBlock  = styled.div`
  width: 512px;
  margin: 0 auto;
  margin-top: 50px;
  border: 1px solid black;
  padding: 10px;
`

const Circle = styled.section`
  width: 100px;
  height: 100px;
  background: ${props=> props.color || 'black'};
  border-radius: 50%;
`;

function App() {
  const [ dialog, setDialog ] = useState(false);
  const onClick = () => {
    setDialog(true);
  }
  const onCancel = () => {
    setDialog(false);
    console.log('취소');
  }
  const onConfirm = () => {
    setDialog(false);
    console.log('확인');
  }
  return (
    <div className="App">
      <Circle color="blue"/>
      <Circle/>
      <Circle color='pink'/>
      <AppBlock>
        <Button>button1</Button>
        <Button>button2</Button>
        <Button onClick={onClick}>button3</Button>
      </AppBlock>
      <Dialog title="정말로 삭제하시겠습니까?"
      confirmText="삭제" cancelText="취소"
      visible={dialog}
      onCancel={onCancel} onConfirm={onConfirm}>
        데이터를 삭제하시겠습니까?
      </Dialog>
      <Box/>
    </div>
  );
}

export default App;

 

Button.js

import React from 'react';
import styled from 'styled-components';

const StyleButton = styled.button`
    /*공통스타일*/
    display: inline-flex;
    outline: none;
    border: none;
    border-radius: 4px;
    color: white;
    font-weight: bold;
    padding: 0 16px;
    line-height: 40px;
    font-size: 16px;
    background: #339af0;
    &:hover {
        background: #1c7ed6;
    }
    &+& {
        margin-left: 10px;
    }
`
const Button = ({children, onClick}) => {
    return (
            <StyleButton onClick={onClick}>{children}</StyleButton>
    );
};

export default Button;

Dialog.js

import React from 'react';
import styled from 'styled-components';
import Button from './Button';

// 어두운배경 div
const DarkBackground = styled.div`
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.7);
`;
// 컨텐츠 블럭
const DialogBlock = styled.div`
    width: 320px;
    padding: 20px;
    border-radius: 6px;
    background: #fff;
    h3 {
        margin: 0;
        font-size: 24px;
    }
    p {
        font-size: 18px;
    }
`
// 버튼그룹
const ButtonGroup = styled.div`
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
`
const Dialog = ({title, children, confirmText, cancelText, visible, onCancel, onConfirm}) => {
    if(!visible) return null;
    return (
        <div>
            <DarkBackground>
                <DialogBlock>
                    <h3>{title}</h3>
                    <p>{children}</p>
                    <ButtonGroup>
                        <Button onClick={onCancel}>{cancelText}</Button>
                        <Button onClick={onConfirm}>{confirmText}</Button>
                    </ButtonGroup>
                </DialogBlock>
            </DarkBackground>          
        </div>
    );
};

export default Dialog;

Box.js

import React from 'react';
import styles from './Box.module.css';

const Box = () => {
    return (
        <div className={styles.Box}>
            나는 박스입니다.
        </div>
    );
};

export default Box;

Box.module.css

.Box {
    background: blue;
    color: #fff;
    padding: 20px;
}

'노력이 좋아서' 카테고리의 다른 글

<step68>'react_createContext'  (0) 2022.06.27
<step67>'react_useReducer'  (0) 2022.06.24
<step66>'react_scss'  (0) 2022.06.23
<step65>'react_useEffect'  (0) 2022.06.22
<step65>'react_todolist실습'  (0) 2022.06.22

    티스토리툴바

    개인정보

    • 티스토리 홈
    • 포럼
    • 로그인

    단축키

    내 블로그

    내 블로그 - 관리자 홈 전환
    Q
    Q
    새 글 쓰기
    W
    W

    블로그 게시글

    글 수정 (권한 있는 경우)
    E
    E
    댓글 영역으로 이동
    C
    C

    모든 영역

    이 페이지의 URL 복사
    S
    S
    맨 위로 이동
    T
    T
    티스토리 홈 이동
    H
    H
    단축키 안내
    Shift + /
    ⇧ + /

    * 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.