๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • GDG on campus Ewha Tech Blog
3-1๊ธฐ ์Šคํ„ฐ๋””/React Native

[2์ฃผ์ฐจ] 4์žฅ ์Šคํƒ€์ผ๋ง

by ์„ ๋‹ฌ 2021. 11. 8.

๐ŸŽจ ์Šคํƒ€์ผ๋ง

๋ฆฌ์•กํŠธ ๋„ค์ดํ‹ฐ๋ธŒ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ด์šฉํ•ด ์Šคํƒ€์ผ๋ง


๋ฐฉ๋ฒ•1. ์ธ๋ผ์ธ ์Šคํƒ€์ผ๋ง

: ์ปดํฌ๋„ŒํŠธ์— ์ง์ ‘ ์Šคํƒ€์ผ ์ž…๋ ฅ

HTML์˜ ์ธ๋ผ์ธ ์Šคํƒ€์ผ๋ง๊ณผ ๋น„์Šท,
but ๊ฐ์ฒด ํ˜•ํƒœ๋กœ ์ „๋‹ฌํ•ด์•ผํ•จ (HTML์€ ๋ฌธ์ž์—ด ํ˜•ํƒœ๋กœ ์Šคํƒ€์ผ ์ž…๋ ฅ)

  • ์žฅ์  :
    ์–ด๋–ค ์Šคํƒ€์ผ์ด ์ ์šฉ๋˜๋Š”์ง€ ์ž˜ ๋ณด์ธ๋‹ค
  • ๋‹จ์  :
    ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ์— ๋™์ผํ•œ ์ฝ”๋“œ ๋ฐ˜๋ณต
    ํ•ด๋‹น ์Šคํƒ€์ผ ์ ์šฉ ์ด์œ ๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•˜๊ธฐ ์–ด๋ ค์›€
<Text
style={{
       flex: 1,
       backgroundColor: "#fff",
      }}
        >
          ๋‚ด์šฉ
</Text>

๋ฐฉ๋ฒ•2. ํด๋ž˜์Šค ์Šคํƒ€์ผ๋ง

: ์Šคํƒ€์ผ ์‹œํŠธ์— ์Šคํƒ€์ผ์„ ์ •์˜ํ•˜๊ณ  ์ปดํฌ๋„ŒํŠธ์—๋Š” ์ •์˜๋œ ์Šคํƒ€์ผ์˜ ์ด๋ฆ„์œผ๋กœ ์ ์šฉ

= CSS ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๊ณผ ์œ ์‚ฌ

 

  • ์žฅ์  : ์ „์ฒด์ ์ธ ์Šคํƒ€์ผ์„ ๊ด€๋ฆฌํ•˜๊ธฐ๊ฐ€ ์‰ฝ๋‹ค
const App = () => {
  return (
      <Text style={styles.text}>Inline Styling - Text</Text>
  );
};
const styles = StyleSheet.create({
  text: {
    padding: 10,
    fontSize: 26,
    fontWeight: "600",
    color: "black",
  },
});

ํ•œ๋ฒˆ์— ๋‘๊ฐ€์ง€ ์Šคํƒ€์ผ๋ง ์ ์šฉํ•˜๊ธฐ

  • ํด๋ž˜์Šค์Šคํƒ€์ผ๋ง ์ธ๋ผ์ธ์Šคํƒ€์ผ๋ง ๋™์‹œ์—
<Text style={(textStyles.text, { color: "green" })}> Inline Styling - Text </Text>
  • ํด๋ž˜์Šค์Šคํƒ€์ผ๋ง ๋‘๊ฐœ ์ด์ƒ ๋™์‹œ์— ์ ์šฉ
<Text style={[textStyles.text, textStyles.error]}> Inline Styling - Error </Text>

 

๊ฐ„๋‹จํ•˜๊ฒŒ ํ™”๋ฉด์„ ํ™•์ธํ•˜๋Š” ์ƒํ™ฉ -> ์ธ๋ผ์ธ์Šคํƒ€์ผ ใ„ฑใ…Š
์žฅ๊ธฐ์ ์œผ๋กœ๋Š” -> ํด๋ž˜์Šค ์Šคํƒ€์ผ๋ง์ด ๋‚˜์Œ

 


๐ŸŒท ์Šคํƒ€์ผ ์†์„ฑ๋“ค


1. flex

: width ๋‚˜ height ์™€ ๋‹ฌ๋ฆฌ ๋น„์œจ๋กœ ํฌ๊ธฐ๋ฅผ ๊ฒฐ์ •

  • ์ˆซ์ž๋ฅผ ๊ฐ’์œผ๋กœ ๋ฐ›์Œ (๋‹จ์œ„๊ฐ€ ์—†์Œ)
  • ๊ฐ’์ด 0์ผ๋•Œ -> width height ๋”ฐ๋ผ ๊ฒฐ์ •
  • ๊ฐ’์ด ์–‘์ˆ˜์ผ๋•Œ -> flex ๊ฐ’์— ๋น„๋ก€ํ•˜์—ฌ ํฌ๊ธฐ ์กฐ์ •

2. ์ •๋ ฌ

flexDierection

: ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ์Œ“์ด๋Š” ๋ฐฉํ–ฅ์„ ๋ณ€๊ฒฝ

  • column (๊ธฐ๋ณธ) : ์„ธ๋กœ๋ฐฉํ–ฅ
  • column-reverse : ์„ธ๋กœ๋ฐฉํ–ฅ ์—ญ์ˆœ
  • row : ๊ฐ€๋กœ๋ฐฉํ–ฅ
  • row-reverse : ๊ฐ€๋กœ๋ฐฉํ–ฅ ์—ญ์ˆœ

 

justifyComponent

: flexDirection ์—์„œ ๊ฒฐ์ •ํ•œ ๋ฐฉํ–ฅ๊ณผ ๋™์ผํ•œ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ

  • flex-start (๊ธฐ๋ณธ) : ์‹œ์ž‘์ ๋ถ€ํ„ฐ
  • flex-end : ๋์—์„œ๋ถ€ํ„ฐ
  • center : ์ค‘์•™
  • space-between : ์ปดํฌ๋„ŒํŠธ ์‚ฌ์ด์˜ ๊ณต๊ฐ„์„ ๋™์ผํ•˜๊ฒŒ
  • space-around : ์ปดํฌ๋„ŒํŠธ ๊ฐ๊ฐ์˜ ์ฃผ๋ณ€ ๊ณต๊ฐ„์„ ๋™์ผํ•˜๊ฒŒ
  • space-evenly : ์ปดํฌ๋„ŒํŠธ ์‚ฌ์ด์™€ ์–‘๋ ๊ณต๊ฐ„์„ ๋™์ผํ•˜๊ฒŒ

 

alignItems

: flexDirection ์—์„œ ๊ฒฐ์ •ํ•œ ๋ฐฉํ–ฅ๊ณผ ์ˆ˜์ž‘์ธ ๋ฐฉํ–ฅ์œผ๋กœ ์ •๋ ฌ

  • flex-start (๊ธฐ๋ณธ) : ์‹œ์ž‘์ ์—์„œ๋ถ€ํ„ฐ
  • flex-end : ๋์—์„œ๋ถ€ํ„ฐ
  • center : ์ค‘์•™
  • stretch : alitnItems ์˜ ๋ฐฉํ–ฅ์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ํ™•์žฅ
  • baseline : ๋‚ด๋ถ€์˜ ํ…์ŠคํŠธ ๋ฒ ์ด์Šค๋ผ์ธ ์„ ๊ธฐ์ค€์œผ๋กœ

3. ๊ทธ๋ฆผ์ž

iOS

  • shadowColor : ๊ทธ๋ฆผ์ž์˜ ์ƒ‰
  • shadowOffset : (width ์™€ height ์ด์šฉ) ๊ทธ๋ฆผ์ž ๊ฑฐ๋ฆฌ
  • shadowOpacity : ๊ทธ๋ฆผ์ž์˜ ๋ถˆํˆฌ๋ช…๋„
  • shadowRadius : ๊ทธ๋ฆผ์ž์˜ ํ๋ฆผ ๋ฐ˜๊ฒฝ

android

  • elevation : ๊ทธ๋ฆผ์ž์˜ ๊ณ ๋„

platform

: ํ”Œ๋žซํผ (iOS, android) ๋ณ„๋กœ ๋‹ค๋ฅธ ์ฝ”๋“œ๊ฐ€ ์ ์šฉ๋˜๊ฒŒ ํ•˜๋Š” ๋ฆฌ์•กํŠธ ๋ชจ๋“ˆ
https://reactnative.dev/docs/platform-specific-code#platform-module

...Platform.select({
      ios: {
        shadowColor: "#000",
        shadowOffset: {
          width: 10,
          height: 10,
        },
        shadowOpacity: 0.5,
        shadowRadius: 10,
      },
      android: {
        elevation: 20,
      },
    }),

 


๐Ÿ–Œ ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธ

: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ ์•ˆ์— ์Šคํƒ€์ผ์„ ์ž‘์„ฑํ•˜๋Š” CSS-in-JS ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
= ์Šคํƒ€์ผ์ด ์ ์šฉ๋œ ์ปดํฌ๋„ŒํŠธ


https://styled-components.com/

 

styled-components

Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress ๐Ÿ’…๐Ÿพ

styled-components.com

 

  • ์นด๋ฉœํ‘œ๊ธฐ๋ฒ•์ด ์•„๋‹Œ ํ•˜์ดํ”ˆ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • CSS ์†์„ฑ๊ณผ ์ด๋ฆ„์ด ๊ฐ™์„๋•Œ ํƒ€์ž…๊ณผ ๋‹จ์œ„๋„ ํ†ต์ผ
  • ์ž…๋ ฅ๊ฐ’์— ๋งž์ถฐ ํƒ€์ž…์ด ๋‹ฌ๋ผ์ง€๋Š” ๊ฒฝ์šฐ๊ฐ€ ์—†๋‹ค
  • ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ์ปดํฌ๋„ŒํŠธ๋“ค์„ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค
  • Tagged Template Literals

์„ค์น˜

npm install styled-components


๊ธฐ๋ณธ๋ฌธ๋ฒ•

import styled from 'styled-components/native'; 

const ์ด๋ฆ„ = styled.์ปดํฌ๋„ŒํŠธ์ด๋ฆ„`
    ๋‚ด์šฉ
`

ex

const TextComponent = styled.Text`
    color: #fff;
`

์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์ปดํฌ๋„ŒํŠธ ์žฌ์‚ฌ์šฉ

const ์ปดํฌ๋„ŒํŠธ์ด๋ฆ„ = styled(์ƒ์†๋ฐ›์„์ปดํฌ๋„ŒํŠธ์ด๋ฆ„)`
    ๋‚ด์šฉ
`

const NameTextComponent = styled(TextComponent)`
    color: blue;
`

props ์‚ฌ์šฉํ•˜๊ธฐ

const ์ด๋ฆ„ = styled.์ปดํฌ๋„ŒํŠธ์ด๋ฆ„`
  ์†์„ฑ: ${(props) =>
    ์กฐ๊ฑด ? ์ฐธ์ผ๋•Œ์˜ ์†์„ฑ๊ฐ’ : ๊ฑฐ์ง“์ผ๋•Œ์˜ ์†์„ฑ๊ฐ’};
`;

ex

const ButtonContainer = styled.TouchableOpacity`
  background-color: ${(props) =>
    props.title === "Seondal" ? "#3498db" : "#9b59b6"};
`;

attrs

  • ์Šคํƒ€์ผ์„ ์ž‘์„ฑํ•˜๋Š” ๊ณณ์—์„œ ์ปดํฌ๋„ŒํŠธ์˜ ์†์„ฑ ์„ค์ • ๊ฐ€๋Šฅ
  • ์†์„ฑ ์„ค์ •ํ•  ๋•Œ ์ „๋‹ฌ๋œ props ์‚ฌ์šฉ ๊ฐ€๋Šฅ -> props ๋”ฐ๋ผ ์†์„ฑ ์„ค์ •
const ์ด๋ฆ„ = styled.์ปดํฌ๋„ŒํŠธ์ด๋ฆ„.attrs((props) => ({
  ์†์„ฑ: props.props์š”์†Œ,
}))`
  ์†์„ฑ1: ${(props) => props.props์š”์†Œ};
  ์†์„ฑ2: ์†์„ฑ๊ฐ’;
`;

ThemeProvider

: Context API ๋ฅผ ํ™œ์šฉํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์ฒด์—์„œ ์Šคํƒ€์ผ๋“œ ์ปดํฌ๋„ŒํŠธ๋ฅผ ์ด์šฉํ• ๋•Œ ๋ฏธ๋ฆฌ ์ •์˜ํ•œ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก props ๋กœ ์ „๋‹ฌ

์•ฑ๊ฐœ๋ฐœ์ด๋‚˜ ์›น๊ฐœ๋ฐœ์—์„œ ๋‚˜์ดํŠธ๋ชจ๋“œ ๋“ฑ์„ ์„ค์ •ํ• ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„๋“ฏ

 


 

๋Œ“๊ธ€