Skip to content

Commit b81ceaa

Browse files
committed
add addVariant shorthand signature
The current API looks like this: ```js addVariant('name', ({ format, wrap }) => { // Wrap in an atRule wrap(postcss.atRule({ name: 'media', params: '(prefers-reduced-motion: reduce)' })) // "Mutate" the selector, for example prepend `.dark` format('.dark &') }) ``` It is also pretty common to have this: ```js addVariant('name', ({ format }) => format('.dark &')) ``` So we simplified this to: ```js addVariant('name', '.dark &') ``` It is also pretty common to have this: ```js addVariant('name', ({ wrap }) => wrap(postcss.atRule({ name: 'media', params: '(prefers-reduced-motion: reduce)' }))) ``` So we simplified this to: ```js addVariant('name', '@media (prefers-reduced-motion: reduce)') ```
1 parent 25c8000 commit b81ceaa

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/lib/setupContextUtils.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,18 @@ function buildPluginApi(tailwindConfig, context, { variantList, variantMap, offs
186186

187187
return {
188188
addVariant(variantName, variantFunctions, options = {}) {
189-
variantFunctions = [].concat(variantFunctions)
189+
variantFunctions = [].concat(variantFunctions).map((variantFunction) => {
190+
if (typeof variantFunction !== 'string') {
191+
return variantFunction
192+
}
193+
194+
if (!variantFunction.startsWith('@')) {
195+
return ({ format }) => format(variantFunction)
196+
}
197+
198+
let [, name, params] = /@(.*?) (\(.*\))/g.exec(variantFunction)
199+
return ({ wrap }) => wrap(postcss.atRule({ name, params }))
200+
})
190201

191202
insertInto(variantList, variantName, options)
192203
variantMap.set(variantName, variantFunctions)

0 commit comments

Comments
 (0)