reifyui 0.13.12 → 0.13.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/chart.cjs CHANGED
@@ -31,13 +31,77 @@ var chart_exports = {};
31
31
  __export(chart_exports, {
32
32
  Chart: () => Chart,
33
33
  chartTokens: () => chartTokens,
34
+ safeChartOption: () => safeChartOption,
35
+ safeHref: () => safeHref,
34
36
  useChartTokens: () => useChartTokens
35
37
  });
36
38
  module.exports = __toCommonJS(chart_exports);
37
39
 
38
40
  // src/chart/Chart.jsx
39
41
  var import_react = require("react");
42
+
43
+ // src/chart/safe.js
44
+ var SAFE_HREF = /^(https?:\/\/|mailto:)/i;
45
+ function safeHref(v) {
46
+ return typeof v === "string" && SAFE_HREF.test(v.replace(/[\u0000- \u007f]/g, "")) ? v : "";
47
+ }
48
+ var ESC = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
49
+ var esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
50
+ var isPlain = (v) => {
51
+ if (!v || typeof v !== "object" || Array.isArray(v)) return false;
52
+ const p = Object.getPrototypeOf(v);
53
+ return p === Object.prototype || p === null;
54
+ };
55
+ function safeChartOption(option) {
56
+ if (!isPlain(option) && !Array.isArray(option)) return option;
57
+ const seen = /* @__PURE__ */ new Map();
58
+ const escaped = /* @__PURE__ */ new Set();
59
+ const walk = (n) => {
60
+ if (Array.isArray(n)) {
61
+ if (seen.has(n)) return seen.get(n);
62
+ const out2 = [];
63
+ seen.set(n, out2);
64
+ for (const x of n) out2.push(walk(x));
65
+ return out2;
66
+ }
67
+ if (!isPlain(n)) return n;
68
+ if (seen.has(n)) return seen.get(n);
69
+ const out = {};
70
+ seen.set(n, out);
71
+ for (const k of Object.keys(n)) {
72
+ const v = n[k];
73
+ if ((k === "link" || k === "sublink") && typeof v === "string") {
74
+ if (safeHref(v)) out[k] = v;
75
+ } else if (k === "tooltip") {
76
+ out[k] = tooltip(v);
77
+ } else if (k === "dataView" && isPlain(v)) {
78
+ out[k] = dataView(walk(v));
79
+ } else {
80
+ out[k] = walk(v);
81
+ }
82
+ }
83
+ return out;
84
+ };
85
+ const tooltip = (v) => {
86
+ if (v === true) return { renderMode: "richText" };
87
+ if (Array.isArray(v)) return v.map((t) => isPlain(t) ? { ...walk(t), renderMode: "richText" } : walk(t));
88
+ return isPlain(v) ? { ...walk(v), renderMode: "richText" } : walk(v);
89
+ };
90
+ const dataView = (d) => {
91
+ if (escaped.has(d)) return d;
92
+ escaped.add(d);
93
+ if (typeof d.title === "string") d.title = esc(d.title);
94
+ if (Array.isArray(d.lang)) d.lang = d.lang.map((s) => typeof s === "string" ? esc(s) : s);
95
+ return d;
96
+ };
97
+ return walk(option);
98
+ }
99
+
100
+ // src/chart/Chart.jsx
40
101
  var import_jsx_runtime = require("react/jsx-runtime");
102
+ function applyOption(chart, option, notMerge) {
103
+ chart.setOption(safeChartOption(option), notMerge);
104
+ }
41
105
  function Chart(props) {
42
106
  const { option, renderer = "canvas", notMerge = true, onInit, label, fallback = null, className = "" } = props;
43
107
  const host = (0, import_react.useRef)(null);
@@ -57,7 +121,7 @@ function Chart(props) {
57
121
  inst.current = chart;
58
122
  const { option: opt, notMerge: nm, onInit: cb } = latest.current;
59
123
  if (opt) {
60
- chart.setOption(opt, nm);
124
+ applyOption(chart, opt, nm);
61
125
  applied.current = opt;
62
126
  }
63
127
  if (cb) cb(chart);
@@ -77,7 +141,7 @@ function Chart(props) {
77
141
  }, [renderer]);
78
142
  (0, import_react.useEffect)(() => {
79
143
  if (!inst.current || !option || applied.current === option) return;
80
- inst.current.setOption(option, notMerge);
144
+ applyOption(inst.current, option, notMerge);
81
145
  applied.current = option;
82
146
  }, [option, notMerge]);
83
147
  (0, import_react.useEffect)(() => {
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/chart/index.js","../src/chart/Chart.jsx","../src/chart/tokens.js","../src/chart/useChartTokens.js"],"sourcesContent":["// The chart entry — IMPORT VIA SUBPATH ONLY ('reifyui/chart'), never from the root index.\n//\n// echarts is an optional peer and loads lazily inside Chart. A root re-export would put it in the\n// dependency graph of every consumer, so an app that renders a Button and no chart would have to\n// resolve a charting library to build. Same rule, same reason, as 'reifyui/slides'.\nexport { Chart } from './Chart.jsx';\n// The theme as strings a chart option can hold — series colours, axis ink, tooltip chrome. CSS\n// variables do not reach inside a canvas; these do.\nexport { chartTokens } from './tokens.js';\nexport { useChartTokens } from './useChartTokens.js';\n","// Chart — an ECharts option, drawn. That is the whole component.\n//\n// It deliberately knows nothing about series, axes or data shape: the option IS the contract, so\n// anything ECharts can draw, this draws, and the thing that decides what a chart should look like\n// (a person, or an agent writing the option) is not limited by a props table. What it does own is\n// the part every hand-rolled wrapper gets wrong: creating the instance once, disposing it on\n// unmount, and resizing it when its box changes — a chart inside a resizable panel that never\n// hears about the resize is the most common broken dashboard there is.\n//\n// echarts is an OPTIONAL peer, loaded lazily here, which is why this lives at `reifyui/chart` and\n// not in the root index — the same reason `reifyui/slides` is its own entry. An app that imports\n// a Button must not resolve a charting library.\nimport { useEffect, useRef, useState } from 'react';\n\n/**\n * option the ECharts option. Applied whenever the REFERENCE changes, so build it in a\n * useMemo — a fresh object every render redraws the chart every render\n * renderer 'canvas' (default) or 'svg'; svg stays crisp when the page is printed or scaled\n * notMerge replace the previous option rather than merging into it (default true: a redraw\n * after new data should not inherit last query's series)\n * onInit the instance, once, for the things only the instance can do (export an image)\n * label the chart's accessible name; a canvas has no text for a screen reader to find\n * fallback drawn instead when the chart code cannot be loaded at all\n */\nexport function Chart(props) {\n const { option, renderer = 'canvas', notMerge = true, onInit, label, fallback = null, className = '' } = props;\n const host = useRef(null);\n const inst = useRef(null);\n const applied = useRef(null); // the option the instance is currently showing\n const [failed, setFailed] = useState(false);\n // Read through refs inside the init effect so a new option or callback does not tear the\n // instance down and rebuild it.\n const latest = useRef({ option, notMerge, onInit });\n latest.current = { option, notMerge, onInit };\n\n useEffect(() => {\n let dead = false;\n (async () => {\n try {\n const mod = await import('echarts');\n const echarts = typeof mod.init === 'function' ? mod : (mod.default || mod);\n if (dead || !host.current) return;\n const chart = echarts.init(host.current, null, { renderer });\n inst.current = chart;\n const { option: opt, notMerge: nm, onInit: cb } = latest.current;\n if (opt) { chart.setOption(opt, nm); applied.current = opt; }\n if (cb) cb(chart);\n } catch {\n if (!dead) setFailed(true); // no chart code: say so, never draw a fake one\n }\n })();\n return () => {\n dead = true;\n try { inst.current?.dispose(); } catch { /* already gone with the DOM */ }\n inst.current = null;\n applied.current = null;\n };\n }, [renderer]);\n\n // `applied` keeps this from redrawing the option the init above just drew — the instance\n // arrives asynchronously, so both paths run on the first render that has one.\n useEffect(() => {\n if (!inst.current || !option || applied.current === option) return;\n inst.current.setOption(option, notMerge);\n applied.current = option;\n }, [option, notMerge]);\n\n useEffect(() => {\n const el = host.current;\n if (!el || typeof ResizeObserver !== 'function') return undefined;\n let frame = 0;\n const ro = new ResizeObserver(() => {\n // One resize per frame: a drag on a panel corner fires the observer on every pointer move.\n cancelAnimationFrame(frame);\n frame = requestAnimationFrame(() => inst.current?.resize());\n });\n ro.observe(el);\n return () => { cancelAnimationFrame(frame); ro.disconnect(); };\n }, []);\n\n if (failed) return <div className={['uic-chart', 'is-unavailable', className].filter(Boolean).join(' ')}>{fallback}</div>;\n return (\n <div\n ref={host}\n className={['uic-chart', className].filter(Boolean).join(' ')}\n role={label ? 'img' : undefined}\n aria-label={label}\n />\n );\n}\n","// The theme, as values a chart library can use.\n//\n// A chart draws into a canvas, where CSS custom properties do not reach: series colours, axis\n// labels and tooltip chrome all arrive as strings in an option object. Without this, every app\n// hardcodes hex in its chart code and the charts are the one part of the product that does not\n// follow the theme. Reading the same --uic-* tokens the stylesheets read keeps them together.\n\n// Same policy as the `var(--uic-x, …)` fallbacks in the stylesheets: a host that never loaded a\n// token layer still gets the library's own light palette rather than whatever the chart library\n// defaults to. Values are themes/light.css verbatim.\nconst FALLBACK = {\n cat: ['#2a78d6', '#eb6834', '#1baf7a', '#eda100', '#e87ba4', '#008300', '#4a3aa7', '#e34948'],\n ink: '#111317', ink2: '#3A3D45', mute: '#5B5D66',\n line: '#DDDFE6', lineSoft: '#EFF1F4', surface: '#FFFFFF',\n brand: '#285AFF', ok: '#16a34a', warn: '#F59E0B', bad: '#b91c1c',\n font: 'inherit',\n};\n\nfunction read(style, name, fallback) {\n const v = style.getPropertyValue(name).trim();\n return v || fallback;\n}\n\n/**\n * Resolve the chart tokens in effect for `el` — pass the element the chart lives in, so a panel\n * inside a dark region resolves the dark values even when the page is light.\n *\n * `palette` is a categorical scale: assign it IN ORDER, one colour per series, and never cycle\n * it. Past eight series, colour has stopped being an encoding — fold the tail into one \"other\"\n * series or split the chart. On a light surface three of the eight sit below 3:1 against white,\n * so identity must not rest on colour alone: keep the legend, or label the series directly.\n */\nexport function chartTokens(el) {\n const target = (el && el.nodeType === 1 ? el : null)\n || (typeof document !== 'undefined' ? document.documentElement : null);\n if (!target || typeof getComputedStyle !== 'function') return { ...FALLBACK, palette: FALLBACK.cat };\n const s = getComputedStyle(target);\n return {\n palette: FALLBACK.cat.map((hex, i) => read(s, `--uic-cat-${i + 1}`, hex)),\n ink: read(s, '--uic-ink', FALLBACK.ink),\n ink2: read(s, '--uic-ink-2', FALLBACK.ink2),\n mute: read(s, '--uic-mute', FALLBACK.mute),\n line: read(s, '--uic-line', FALLBACK.line),\n lineSoft: read(s, '--uic-line-soft', FALLBACK.lineSoft),\n surface: read(s, '--uic-surface', FALLBACK.surface),\n brand: read(s, '--uic-brand', FALLBACK.brand),\n ok: read(s, '--uic-ok', FALLBACK.ok),\n warn: read(s, '--uic-warn', FALLBACK.warn),\n bad: read(s, '--uic-fail-ink', FALLBACK.bad),\n font: read(s, '--uic-font', FALLBACK.font),\n };\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport { chartTokens } from './tokens.js';\n\n/**\n * chartTokens(), re-read when the theme changes.\n *\n * A chart's colours are baked into its option at build time, so flipping light↔dark leaves every\n * chart on the page painted for the theme that is gone until something re-renders it. There is no\n * event for \"the tokens changed\", so this watches the two things that change them: the attributes\n * a theme is switched with (a class or data-* on <html>/<body>), and the OS preference.\n *\n * Returns [tokens, ref] — put the ref on the element the chart lives in, or leave it unused and\n * the document's own tokens are read.\n */\nexport function useChartTokens() {\n const ref = useRef(null);\n const [tokens, setTokens] = useState(() => chartTokens(null));\n const reread = useCallback(() => setTokens(chartTokens(ref.current)), []);\n\n useEffect(() => {\n reread();\n if (typeof MutationObserver !== 'function') return undefined;\n const obs = new MutationObserver(reread);\n const opts = { attributes: true, attributeFilter: ['class', 'style', 'data-theme'] };\n obs.observe(document.documentElement, opts);\n if (document.body) obs.observe(document.body, opts);\n const mq = typeof matchMedia === 'function' ? matchMedia('(prefers-color-scheme: dark)') : null;\n mq?.addEventListener?.('change', reread);\n return () => { obs.disconnect(); mq?.removeEventListener?.('change', reread); };\n }, [reread]);\n\n return [tokens, ref];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYA,mBAA4C;AAoEvB;AAxDd,SAAS,MAAM,OAAO;AAC3B,QAAM,EAAE,QAAQ,WAAW,UAAU,WAAW,MAAM,QAAQ,OAAO,WAAW,MAAM,YAAY,GAAG,IAAI;AACzG,QAAM,WAAO,qBAAO,IAAI;AACxB,QAAM,WAAO,qBAAO,IAAI;AACxB,QAAM,cAAU,qBAAO,IAAI;AAC3B,QAAM,CAAC,QAAQ,SAAS,QAAI,uBAAS,KAAK;AAG1C,QAAM,aAAS,qBAAO,EAAE,QAAQ,UAAU,OAAO,CAAC;AAClD,SAAO,UAAU,EAAE,QAAQ,UAAU,OAAO;AAE5C,8BAAU,MAAM;AACd,QAAI,OAAO;AACX,KAAC,YAAY;AACX,UAAI;AACF,cAAM,MAAM,MAAM,OAAO,SAAS;AAClC,cAAM,UAAU,OAAO,IAAI,SAAS,aAAa,MAAO,IAAI,WAAW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BAAiB,SAAS,OAAO;AAAA,IAClD,OAAO,KAAK,GAAG,eAAe,SAAS,KAAK;AAAA,IAC5C,IAAI,KAAK,GAAG,YAAY,SAAS,EAAE;AAAA,IACnC,MAAM,KAAK,GAAG,cAAc,SAAS,IAAI;AAAA,IACzC,KAAK,KAAK,GAAG,kBAAkB,SAAS,GAAG;AAAA,IAC3C,MAAM,KAAK,GAAG,cAAc,SAAS,IAAI;AAAA,EAC3C;AACF;;;ACnDA,IAAAA,gBAAyD;AAclD,SAAS,iBAAiB;AAC/B,QAAM,UAAM,sBAAO,IAAI;AACvB,QAAM,CAAC,QAAQ,SAAS,QAAI,wBAAS,MAAM,YAAY,IAAI,CAAC;AAC5D,QAAM,aAAS,2BAAY,MAAM,UAAU,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC;AAExE,+BAAU,MAAM;AACd,WAAO;AACP,QAAI,OAAO,qBAAqB,WAAY,QAAO;AACnD,UAAM,MAAM,IAAI,iBAAiB,MAAM;AACvC,UAAM,OAAO,EAAE,YAAY,MAAM,iBAAiB,CAAC,SAAS,SAAS,YAAY,EAAE;AACnF,QAAI,QAAQ,SAAS,iBAAiB,IAAI;AAC1C,QAAI,SAAS,KAAM,KAAI,QAAQ,SAAS,MAAM,IAAI;AAClD,UAAM,KAAK,OAAO,eAAe,aAAa,WAAW,8BAA8B,IAAI;AAC3F,QAAI,mBAAmB,UAAU,MAAM;AACvC,WAAO,MAAM;AAAE,UAAI,WAAW;AAAG,UAAI,sBAAsB,UAAU,MAAM;AAAA,IAAG;AAAA,EAChF,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO,CAAC,QAAQ,GAAG;AACrB;","names":["import_react"]}
1
+ {"version":3,"sources":["../src/chart/index.js","../src/chart/Chart.jsx","../src/chart/safe.js","../src/chart/tokens.js","../src/chart/useChartTokens.js"],"sourcesContent":["// The chart entry — IMPORT VIA SUBPATH ONLY ('reifyui/chart'), never from the root index.\n//\n// echarts is an optional peer and loads lazily inside Chart. A root re-export would put it in the\n// dependency graph of every consumer, so an app that renders a Button and no chart would have to\n// resolve a charting library to build. Same rule, same reason, as 'reifyui/slides'.\nexport { Chart } from './Chart.jsx';\n// What the chart does to every option before ECharts sees it, for a host that sets options on the instance itself.\nexport { safeChartOption, safeHref } from './safe.js';\n// The theme as strings a chart option can hold — series colours, axis ink, tooltip chrome. CSS\n// variables do not reach inside a canvas; these do.\nexport { chartTokens } from './tokens.js';\nexport { useChartTokens } from './useChartTokens.js';\n","// Chart — an ECharts option, drawn. That is the whole component.\n//\n// It deliberately knows nothing about series, axes or data shape: the option IS the contract, so\n// anything ECharts can draw, this draws, and the thing that decides what a chart should look like\n// (a person, or an agent writing the option) is not limited by a props table. What it does own is\n// the part every hand-rolled wrapper gets wrong: creating the instance once, disposing it on\n// unmount, and resizing it when its box changes — a chart inside a resizable panel that never\n// hears about the resize is the most common broken dashboard there is.\n//\n// echarts is an OPTIONAL peer, loaded lazily here, which is why this lives at `reifyui/chart` and\n// not in the root index — the same reason `reifyui/slides` is its own entry. An app that imports\n// a Button must not resolve a charting library.\nimport { useEffect, useRef, useState } from 'react';\nimport { safeChartOption } from './safe.js';\n\n// Every option reaches ECharts through here, made inert first (safe.js): a tooltip draws text, a link is http(s) or\n// mailto, the data view's words are escaped. An option a host sets on the instance it got from onInit is its own.\nexport function applyOption(chart, option, notMerge) {\n chart.setOption(safeChartOption(option), notMerge);\n}\n\n/**\n * option the ECharts option. Applied whenever the REFERENCE changes, so build it in a\n * useMemo — a fresh object every render redraws the chart every render\n * renderer 'canvas' (default) or 'svg'; svg stays crisp when the page is printed or scaled\n * notMerge replace the previous option rather than merging into it (default true: a redraw\n * after new data should not inherit last query's series)\n * onInit the instance, once, for the things only the instance can do (export an image)\n * label the chart's accessible name; a canvas has no text for a screen reader to find\n * fallback drawn instead when the chart code cannot be loaded at all\n */\nexport function Chart(props) {\n const { option, renderer = 'canvas', notMerge = true, onInit, label, fallback = null, className = '' } = props;\n const host = useRef(null);\n const inst = useRef(null);\n const applied = useRef(null); // the option the instance is currently showing\n const [failed, setFailed] = useState(false);\n // Read through refs inside the init effect so a new option or callback does not tear the\n // instance down and rebuild it.\n const latest = useRef({ option, notMerge, onInit });\n latest.current = { option, notMerge, onInit };\n\n useEffect(() => {\n let dead = false;\n (async () => {\n try {\n const mod = await import('echarts');\n const echarts = typeof mod.init === 'function' ? mod : (mod.default || mod);\n if (dead || !host.current) return;\n const chart = echarts.init(host.current, null, { renderer });\n inst.current = chart;\n const { option: opt, notMerge: nm, onInit: cb } = latest.current;\n if (opt) { applyOption(chart, opt, nm); applied.current = opt; }\n if (cb) cb(chart);\n } catch {\n if (!dead) setFailed(true); // no chart code: say so, never draw a fake one\n }\n })();\n return () => {\n dead = true;\n try { inst.current?.dispose(); } catch { /* already gone with the DOM */ }\n inst.current = null;\n applied.current = null;\n };\n }, [renderer]);\n\n // `applied` keeps this from redrawing the option the init above just drew — the instance\n // arrives asynchronously, so both paths run on the first render that has one.\n useEffect(() => {\n if (!inst.current || !option || applied.current === option) return;\n applyOption(inst.current, option, notMerge);\n applied.current = option;\n }, [option, notMerge]);\n\n useEffect(() => {\n const el = host.current;\n if (!el || typeof ResizeObserver !== 'function') return undefined;\n let frame = 0;\n const ro = new ResizeObserver(() => {\n // One resize per frame: a drag on a panel corner fires the observer on every pointer move.\n cancelAnimationFrame(frame);\n frame = requestAnimationFrame(() => inst.current?.resize());\n });\n ro.observe(el);\n return () => { cancelAnimationFrame(frame); ro.disconnect(); };\n }, []);\n\n if (failed) return <div className={['uic-chart', 'is-unavailable', className].filter(Boolean).join(' ')}>{fallback}</div>;\n return (\n <div\n ref={host}\n className={['uic-chart', className].filter(Boolean).join(' ')}\n role={label ? 'img' : undefined}\n aria-label={label}\n />\n );\n}\n","// An ECharts option can come from anywhere (a person, a saved dashboard, an agent writing it), so the chart keeps it\n// inert before ECharts sees it (found 2026-11-10). ECharts writes some option strings into the page as HTML or opens\n// them as URLs:\n// - a tooltip's content (its formatter, series and data names) in the default HTML render mode;\n// - a title's link / sublink and a sunburst or treemap node's link, opened in a window that keeps the page's origin;\n// - the data view's title and button labels (toolbox.feature.dataView.title / lang).\n// So every tooltip draws in the chart itself (renderMode 'richText': text, never markup), a link is kept only when it is\n// http(s) or mailto, and the data view's words are escaped. The option passed in is never changed: plain objects and\n// arrays are copied, anything else (functions, typed arrays, dates, gradient objects) is passed through as it is.\nconst SAFE_HREF = /^(https?:\\/\\/|mailto:)/i;\n\nexport function safeHref(v) {\n return typeof v === 'string' && SAFE_HREF.test(v.replace(/[\\u0000- \\u007f]/g, '')) ? v : '';\n}\n\nconst ESC = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '\"': '&quot;', \"'\": '&#39;' };\nconst esc = (s) => String(s).replace(/[&<>\"']/g, (c) => ESC[c]);\n\nconst isPlain = (v) => {\n if (!v || typeof v !== 'object' || Array.isArray(v)) return false;\n const p = Object.getPrototypeOf(v);\n return p === Object.prototype || p === null;\n};\n\nexport function safeChartOption(option) {\n if (!isPlain(option) && !Array.isArray(option)) return option;\n const seen = new Map();\n const escaped = new Set();\n const walk = (n) => {\n if (Array.isArray(n)) {\n if (seen.has(n)) return seen.get(n);\n const out = []; seen.set(n, out);\n for (const x of n) out.push(walk(x));\n return out;\n }\n if (!isPlain(n)) return n;\n if (seen.has(n)) return seen.get(n);\n const out = {}; seen.set(n, out);\n for (const k of Object.keys(n)) {\n const v = n[k];\n if ((k === 'link' || k === 'sublink') && typeof v === 'string') {\n if (safeHref(v)) out[k] = v;\n } else if (k === 'tooltip') {\n out[k] = tooltip(v);\n } else if (k === 'dataView' && isPlain(v)) {\n out[k] = dataView(walk(v));\n } else {\n out[k] = walk(v);\n }\n }\n return out;\n };\n const tooltip = (v) => {\n if (v === true) return { renderMode: 'richText' };\n if (Array.isArray(v)) return v.map((t) => (isPlain(t) ? { ...walk(t), renderMode: 'richText' } : walk(t)));\n return isPlain(v) ? { ...walk(v), renderMode: 'richText' } : walk(v);\n };\n const dataView = (d) => {\n if (escaped.has(d)) return d; // the same data view reached twice: escape once\n escaped.add(d);\n if (typeof d.title === 'string') d.title = esc(d.title);\n if (Array.isArray(d.lang)) d.lang = d.lang.map((s) => (typeof s === 'string' ? esc(s) : s));\n return d;\n };\n return walk(option);\n}\n","// The theme, as values a chart library can use.\n//\n// A chart draws into a canvas, where CSS custom properties do not reach: series colours, axis\n// labels and tooltip chrome all arrive as strings in an option object. Without this, every app\n// hardcodes hex in its chart code and the charts are the one part of the product that does not\n// follow the theme. Reading the same --uic-* tokens the stylesheets read keeps them together.\n\n// Same policy as the `var(--uic-x, …)` fallbacks in the stylesheets: a host that never loaded a\n// token layer still gets the library's own light palette rather than whatever the chart library\n// defaults to. Values are themes/light.css verbatim.\nconst FALLBACK = {\n cat: ['#2a78d6', '#eb6834', '#1baf7a', '#eda100', '#e87ba4', '#008300', '#4a3aa7', '#e34948'],\n ink: '#111317', ink2: '#3A3D45', mute: '#5B5D66',\n line: '#DDDFE6', lineSoft: '#EFF1F4', surface: '#FFFFFF',\n brand: '#285AFF', ok: '#16a34a', warn: '#F59E0B', bad: '#b91c1c',\n font: 'inherit',\n};\n\nfunction read(style, name, fallback) {\n const v = style.getPropertyValue(name).trim();\n return v || fallback;\n}\n\n/**\n * Resolve the chart tokens in effect for `el` — pass the element the chart lives in, so a panel\n * inside a dark region resolves the dark values even when the page is light.\n *\n * `palette` is a categorical scale: assign it IN ORDER, one colour per series, and never cycle\n * it. Past eight series, colour has stopped being an encoding — fold the tail into one \"other\"\n * series or split the chart. On a light surface three of the eight sit below 3:1 against white,\n * so identity must not rest on colour alone: keep the legend, or label the series directly.\n */\nexport function chartTokens(el) {\n const target = (el && el.nodeType === 1 ? el : null)\n || (typeof document !== 'undefined' ? document.documentElement : null);\n if (!target || typeof getComputedStyle !== 'function') return { ...FALLBACK, palette: FALLBACK.cat };\n const s = getComputedStyle(target);\n return {\n palette: FALLBACK.cat.map((hex, i) => read(s, `--uic-cat-${i + 1}`, hex)),\n ink: read(s, '--uic-ink', FALLBACK.ink),\n ink2: read(s, '--uic-ink-2', FALLBACK.ink2),\n mute: read(s, '--uic-mute', FALLBACK.mute),\n line: read(s, '--uic-line', FALLBACK.line),\n lineSoft: read(s, '--uic-line-soft', FALLBACK.lineSoft),\n surface: read(s, '--uic-surface', FALLBACK.surface),\n brand: read(s, '--uic-brand', FALLBACK.brand),\n ok: read(s, '--uic-ok', FALLBACK.ok),\n warn: read(s, '--uic-warn', FALLBACK.warn),\n bad: read(s, '--uic-fail-ink', FALLBACK.bad),\n font: read(s, '--uic-font', FALLBACK.font),\n };\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport { chartTokens } from './tokens.js';\n\n/**\n * chartTokens(), re-read when the theme changes.\n *\n * A chart's colours are baked into its option at build time, so flipping light↔dark leaves every\n * chart on the page painted for the theme that is gone until something re-renders it. There is no\n * event for \"the tokens changed\", so this watches the two things that change them: the attributes\n * a theme is switched with (a class or data-* on <html>/<body>), and the OS preference.\n *\n * Returns [tokens, ref] — put the ref on the element the chart lives in, or leave it unused and\n * the document's own tokens are read.\n */\nexport function useChartTokens() {\n const ref = useRef(null);\n const [tokens, setTokens] = useState(() => chartTokens(null));\n const reread = useCallback(() => setTokens(chartTokens(ref.current)), []);\n\n useEffect(() => {\n reread();\n if (typeof MutationObserver !== 'function') return undefined;\n const obs = new MutationObserver(reread);\n const opts = { attributes: true, attributeFilter: ['class', 'style', 'data-theme'] };\n obs.observe(document.documentElement, opts);\n if (document.body) obs.observe(document.body, opts);\n const mq = typeof matchMedia === 'function' ? matchMedia('(prefers-color-scheme: dark)') : null;\n mq?.addEventListener?.('change', reread);\n return () => { obs.disconnect(); mq?.removeEventListener?.('change', reread); };\n }, [reread]);\n\n return [tokens, ref];\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACYA,mBAA4C;;;ACH5C,IAAM,YAAY;AAEX,SAAS,SAAS,GAAG;AAC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,UAAI,WAAW;AAAG,UAAI,sBAAsB,UAAU,MAAM;AAAA,IAAG;AAAA,EAChF,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO,CAAC,QAAQ,GAAG;AACrB;","names":["out","import_react"]}
package/dist/chart.d.ts CHANGED
@@ -55,3 +55,11 @@ export function chartTokens(el?: Element | null): ChartTokens;
55
55
  /** chartTokens(), re-read when the theme changes (a class/data-* flip on <html> or <body>, or the
56
56
  * OS preference). Put the returned ref on the element the chart lives in. */
57
57
  export function useChartTokens(): [ChartTokens, RefObject<HTMLElement | null>];
58
+
59
+ /** A copy of an option with what ECharts would write as HTML or open as a URL made inert: every tooltip draws text
60
+ * (renderMode 'richText'), a link or sublink is kept only for http(s) or mailto, and the data view's title and labels
61
+ * are escaped. Chart applies it to every option; call it yourself before setOption on the instance from onInit. */
62
+ export function safeChartOption<T>(option: T): T;
63
+ /** The value when it is an http(s) or mailto URL, else ''. */
64
+ export function safeHref(value: unknown): string;
65
+
package/dist/chart.js CHANGED
@@ -2,7 +2,69 @@ import "./chunk-PZ5AY32C.js";
2
2
 
3
3
  // src/chart/Chart.jsx
4
4
  import { useEffect, useRef, useState } from "react";
5
+
6
+ // src/chart/safe.js
7
+ var SAFE_HREF = /^(https?:\/\/|mailto:)/i;
8
+ function safeHref(v) {
9
+ return typeof v === "string" && SAFE_HREF.test(v.replace(/[\u0000- \u007f]/g, "")) ? v : "";
10
+ }
11
+ var ESC = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" };
12
+ var esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
13
+ var isPlain = (v) => {
14
+ if (!v || typeof v !== "object" || Array.isArray(v)) return false;
15
+ const p = Object.getPrototypeOf(v);
16
+ return p === Object.prototype || p === null;
17
+ };
18
+ function safeChartOption(option) {
19
+ if (!isPlain(option) && !Array.isArray(option)) return option;
20
+ const seen = /* @__PURE__ */ new Map();
21
+ const escaped = /* @__PURE__ */ new Set();
22
+ const walk = (n) => {
23
+ if (Array.isArray(n)) {
24
+ if (seen.has(n)) return seen.get(n);
25
+ const out2 = [];
26
+ seen.set(n, out2);
27
+ for (const x of n) out2.push(walk(x));
28
+ return out2;
29
+ }
30
+ if (!isPlain(n)) return n;
31
+ if (seen.has(n)) return seen.get(n);
32
+ const out = {};
33
+ seen.set(n, out);
34
+ for (const k of Object.keys(n)) {
35
+ const v = n[k];
36
+ if ((k === "link" || k === "sublink") && typeof v === "string") {
37
+ if (safeHref(v)) out[k] = v;
38
+ } else if (k === "tooltip") {
39
+ out[k] = tooltip(v);
40
+ } else if (k === "dataView" && isPlain(v)) {
41
+ out[k] = dataView(walk(v));
42
+ } else {
43
+ out[k] = walk(v);
44
+ }
45
+ }
46
+ return out;
47
+ };
48
+ const tooltip = (v) => {
49
+ if (v === true) return { renderMode: "richText" };
50
+ if (Array.isArray(v)) return v.map((t) => isPlain(t) ? { ...walk(t), renderMode: "richText" } : walk(t));
51
+ return isPlain(v) ? { ...walk(v), renderMode: "richText" } : walk(v);
52
+ };
53
+ const dataView = (d) => {
54
+ if (escaped.has(d)) return d;
55
+ escaped.add(d);
56
+ if (typeof d.title === "string") d.title = esc(d.title);
57
+ if (Array.isArray(d.lang)) d.lang = d.lang.map((s) => typeof s === "string" ? esc(s) : s);
58
+ return d;
59
+ };
60
+ return walk(option);
61
+ }
62
+
63
+ // src/chart/Chart.jsx
5
64
  import { jsx } from "react/jsx-runtime";
65
+ function applyOption(chart, option, notMerge) {
66
+ chart.setOption(safeChartOption(option), notMerge);
67
+ }
6
68
  function Chart(props) {
7
69
  const { option, renderer = "canvas", notMerge = true, onInit, label, fallback = null, className = "" } = props;
8
70
  const host = useRef(null);
@@ -22,7 +84,7 @@ function Chart(props) {
22
84
  inst.current = chart;
23
85
  const { option: opt, notMerge: nm, onInit: cb } = latest.current;
24
86
  if (opt) {
25
- chart.setOption(opt, nm);
87
+ applyOption(chart, opt, nm);
26
88
  applied.current = opt;
27
89
  }
28
90
  if (cb) cb(chart);
@@ -42,7 +104,7 @@ function Chart(props) {
42
104
  }, [renderer]);
43
105
  useEffect(() => {
44
106
  if (!inst.current || !option || applied.current === option) return;
45
- inst.current.setOption(option, notMerge);
107
+ applyOption(inst.current, option, notMerge);
46
108
  applied.current = option;
47
109
  }, [option, notMerge]);
48
110
  useEffect(() => {
@@ -135,6 +197,8 @@ function useChartTokens() {
135
197
  export {
136
198
  Chart,
137
199
  chartTokens,
200
+ safeChartOption,
201
+ safeHref,
138
202
  useChartTokens
139
203
  };
140
204
  //# sourceMappingURL=chart.js.map
package/dist/chart.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/chart/Chart.jsx","../src/chart/tokens.js","../src/chart/useChartTokens.js"],"sourcesContent":["// Chart — an ECharts option, drawn. That is the whole component.\n//\n// It deliberately knows nothing about series, axes or data shape: the option IS the contract, so\n// anything ECharts can draw, this draws, and the thing that decides what a chart should look like\n// (a person, or an agent writing the option) is not limited by a props table. What it does own is\n// the part every hand-rolled wrapper gets wrong: creating the instance once, disposing it on\n// unmount, and resizing it when its box changes — a chart inside a resizable panel that never\n// hears about the resize is the most common broken dashboard there is.\n//\n// echarts is an OPTIONAL peer, loaded lazily here, which is why this lives at `reifyui/chart` and\n// not in the root index — the same reason `reifyui/slides` is its own entry. An app that imports\n// a Button must not resolve a charting library.\nimport { useEffect, useRef, useState } from 'react';\n\n/**\n * option the ECharts option. Applied whenever the REFERENCE changes, so build it in a\n * useMemo — a fresh object every render redraws the chart every render\n * renderer 'canvas' (default) or 'svg'; svg stays crisp when the page is printed or scaled\n * notMerge replace the previous option rather than merging into it (default true: a redraw\n * after new data should not inherit last query's series)\n * onInit the instance, once, for the things only the instance can do (export an image)\n * label the chart's accessible name; a canvas has no text for a screen reader to find\n * fallback drawn instead when the chart code cannot be loaded at all\n */\nexport function Chart(props) {\n const { option, renderer = 'canvas', notMerge = true, onInit, label, fallback = null, className = '' } = props;\n const host = useRef(null);\n const inst = useRef(null);\n const applied = useRef(null); // the option the instance is currently showing\n const [failed, setFailed] = useState(false);\n // Read through refs inside the init effect so a new option or callback does not tear the\n // instance down and rebuild it.\n const latest = useRef({ option, notMerge, onInit });\n latest.current = { option, notMerge, onInit };\n\n useEffect(() => {\n let dead = false;\n (async () => {\n try {\n const mod = await import('echarts');\n const echarts = typeof mod.init === 'function' ? mod : (mod.default || mod);\n if (dead || !host.current) return;\n const chart = echarts.init(host.current, null, { renderer });\n inst.current = chart;\n const { option: opt, notMerge: nm, onInit: cb } = latest.current;\n if (opt) { chart.setOption(opt, nm); applied.current = opt; }\n if (cb) cb(chart);\n } catch {\n if (!dead) setFailed(true); // no chart code: say so, never draw a fake one\n }\n })();\n return () => {\n dead = true;\n try { inst.current?.dispose(); } catch { /* already gone with the DOM */ }\n inst.current = null;\n applied.current = null;\n };\n }, [renderer]);\n\n // `applied` keeps this from redrawing the option the init above just drew — the instance\n // arrives asynchronously, so both paths run on the first render that has one.\n useEffect(() => {\n if (!inst.current || !option || applied.current === option) return;\n inst.current.setOption(option, notMerge);\n applied.current = option;\n }, [option, notMerge]);\n\n useEffect(() => {\n const el = host.current;\n if (!el || typeof ResizeObserver !== 'function') return undefined;\n let frame = 0;\n const ro = new ResizeObserver(() => {\n // One resize per frame: a drag on a panel corner fires the observer on every pointer move.\n cancelAnimationFrame(frame);\n frame = requestAnimationFrame(() => inst.current?.resize());\n });\n ro.observe(el);\n return () => { cancelAnimationFrame(frame); ro.disconnect(); };\n }, []);\n\n if (failed) return <div className={['uic-chart', 'is-unavailable', className].filter(Boolean).join(' ')}>{fallback}</div>;\n return (\n <div\n ref={host}\n className={['uic-chart', className].filter(Boolean).join(' ')}\n role={label ? 'img' : undefined}\n aria-label={label}\n />\n );\n}\n","// The theme, as values a chart library can use.\n//\n// A chart draws into a canvas, where CSS custom properties do not reach: series colours, axis\n// labels and tooltip chrome all arrive as strings in an option object. Without this, every app\n// hardcodes hex in its chart code and the charts are the one part of the product that does not\n// follow the theme. Reading the same --uic-* tokens the stylesheets read keeps them together.\n\n// Same policy as the `var(--uic-x, …)` fallbacks in the stylesheets: a host that never loaded a\n// token layer still gets the library's own light palette rather than whatever the chart library\n// defaults to. Values are themes/light.css verbatim.\nconst FALLBACK = {\n cat: ['#2a78d6', '#eb6834', '#1baf7a', '#eda100', '#e87ba4', '#008300', '#4a3aa7', '#e34948'],\n ink: '#111317', ink2: '#3A3D45', mute: '#5B5D66',\n line: '#DDDFE6', lineSoft: '#EFF1F4', surface: '#FFFFFF',\n brand: '#285AFF', ok: '#16a34a', warn: '#F59E0B', bad: '#b91c1c',\n font: 'inherit',\n};\n\nfunction read(style, name, fallback) {\n const v = style.getPropertyValue(name).trim();\n return v || fallback;\n}\n\n/**\n * Resolve the chart tokens in effect for `el` — pass the element the chart lives in, so a panel\n * inside a dark region resolves the dark values even when the page is light.\n *\n * `palette` is a categorical scale: assign it IN ORDER, one colour per series, and never cycle\n * it. Past eight series, colour has stopped being an encoding — fold the tail into one \"other\"\n * series or split the chart. On a light surface three of the eight sit below 3:1 against white,\n * so identity must not rest on colour alone: keep the legend, or label the series directly.\n */\nexport function chartTokens(el) {\n const target = (el && el.nodeType === 1 ? el : null)\n || (typeof document !== 'undefined' ? document.documentElement : null);\n if (!target || typeof getComputedStyle !== 'function') return { ...FALLBACK, palette: FALLBACK.cat };\n const s = getComputedStyle(target);\n return {\n palette: FALLBACK.cat.map((hex, i) => read(s, `--uic-cat-${i + 1}`, hex)),\n ink: read(s, '--uic-ink', FALLBACK.ink),\n ink2: read(s, '--uic-ink-2', FALLBACK.ink2),\n mute: read(s, '--uic-mute', FALLBACK.mute),\n line: read(s, '--uic-line', FALLBACK.line),\n lineSoft: read(s, '--uic-line-soft', FALLBACK.lineSoft),\n surface: read(s, '--uic-surface', FALLBACK.surface),\n brand: read(s, '--uic-brand', FALLBACK.brand),\n ok: read(s, '--uic-ok', FALLBACK.ok),\n warn: read(s, '--uic-warn', FALLBACK.warn),\n bad: read(s, '--uic-fail-ink', FALLBACK.bad),\n font: read(s, '--uic-font', FALLBACK.font),\n };\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport { chartTokens } from './tokens.js';\n\n/**\n * chartTokens(), re-read when the theme changes.\n *\n * A chart's colours are baked into its option at build time, so flipping light↔dark leaves every\n * chart on the page painted for the theme that is gone until something re-renders it. There is no\n * event for \"the tokens changed\", so this watches the two things that change them: the attributes\n * a theme is switched with (a class or data-* on <html>/<body>), and the OS preference.\n *\n * Returns [tokens, ref] — put the ref on the element the chart lives in, or leave it unused and\n * the document's own tokens are read.\n */\nexport function useChartTokens() {\n const ref = useRef(null);\n const [tokens, setTokens] = useState(() => chartTokens(null));\n const reread = useCallback(() => setTokens(chartTokens(ref.current)), []);\n\n useEffect(() => {\n reread();\n if (typeof MutationObserver !== 'function') return undefined;\n const obs = new MutationObserver(reread);\n const opts = { attributes: true, attributeFilter: ['class', 'style', 'data-theme'] };\n obs.observe(document.documentElement, opts);\n if (document.body) obs.observe(document.body, opts);\n const mq = typeof matchMedia === 'function' ? matchMedia('(prefers-color-scheme: dark)') : null;\n mq?.addEventListener?.('change', reread);\n return () => { obs.disconnect(); mq?.removeEventListener?.('change', reread); };\n }, [reread]);\n\n return [tokens, ref];\n}\n"],"mappings":";;;AAYA,SAAS,WAAW,QAAQ,gBAAgB;AAoEvB;AAxDd,SAAS,MAAM,OAAO;AAC3B,QAAM,EAAE,QAAQ,WAAW,UAAU,WAAW,MAAM,QAAQ,OAAO,WAAW,MAAM,YAAY,GAAG,IAAI;AACzG,QAAM,OAAO,OAAO,IAAI;AACxB,QAAM,OAAO,OAAO,IAAI;AACxB,QAAM,UAAU,OAAO,IAAI;AAC3B,QAAM,CAAC,QAAQ,SAAS,IAAI,SAAS,KAAK;AAG1C,QAAM,SAAS,OAAO,EAAE,QAAQ,UAAU,OAAO,CAAC;AAClD,SAAO,UAAU,EAAE,QAAQ,UAAU,OAAO;AAE5C,YAAU,MAAM;AACd,QAAI,OAAO;AACX,KAAC,YAAY;AACX,UAAI;AACF,cAAM,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chart/safe.js","../src/chart/tokens.js","../src/chart/useChartTokens.js"],"sourcesContent":["// Chart — an ECharts option, drawn. That is the whole component.\n//\n// It deliberately knows nothing about series, axes or data shape: the option IS the contract, so\n// anything ECharts can draw, this draws, and the thing that decides what a chart should look like\n// (a person, or an agent writing the option) is not limited by a props table. What it does own is\n// the part every hand-rolled wrapper gets wrong: creating the instance once, disposing it on\n// unmount, and resizing it when its box changes — a chart inside a resizable panel that never\n// hears about the resize is the most common broken dashboard there is.\n//\n// echarts is an OPTIONAL peer, loaded lazily here, which is why this lives at `reifyui/chart` and\n// not in the root index — the same reason `reifyui/slides` is its own entry. An app that imports\n// a Button must not resolve a charting library.\nimport { useEffect, useRef, useState } from 'react';\nimport { safeChartOption } from './safe.js';\n\n// Every option reaches ECharts through here, made inert first (safe.js): a tooltip draws text, a link is http(s) or\n// mailto, the data view's words are escaped. An option a host sets on the instance it got from onInit is its own.\nexport function applyOption(chart, option, notMerge) {\n chart.setOption(safeChartOption(option), notMerge);\n}\n\n/**\n * option the ECharts option. Applied whenever the REFERENCE changes, so build it in a\n * useMemo — a fresh object every render redraws the chart every render\n * renderer 'canvas' (default) or 'svg'; svg stays crisp when the page is printed or scaled\n * notMerge replace the previous option rather than merging into it (default true: a redraw\n * after new data should not inherit last query's series)\n * onInit the instance, once, for the things only the instance can do (export an image)\n * label the chart's accessible name; a canvas has no text for a screen reader to find\n * fallback drawn instead when the chart code cannot be loaded at all\n */\nexport function Chart(props) {\n const { option, renderer = 'canvas', notMerge = true, onInit, label, fallback = null, className = '' } = props;\n const host = useRef(null);\n const inst = useRef(null);\n const applied = useRef(null); // the option the instance is currently showing\n const [failed, setFailed] = useState(false);\n // Read through refs inside the init effect so a new option or callback does not tear the\n // instance down and rebuild it.\n const latest = useRef({ option, notMerge, onInit });\n latest.current = { option, notMerge, onInit };\n\n useEffect(() => {\n let dead = false;\n (async () => {\n try {\n const mod = await import('echarts');\n const echarts = typeof mod.init === 'function' ? mod : (mod.default || mod);\n if (dead || !host.current) return;\n const chart = echarts.init(host.current, null, { renderer });\n inst.current = chart;\n const { option: opt, notMerge: nm, onInit: cb } = latest.current;\n if (opt) { applyOption(chart, opt, nm); applied.current = opt; }\n if (cb) cb(chart);\n } catch {\n if (!dead) setFailed(true); // no chart code: say so, never draw a fake one\n }\n })();\n return () => {\n dead = true;\n try { inst.current?.dispose(); } catch { /* already gone with the DOM */ }\n inst.current = null;\n applied.current = null;\n };\n }, [renderer]);\n\n // `applied` keeps this from redrawing the option the init above just drew — the instance\n // arrives asynchronously, so both paths run on the first render that has one.\n useEffect(() => {\n if (!inst.current || !option || applied.current === option) return;\n applyOption(inst.current, option, notMerge);\n applied.current = option;\n }, [option, notMerge]);\n\n useEffect(() => {\n const el = host.current;\n if (!el || typeof ResizeObserver !== 'function') return undefined;\n let frame = 0;\n const ro = new ResizeObserver(() => {\n // One resize per frame: a drag on a panel corner fires the observer on every pointer move.\n cancelAnimationFrame(frame);\n frame = requestAnimationFrame(() => inst.current?.resize());\n });\n ro.observe(el);\n return () => { cancelAnimationFrame(frame); ro.disconnect(); };\n }, []);\n\n if (failed) return <div className={['uic-chart', 'is-unavailable', className].filter(Boolean).join(' ')}>{fallback}</div>;\n return (\n <div\n ref={host}\n className={['uic-chart', className].filter(Boolean).join(' ')}\n role={label ? 'img' : undefined}\n aria-label={label}\n />\n );\n}\n","// An ECharts option can come from anywhere (a person, a saved dashboard, an agent writing it), so the chart keeps it\n// inert before ECharts sees it (found 2026-11-10). ECharts writes some option strings into the page as HTML or opens\n// them as URLs:\n// - a tooltip's content (its formatter, series and data names) in the default HTML render mode;\n// - a title's link / sublink and a sunburst or treemap node's link, opened in a window that keeps the page's origin;\n// - the data view's title and button labels (toolbox.feature.dataView.title / lang).\n// So every tooltip draws in the chart itself (renderMode 'richText': text, never markup), a link is kept only when it is\n// http(s) or mailto, and the data view's words are escaped. The option passed in is never changed: plain objects and\n// arrays are copied, anything else (functions, typed arrays, dates, gradient objects) is passed through as it is.\nconst SAFE_HREF = /^(https?:\\/\\/|mailto:)/i;\n\nexport function safeHref(v) {\n return typeof v === 'string' && SAFE_HREF.test(v.replace(/[\\u0000- \\u007f]/g, '')) ? v : '';\n}\n\nconst ESC = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '\"': '&quot;', \"'\": '&#39;' };\nconst esc = (s) => String(s).replace(/[&<>\"']/g, (c) => ESC[c]);\n\nconst isPlain = (v) => {\n if (!v || typeof v !== 'object' || Array.isArray(v)) return false;\n const p = Object.getPrototypeOf(v);\n return p === Object.prototype || p === null;\n};\n\nexport function safeChartOption(option) {\n if (!isPlain(option) && !Array.isArray(option)) return option;\n const seen = new Map();\n const escaped = new Set();\n const walk = (n) => {\n if (Array.isArray(n)) {\n if (seen.has(n)) return seen.get(n);\n const out = []; seen.set(n, out);\n for (const x of n) out.push(walk(x));\n return out;\n }\n if (!isPlain(n)) return n;\n if (seen.has(n)) return seen.get(n);\n const out = {}; seen.set(n, out);\n for (const k of Object.keys(n)) {\n const v = n[k];\n if ((k === 'link' || k === 'sublink') && typeof v === 'string') {\n if (safeHref(v)) out[k] = v;\n } else if (k === 'tooltip') {\n out[k] = tooltip(v);\n } else if (k === 'dataView' && isPlain(v)) {\n out[k] = dataView(walk(v));\n } else {\n out[k] = walk(v);\n }\n }\n return out;\n };\n const tooltip = (v) => {\n if (v === true) return { renderMode: 'richText' };\n if (Array.isArray(v)) return v.map((t) => (isPlain(t) ? { ...walk(t), renderMode: 'richText' } : walk(t)));\n return isPlain(v) ? { ...walk(v), renderMode: 'richText' } : walk(v);\n };\n const dataView = (d) => {\n if (escaped.has(d)) return d; // the same data view reached twice: escape once\n escaped.add(d);\n if (typeof d.title === 'string') d.title = esc(d.title);\n if (Array.isArray(d.lang)) d.lang = d.lang.map((s) => (typeof s === 'string' ? esc(s) : s));\n return d;\n };\n return walk(option);\n}\n","// The theme, as values a chart library can use.\n//\n// A chart draws into a canvas, where CSS custom properties do not reach: series colours, axis\n// labels and tooltip chrome all arrive as strings in an option object. Without this, every app\n// hardcodes hex in its chart code and the charts are the one part of the product that does not\n// follow the theme. Reading the same --uic-* tokens the stylesheets read keeps them together.\n\n// Same policy as the `var(--uic-x, …)` fallbacks in the stylesheets: a host that never loaded a\n// token layer still gets the library's own light palette rather than whatever the chart library\n// defaults to. Values are themes/light.css verbatim.\nconst FALLBACK = {\n cat: ['#2a78d6', '#eb6834', '#1baf7a', '#eda100', '#e87ba4', '#008300', '#4a3aa7', '#e34948'],\n ink: '#111317', ink2: '#3A3D45', mute: '#5B5D66',\n line: '#DDDFE6', lineSoft: '#EFF1F4', surface: '#FFFFFF',\n brand: '#285AFF', ok: '#16a34a', warn: '#F59E0B', bad: '#b91c1c',\n font: 'inherit',\n};\n\nfunction read(style, name, fallback) {\n const v = style.getPropertyValue(name).trim();\n return v || fallback;\n}\n\n/**\n * Resolve the chart tokens in effect for `el` — pass the element the chart lives in, so a panel\n * inside a dark region resolves the dark values even when the page is light.\n *\n * `palette` is a categorical scale: assign it IN ORDER, one colour per series, and never cycle\n * it. Past eight series, colour has stopped being an encoding — fold the tail into one \"other\"\n * series or split the chart. On a light surface three of the eight sit below 3:1 against white,\n * so identity must not rest on colour alone: keep the legend, or label the series directly.\n */\nexport function chartTokens(el) {\n const target = (el && el.nodeType === 1 ? el : null)\n || (typeof document !== 'undefined' ? document.documentElement : null);\n if (!target || typeof getComputedStyle !== 'function') return { ...FALLBACK, palette: FALLBACK.cat };\n const s = getComputedStyle(target);\n return {\n palette: FALLBACK.cat.map((hex, i) => read(s, `--uic-cat-${i + 1}`, hex)),\n ink: read(s, '--uic-ink', FALLBACK.ink),\n ink2: read(s, '--uic-ink-2', FALLBACK.ink2),\n mute: read(s, '--uic-mute', FALLBACK.mute),\n line: read(s, '--uic-line', FALLBACK.line),\n lineSoft: read(s, '--uic-line-soft', FALLBACK.lineSoft),\n surface: read(s, '--uic-surface', FALLBACK.surface),\n brand: read(s, '--uic-brand', FALLBACK.brand),\n ok: read(s, '--uic-ok', FALLBACK.ok),\n warn: read(s, '--uic-warn', FALLBACK.warn),\n bad: read(s, '--uic-fail-ink', FALLBACK.bad),\n font: read(s, '--uic-font', FALLBACK.font),\n };\n}\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport { chartTokens } from './tokens.js';\n\n/**\n * chartTokens(), re-read when the theme changes.\n *\n * A chart's colours are baked into its option at build time, so flipping light↔dark leaves every\n * chart on the page painted for the theme that is gone until something re-renders it. There is no\n * event for \"the tokens changed\", so this watches the two things that change them: the attributes\n * a theme is switched with (a class or data-* on <html>/<body>), and the OS preference.\n *\n * Returns [tokens, ref] — put the ref on the element the chart lives in, or leave it unused and\n * the document's own tokens are read.\n */\nexport function useChartTokens() {\n const ref = useRef(null);\n const [tokens, setTokens] = useState(() => chartTokens(null));\n const reread = useCallback(() => setTokens(chartTokens(ref.current)), []);\n\n useEffect(() => {\n reread();\n if (typeof MutationObserver !== 'function') return undefined;\n const obs = new MutationObserver(reread);\n const opts = { attributes: true, attributeFilter: ['class', 'style', 'data-theme'] };\n obs.observe(document.documentElement, opts);\n if (document.body) obs.observe(document.body, opts);\n const mq = typeof matchMedia === 'function' ? matchMedia('(prefers-color-scheme: dark)') : null;\n mq?.addEventListener?.('change', reread);\n return () => { obs.disconnect(); mq?.removeEventListener?.('change', reread); };\n }, [reread]);\n\n return [tokens, ref];\n}\n"],"mappings":";;;AAYA,SAAS,WAAW,QAAQ,gBAAgB;;;ACH5C,IAAM,YAAY;AAEX,SAAS,SAAS,GAAG;AAC1B,SAAO,OAAO,MAAM,YAAY,UAAU,KAAK,EAAE,QAAQ,qBAAqB,EAAE,CAAC,IAAI,IAAI;AAC3F;AAEA,IAAM,MAAM,EAAE,KAAK,SAAS,KAAK,QAAQ,KAAK,QAAQ,KAAK,UAAU,KAAK,QAAQ;AAClF,IAAM,MAAM,CAAC,MAAM,OAAO,CAAC,EAAE,QAAQ,YAAY,CAAC,MAAM,IAAI,CAAC,CAAC;AAE9D,IAAM,UAAU,CAAC,MAAM;AACrB,MAAI,CA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AG,iBAAiB,SAAS,OAAO;AAAA,IAClD,OAAO,KAAK,GAAG,eAAe,SAAS,KAAK;AAAA,IAC5C,IAAI,KAAK,GAAG,YAAY,SAAS,EAAE;AAAA,IACnC,MAAM,KAAK,GAAG,cAAc,SAAS,IAAI;AAAA,IACzC,KAAK,KAAK,GAAG,kBAAkB,SAAS,GAAG;AAAA,IAC3C,MAAM,KAAK,GAAG,cAAc,SAAS,IAAI;AAAA,EAC3C;AACF;;;ACnDA,SAAS,aAAa,aAAAC,YAAW,UAAAC,SAAQ,YAAAC,iBAAgB;AAclD,SAAS,iBAAiB;AAC/B,QAAM,MAAMC,QAAO,IAAI;AACvB,QAAM,CAAC,QAAQ,SAAS,IAAIC,UAAS,MAAM,YAAY,IAAI,CAAC;AAC5D,QAAM,SAAS,YAAY,MAAM,UAAU,YAAY,IAAI,OAAO,CAAC,GAAG,CAAC,CAAC;AAExE,EAAAC,WAAU,MAAM;AACd,WAAO;AACP,QAAI,OAAO,qBAAqB,WAAY,QAAO;AACnD,UAAM,MAAM,IAAI,iBAAiB,MAAM;AACvC,UAAM,OAAO,EAAE,YAAY,MAAM,iBAAiB,CAAC,SAAS,SAAS,YAAY,EAAE;AACnF,QAAI,QAAQ,SAAS,iBAAiB,IAAI;AAC1C,QAAI,SAAS,KAAM,KAAI,QAAQ,SAAS,MAAM,IAAI;AAClD,UAAM,KAAK,OAAO,eAAe,aAAa,WAAW,8BAA8B,IAAI;AAC3F,QAAI,mBAAmB,UAAU,MAAM;AACvC,WAAO,MAAM;AAAE,UAAI,WAAW;AAAG,UAAI,sBAAsB,UAAU,MAAM;AAAA,IAAG;AAAA,EAChF,GAAG,CAAC,MAAM,CAAC;AAEX,SAAO,CAAC,QAAQ,GAAG;AACrB;","names":["out","useEffect","useRef","useState","useRef","useState","useEffect"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reifyui",
3
- "version": "0.13.12",
3
+ "version": "0.13.13",
4
4
  "description": "The open-source rendering & interaction layer for agentic applications. Stream and render any agent's output as polished, application-native UI — model-agnostic, framework-agnostic, design-system-native. The foundation for building generative-UI and agent products.",
5
5
  "keywords": [
6
6
  "react",
@@ -113,7 +113,8 @@
113
113
  "build": "tsup",
114
114
  "prepublishOnly": "npm run build",
115
115
  "clean": "rm -rf dist",
116
- "prepare": "npm run build"
116
+ "prepare": "npm run build",
117
+ "test": "node test/run.mjs"
117
118
  },
118
119
  "peerDependencies": {
119
120
  "echarts": "^5 || ^6",
@@ -11,6 +11,13 @@
11
11
  // not in the root index — the same reason `reifyui/slides` is its own entry. An app that imports
12
12
  // a Button must not resolve a charting library.
13
13
  import { useEffect, useRef, useState } from 'react';
14
+ import { safeChartOption } from './safe.js';
15
+
16
+ // Every option reaches ECharts through here, made inert first (safe.js): a tooltip draws text, a link is http(s) or
17
+ // mailto, the data view's words are escaped. An option a host sets on the instance it got from onInit is its own.
18
+ export function applyOption(chart, option, notMerge) {
19
+ chart.setOption(safeChartOption(option), notMerge);
20
+ }
14
21
 
15
22
  /**
16
23
  * option the ECharts option. Applied whenever the REFERENCE changes, so build it in a
@@ -43,7 +50,7 @@ export function Chart(props) {
43
50
  const chart = echarts.init(host.current, null, { renderer });
44
51
  inst.current = chart;
45
52
  const { option: opt, notMerge: nm, onInit: cb } = latest.current;
46
- if (opt) { chart.setOption(opt, nm); applied.current = opt; }
53
+ if (opt) { applyOption(chart, opt, nm); applied.current = opt; }
47
54
  if (cb) cb(chart);
48
55
  } catch {
49
56
  if (!dead) setFailed(true); // no chart code: say so, never draw a fake one
@@ -61,7 +68,7 @@ export function Chart(props) {
61
68
  // arrives asynchronously, so both paths run on the first render that has one.
62
69
  useEffect(() => {
63
70
  if (!inst.current || !option || applied.current === option) return;
64
- inst.current.setOption(option, notMerge);
71
+ applyOption(inst.current, option, notMerge);
65
72
  applied.current = option;
66
73
  }, [option, notMerge]);
67
74
 
@@ -55,3 +55,11 @@ export function chartTokens(el?: Element | null): ChartTokens;
55
55
  /** chartTokens(), re-read when the theme changes (a class/data-* flip on <html> or <body>, or the
56
56
  * OS preference). Put the returned ref on the element the chart lives in. */
57
57
  export function useChartTokens(): [ChartTokens, RefObject<HTMLElement | null>];
58
+
59
+ /** A copy of an option with what ECharts would write as HTML or open as a URL made inert: every tooltip draws text
60
+ * (renderMode 'richText'), a link or sublink is kept only for http(s) or mailto, and the data view's title and labels
61
+ * are escaped. Chart applies it to every option; call it yourself before setOption on the instance from onInit. */
62
+ export function safeChartOption<T>(option: T): T;
63
+ /** The value when it is an http(s) or mailto URL, else ''. */
64
+ export function safeHref(value: unknown): string;
65
+
@@ -4,6 +4,8 @@
4
4
  // dependency graph of every consumer, so an app that renders a Button and no chart would have to
5
5
  // resolve a charting library to build. Same rule, same reason, as 'reifyui/slides'.
6
6
  export { Chart } from './Chart.jsx';
7
+ // What the chart does to every option before ECharts sees it, for a host that sets options on the instance itself.
8
+ export { safeChartOption, safeHref } from './safe.js';
7
9
  // The theme as strings a chart option can hold — series colours, axis ink, tooltip chrome. CSS
8
10
  // variables do not reach inside a canvas; these do.
9
11
  export { chartTokens } from './tokens.js';
@@ -0,0 +1,66 @@
1
+ // An ECharts option can come from anywhere (a person, a saved dashboard, an agent writing it), so the chart keeps it
2
+ // inert before ECharts sees it (found 2026-11-10). ECharts writes some option strings into the page as HTML or opens
3
+ // them as URLs:
4
+ // - a tooltip's content (its formatter, series and data names) in the default HTML render mode;
5
+ // - a title's link / sublink and a sunburst or treemap node's link, opened in a window that keeps the page's origin;
6
+ // - the data view's title and button labels (toolbox.feature.dataView.title / lang).
7
+ // So every tooltip draws in the chart itself (renderMode 'richText': text, never markup), a link is kept only when it is
8
+ // http(s) or mailto, and the data view's words are escaped. The option passed in is never changed: plain objects and
9
+ // arrays are copied, anything else (functions, typed arrays, dates, gradient objects) is passed through as it is.
10
+ const SAFE_HREF = /^(https?:\/\/|mailto:)/i;
11
+
12
+ export function safeHref(v) {
13
+ return typeof v === 'string' && SAFE_HREF.test(v.replace(/[\u0000- \u007f]/g, '')) ? v : '';
14
+ }
15
+
16
+ const ESC = { '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' };
17
+ const esc = (s) => String(s).replace(/[&<>"']/g, (c) => ESC[c]);
18
+
19
+ const isPlain = (v) => {
20
+ if (!v || typeof v !== 'object' || Array.isArray(v)) return false;
21
+ const p = Object.getPrototypeOf(v);
22
+ return p === Object.prototype || p === null;
23
+ };
24
+
25
+ export function safeChartOption(option) {
26
+ if (!isPlain(option) && !Array.isArray(option)) return option;
27
+ const seen = new Map();
28
+ const escaped = new Set();
29
+ const walk = (n) => {
30
+ if (Array.isArray(n)) {
31
+ if (seen.has(n)) return seen.get(n);
32
+ const out = []; seen.set(n, out);
33
+ for (const x of n) out.push(walk(x));
34
+ return out;
35
+ }
36
+ if (!isPlain(n)) return n;
37
+ if (seen.has(n)) return seen.get(n);
38
+ const out = {}; seen.set(n, out);
39
+ for (const k of Object.keys(n)) {
40
+ const v = n[k];
41
+ if ((k === 'link' || k === 'sublink') && typeof v === 'string') {
42
+ if (safeHref(v)) out[k] = v;
43
+ } else if (k === 'tooltip') {
44
+ out[k] = tooltip(v);
45
+ } else if (k === 'dataView' && isPlain(v)) {
46
+ out[k] = dataView(walk(v));
47
+ } else {
48
+ out[k] = walk(v);
49
+ }
50
+ }
51
+ return out;
52
+ };
53
+ const tooltip = (v) => {
54
+ if (v === true) return { renderMode: 'richText' };
55
+ if (Array.isArray(v)) return v.map((t) => (isPlain(t) ? { ...walk(t), renderMode: 'richText' } : walk(t)));
56
+ return isPlain(v) ? { ...walk(v), renderMode: 'richText' } : walk(v);
57
+ };
58
+ const dataView = (d) => {
59
+ if (escaped.has(d)) return d; // the same data view reached twice: escape once
60
+ escaped.add(d);
61
+ if (typeof d.title === 'string') d.title = esc(d.title);
62
+ if (Array.isArray(d.lang)) d.lang = d.lang.map((s) => (typeof s === 'string' ? esc(s) : s));
63
+ return d;
64
+ };
65
+ return walk(option);
66
+ }