Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/cli/src/utils/transformers/transform-tw-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,20 +89,33 @@ export function applyPrefix(input: string, prefix: string = '') {
const prefixed: string[] = []
for (const className of classNames) {
const [variant, value, modifier] = splitClassName(className)
const valueWithPrefix = handleNegativeVariants(value, prefix)
if (variant) {
modifier
? prefixed.push(`${variant}:${prefix}${value}/${modifier}`)
: prefixed.push(`${variant}:${prefix}${value}`)
? prefixed.push(`${variant}:${valueWithPrefix}/${modifier}`)
: prefixed.push(`${variant}:${valueWithPrefix}`)
}
else {
modifier
? prefixed.push(`${prefix}${value}/${modifier}`)
: prefixed.push(`${prefix}${value}`)
? prefixed.push(`${valueWithPrefix}/${modifier}`)
: prefixed.push(`${valueWithPrefix}`)
}
}
return prefixed.join(' ')
}

function handleNegativeVariants(value: string | null, prefix: string = '') {
if (!prefix || !value) {
return value
}

if (value.startsWith('-')) {
return `-${prefix}${value.substring(1)}`
}

return `${prefix}${value}`
}

export function applyPrefixesCss(css: string, prefix: string) {
const lines = css.split('\n')
for (const line of lines) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ exports[`transform tailwind prefix 4`] = `
:class="
cn(
'tw-flex',
orientation === 'horizontal' ? 'tw--ml-4' : 'tw--mt-4 tw-flex-col',
orientation === 'horizontal' ? '-tw-ml-4' : '-tw-mt-4 tw-flex-col',
props.class,
)
"
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/test/utils/apply-prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ describe('apply tailwind prefix', () => {
output:
'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',
},
{
input:
'-mt-8 hover:-mt-8 focus:-mt-8 active:-mt-8 disabled:-mt-8',
output:
'-tw-mt-8 hover:-tw-mt-8 focus:-tw-mt-8 active:-tw-mt-8 disabled:-tw-mt-8',
},
])(`applyTwPrefix($input) -> $output`, ({ input, output }) => {
expect(applyPrefix(input, 'tw-')).toBe(output)
})
Expand Down