-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathClassTable.js
More file actions
213 lines (199 loc) · 6.85 KB
/
Copy pathClassTable.js
File metadata and controls
213 lines (199 loc) · 6.85 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import dlv from 'dlv'
import { memo } from 'react'
import { defaultConfig } from '@/utils/defaultConfig'
import { isObject } from '@/utils/isObject'
import { castArray } from '@/utils/castArray'
import clsx from 'clsx'
import { Heading } from '@/components/Heading'
import nameClass from 'tailwindcss/lib/util/nameClass'
let normalizeProperties = function (input) {
if (typeof input !== 'object') return input
if (Array.isArray(input)) return input.map(normalizeProperties)
return Object.keys(input).reduce((newObj, key) => {
let val = input[key]
let newVal = typeof val === 'object' ? normalizeProperties(val) : val
newObj[key.replace(/([a-z])([A-Z])/g, (m, p1, p2) => `${p1}-${p2.toLowerCase()}`)] = newVal
return newObj
}, {})
}
function getUtilities(plugin) {
if (!plugin) return {}
const utilities = {}
function addUtilities(utils) {
utils = Array.isArray(utils) ? utils : [utils]
for (let i = 0; i < utils.length; i++) {
for (let prop in utils[i]) {
utilities[prop] = normalizeProperties(utils[i][prop])
}
}
}
plugin()({
addUtilities,
addBase() {},
matchUtilities: (matches, { values }) => {
let modifierValues = Object.entries(values)
let result = Object.entries(matches).flatMap(([name, utilityFunction]) => {
return modifierValues
.map(([modifier, value]) => {
let declarations = utilityFunction(value, {
includeRules(rules) {
addUtilities(rules)
},
})
if (!declarations) {
return null
}
return {
[nameClass(name, modifier)]: declarations,
}
})
.filter(Boolean)
})
for (let obj of result) {
for (let key in obj) {
let deleteKey = false
for (let subkey in obj[key]) {
if (subkey.includes('&')) {
result.push({
[subkey.replace(/&/g, key)]: obj[key][subkey],
})
deleteKey = true
}
}
if (deleteKey) delete obj[key]
}
}
addUtilities(result)
},
config: () => ({
mode: 'aot',
future: 'all',
}),
theme: (path, defaultValue) => dlv(defaultConfig.theme, path, defaultValue),
variants: () => [],
e: (x) => x.replace(/([:.])/g, '\\$1'),
corePlugins: () => true,
})
return utilities
}
function stringifyProperties(
properties,
{ filter = () => true, transformValue = (x) => x, indent = 0 } = {}
) {
let lines = []
Object.keys(properties).forEach((property) => {
if (isObject(properties[property])) {
lines.push(`${property} {`)
lines.push(
stringifyProperties(properties[property], { filter, transformValue, indent: indent + 1 })
)
lines.push('}')
} else {
castArray(properties[property]).forEach((value, i) => {
if (!filter(property, value, properties)) return
lines.push(`${' '.repeat(indent)}${property}: ${transformValue(value)};`)
})
}
})
return lines.join('\n')
}
export const ClassTable = memo(
({
plugin,
filterProperties,
preview,
sort = (x) => x,
transformSelector = (x) => (x.length === 1 ? x : x.slice(1).replace(/\\/g, '')),
transformProperties = ({ properties }) => properties,
transformValue,
custom,
}) => {
const utilities = {}
castArray(plugin).forEach((p) => {
Object.assign(utilities, getUtilities(p))
})
return (
<div className="relative overflow-hidden border-b border-gray-200">
<Heading level={2} id="class-reference" toc={true} className="relative">
<span className="sr-only">Default class reference</span>
</Heading>
<div
className={clsx(
'scrollbar-w-2 scrollbar-track-gray-lighter scrollbar-thumb-rounded scrollbar-thumb-gray scrolling-touch overflow-y-auto',
{ 'lg:max-h-sm': Object.keys(utilities).length > 12 }
)}
>
{custom || (
<table className="w-full border-collapse text-left">
<thead>
<tr>
<th className="sticky top-0 z-20 bg-white p-0 text-sm font-semibold text-gray-600">
<div className="border-b border-gray-200 pb-2 pr-2">Class</div>
</th>
<th
className={clsx(
'sticky top-0 z-20 bg-white p-0 text-sm font-semibold text-gray-600',
{
'hidden sm:table-cell': preview,
}
)}
>
<div
className={clsx('border-b border-gray-200 pb-2 pl-2', { 'pr-2': preview })}
>
Properties
</div>
</th>
{preview && (
<th className="sticky top-0 z-20 bg-white p-0 text-sm font-semibold text-gray-600">
<div className="border-b border-gray-200 pb-2 pl-2">
<span className="sr-only">Preview</span>
</div>
</th>
)}
</tr>
</thead>
<tbody className="align-baseline">
{sort(Object.keys(utilities)).map((utility, i) => {
let selector = utility
let properties = utilities[selector]
return (
<tr key={utility}>
<td
translate="no"
className={clsx(
'whitespace-nowrap py-2 pr-2 font-mono text-xs text-violet-600',
{
'border-t border-gray-200': i !== 0,
}
)}
>
{transformSelector(selector)}
</td>
<td
translate="no"
className={clsx('whitespace-pre py-2 pl-2 font-mono text-xs text-sky-600', {
'border-t border-gray-200': i !== 0,
'hidden sm:table-cell sm:pr-2': preview,
})}
>
{stringifyProperties(transformProperties({ selector, properties }), {
filter: filterProperties,
transformValue,
})}
</td>
{preview &&
preview(properties, {
className: i === 0 ? '' : 'border-t border-gray-200',
})}
</tr>
)
})}
</tbody>
</table>
)}
</div>
</div>
)
}
)