Skip to content

Commit ae8eb38

Browse files
committed
fix(tailwind): prefix misapplied on negative value
1 parent 1c8b5d6 commit ae8eb38

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

packages/cli/src/utils/transformers/transform-tw-prefix.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,20 +89,33 @@ export function applyPrefix(input: string, prefix: string = '') {
8989
const prefixed: string[] = []
9090
for (const className of classNames) {
9191
const [variant, value, modifier] = splitClassName(className)
92+
const valueWithPrefix = handleNegativeVariants(value, prefix)
9293
if (variant) {
9394
modifier
94-
? prefixed.push(`${variant}:${prefix}${value}/${modifier}`)
95-
: prefixed.push(`${variant}:${prefix}${value}`)
95+
? prefixed.push(`${variant}:${valueWithPrefix}/${modifier}`)
96+
: prefixed.push(`${variant}:${valueWithPrefix}`)
9697
}
9798
else {
9899
modifier
99-
? prefixed.push(`${prefix}${value}/${modifier}`)
100-
: prefixed.push(`${prefix}${value}`)
100+
? prefixed.push(`${valueWithPrefix}/${modifier}`)
101+
: prefixed.push(`${valueWithPrefix}`)
101102
}
102103
}
103104
return prefixed.join(' ')
104105
}
105106

107+
function handleNegativeVariants(value: string | null, prefix: string = '') {
108+
if (!prefix || !value) {
109+
return value
110+
}
111+
112+
if (value.startsWith('-')) {
113+
return `-${prefix}${value.substring(1)}`
114+
}
115+
116+
return `${prefix}${value}`
117+
}
118+
106119
export function applyPrefixesCss(css: string, prefix: string) {
107120
const lines = css.split('\n')
108121
for (const line of lines) {

packages/cli/test/utils/__snapshots__/transform-tw-prefix.test.ts.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ exports[`transform tailwind prefix 4`] = `
6767
:class="
6868
cn(
6969
'tw-flex',
70-
orientation === 'horizontal' ? 'tw--ml-4' : 'tw--mt-4 tw-flex-col',
70+
orientation === 'horizontal' ? '-tw-ml-4' : '-tw-mt-4 tw-flex-col',
7171
props.class,
7272
)
7373
"

packages/cli/test/utils/apply-prefix.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ describe('apply tailwind prefix', () => {
3636
output:
3737
'tw-absolute tw-right-4 tw-top-4 tw-bg-primary tw-rounded-sm tw-opacity-70 tw-ring-offset-background tw-transition-opacity hover:tw-opacity-100 focus:tw-outline-none focus:tw-ring-2 focus:tw-ring-ring focus:tw-ring-offset-2 disabled:tw-pointer-events-none data-[state=open]:tw-bg-secondary',
3838
},
39+
{
40+
input:
41+
'-mt-8 hover:-mt-8 focus:-mt-8 active:-mt-8 disabled:-mt-8',
42+
output:
43+
'-tw-mt-8 hover:-tw-mt-8 focus:-tw-mt-8 active:-tw-mt-8 disabled:-tw-mt-8',
44+
},
3945
])(`applyTwPrefix($input) -> $output`, ({ input, output }) => {
4046
expect(applyPrefix(input, 'tw-')).toBe(output)
4147
})

0 commit comments

Comments
 (0)