App.js
import Input from "./Input";
import Input2 from "./Input2";
import "./styles.css";
export default function App() {
return (
<div className="App">
<Input />
<br />
<br />
<Input2 />
</div>
);
}
Input.jsx
import React, { useState } from "react";
function Input() {
const [text, setText] = useState();
// input๊ฐ์ ๊ด๋ฆฌํ๊ธฐ ์ํ ์ํ ์ค์
const cc = (event) => {
// console.log(event.target.value);
setText(event.target.value);
// input์ ์
๋ ฅํ ๋ด์ฉ(onChange ์ด๋ฒคํธ๋ก ์ธ์ํ ๋ด์ฉ)์ text ๋ณ์๋ก ์ง์ด๋ฃ์
};
const reset = () => {
setText("");
};
return (
<>
<input type="text" value={text} onChange={cc} />
<button
onClick={() => {
// setText(""); // ์ด๊ธฐํ ๋ฒํผ ํด๋ฆญ ์ input ์ด๊ธฐํ
reset();
}}
>
์ด๊ธฐํ
</button>
<div>
<b>์
๋ ฅ๊ฐ : </b>
{text}
</div>
</>
);
}
export default Input;
Input2.jsx
import React, { useState } from "react";
function Input2() {
const [inputs, setInputs] = useState({
// ์ค๋ธ์ ํธ ํ์
id: "",
nickname: ""
}); // input์ ๊ฐ์ด 2๊ฐ -> ์ค๋ธ์ ํธ๋ก
const { id, nickname } = inputs; // ๋น๊ตฌ์กฐํ ๋น์ ํตํด ์ถ์ถ
const change = (event) => {
console.log("name? : ", event.target.name);
console.log("value? : ", event.target.value);
// const { name, value } = event.target; // ์ถ์ถ
const name = event.target.name; // ๋ด๊ฐ ์ง์ ํ ๊ฒ
const value = event.target.value; // ์
๋ ฅ๊ฐ(์๋ ์๋ ๊ฒ)
// ๋์ผํ ์ฝ๋
const nextInputs = {
...inputs,
[name]: value
// ๊ธฐ์กด ๊ฐ์ฒด๋ฅผ ์ ๋ถ ๊ฐ์ ธ์ด(์คํ๋ ๋๋ฌธ๋ฒ)
// [name]์ id, nickname์ด ๋ค์ด์์ ์์ ๋ฎ์ด ์์
};
setInputs(nextInputs);
};
const reset = () => {
setInputs({
id: "",
nickname: ""
});
};
return (
<>
<input
type="text"
name="id"
onChange={change}
placeholder="์ด๋ฆ"
value={id}
/>
<input
type="text"
name="nickname"
onChange={change}
placeholder="๋๋ค์"
value={nickname}
/>
<button onClick={reset}>์ด๊ธฐํ</button>
<div>
<b>์
๋ ฅ๊ฐ : </b>
{id}({nickname})
</div>
</>
);
}
export default Input2;
scss
// mixin ์คํ์ผ ์ ์
@mixin box-style() {
width: 100px;
height: 100px;
background: orange;
color: red;
display: flex;
justify-content: center;
align-items: center;
font: {
size: 2rem;
weight: bold;
}
}
// ์ธ์๊ฐ์ ์ง์ (parameter), ์ธ์์ ๊ธฐ๋ณธ๊ฐ๋ ์ค์
@mixin box-style2($bg-color: lightgray, $text-color: dimgray) {
width: 100px;
height: 100px;
background: $bg-color;
color: $text-color;
display: flex;
justify-content: center;
align-items: center;
font: {
size: 2rem;
weight: bold;
}
}
@mixin border-line($width: 2px, $style: solid, $color: black){
border: $width $style $color;
}
// ๋ฏน์ค์ธ ํธ์ถ
.b1 {
@include box-style;
}
.b2 {
@include box-style;
color: blue;
}
// ๋ฏน์ค์ธ ํธ์ถ(์ธ์๊ฐ ์ง์ ๋ ์ํ)
.b3 {
// ์ธ์๋ฅผ ์ ์ง ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ ์ ์ฉ
@include box-style2(powderblue);
@include border-line;
}
.b4 {
@include box-style2(#a0c1e2, white);
@include border-line(10px, dotted, #336699);
}
.wrap {
display: flex;
column-gap: 30px;
row-gap: 30px;
flex-wrap: wrap;
}
html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>sass05</title>
<link rel="stylesheet" href="style/main.css" />
</head>
<body>
<div class="wrap">
<div class="b1">BOX1</div>
<div class="b2">BOX2</div>
<div class="b3">BOX3</div>
<div class="b4">BOX4</div>
</div>
</body>
</html>
scss
@mixin btn-style() {
@content;
width: 100px;
height: 50px;
border: none;
}
@mixin btn-style2($hv-bgcolor: gray) {
@content;
width: 100px;
height: 50px;
border: none;
// ๋ถ๋ชจ์ hover
&:hover {
background: $hv-bgcolor;
}
}
.b1 {
@include btn-style {
background: coral;
color: white;
}
}
.b2 {
@include btn-style2(skyblue);
background: beige;
}
.b3 {
@include btn-style2;
background: cornflowerblue;
}
html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>sass06</title>
<link rel="stylesheet" href="style/main.css" />
</head>
<body>
<button class="b1">BUTTON 1</button>
<button class="b2">BUTTON 2</button>
<button class="b3">BUTTON 3</button>
<button class="b4">BUTTON 4</button>
</body>
</html>
์ผ๋ฐ
hover์
main.scss
@import "./mixin.scss"; // ๋ถ๋ฌ์ฌ ๋ ์ธ๋๋ฐ ์๋ต
@import "./variables.scss";
div {
@include box-style($main-color, $sub-color);
@include border-line;
}
_mixin.scss
@mixin box-style($bg-color: lightgray, $text-color: dimgray) {
width: 500px;
height: 500px;
background: $bg-color;
color: $text-color;
display: flex;
justify-content: center;
align-items: center;
font: {
size: 2rem;
weight: bold;
}
}
@mixin border-line($width: 2px, $style: solid, $color: black){
border: $width $style $color;
}
_variables.scss
$main-color: powderblue;
$sub-color: magenta;
html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>sass07</title>
<link rel="stylesheet" href="style/main.css" />
</head>
<body>
<div>์ฌ๋ฌ๊ฐ์ sassํ์ผ์ ์ฌ์ฉ</div>
</body>
</html>
scss
/*
๋ค๋ฅธ ํด๋์ค์ ์คํ์ผ์ ๊ทธ๋๋ก ๊ฐ์ ธ์์ ์ธ ์ ์๋ค
@extand ํด๋์ค๋ช
*/
.aaa {
padding: 20px;
display: flex;
justify-content: center;
align-items: center;
background: lightblue;
color: darkblue;
}
.bbb {
@extend .aaa;
background: peachpuff;
}
.btn {
width: 100px;
height: 40px;
background: #badbee;
border: 1px solid #5e84b6;
border-radius: 8px;
}
input[type="text"] {
@extend .btn;
width: 300px;
padding-left: 20px;
}
/*
ํ๋ ์ด์คํ๋(placeholder) - ์ ๋ นํด๋์ค %
@extend์ ๊ฐ์ ธ๋ค ์ฐ๋ ์ฉ๋๋ก๋ง ๋ง๋ค์ด์ง
css ์ธํธ๋ค์ ๋ณ์์ฒ๋ผ ์ ์ธํด์ ์ฌ์ฉ
*/
%box {
display: flex;
justify-content: center;
align-items: center;
border: 2px solid gray;
width: 200px;
height: 200px;
}
.box1 {
@extend %box;
}
.box2 {
@extend %box;
background: orange;
}
html
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>scss</title>
<link rel="stylesheet" href="style/main.css" />
</head>
<body>
<h1 class="aaa">Lorem ipsum dolor sit amet</h1>
<h1 class="bbb">Lorem ipsum dolor sit amet</h1>
<button class="btn">BUTTON</button>
<input type="text" placeholder="ํ๊ณ ์ถ์ ๋ง์ ๋จ๊ฒจ ์ฃผ์ธ์.">
<br><br>
<div class="box1">BOX 1</div>
<br>
<div class="box2">BOX 2</div>
</body>
</html>