This commit is contained in:
2026-05-07 10:21:34 +08:00
parent 1980364521
commit eeda1e68e8
4 changed files with 90 additions and 31 deletions

View File

@@ -1,4 +1,4 @@
import './index.css' import "./index.css";
export * from './utils/cpm.ts'; export * from "./utils/cpm.ts";
export * from './utils/cvr.ts'; export * from "./utils/cvr.ts";
export * from './utils/prefix-list.ts'; export * from "./utils/prefix-list.ts";

View File

@@ -1,42 +1,62 @@
import { defaultPrefixList } from "./prefix-list"; import { defaultPrefixList } from "./prefix-list";
function prefixParce(cls: string): { prefix: string; rest: string } { function matchLongestPrefix(cls: string, prefixList: string[]): string {
const index = cls.indexOf("-"); let bestPrefix: string | undefined;
if (index === -1) return { prefix: "", rest: cls };
const prefix = cls.slice(0, index); for (const prefix of prefixList) {
const rest = cls.slice(index + 1); const reg = new RegExp(`^${prefix}(?:-|$)`);
return { prefix, rest }; if (reg.test(cls) && (!bestPrefix || prefix.length > bestPrefix.length)) {
bestPrefix = prefix;
}
} }
return bestPrefix ?? cls;
}
// 重载 1
export function cpm(...classes: Array<string | readonly string[]>): string;
// 重载 2
export function cpm( export function cpm(
options?: { externalPrefixList?: string[] }, options: { extendedPrefixList?: string[] },
...classes: (string | string[])[] ...classes: Array<string | readonly string[]>
): string;
export function cpm(
arg1: { extendedPrefixList?: string[] } | string | readonly string[],
...rest: Array<string | readonly string[]>
): string { ): string {
const map = new Map<string, string>(); const map = new Map<string, string>();
// ✅ 明确拆分 options / classes
let options: { extendedPrefixList?: string[] } = {};
let classes: Array<string | readonly string[]>;
if (typeof arg1 === "object" && !Array.isArray(arg1)) {
options = arg1 as { extendedPrefixList?: string[] };
classes = rest;
} else {
classes = [arg1, ...rest];
}
const mergedPrefixList: string[] = [ const mergedPrefixList: string[] = [
...new Set([...(options?.externalPrefixList ?? []), ...defaultPrefixList]), ...new Set([...(options.extendedPrefixList ?? []), ...defaultPrefixList]),
]; ];
classes.forEach((item) => { classes.forEach((item) => {
if (Array.isArray(item)) { if (Array.isArray(item)) {
item.forEach((i) => { item.forEach((i) => {
const { prefix, rest } = prefixParce(i); const bestPrefix = matchLongestPrefix(i, mergedPrefixList);
if (mergedPrefixList.includes(prefix)) { map.set(bestPrefix, i);
map.set(prefix, rest);
}
}); });
return; return;
} }
if (typeof item === "string") { if (typeof item === "string") {
const { prefix, rest } = prefixParce(item); const bestPrefix = matchLongestPrefix(item, mergedPrefixList);
if (mergedPrefixList.includes(prefix)) { map.set(bestPrefix, item);
map.set(prefix, rest);
}
} }
}); });
return Array.from(map.entries()) return Object.values(map).join(" ");
.map(([prefix, rest]) => `${prefix}-${rest}`)
.join(" ");
} }

View File

@@ -1 +1,46 @@
export function cvr() {} import { cpm } from "./cpm";
type VariantsConfig<V extends Record<string, Record<string, string>>> = {
base?: string;
variants?: V;
compoundVariants?: Array<
Partial<{ [K in keyof V]: keyof V[K] }> & { class: string }
>;
};
type VariantProps<V extends Record<string, Record<string, string>>> = Partial<{
[K in keyof V]: keyof V[K];
}>;
export function cvr<V extends Record<string, Record<string, string>>>(
config: VariantsConfig<V>,
) {
return function (props: VariantProps<V> = {}): string {
const classes: string[] = [];
if (config.base) classes.push(config.base);
if (config.variants) {
for (const [key, map] of Object.entries(config.variants) as any) {
const value = props[key];
if (value && map[value]) {
classes.push(map[value]);
}
}
}
if (config.compoundVariants) {
for (const cv of config.compoundVariants) {
const match = Object.entries(cv)
.filter(([k]) => k !== "class")
.every(([k, v]) => props[k] === v);
if (match && cv.class) {
classes.push(cv.class);
}
}
}
return cpm(...classes);
};
}

View File

@@ -36,9 +36,3 @@ export const defaultPrefixList = [
"drop-shadow", "drop-shadow",
"select", "select",
]; ];
// map 里面 key 是 prefixvalue 是完整 class。
// prefix 用正则来获取,先检测是否包含前缀,再检测这个前缀是否在开头,再检测前缀后面是否跟着“-”(就怕匹配到首字母)
// 用正则套壳花括号注入前缀。正则要求1在开头在尾部跟着“-”或者“没有其他字符”
// const reg = new RegExp(`^${prefix}(?:-|$)`);
// class如果一次匹配2个prefix那么就采用较长的那个prefix因为这种情况肯定是“较短子字符串”的副作用