Framework/React

[Material UI] Typography variant μ»€μŠ€ν…€ ν•˜λŠ” 방법

yuri lee 2023. 7. 10. 23:07
λ°˜μ‘ν˜•

Intro

μ•ˆλ…•ν•˜μ„Έμš”. 이번 μ‹œκ°„μ—λŠ” MUI Typography Component μ—μ„œ variant 속성을 μ»€μŠ€ν…€ ν•˜λŠ” 방법에 λŒ€ν•΄ μ•Œμ•„λ³΄κ² μŠ΅λ‹ˆλ‹€. 

 

MUI Typography variant

Typography 의 κΈ°λ³Έ variant 속성은 λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€. h1, h2, h3, h4, h5, h6, subtitle1, subtitle2, body1, body2 κ°€ μžˆμŠ΅λ‹ˆλ‹€. 이 외에도 직접 μ»€μŠ€ν…€ν•˜μ—¬ variantλ₯Ό μΆ”κ°€ν•˜κ³  싢을 λ•Œκ°€ μžˆμ„ κ²λ‹ˆλ‹€. 

const theme = createTheme({
  components: {
    MuiTypography: {
      defaultProps: {
        variantMapping: {
          h1: 'h2',
          h2: 'h2',
          h3: 'h2',
          h4: 'h2',
          h5: 'h2',
          h6: 'h2',
          subtitle1: 'h2',
          subtitle2: 'h2',
          body1: 'span',
          body2: 'span',
        },
      },
    },
  },
});

 

How to do

 

Step 1. Update the theme's typography object

μΆ”κ°€ν•˜κ³ μž ν•˜λŠ” variant nameκ³Ό μŠ€νƒ€μΌ 속성을 μΆ”κ°€ν•©λ‹ˆλ‹€. 

const theme = createTheme({
  typography: {
    poster: {
      fontSize: '4rem',
      color: 'red',
    },
    // Disable h3 variant
    h3: undefined,
  },
});

 

 

Step 2. (Optional) Set the default semantic element for your new variant

const theme = createTheme({
  typography: {
    poster: {
      fontSize: 64,
      color: 'red',
    },
    // Disable h3 variant
    h3: undefined,
  },
  components: {
    MuiTypography: {
      defaultProps: {
        variantMapping: {
          // Map the new variant to render a <h1> by default
          poster: 'h1',
        },
      },
    },
  },
});

 

Step 3. Update the necessary typings (if you are using TypeScript)

νƒ€μž…μŠ€ν¬λ¦½νŠΈλ₯Ό μ‚¬μš©ν•œλ‹€λ©΄, μ•„λž˜μ˜ 선언을 κΌ­ μΆ”κ°€ν•΄μ€˜μ•Ό ν•©λ‹ˆλ‹€. μΆ”κ°€ μ•ˆν•˜λ©΄ 였λ₯˜κ°€ λ‚˜λ”λΌκ³ μš”. 

declare module '@mui/material/styles' {
  interface TypographyVariants {
    poster: React.CSSProperties;
  }

  // allow configuration using `createTheme`
  interface TypographyVariantsOptions {
    poster?: React.CSSProperties;
  }
}

// Update the Typography's variant prop options
declare module '@mui/material/Typography' {
  interface TypographyPropsVariantOverrides {
    poster: true;
    h3: false;
  }
}

 

Step 4. You can now use the new variant

μΆ”κ°€ν•œ variantκ°€ μ œλŒ€λ‘œ μ μš©λ˜μ—ˆλŠ”μ§€ ν™•μΈν•΄λ΄…λ‹ˆλ‹€. 

<Typography variant="poster">poster</Typography>;

/* This variant is no longer supported. If you are using TypeScript it will give an error */
<Typography variant="h3">h3</Typography>;

 

Conclusion

μ²˜μŒμ—λŠ” μ–΄λ–»κ²Œ μ»€μŠ€ν…€μ„ ν•΄μ•Ό ν•˜λŠ”μ§€ λ§‰λ§‰ν–ˆλŠ”λ°, μ—­μ‹œ 정닡은 곡식 λ¬Έμ„œμ— λ‚˜μ™€ μžˆμ—ˆμŠ΅λ‹ˆλ‹€ :-) !! 

 


https://mui.com/material-ui/customization/typography/#adding-amp-disabling-variants

λ°˜μ‘ν˜•