Skip to content

pantoken / packages/utils/src / makeResolver

Función: makeResolver()

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

Beta

Construir un resolvedor que expanda referencias var(--x) a valores hoja concretos contra base (más cualquier overrides). Con mode colapsa light-dark() a esa rama; sin él, deja light-dark() en su lugar.

Parámetros

base

readonly Token[]

El conjunto de tokens con el que resolver las referencias.

options?

ResolveOptions = {}

ResolveOptions.

Devuelve

Una función que resuelve una cadena de valor.

(value) => string

Ejemplos

Expandir una cadena de referencias hasta su hoja concreta

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"

Colapsar light-dark() con un modo, o mantenerlo sin uno

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"

Aplicar anulaciones por capas que prevalecen en colisiones de nombres

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"