본문 바로가기
Next

[Next] Next+styled components에서 SVG 커스텀하기

by lvd-hy 2024. 1. 8.
반응형

 

1. SVG 로더 설치

npm i @svgr/webpack

2. next.config.js 설정

module.exports = {
  webpack(config) {
    const fileLoaderRule = config.module.rules.find((rule) =>
      rule.test?.test?.('.svg'),
    )

    config.module.rules.push(
      {
        ...fileLoaderRule,
        test: /\.svg$/i,
        resourceQuery: /url/, // *.svg?url
      },
      {
        test: /\.svg$/i,
        issuer: fileLoaderRule.issuer,
        resourceQuery: { not: [...fileLoaderRule.resourceQuery.not, /url/] }, // exclude if *.svg?url
        use: ['@svgr/webpack'],
      },
    )

    fileLoaderRule.exclude = /\.svg$/i

    return config
  },

}

3. 커스텀하기

3-1. SVG파일의 변경을 원하는 속성의 값 current로 변경해주기

3-2. styled-components로 스타일링 주기

아래 처럼 적용이 잘된다! (적용할려고 엄청 헤맨거 안비밀)

 

Reference

https://styled-components.com/docs/basics#extending-styles

 

styled-components: Basics

Get Started with styled-components basics.

styled-components.com

https://react-svgr.com/docs/next/

 

SVGR - Transforms SVG into React Components. - SVGR

Transforms SVG into React Components.

react-svgr.com

 

반응형

'Next' 카테고리의 다른 글

[Next] ReferenceError: window is not defined  (0) 2024.02.14
[Next] Next.js + Styled Components 사용하기  (0) 2024.01.08
[Next] next.js 13 초기 세팅  (0) 2023.10.13

댓글