javascript - 顺风 CSS : Referencing to custom color in tailwind. config.js

标签 javascript tailwind-css tailwind-in-js module-export

为了简化我的主题,我想引用我定义的自定义颜色,然后将它传递给一个函数以获得更亮或更暗的变体。

我使用以下(部分)代码扩展默认颜色主题:

module.exports = {
    theme: {
        extend: {
            colors: {
                primary: {
                    DEFAULT: '#325889',
                    light: '#5aacbb',
                    lighter: '#5ebebf',
                },
            },
        },
    },
}

现在我的目标是以某种方式引用另一个自定义颜色变体中的 colors.primary 以将其传递到自定义函数中,如下所示:

module.exports = {
    theme: {
        extend: {
            colors: {
                primary: {
                    DEFAULT: '#325889',
                    light: '#5aacbb',
                    lighter: '#5ebebf',
                },
                gradient: {
                    '0\/3': this.theme.extend.colors.primary,
                    '1\/3': getGradientStop(this.theme.extend.colors.primary, this.theme.extend.colors.primary.lighter, 33.333),
                    '2\/3': getGradientStop(this.theme.extend.colors.primary, this.theme.extend.colors.primary.lighter, 66.666),
                    '3\/3': this.theme.extend.colors.primary.lighter,
                }
            },
        },
    },
}

但是,我似乎无法以任何方式引用原色。我尝试了 this.colors.primarythis.theme.extend.colors.primary 但似乎无法启动和运行它。

任何有关如何执行此操作的线索将不胜感激。

干杯!

最佳答案

您可以创建新变量来保留扩展颜色的值:

const primary = '#325889';
const primaryLight = '#5aacbb';
const primaryLighter = '#5ebebf';

module.exports = {
    theme: {
        extend: {
            colors: {
                primary: {
                    DEFAULT: primary,
                    light: primaryLight,
                    lighter: primaryLighter,
                },
                gradient: {
                    '0\/3': primary,
                    '1\/3': getGradientStop(primary, primaryLighter, 33.333),
                    '2\/3': getGradientStop(primary, primaryLighter, 66.666),
                    '3\/3': primaryLighter,
                }
            },
        },
    },
};

关于javascript - 顺风 CSS : Referencing to custom color in tailwind. config.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69414890/

相关文章:

javascript - 来自 javascript 的字符串 "true"在 php 中无法识别为该字符串值

javascript - 单击事件不适用于动态添加的 Li->span->id

html - 使用带有顺风的 flex-row 和 flex-col 无法使用计算机和电话正确对齐

html - Flexbox child 的高度不一样

css - 为什么 tailwind 中只生成一些 css 类?

javascript - Tailwindcss 不适用于 NextJS 12。实验性功能

javascript - Bootstrap 导航栏悬停时不显示下拉菜单

css - Tailwind CSS - 固定位置的溢出页脚

reactjs - 如何防止 Tailwind 在聚焦时更改文本输入字段的边框?

javascript - 跨浏览器javascript函数查找页面中元素的实际位置