File size: 5,765 Bytes
1e92f2d |
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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
import { type Dict, isFunction, isString, memo } from "../utils"
import { colorMix } from "./color-mix"
import { mapToJson } from "./map-to-json"
import type {
TokenDictionary,
Utility,
UtilityConfig,
UtilityPropertyConfig,
} from "./types"
interface Options {
tokens: TokenDictionary
config: UtilityConfig
}
function normalize(config: UtilityPropertyConfig | undefined) {
return config
}
function normalizeConfig(config: UtilityConfig) {
return Object.fromEntries(
Object.entries(config).map(([property, propertyConfig]) => {
return [property, normalize(propertyConfig)]
}),
)
}
export function createUtility(options: Options) {
const configs = normalizeConfig(options.config)
const tokens = options.tokens
const shorthands = new Map<string, string>()
const propValues = new Map<string, Dict>()
function register(property: string, config: UtilityPropertyConfig) {
configs[property] = normalize(config)
assignProperty(property, config)
}
const assignProperty = (property: string, config: UtilityPropertyConfig) => {
const values = getPropertyValues(config)
if (!values) return
propValues.set(property, values)
assignPropertyType(property, config)
}
const assignProperties = () => {
for (const [prop, config] of Object.entries(configs)) {
if (!config) continue
assignProperty(prop, config)
}
}
const assignShorthands = () => {
for (const [property, config] of Object.entries(configs)) {
const { shorthand } = config ?? {}
if (!shorthand) continue
const values = Array.isArray(shorthand) ? shorthand : [shorthand]
values.forEach((name) => shorthands.set(name, property))
}
}
const assignColorPaletteProperty = () => {
const values = mapToJson(tokens.colorPaletteMap) as Record<string, any>
register("colorPalette", {
values: Object.keys(values),
transform: memo((value) => values[value]),
})
}
const propTypes = new Map<string, Set<string>>()
const assignPropertyType = (
property: string,
config: UtilityPropertyConfig | undefined,
) => {
if (!config) return
const values = getPropertyValues(config, (key) => `type:Tokens["${key}"]`)
if (typeof values === "object" && values.type) {
propTypes.set(property, new Set([`type:${values.type}`]))
return
}
if (values) {
const keys = new Set(Object.keys(values))
propTypes.set(property, keys)
}
const set = propTypes.get(property) ?? new Set()
if (config.property) {
propTypes.set(property, set.add(`CssProperties["${config.property}"]`))
}
}
const assignPropertyTypes = () => {
for (const [property, propertyConfig] of Object.entries(configs)) {
if (!propertyConfig) continue
assignPropertyType(property, propertyConfig)
}
}
const addPropertyType = (property: string, type: string[]) => {
const set = propTypes.get(property) ?? new Set()
propTypes.set(property, new Set([...set, ...type]))
}
const getTypes = () => {
const map = new Map<string, string[]>()
for (const [prop, values] of propTypes.entries()) {
// When tokens does not exist in the config
if (values.size === 0) {
map.set(prop, ["string"])
continue
}
const typeValues = Array.from(values).map((key) => {
if (key.startsWith("CssProperties")) return key
if (key.startsWith("type:")) return key.replace("type:", "")
return JSON.stringify(key)
})
map.set(prop, typeValues)
}
return map
}
const getPropertyValues = (
config: UtilityPropertyConfig,
resolveFn?: (key: string) => string,
) => {
const { values } = config
const fn = (key: string) => {
const value = resolveFn?.(key)
return value ? { [value]: value } : undefined
}
if (isString(values)) {
return fn?.(values) ?? tokens.getCategoryValues(values) ?? {}
}
if (Array.isArray(values)) {
return values.reduce<Dict<string>>((result, value) => {
result[value] = value
return result
}, {})
}
if (isFunction(values)) {
return values(resolveFn ? fn : tokens.getCategoryValues)
}
return values
}
const defaultTransform = memo((prop: string, value: string) => {
return {
[prop]: prop.startsWith("--") ? tokens.getVar(value, value) : value,
}
})
const tokenFn = Object.assign(tokens.getVar, {
raw: (path: string) => tokens.getByName(path),
})
const transform = memo((prop: string, raw: any) => {
const key = resolveShorthand(prop)
// skip emotion generated keyframes
if (isString(raw) && !raw.includes("_EMO_")) {
raw = tokens.expandReferenceInValue(raw)
}
const config = configs[key]
if (!config) {
return defaultTransform(key, raw)
}
const value = propValues.get(key)?.[raw]
if (!config.transform) {
return defaultTransform(prop, value ?? raw)
}
const _colorMix = (value: string) => colorMix(value, tokenFn)
return config.transform(value ?? raw, {
raw,
token: tokenFn,
utils: { colorMix: _colorMix },
})
})
function build() {
assignShorthands()
assignColorPaletteProperty()
assignProperties()
assignPropertyTypes()
}
build()
const hasShorthand = shorthands.size > 0
const resolveShorthand = memo((prop: string) => {
return shorthands.get(prop) ?? prop
})
const keys = () => {
return [...Array.from(shorthands.keys()), ...Object.keys(configs)]
}
const instance: Utility = {
keys,
hasShorthand,
transform,
shorthands,
resolveShorthand,
register,
getTypes,
addPropertyType,
}
return instance
}
|