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