Repository navigation
Expand file tree
/
Copy pathClassTable.js
More file actions
167 lines (161 loc) · 5.73 KB
/
Copy pathClassTable.js
File metadata and controls
167 lines (161 loc) · 5.73 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
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'
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 = {}
plugin()({
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])
}
}
},
config: () => ({
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="border-b border-gray-200 overflow-hidden relative">
<Heading level={2} id="class-reference" toc={true} className="relative">
<span className="sr-only">Default class reference</span>
</Heading>
<div
className={clsx(
'overflow-y-auto scrollbar-w-2 scrollbar-track-gray-lighter scrollbar-thumb-rounded scrollbar-thumb-gray scrolling-touch',
{ 'lg:max-h-sm': Object.keys(utilities).length > 12 }
)}
>
{custom || (
<table className="w-full text-left border-collapse">
<thead>
<tr>
<th className="z-20 sticky top-0 text-sm font-semibold text-gray-600 bg-white p-0">
<div className="pb-2 pr-2 border-b border-gray-200">Class</div>
</th>
<th
className={clsx(
'z-20 sticky top-0 text-sm font-semibold text-gray-600 bg-white p-0',
{
'hidden sm:table-cell': preview,
}
)}
>
<div
className={clsx('pb-2 pl-2 border-b border-gray-200', { 'pr-2': preview })}
>
Properties
</div>
</th>
{preview && (
<th className="z-20 sticky top-0 text-sm font-semibold text-gray-600 bg-white p-0">
<div className="pb-2 pl-2 border-b border-gray-200">
<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
className={clsx(
'py-2 pr-2 font-mono text-xs text-violet-600 whitespace-nowrap',
{
'border-t border-gray-200': i !== 0,
}
)}
>
{transformSelector(selector)}
</td>
<td
className={clsx(
'py-2 pl-2 font-mono text-xs text-light-blue-600 whitespace-pre',
{
'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>
)
}
)