Skip to content

pantoken / packages/utils/src / makeResolver

פונקציה: makeResolver()

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

בטא

בנה פותר (resolver) שמרחיב הפניות var(--x) לערכי עלים קונקרטיים על פי base (ולצד זאת כל overrides). עם mode הוא מצמצם את light-dark() לענף ההוא; בלעדיו הוא משאיר light-dark() כפי שהיא.

פרמטרים

base

readonly Token[]

מערך הטוקנים מולו מפוענחות ההפניות.

options?

ResolveOptions = {}

ResolveOptions.

מחזיר

פונקציה שמפענחת מחרוזת ערך.

(value) => string

דוגמאות

הרחב שרשרת הפניות לערך עלה קונקרטי

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"

כווץ light-dark() עם מצב, או השאר אותה ללא מצב

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"

שכבת עקיפות שמנצחת בהתנגשות שמות

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"