pantoken / packages/utils/src / makeResolver
Function: makeResolver()
makeResolver(
base,options?): (value) =>string
Beta
Build a resolver that expands var(--x) references to concrete leaf values against base (plus any overrides). With mode it collapses light-dark() to that branch; without, it leaves light-dark() in place.
Parameters
base
readonly Token[]
The token set to resolve references against.
options?
ResolveOptions = {}
Returns
A function that resolves a value string.
(value) => string
Examples
Expand a reference chain to its concrete leaf
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"Collapse light-dark() with a mode, or keep it without one
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"Layer overrides that win on name collisions
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"