|
| 1 | +const plugin = require('tailwindcss/plugin') |
| 2 | +const merge = require('lodash/merge') |
| 3 | +const pluginComponents = require('./components') |
| 4 | +const variablesApi = require('../../../api') |
| 5 | +// const variablesApi = require('@mertasan/tailwindcss-variables/api') |
| 6 | + |
| 7 | +/** |
| 8 | + * @typedef {Object} plugin |
| 9 | + * @property {function} withOptions |
| 10 | + */ |
| 11 | +module.exports = plugin.withOptions( |
| 12 | + function(options) { |
| 13 | + return function({ addComponents, theme, options, config }) { |
| 14 | + const myVariables = { |
| 15 | + DEFAULT: { |
| 16 | + colors: { |
| 17 | + primary: 'black', |
| 18 | + secondary: 'white', |
| 19 | + warning: 'pink', |
| 20 | + }, |
| 21 | + }, |
| 22 | + '.admin': { |
| 23 | + colors: { |
| 24 | + primary: 'blue', |
| 25 | + secondary: 'green', |
| 26 | + warning: 'gray', |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | + let pluginOptions = merge( |
| 31 | + { |
| 32 | + variablePrefix: '--prefix1', |
| 33 | + darkSelector: null, // default: .dark |
| 34 | + darkToRoot: false, // default: true ( :root.dark or .dark ) |
| 35 | + }, |
| 36 | + theme('myPlugin.variableOptions', {}), |
| 37 | + ) |
| 38 | + |
| 39 | + addComponents(variablesApi.variables(myVariables, pluginOptions)) |
| 40 | + |
| 41 | + myVariables.DEFAULT.colors.primary = 'yellow' |
| 42 | + addComponents(variablesApi.darkVariables(myVariables, pluginOptions, config('darkMode'))) |
| 43 | + |
| 44 | + // Automatically register components via API. |
| 45 | + addComponents(variablesApi.getComponents(null, theme('myPlugin.components', {}))) |
| 46 | + } |
| 47 | + }, |
| 48 | + function(options) { |
| 49 | + return { |
| 50 | + // darkMode: 'class', // or media |
| 51 | + theme: { |
| 52 | + myPlugin: (theme) => ({ |
| 53 | + variableOptions: { |
| 54 | + variablePrefix: '--prefix2', |
| 55 | + }, |
| 56 | + components: pluginComponents(theme), |
| 57 | + }), |
| 58 | + }, |
| 59 | + } |
| 60 | + }, |
| 61 | +) |
0 commit comments