Skip to content

Commit

Permalink
fix: fix tailwind function for classes with multiple hyphens
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed May 5, 2023
1 parent 00ee772 commit 26bbcac
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/functions/tailwind.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ describe('Tailwind', () => {
'w-full border border-gray-300 rounded xl:p-4 mt-2 mb-4 shadow-xl p-4'
)
})

it('should support multi `-` classes', () => {
const gridClasses = twClassNameFn('grid grid-cols-3')

expect(gridClasses('grid-cols-4')).toBe('grid grid-cols-4')
})
})
10 changes: 8 additions & 2 deletions src/functions/tailwind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,19 @@ export const twClassNameFn = (twClasses: string[] | string) => {
typeof classNames === 'string' ? classNames.split(' ') : classNames

let classesToApply = defaultClasses.map(className => {
const [prefix, value] = className.split('-')
const parts = className.split('-')

const value = parts.length > 1 ? parts.pop() : undefined
const prefix = parts.join('-')

return {prefix, value, className}
})

classes.forEach(className => {
const [prefix, value] = className.split('-')
const parts = className.split('-')

const value = parts.length > 1 ? parts.pop() : undefined
const prefix = parts.join('-')

classesToApply = classesToApply.filter(applicable => {
return applicable.prefix !== prefix
Expand Down

0 comments on commit 26bbcac

Please sign in to comment.