React Style
React μμ μ€νμΌλ§νλ λ°©λ²μ μμλ΄
λλ€.
React μμ μ€νμΌλ§ νλ λ°©λ²
β’
β’
β’
BootStrap
β’
React-BootStrap
inline style
μ»΄ν¬λνΈμ style μμ±μ μ§μ μ€νμΌ μμ±μ μ μνλ λ°©λ²
λ¬Έλ²
<μ»΄ν¬λνΈ style={ { μμ±1 : 'κ°1', μμ±2: 'κ°2' } } />
JavaScript
볡μ¬
μ»΄ν¬λνΈμ style μμ±μ μ μνμ¬ { } ννμ μμμ, { } κ°μ²΄μ ννλ‘ μ€νμΌμ μ§μ ν©λλ€.
β’
μ€νμΌ μμ± : μΌλ°μ μΌλ‘ CSS μ€νμΌ μνΈμμλ μ€νμΌ μμ±μ μΌλ°₯ μΌμ΄μ€(βstyle-propertyβ) λ‘ μ§μ νμ§λ§, React μ μ€νμΌ μμ±μ μΉ΄λ© μΌμ΄μ€(βstylePropertyβ) λ‘ μ§μ ν©λλ€.
β’
μ€νμΌ κ° : βκ°β λλ βκ°β λ°μ΄νλ‘ λ¬Άμ΄μ μ§μ ν©λλ€.
μμ μ½λ
μΈλΌμΈ μ€νμΌμ μ μ©νμ¬ λ²νΌμ λ§λ€μ΄ λ΄
λλ€.
κΈ°λ³Έ λ²νΌ
Button.jsx
import React from 'react'
const Button = () => {
return (
<button style={{
backgroundColor: 'royalblue',
color: 'white',
fontWeight: 'bold',
outline: 'none',
border: 'none',
margin: '0 2px',
padding: '10px 24px',
borderRadius: '6px',
cursor: 'pointer',
}}>Button</button>
)
}
export default Button
JavaScript
볡μ¬
컀μ€ν λ²νΌ
CustomButton.jsx
import React from 'react'
const CustomButton = ({ text, btnStyle, btnSize, onClick }) => {
let color = 'white';
let backgroundColor = 'black';
let padding = '12px 24px';
let display = 'inline-block';
let fontSize = '14px';
let width = 'auto';
let margin = '0 4px';
switch (btnStyle) {
case 'primary': backgroundColor = '#007BFF'; break;
case 'secondary': backgroundColor = '#6C757D'; break;
case 'success': backgroundColor = '#28A745'; break;
case 'danger': backgroundColor = '#DC3545'; break;
case 'warning': backgroundColor = '#FFC107'; color = 'black'; break;
case 'info': backgroundColor = '#17A2B8'; break;
case 'light': backgroundColor = '#F8F9FA'; color = 'black'; break;
case 'dark': backgroundColor = '#343A40'; break;
default: break;
}
switch (btnSize) {
case 'xs': padding = '2px 8px'; fontSize = '10px'; break;
case 'sm': padding = '6px 12px'; fontSize = '12px'; break;
case 'md': padding = '12px 24px'; fontSize = '14px'; break;
case 'lg': padding = '14px 32px'; fontSize = '16px'; break;
case 'xl': padding = '18px 40px'; fontSize = '18px'; break;
case 'block':
padding = '18px 40px'; display = 'block'; width = '100%';
margin = '10px 6px'; fontSize = '20px';
break;
default: break;
}
let style = {
display : display,
width: width,
backgroundColor : backgroundColor,
color : color,
fontWeight: 'bold',
outline: 'none',
border: 'none',
margin: margin,
padding: padding,
fontSize: fontSize,
borderRadius: '6px',
cursor: 'pointer',
}
return (
<button style={style} onClick={() => onClick()}>{text}</button>
)
}
export default CustomButton
JavaScript
볡μ¬
App.js
import './App.css';
import Button from './components/Button';
import CustomButton from './components/CustomButton';
function App() {
const onClick = () => {
alert('Clicked!')
}
return (
<div className='container'>
<h1>inline style</h1>
<hr/>
<h2>κΈ°λ³Έ λ²νΌ</h2>
<Button />
<hr/>
<h2>컀μ€ν
λ²νΌ</h2>
<h2>xs</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} />
<h2>sm</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} />
<h2>md</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} />
<h2>lg</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} />
<h2>xl</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} />
<h2>block</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} />
</div>
);
}
export default App;
JavaScript
볡μ¬
import CSS
μΈλΆμ μ€νμΌ μνΈ(CSS) νμΌμ μμ±νκ³ , μ΄λ₯Ό λΆλ¬μμ μ€νμΌμ μ μ©νλ λ°©λ²
λ¬Έλ²
import './νμΌλͺ
.css';
JavaScript
볡μ¬
<μ»΄ν¬λνΈ id="κ³ μ νμμ΄λ" className="ν΄λμ€μ΄λ¦" />
JavaScript
볡μ¬
μμμ½λ
import CSS μ μ μ©νμ¬ λ²νΌμ λ§λ€μ΄ λ΄
λλ€.
κΈ°λ³Έ λ²νΌ
Button.jsx
import React from 'react';
import './Button.css';
const Button = ({ id }) => {
return (
<button id={id} className='btn'>Button</button>
)
}
export default Button
JavaScript
볡μ¬
Button.css
.btn {
background-color: royalblue;
color: white;
font-weight: bold;
outline: none;
border: none;
margin: 0 2px;
padding: 10px 24px;
border-radius: 6px;
cursor: pointer;
}
#btn {
background-color: red;
color: white;
font-weight: bold;
outline: none;
border: none;
margin: 0 2px;
padding: 10px 24px;
border-radius: 6px;
cursor: pointer;
}
CSS
볡μ¬
컀μ€ν λ²νΌ
CustomButton.jsx
import React from 'react'
import './CustomButton.css'
const CustomButton = ({ text, btnStyle, btnSize, onClick }) => {
return (
<button className={`btn ${btnStyle} ${btnSize}`} onClick={() => onClick()}>{text}</button>
)
}
export default CustomButton
JavaScript
볡μ¬
CustomButton.css
.btn {
display: inline-block;
width: auto;
background-color: black;
color: white;
font-weight: bold;
outline: none;
border: none;
margin: 0 4px;
padding: 12px 24px;
font-size: 14px;
border-radius: 6px;
cursor: pointer;
}
.btn:hover {
box-shadow: 2px 2px 2px rgba(0,0,0,0.5);
filter: brightness(80%) contrast(100%) saturate(100%);
}
.btn:active {
box-shadow: none;
}
.btn.xs {
padding: 2px 8px;
font-size: 10px;
}
.btn.sm {
padding: 6px 12px;
font-size: 12px;
}
.btn.md {
padding: 12px 24px;
font-size: 14px;
}
.btn.lg {
padding: 14px 32px;
font-size: 16px;
}
.btn.xl {
padding: 18px 40px;
font-size: 18px;
}
.btn.block {
padding: 18px 40px;
display: block;
width: 100%;
margin: 10px 6px;
font-size: 20px;
}
/* Color variations */
.btn.primary { background-color: #007BFF; }
.btn.secondary { background-color: #6C757D; }
.btn.success { background-color: #28A745; }
.btn.danger { background-color: #DC3545; }
.btn.warning { background-color: #FFC107; color: black; }
.btn.info { background-color: #17A2B8; }
.btn.light { background-color: #F8F9FA; color: black; }
.btn.dark { background-color: #343A40; }
CSS
볡μ¬
App.js
import './App.css';
import Button from './components/Button/Button';
import CustomButton from './components/CustomButton/CustomButton';
function App() {
const onClick = () => {
alert('Clicked!')
}
return (
<div className='container'>
<h1>import css</h1>
<hr/>
<h2>κΈ°λ³Έ λ²νΌ</h2>
<Button />
<Button id='btn' />
<hr/>
<h2>컀μ€ν
λ²νΌ</h2>
<h2>xs</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} />
<h2>sm</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} />
<h2>md</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} />
<h2>lg</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} />
<h2>xl</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} />
<h2>block</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} />
</div>
);
}
export default App;
JavaScript
볡μ¬
μ£Όμμ¬ν
μλ‘ λ€λ₯Έ μ»΄ν¬λνΈμμ κ°λ³μ μΌλ‘ λ€λ₯Έ CSS λ₯Ό import νλλΌλ, μ€νμΌμ΄ μ μμ μ μ©λ©λλ€.
κ°λ³μ μΈ μ»΄ν¬λνΈμμ, κ°κ°μ CSS import νλλΌκ³ μ€μ λ‘λ νλμ html μ κ°κ°μ <style> μ λλλ§ λ©λλ€.
λ°λΌμ, μ»΄ν¬λνΈλ³λ‘ import ν CSS νμΌμ΄ κ²°κ³Όμ μΌλ‘λ νλλ‘ ν©μ³μ μ μ μ©λλ κ²μ λ³Ό μ μμ΅λλ€.
Button.jsx μ»΄ν¬λνΈμμ κ°μ κ²½λ‘μ Button.css λ₯Ό ν¬ν¨νκ³ μμ΅λλ€.
CustomButton.jsx μ»΄ν¬λνΈμμ κ°μ κ²½λ‘μ CustomButton.css λ₯Ό ν¬ν¨νκ³ μμ΅λλ€.
μ€μ λ‘, κ° μ»΄ν¬λνΈμμ import ν CSS λ index.html νμΌμ <style> νκ·Έλ‘ μ¬λ¬ κ°κ° λλλ§ λ©λλ€.
β’
<style> : index.css
β’
<style> : App.css
β’
<style> : Button.css
β’
<style> : CustomButton.css
μ΄λ κ² κ°λ³λ‘ import ν css νμΌμΌ νλμ html λ‘ μ μμμ μ€μ λλ€λ³΄λ, λ€λ₯Έ css νμΌμμ μ§μ ν μ νμκ° κ°μΌλ©΄, μ€νμΌλ§ μ€λ³΅λλ νμμ΄ λ°μν©λλ€.
μ¬κΈ°μλ, Button.css μμλ .btn μ νμλ₯Ό μ§μ νκ³ , CustomButton.css μμλ .btn μ νμλ₯Ό μ§μ νκ³ μμ΅λλ€. μ΄λ¬ν λ°©μμΌλ‘ μ§μ ν λλ, λ€λ₯Έ μ»΄ν¬λνΈμ CSS λΌλ©΄ κ°μ μ΄λ¦μ μ νμμ μ¬μ©νμ§ μλ κ²μ΄ μ’μ΅λλ€.
κ·Έλ°λ°, μ νμκ° μ€λ³΅λλλΌλ, κ° μ»΄ν¬λνΈ λ³λ‘ CSS λ₯Ό μ μ©μμΌ λ°λ‘ κ΄λ¦¬ν μλ μμκΉμ?
μ΄μ λν ν΄κ²°λ°©λ²μ΄ λ°λ‘ CSS Module μ
λλ€.
import CSS Module
CSSλ₯Ό λͺ¨λννμ¬ μ»΄ν¬λνΈ μμ€μμ μ€νμΌμ κ΄λ¦¬ν μ μλλ‘ λμμ£Όλ κΈ°μ
CSS Moduleμ ν΄λμ€ μ νμμ λν μ§μ λ²μλ‘μ μ€νμΌμ μ 곡ν©λλ€.
μ»΄ν¬λνΈλ³ ν΄λμ€ μ νμλ₯Ό μ 곡νκΈ° μν¨
ν΄λμ€ μ νμλ₯Ό μ€μ¬μΌλ‘ μ€μ½νκ° μ§μ λ©λλ€. idλ νκ·Έ μ νμλ₯Ό μ¬μ©νλ €λ©΄ λ€λ₯Έ μ€νμΌ λ°©λ²μ μ¬μ©νκ±°λ μ μ μ€νμΌλ‘ μ μν΄μΌ ν©λλ€.
1.
κ³ μ ν μ€μ½ν: CSS Modulesμ ν΄λμ€ μ΄λ¦μ μ§μμ μΌλ‘ μ€μ½νννμ¬ μ μ λ€μμ€νμ΄μ€μμμ μΆ©λμ λ°©μ§ν©λλ€. κ° λͺ¨λμ μ체 κ³ μ ν ν΄λμ€ μ΄λ¦μ μμ±ν©λλ€.
2.
ν΄λμ€ μ΄λ¦ λ³ν: CSS Modulesμ ν΄λμ€ μ΄λ¦μ μλμΌλ‘ λ³ννμ¬ μ»΄ν¬λνΈμ ν΄λμ€ μ΄λ¦κ³Ό μΆ©λνμ§ μλλ‘ ν©λλ€. μΌλ°μ μΌλ‘ "camelCase"λ‘ μμ±λ ν΄λμ€ μ΄λ¦μ νμ΄νμ ν¬ν¨νλ "kebab-case"λ‘ λ³νλ©λλ€.
3.
ν΄λμ€ μ΄λ¦ μ¬μ©: JavaScript μ½λμμ CSS Modulesμ μ¬μ©ν λλ ν΄λΉ λͺ¨λμμ λ΄λ³΄λΈ ν΄λμ€λ₯Ό λΆλ¬μμ μ¬μ©ν©λλ€. μ΄λ₯Ό ν΅ν΄ μ€νμΌμ μ μ©νκ³ κ΄λ¦¬ν μ μμ΅λλ€.
μ€μ λ‘ style νκ·Έμ λλλ§ λ λ, κ° CSS λͺ¨λμμ μ§μ ν .btn μ νμκ° κ³ μ ν μ΄λ¦μΌλ‘ μ§μ λμ΄μ λλλ§ λ©λλ€.
λ¬Έλ²
{νμΌλͺ
}.module.css νμμΌλ‘ νμΌ μμ±
import styles from './νμΌλͺ
.module.css';
JavaScript
볡μ¬
<μ»΄ν¬λνΈ className={styles.ν΄λμ€μμ±} />
JavaScript
볡μ¬
κ΄λ‘
CSS module μ ν΄λμ€ μ νμλ μΉ΄λ© μΌμ΄μ€λ‘ μ°λ κ²μ΄ κ΄λ‘ (μΌλ°₯ μΌμ΄μ€λ κ°λ₯)
μΉ΄λ©μΌμ΄μ€ : .formInput β‘ { styles.formInput }
μΌλ°₯μΌμ΄μ€ : .form-input β‘ { styles['form-input']}
JavaScript
볡μ¬
κΈ°λ³Έ λ²νΌ
Button.jsx
import React from 'react';
// import './Button.css';
import styles from './Button.module.css';
const Button = ({ id }) => {
return (
<button id={id} className={styles.btn}>Button</button>
)
}
export default Button
JavaScript
볡μ¬
Button.module.css
.btn {
background-color: royalblue;
color: white;
font-weight: bold;
outline: none;
border: none;
margin: 0 2px;
padding: 10px 24px;
border-radius: 6px;
cursor: pointer;
}
#btn {
background-color: red;
color: white;
font-weight: bold;
outline: none;
border: none;
margin: 0 2px;
padding: 10px 24px;
border-radius: 6px;
cursor: pointer;
}
CSS
볡μ¬
컀μ€ν λ²νΌ
CustomButton.jsx
import React from 'react'
// import './CustomButton.css'
import styles from './CustomButton.module.css';
const CustomButton = ({ text, btnStyle, btnSize, onClick }) => {
return (
<button className={`${styles.btn} ${styles[btnStyle]} ${styles[btnSize]}`}
onClick={() => onClick()}>{text}</button>
)
}
export default CustomButton
JavaScript
볡μ¬
CustomButton.module.css
.btn {
display: inline-block;
width: auto;
background-color: black;
color: white;
font-weight: bold;
outline: none;
border: none;
margin: 0 4px;
padding: 12px 24px;
font-size: 14px;
border-radius: 6px;
cursor: pointer;
}
.btn:hover {
box-shadow: 2px 2px 2px rgba(0,0,0,0.5);
filter: brightness(80%) contrast(100%) saturate(100%);
}
.btn:active {
box-shadow: none;
}
.btn.xs {
padding: 2px 8px;
font-size: 10px;
}
.btn.sm {
padding: 6px 12px;
font-size: 12px;
}
.btn.md {
padding: 12px 24px;
font-size: 14px;
}
.btn.lg {
padding: 14px 32px;
font-size: 16px;
}
.btn.xl {
padding: 18px 40px;
font-size: 18px;
}
.btn.block {
padding: 18px 40px;
display: block;
width: 100%;
margin: 10px 6px;
font-size: 20px;
}
/* Color variations */
.btn.primary { background-color: #007BFF; }
.btn.secondary { background-color: #6C757D; }
.btn.success { background-color: #28A745; }
.btn.danger { background-color: #DC3545; }
.btn.warning { background-color: #FFC107; color: black; }
.btn.info { background-color: #17A2B8; }
.btn.light { background-color: #F8F9FA; color: black; }
.btn.dark { background-color: #343A40; }
CSS
볡μ¬
App.js
import './App.css';
import Button from './components/Button/Button';
import CustomButton from './components/CustomButton/CustomButton';
function App() {
const onClick = () => {
alert('Clicked!')
}
return (
<div className='container'>
<h1>import css</h1>
<hr/>
<h2>κΈ°λ³Έ λ²νΌ</h2>
<Button />
<Button id='btn' />
<hr/>
<h2>컀μ€ν
λ²νΌ</h2>
<h2>xs</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} />
<h2>sm</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} />
<h2>md</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} />
<h2>lg</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} />
<h2>xl</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} />
<h2>block</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} />
</div>
);
}
export default App;
JavaScript
볡μ¬
styled-components
React μμ μ€νμΌμ΄ μ μ©λ μ»΄ν¬λνΈλ₯Ό μ¬μ©ν μ μκ² ν΄μ£Όλ λΌμ΄λΈλ¬λ¦¬
μ€μΉ
npm install styled-components
Shell
볡μ¬
λ¬Έλ²
import styled from 'styled-components';
const μ»΄ν¬λνΈλͺ
= styled.νκ·Έλͺ
`
μμ±1 : κ°1;
μμ±2 : κ°2;
`;
JavaScript
볡μ¬
1.
styled-components λΌμ΄λΈλ¬λ¦¬λ₯Ό import ν©λλ€.
2.
styled.νκ·Έλͺ
- μ¬μ©ν νκ·Έ μ΄λ¦μ μ§μ ν©λλ€.
3.
` ` ν
νλ¦Ώ 리ν°λ΄μ μ¬μ©νμ¬ μ€νμΌ μμ±μ μ§μ ν©λλ€.
a.
μμ±μ μΌλ°₯ μΌμ΄μ€(βstyle-propertyβ)λ‘ μ§μ ν©λλ€.
b.
κ°μ λ°μ΄ν(ββ) μμ΄ μ§μ ν©λλ€.
μμ μ½λ
styled-components λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νμ¬ λ²νΌμ λ§λ€μ΄ λ΄
λλ€.
κΈ°λ³Έ λ²νΌ
Button.jsx
import React from 'react'
import styled from 'styled-components';
// λ²νΌ μ€νμΌ
const buttonStyles = `
font-weight: bold;
outline: none;
border: none;
margin: 0 2px;
padding: 10px 24px;
border-radius: 6px;
cursor: pointer;
`;
// μΌλ° λ²νΌ μ€νμΌ
const standard = `
background-color: royalblue;
color: white;
`;
// IDκ° "btn"μΈ λ²νΌ μ€νμΌ
const unique = `
background-color: red;
color: white;
`;
// μ€νμΌλ μ»΄ν¬λνΈ μμ±
const StyledButton = styled.button`
${buttonStyles}
${props => props.id === 'btn' ? unique : standard}
`;
// Button μ»΄ν¬λνΈ
const Button = ({ id }) => {
return <StyledButton id={id}>Button</StyledButton>;
}
export default Button;
JavaScript
볡μ¬
컀μ€ν λ²νΌ
CustomButton.jsx
import React from 'react';
import styled from 'styled-components';
const StyledButton = styled.button`
display: ${props => props.display || 'inline-block'};
width: ${props => props.width || 'auto'};
background-color: ${props => props.backgroundColor || 'black'};
color: ${props => props.color || 'white'};
font-weight: bold;
outline: none;
border: none;
margin: ${props => props.margin || '0 4px'};
padding: ${props => props.padding || '12px 24px'};
font-size: ${props => props.fontSize || '14px'};
border-radius: 6px;
cursor: pointer;
`;
const CustomButton = ({ text, btnStyle, btnSize, onClick }) => {
let backgroundColor = 'black';
let color = 'white';
let padding = '12px 24px';
let display = 'inline-block';
let fontSize = '14px';
let width = 'auto';
let margin = '0 4px';
switch (btnStyle) {
case 'primary': backgroundColor = '#007BFF'; break;
case 'secondary': backgroundColor = '#6C757D'; break;
case 'success': backgroundColor = '#28A745';break;
case 'danger': backgroundColor = '#DC3545'; break;
case 'warning': backgroundColor = '#FFC107'; color = 'black'; break;
case 'info': backgroundColor = '#17A2B8'; break;
case 'light': backgroundColor = '#F8F9FA'; color = 'black'; break;
case 'dark': backgroundColor = '#343A40'; break;
default: break;
}
switch (btnSize) {
case 'xs': padding = '2px 8px'; fontSize = '10px'; break;
case 'sm': padding = '6px 12px'; fontSize = '12px'; break;
case 'md': padding = '12px 24px'; fontSize = '14px'; break;
case 'lg': padding = '14px 32px'; fontSize = '16px'; break;
case 'xl': padding = '18px 40px'; fontSize = '18px'; break;
case 'block': padding = '18px 40px'; display = 'block'; width = '100%'; margin = '10px 6px'; fontSize = '20px'; break;
default: break;
}
return (
<StyledButton
backgroundColor={backgroundColor}
color={color}
padding={padding}
display={display}
fontSize={fontSize}
width={width}
margin={margin}
onClick={onClick}
>
{text}
</StyledButton>
);
};
export default CustomButton;
JavaScript
볡μ¬
App.js
import './App.css';
import Button from './components/Button';
import CustomButton from './components/CustomButton';
function App() {
const onClick = () => {
alert('Clicked!')
}
return (
<div className='container'>
<h1>styled components</h1>
<hr/>
<h2>κΈ°λ³Έ λ²νΌ</h2>
<Button />
<Button id='btn' />
<hr/>
<h2>컀μ€ν
λ²νΌ</h2>
<h2>xs</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} />
<h2>sm</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} />
<h2>md</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} />
<h2>lg</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} />
<h2>xl</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} />
<h2>block</h2>
<CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} />
</div>
);
}
export default App;
JavaScript
볡μ¬
BootStrap
λ°μν μΉ λμμΈ λ° κ°λ°μ μν HTML, CSS λ° JavaScript νλ μμν¬
λ¬Έλ²
<μ»΄ν¬λνΈ className="미리μ μλ ν΄λμ€μμ±" />
JavaScript
볡μ¬
μμ
<button className="btn btn-primary">Button</button>
JavaScript
볡μ¬
μμ μ½λ
bootstrap μ μ¬μ©νμ¬ λ²νΌμ λ§λ€μ΄ λ΄
λλ€.
κΈ°λ³Έ λ²νΌ
Button.jsx
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css'; // β
Bootstrap CSSλ₯Ό μΆκ°
const Button = () => {
return (
<button className="btn btn-primary">Button</button>
);
}
export default Button;
JavaScript
볡μ¬
컀μ€ν λ²νΌ
CustomButton.jsx
import React from 'react';
import 'bootstrap/dist/css/bootstrap.min.css'; // β
Bootstrap CSSλ₯Ό μΆκ°
const CustomButton = ({ text, btnStyle, btnSize, onClick }) => {
let buttonClasses = 'btn';
switch (btnStyle) {
case 'primary': buttonClasses += ' btn-primary'; break;
case 'secondary': buttonClasses += ' btn-secondary'; break;
case 'success': buttonClasses += ' btn-success'; break;
case 'danger': buttonClasses += ' btn-danger'; break;
case 'warning': buttonClasses += ' btn-warning'; break;
case 'info': buttonClasses += ' btn-info'; break;
case 'light': buttonClasses += ' btn-light'; break;
case 'dark': buttonClasses += ' btn-dark'; break;
default: break;
}
switch (btnSize) {
case 'xs': buttonClasses += ' btn-xs'; break;
case 'sm': buttonClasses += ' btn-sm'; break;
case 'md': buttonClasses += ' btn-md'; break;
case 'lg': buttonClasses += ' btn-lg'; break;
case 'xl': buttonClasses += ' '; break; // bootstrap5 λ²μ btn-xl β deprecated
case 'block': buttonClasses += ' '; break; // bootstrap5 λ²μ btn-block β deprecated
default: break;
}
return (
<button className={buttonClasses} onClick={() => onClick()}>{text}</button>
);
}
export default CustomButton;
JavaScript
볡μ¬
App.js
import './App.css';
import Button from './components/Button';
import CustomButton from './components/CustomButton';
function App() {
const onClick = () => {
alert('Clicked!')
}
return (
<div className='container'>
<h1>bootstrap</h1>
<hr/>
<h2>κΈ°λ³Έ λ²νΌ</h2>
<Button />
<hr/>
<h2>컀μ€ν
λ²νΌ</h2>
<h2>xs</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} />
</div>
<h2>sm</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} />
</div>
<h2>md</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} />
</div>
<h2>lg</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} />
</div>
<h2>xl</h2>
<div class="d-grid gap-2 col-6 mx-auto">
<CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} />
</div>
<h2>block</h2>
<div class="d-grid gap-2">
<CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} />
</div>
</div>
);
}
export default App;
JavaScript
볡μ¬
React-BootStrap
Bootstrapμ React μ ν리μΌμ΄μ
μμ μ¬μ©ν μ μλλ‘ React μ»΄ν¬λνΈλ‘ κ°μΌ λΌμ΄λΈλ¬λ¦¬
Reactμ Bootstrapμ μ‘°ν©νμ¬ ν¨κ³Όμ μΌλ‘ μ¬μ©ν μ μλλ‘ ν΄μ£Όλ©°, Reactμ μ»΄ν¬λνΈ κΈ°λ° μν€ν
μ²λ₯Ό νμ©νμ¬ Bootstrapμ λμ± μ μ°νκ² μ¬μ©ν μ μκ² ν©λλ€.
μ€μΉ
npm install react-bootstrap bootstrap
Shell
볡μ¬
λ¬Έλ²
// λ°©λ² 1οΈβ£
import μ»΄ν¬λνΈ from 'react-bootstrap/μ»΄ν¬λνΈ';
// λ°©λ² 2οΈβ£
import { μ»΄ν¬λνΈ} from 'react-bootstrap';
<μ»΄ν¬λνΈ μ΅μ
="κ°" μ΅μ
2="κ°" />
JavaScript
볡μ¬
μμ
import Button from 'react-bootstrap/Button';
<Button variant='primary' onClick={onClick}>Button</Button>
JavaScript
볡μ¬
μμ μ½λ
react-bootstrap μ μ¬μ©νμ¬ λ²νΌμ λ§λ€μ΄ λ΄
λλ€.
κΈ°λ³Έ λ²νΌ
ReactButton.jsx
import React from 'react';
import Button from 'react-bootstrap/Button';
const ReactButton = () => {
return (
<Button variant='primary'>Button</Button>
);
}
export default ReactButton;
JavaScript
볡μ¬
컀μ€ν λ²νΌ
CustomButton.jsx
import 'bootstrap/dist/css/bootstrap.min.css';
import Button from 'react-bootstrap/Button';
const CustomButton = ({ text, btnStyle, btnSize, onClick }) => {
let variant = 'primary';
switch (btnStyle) {
case 'secondary': variant = 'secondary'; break;
case 'success': variant = 'success'; break;
case 'danger': variant = 'danger'; break;
case 'warning': variant = 'warning'; break;
case 'info': variant = 'info'; break;
case 'light': variant = 'light'; break;
case 'dark': variant = 'dark'; break;
default: break;
}
let size = '';
switch (btnSize) {
case 'xs': size = 'sm'; break;
case 'sm': size = 'sm'; break;
case 'md': size = ''; break;
case 'lg': size = 'lg'; break;
default: break;
}
return (
<Button variant={variant} size={size} onClick={() => onClick()}>{text}</Button>
);
}
export default CustomButton;
JavaScript
볡μ¬
App.js
import './App.css';
import CustomButton from './components/CustomButton';
import ReactButton from './components/ReactButton';
function App() {
const onClick = () => {
alert('Clicked!')
}
return (
<div className='container'>
<h1>react-bootstrap</h1>
<hr/>
<h2>κΈ°λ³Έ λ²νΌ</h2>
<ReactButton />
<hr/>
<h2>컀μ€ν
λ²νΌ</h2>
<h2>xs</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='xs' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xs' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xs' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xs' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xs' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xs' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xs' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xs' onClick={onClick} />
</div>
<h2>sm</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='sm' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='sm' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='sm' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='sm' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='sm' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='sm' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='sm' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='sm' onClick={onClick} />
</div>
<h2>md</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='md' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='md' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='md' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='md' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='md' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='md' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='md' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='md' onClick={onClick} />
</div>
<h2>lg</h2>
<div className='d-flex justify-content-between align-items-center'>
<CustomButton text='primary' btnStyle='primary' btnSize='lg' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='lg' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='lg' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='lg' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='lg' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='lg' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='lg' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='lg' onClick={onClick} />
</div>
<h2>xl</h2>
<div class="d-grid gap-2 col-6 mx-auto">
<CustomButton text='primary' btnStyle='primary' btnSize='xl' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='xl' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='xl' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='xl' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='xl' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='xl' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='xl' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='xl' onClick={onClick} />
</div>
<h2>block</h2>
<div class="d-grid gap-2">
<CustomButton text='primary' btnStyle='primary' btnSize='block' onClick={onClick} />
<CustomButton text='secondary' btnStyle='secondary' btnSize='block' onClick={onClick} />
<CustomButton text='success' btnStyle='success' btnSize='block' onClick={onClick} />
<CustomButton text='danger' btnStyle='danger' btnSize='block' onClick={onClick} />
<CustomButton text='warning' btnStyle='warning' btnSize='block' onClick={onClick} />
<CustomButton text='info' btnStyle='info' btnSize='block' onClick={onClick} />
<CustomButton text='light' btnStyle='light' btnSize='block' onClick={onClick} />
<CustomButton text='dark' btnStyle='dark' btnSize='block' onClick={onClick} />
</div>
</div>
);
}
export default App;
JavaScript
볡μ¬