generated from yunsii/starter-vite-react-library
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcls.ts
30 lines (24 loc) · 859 Bytes
/
cls.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import clsx from 'clsx'
function normalizeStrings(strings: string[]) {
const lineCommentPattern = /\/\/.*((\r?\n)|$)/g
let result = strings.join(' ')
if (result.includes('//')) {
result = result.replace(lineCommentPattern, '')
}
return result.replace(/\s+/g, ' ').trim()
}
export function handleCls(
strings: TemplateStringsArray,
...expressions: any[]
) {
const classNamesList = strings.reduce((prev, current, currentIndex) => {
const expression = expressions[currentIndex] || ''
prev.push(current, clsx(expression))
return prev
}, [] as string[])
return normalizeStrings(classNamesList)
}
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates
export function cls(strings: TemplateStringsArray, ...expressions: any[]) {
return handleCls(strings, ...expressions)
}