Skip to content

pantoken / packages/utils/src / makeResolver

Hàm: makeResolver()

makeResolver(base, options?): (value) => string

Beta

Xây dựng một bộ phân giải mở rộng các tham chiếu var(--x) thành giá trị lá cụ thể dựa trên base (cộng với bất kỳ overrides nào). Với mode nó thu gọn light-dark() về nhánh đó; nếu không, nó để light-dark() nguyên vị trí.

Tham số

base

chỉ đọc Token[]

Tập token để dùng khi giải các tham chiếu.

options?

ResolveOptions = {}

ResolveOptions.

Trả về

Một hàm giải chuỗi giá trị.

(value) => string

Các ví dụ

Mở rộng chuỗi tham chiếu tới lá cụ thể của nó

ts
import { makeResolver } from "@pantoken/utils";
import type { Token } from "@pantoken/model";

const ir: Token[] = [
  { name: "--instui-leaf", syntax: "<color>", inherits: true, value: "#0374B5" },
  { name: "--instui-brand", syntax: "*", inherits: true, value: "var(--instui-leaf)" },
];

const resolve = makeResolver(ir);
resolve("var(--instui-brand)"); // → "#0374B5"

Thu gọn light-dark() bằng một chế độ, hoặc giữ nguyên nếu không có

ts
import { makeResolver } from "@pantoken/utils";
import type { Token } from "@pantoken/model";

const ir: Token[] = [
  { name: "--instui-bg", syntax: "*", inherits: true, value: "light-dark(#fff, #000)" },
];

makeResolver(ir)("var(--instui-bg)");                 // → "light-dark(#fff, #000)"
makeResolver(ir, { mode: "light" })("var(--instui-bg)"); // → "#fff"
makeResolver(ir, { mode: "dark" })("var(--instui-bg)");  // → "#000"

Ghi đè lớp ưu tiên khi trùng tên

ts
import { makeResolver } from "@pantoken/utils";
import type { Token } from "@pantoken/model";

const ir: Token[] = [
  { name: "--instui-leaf", syntax: "<color>", inherits: true, value: "#0374B5" },
  { name: "--instui-brand", syntax: "*", inherits: true, value: "var(--instui-leaf)" },
];
const overrides: Token[] = [
  { name: "--instui-leaf", syntax: "<color>", inherits: true, value: "#000" },
];

makeResolver(ir, { overrides })("var(--instui-brand)"); // → "#000"