使用GatsbyJS时更改Antd的主题

时间:2018-08-13 22:51:18

标签: antd gatsby

这个GatsbyJS / antd插件页面(https://github.com/bskimball/gatsby-plugin-antd/issues/2)使得在使用GatsbyJS时似乎可以编辑ant.design(antd)主题。提供的代码是

plugins: [
  {
      resolve: 'gatsby-plugin-antd',
      options: {
        style: true
      }
  }
]

但是没有其他信息。人们将在何处更改主题原色(如上所述:https://ant.design/docs/react/customize-theme)。 ant.design页面(https://ant.design/docs/react/customize-theme)表示要通过执行以下操作来更改原色:

"theme": {
  "primary-color": "#1DA57A",
},

目前尚不清楚在GatbsyJS中将哪个变量设置在哪个文件中。

4 个答案:

答案 0 :(得分:3)

此配置对我有用。 我尚未添加babel-plugin-import,也没有修改gatsby-node.js文件。

这是我的package.json文件

{
  "name": "web-master",
  "private": true,
  "description": "Become a fullstack developer",
  "version": "1.0",
  "author": "Vishal Shetty",
  "dependencies": {
    "gatsby": "^2.19.7",
    "gatsby-image": "^2.2.39",
    "gatsby-plugin-manifest": "^2.2.39",
    "gatsby-plugin-offline": "^3.0.32",
    "gatsby-plugin-react-helmet": "^3.1.21",
    "gatsby-plugin-sharp": "^2.4.3",
    "gatsby-source-filesystem": "^2.1.46",
    "gatsby-transformer-sharp": "^2.3.13",
    "prop-types": "^15.7.2",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-helmet": "^5.2.1"
  },
  "devDependencies": {
    "antd": "^3.26.12",
    "gatsby-plugin-antd": "^2.1.0",
    "gatsby-plugin-less": "^3.0.19",
    "less": "^3.11.1",
    "less-loader": "^5.0.0",
    "prettier": "^1.19.1"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "scripts": {
    "build": "gatsby build",
    "develop": "gatsby develop",
    "format": "prettier --write \"**/*.{js,jsx,json,md}\"",
    "start": "npm run develop",
    "serve": "gatsby serve",
    "clean": "gatsby clean"
  }
}

我的gatsby-config.js文件

module.exports = {
  siteMetadata: {
    title: `WebMaster`,
    description: `Become a fullstack developer`,
    author: `@gatsbyjs`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
      },
    },
    {
      resolve: "gatsby-plugin-antd",
      options: {
        style: true,
      },
    },
    {
      resolve: "gatsby-plugin-less",
      options: {
        javascriptEnabled: true,
        modifyVars: {
          "primary-color": "#E4572E",
        },
      },
    },
  ],
}

请注意插件gatsby-plugin-antdgatsby-plugin-less的配置。

就这样,它就可以了。

答案 1 :(得分:2)

GitHub存储库,示例:https://github.com/uciska/gatsby-less-v2。要获取Antd,必须对三个Gatsby文件进行更改。

gastby-config.js示例:

module.exports = {
  siteMetadata: {
    title: 'My site'
  },
  plugins: [
    {
      resolve: `gatsby-plugin-less`,
      options: {
        javascriptEnabled: true,
        modifyVars: {
          'primary-color': '#da3043',
          'font-family': 'Arial',
          'layout-body-background': '#66ff79'
        }
      }
    }
  ]
}

gastby-node.js示例:

exports.onCreateBabelConfig = ({ actions }) => {
  actions.setBabelPlugin({
    name: 'babel-plugin-import',
    options: {
      libraryName: 'antd',
      style: true
    }
  })
}

示例package.json:

{
  "name": "gatsby-starter-hello-world",
  "description": "Gatsby hello world starter",
  "license": "MIT",
  "scripts": {
    "develop": "gatsby develop",
    "build": "gatsby build",
    "serve": "gatsby serve"
  },
  "dependencies": {
    "antd": "^3.6.4",
    "babel-plugin-import": "^1.8.0",
    "gatsby": "next",
    "gatsby-plugin-less": "next",
    "less": "^3.0.4",
    "less-loader": "^4.1.0",
    "react": "^16.3.2",
    "react-dom": "^16.3.2",
    "react-apollo": "^2.1.11"
  }
}

答案 2 :(得分:2)

我已经尝试了几年前写的所有上述答案,但没有奏效。经过几个小时的研究,最后,我能够使用 antd 修改 gatsby 默认变量。解决办法如下:

gatsby-config.js

[
...
...
   {
      resolve: "gatsby-plugin-antd",
      options: {
        style: true,
      },
    },

    {
      resolve: "gatsby-plugin-less",
      options: {
        lessOptions: {
          modifyVars: { //direct child node of lessOptions
            "primary-color": "#C53333", //your preferred color
          },
          javascriptEnabled: true, //direct child node of lessOptions
        },
      },
    },

]

lessless-loader 不需要修改默认的 antd 变量。

package.json

"dependencies": {
  "antd": "^4.12.2",
  "gatsby-plugin-antd": "^2.2.0",
  "gatsby-plugin-less": "^4.7.0",
}

附注:找到所有 ant-design 默认变量 here

答案 3 :(得分:0)

对@cannin的原始答案表示赞赏。我已使用最新的库名称对其进行了更新,并重新组织了文本。

要将ANTD与Gatsby结合使用并覆盖原始样式

  1. 添加依赖项
    yarn add babel-plugin-import less-loader antd gatsby-plugin-less
  2. 更新盖茨比的设置:
    gastby-config.js
    添加到plugins数组

    {
      resolve: `gatsby-plugin-less`,
      options: {
        javascriptEnabled: true,
        modifyVars: {
          'primary-color': '#663399',
          'font-family': 'Arial',
          'layout-body-background': '#66ff79'
        }
      },
    },
    

    gastby-node.js

    exports.onCreateBabelConfig = ({ actions }) => {
      actions.setBabelPlugin({
        name: 'babel-plugin-import',
        options: {
          libraryName: 'antd',
          style: true
        }
      })
    }
    
  3. 在组件中使用antd的示例。此primary按钮应以Gatsby紫色#663399

    显示
    import React from "react"
    import Layout from '../components/layout'
    import {Button} from 'antd'
    
    export default () => (
      <Layout>
        <div>
          <h1>ANTD</h1>
          <p>Such wow. Very React.</p>
          <Button type="primary">Button</Button>
        </div>
      </Layout>
    )