Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src/components/alert/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const Alert: FC<AlertProps> = ({ className, title, variant = 'info', stic
>
<Icon
className={cn('w-4 h-4 min-w-4 min-h-4', {
'mt-0.5': title,
'mt-0.5': !!title,
'-mt-1': !title && sticky,
})}
/>
Expand Down
28 changes: 26 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
import { clsx, type ClassValue } from 'clsx'
import { clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'

export function cn(...inputs: ClassValue[]) {
// Stricter than `ClassValue` from clsx
export type ClassValueStrict = ClassArrayStrict | ClassDictionaryStrict | string | null | false | undefined
export type ClassDictionaryStrict = Record<string, boolean | null | undefined>
export type ClassArrayStrict = ClassValueStrict[]

export function cn(...inputs: ClassValueStrict[]) {
return twMerge(clsx(inputs))
}

{
// Type-only test:
const condition = false
// `clsx` is not strict enough
clsx('class1', 2, condition && 'no3', { class4: true, class5: 5, class6: [{ why: '?' }] })
// @ts-expect-error `cn` should be stricter and disallow this
cn('class1', 2, condition && 'no3', { class4: true, class5: 5, class6: [{ why: '?' }] })
// @ts-expect-error `cn` should be stricter and disallow this
cn('class1', 2)
// @ts-expect-error `cn` should be stricter and disallow this
cn('class1', true, 'class3')
// @ts-expect-error `cn` should be stricter and disallow this
cn('class1', { class5: 5 })
// @ts-expect-error `cn` should be stricter and disallow this
cn('class1', { class6: [{ why: '?' }] })
// Valid
cn('class1', condition && 'no3', { class4: true })
}