Skip to content

pantoken / packages/plugin-kit/src / makeResolver

Funkcja: makeResolver()

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

Beta

Zbuduj resolver, który rozwija odwołania var(--x) do konkretnych wartości liści względem base (oraz wszystkich overrides). Z mode redukuje light-dark() do tej gałęzi; bez niego pozostawia light-dark() na miejscu.

Parametry

base

tylko do odczytu Token[]

Zestaw tokenów, względem którego rozwiązywane są odwołania.

options?

ResolveOptions

ResolveOptions.

Zwraca

Funkcja, która rozwiązuje ciąg wartości.

(value) => string

Przykłady

Rozwiń łańcuch odwołań do jego konkretnego liścia

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"

Zredukuj light-dark() z podanym trybem albo zachowaj go bez trybu

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"

Nakładaj nadpisania, które wygrywają przy kolizjach nazw

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"