Skip to content

Commit

Permalink
fix: nested classNames in components like Sonner were not having pref…
Browse files Browse the repository at this point in the history
…ix applied correctly
  • Loading branch information
danbt committed Apr 19, 2024
1 parent bebc284 commit 156b1a4
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/cli/src/utils/transformers/transform-tw-prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,40 @@ export const transformTwPrefixes: Transformer = async ({
})
}
}

// Handling cases where there are descendant classNames, e.g., in a Sonner component.
if (node.isKind(SyntaxKind.JsxAttribute)) {
const attributeInitializer = node.getInitializer()

if (attributeInitializer?.isKind(SyntaxKind.JsxExpression)) {
const expressionContent = attributeInitializer.getExpression()

if (
expressionContent &&
expressionContent.getKind() === SyntaxKind.ObjectLiteralExpression
) {
const propertyAssignments = expressionContent.getDescendantsOfKind(
SyntaxKind.PropertyAssignment
)

propertyAssignments.forEach((property) => {
if (property.getInitializer()?.isKind(SyntaxKind.StringLiteral)) {
if (property.getName() !== "variant") {
const classValue = property.getInitializer()
if (classValue) {
classValue.replaceWithText(
`"${applyPrefix(
classValue.getText()?.replace(/"/g, ""),
config.tailwind.prefix
)}"`
)
}
}
}
})
}
}
}
})

return sourceFile
Expand Down

0 comments on commit 156b1a4

Please sign in to comment.