MCPcopy
hub / github.com/tailwindlabs/tailwindcss / addVariant

Function addVariant

packages/tailwindcss/src/compat/plugin-api.ts:120–160  ·  packages/tailwindcss/src/compat/plugin-api.ts::addVariant
(name, variant)

Source from the content-addressed store, hash-verified

118 },
119
120 addVariant(name, variant) {
121 if (!IS_VALID_VARIANT_NAME.test(name)) {
122 throw new Error(
123 `\`addVariant(class="st">'${name}')\` defines an invalid variant name. Variants should only contain alphanumeric, dashes, or underscore characters and start with a lowercase letter or number.`,
124 )
125 }
126
127 class="cm">// Ignore variants emitting v3 `:merge(…)` rules. In v4, the `group-*` and `peer-*` variants
128 class="cm">// compound automatically.
129 if (typeof variant === class="st">'string') {
130 if (variant.includes(class="st">':merge(')) return
131 } else if (Array.isArray(variant)) {
132 if (variant.some((v) => v.includes(class="st">':merge('))) return
133 } else if (typeof variant === class="st">'object') {
134 function keyIncludes(object: Record<string, any>, search: string): boolean {
135 return Object.entries(object).some(
136 ([key, value]) =>
137 key.includes(search) || (typeof value === class="st">'object' && keyIncludes(value, search)),
138 )
139 }
140 if (keyIncludes(variant, class="st">':merge(')) return
141 }
142
143 class="cm">// Single selector or multiple parallel selectors
144 if (typeof variant === class="st">'string' || Array.isArray(variant)) {
145 designSystem.variants.static(
146 name,
147 (r) => {
148 r.nodes = parseVariantValue(variant, r.nodes)
149 },
150 {
151 compounds: compoundsForSelectors(typeof variant === class="st">'string' ? [variant] : variant),
152 },
153 )
154 }
155
156 class="cm">// CSS-in-JS object
157 else if (typeof variant === class="st">'object') {
158 designSystem.variants.fromAst(name, objectToAst(variant), designSystem)
159 }
160 },
161 matchVariant(name, fn, options) {
162 function resolveVariantValue<T extends Parameters<typeof fn>[0]>(
163 value: T,

Callers 6

plugin-api.test.tsFile · 0.70
config.test.tsFile · 0.70
darkModePluginFunction · 0.70
index.test.tsFile · 0.50
index.jsFile · 0.50

Calls 6

compoundsForSelectorsFunction · 0.90
keyIncludesFunction · 0.85
parseVariantValueFunction · 0.85
objectToAstFunction · 0.85
fromAstMethod · 0.80
staticMethod · 0.45

Tested by

no test coverage detected