Loading...
Loading...
Pre-built custom directives for json-render — formatting, math, string manipulation, and i18n. Use when working with @json-render/directives, defining custom directives with defineDirective, or adding $format, $math, $concat, $count, $truncate, $pluralize, $join, or $t to specs.
npx skill4agent add vercel-labs/json-render directives@json-render/coreimport { standardDirectives } from '@json-render/directives';
// Wire into prompt generation
const prompt = catalog.prompt({ directives: standardDirectives });
// Wire into the renderer (React example)
import { JSONUIProvider, Renderer } from '@json-render/react';
<JSONUIProvider registry={registry} directives={standardDirectives}>
<Renderer spec={spec} registry={registry} />
</JSONUIProvider>createI18nDirectiveimport { standardDirectives, createI18nDirective } from '@json-render/directives';
const directives = [...standardDirectives, createI18nDirective(config)];defineDirective@json-render/coreimport { defineDirective, resolvePropValue } from '@json-render/core';
import { z } from 'zod';
const doubleDirective = defineDirective({
name: '$double',
description: 'Double a numeric value.',
schema: z.object({
$double: z.unknown(),
}),
resolve(value, ctx) {
const resolved = resolvePropValue(value.$double, ctx);
return (resolved as number) * 2;
},
});$$state$cond$computed$template$item$index$bindState$bindItemresolvePropValue$formatIntldatecurrencynumberpercent{ "$format": "currency", "value": { "$state": "/cart/total" }, "currency": "USD" }
{ "$format": "date", "value": { "$state": "/user/createdAt" } }
{ "$format": "number", "value": 1234567, "notation": "compact" }
{ "$format": "percent", "value": 0.75 }
{ "$format": "date", "value": { "$state": "/post/createdAt" }, "style": "relative" }$formatvaluelocale?currency?notation?style?options?$math{ "$math": "add", "a": { "$state": "/subtotal" }, "b": { "$state": "/tax" } }
{ "$math": "round", "a": 3.7 }addsubtractmultiplydividemodminmaxroundfloorceilabsroundfloorceilabsa0$matha?b?$concat{ "$concat": [{ "$state": "/user/firstName" }, " ", { "$state": "/user/lastName" }] }$concat$count{ "$count": { "$state": "/cart/items" } }.length0$count$truncate{ "$truncate": { "$state": "/post/body" }, "length": 140, "suffix": "..." }$truncatelength?suffix?$pluralize{ "$pluralize": { "$state": "/cart/itemCount" }, "one": "item", "other": "items", "zero": "no items" }"3 items""1 item""no items"$pluralizeoneotherzero?$join{ "$join": { "$state": "/tags" }, "separator": ", " }$joinseparator?createI18nDirectiveimport { createI18nDirective } from '@json-render/directives';
const tDirective = createI18nDirective({
locale: 'en',
messages: {
en: { "greeting": "Hello, {{name}}!", "checkout.submit": "Place Order" },
es: { "greeting": "Hola, {{name}}!", "checkout.submit": "Realizar Pedido" },
},
fallbackLocale: 'en',
});{ "$t": "checkout.submit" }
{ "$t": "greeting", "params": { "name": { "$state": "/user/name" } } }$tparams?localemessagesfallbackLocale?resolvePropValue{
"$format": "currency",
"value": { "$math": "multiply", "a": { "$state": "/price" }, "b": { "$state": "/qty" } },
"currency": "USD"
}$state$math$formatdirectivescreateRenderer// Provider pattern
<JSONUIProvider registry={registry} directives={directives}>
<Renderer spec={spec} registry={registry} />
</JSONUIProvider>
// createRenderer pattern
const MyRenderer = createRenderer(catalog, components);
<MyRenderer spec={spec} directives={directives} />const prompt = catalog.prompt({ directives });| Export | Purpose |
|---|---|
| |
| |
| |
| |
| |
| |
| |
| Factory for |
| Array of all 7 non-factory directives |
| Type for i18n configuration |