-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
60 lines (52 loc) · 1.42 KB
/
index.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { Context, CSSRules, ThemeSection, Directive, MaybeThunk } from 'twind'
import { directive } from 'twind'
declare module 'twind' {
interface Theme {
content?: ThemeSection<string>
}
}
export interface Content {
(value: string): Directive<CSSRules>
(parts: string[], context: Context): CSSRules
}
const KNOWN_VALUES = new Set([
'open-quote',
'close-quote',
'no-open-quote',
'no-close-quote',
'normal',
'none',
'inherit',
'initial',
'unset',
])
const join = (parts: string[]): string => parts.join('-')
const stringify = (parts: string[]): string => {
switch (parts[0]) {
case 'data':
return `attr(${join(parts)})`
case 'attr':
case 'counter':
return `${parts[0]}(${join(parts.slice(1))})`
case 'var':
return `var(--${join(parts)})`
case undefined:
return `attr(data-content)`
default:
return JSON.stringify(join(parts))
}
}
const content$ = (parts: string | string[], { theme }: Context): CSSRules => {
const value = Array.isArray(parts) ? join(parts) : parts
return {
content:
(value && theme('content', [value], '')) ||
(KNOWN_VALUES.has(value) && value) ||
(Array.isArray(parts) ? stringify(parts) : value),
}
}
export const content = ((
value: string | string[],
context: Context,
): CSSRules | Directive<CSSRules> =>
Array.isArray(value) ? content$(value, context) : directive(content$, value)) as Content