最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get colour values from tailwind config file - Stack Overflow

programmeradmin1浏览0评论

I want to access the value of the "accent" colour from the config file so I can assign it to a new colour. I have tried danger: theme('colors.accent') but I'm getting this error > theme is not defined. I want to avoid hard coding the hex colour code again. Is there a way to do this easily?

tailwind.config.js

colors: {

accent: '#57A0D7',
accentlight: '#88BCE2',
accentdark: '#4B8CBD',
accentdarker: '#4682B0',

danger: theme(colors.accent),

I want to access the value of the "accent" colour from the config file so I can assign it to a new colour. I have tried danger: theme('colors.accent') but I'm getting this error > theme is not defined. I want to avoid hard coding the hex colour code again. Is there a way to do this easily?

tailwind.config.js

colors: {

accent: '#57A0D7',
accentlight: '#88BCE2',
accentdark: '#4B8CBD',
accentdarker: '#4682B0',

danger: theme(colors.accent),
Share Improve this question asked Sep 15, 2020 at 19:55 MarcoMarco 211 gold badge1 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

Sure thing. Just set the value you want to appear in multiple places outside of your module.exports and you can use it anywhere as a variable since the config file is just a js file.

const accent = '#57A0D7';

module.exports = {
  theme: {
   colors: {
    accent,
    accentlight: '#88BCE2',
    accentdark: '#4B8CBD',
    accentdarker: '#4682B0',
    danger: accent
   }
  }
 }

Try using a function

colors: {

accent: '#57A0D7',
accentlight: '#88BCE2',
accentdark: '#4B8CBD',
accentdarker: '#4682B0',

danger: theme => theme('colors.accent'),

You can use:

xxx: ({ theme }) => ({
  danger: theme(colors.accent),
})

In your example:

module.exports = {
  theme: {
    colors: {
      accent: '#57A0D7',
      accentlight: '#88BCE2',
      accentdark: '#4B8CBD',
      accentdarker: '#4682B0',

      // ...
    },
    background: ({ theme }) => ({
      danger: theme(colors.accent),
    })
  }
}

As shown here: https://tailwindcss./docs/theme#referencing-other-values

发布评论

评论列表(0)

  1. 暂无评论