@langchain/macaw-components 1.2.1 → 1.2.2

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.
@@ -1,3 +1,4 @@
1
+ import { cn } from "../../utils/cn.js";
1
2
  import { Text } from "../Text/Text.js";
2
3
  import { getFillChartColorForKey } from "../../utils/chartColors.js";
3
4
  import { BarChart } from "../BarChart/BarChart.js";
@@ -37,7 +38,7 @@ var TopListValueLabel = ({ bar, label, shouldAnimate }) => {
37
38
  children: label
38
39
  });
39
40
  };
40
- var TopListOverlay = ({ categories, bandwidth, innerWidth, innerHeight, getCategoryPosition, getItem, categoryLabelWidth, valueLabelPadding, hasNegativeValues, onItemPointerMove, onItemPointerOut, onItemActivate }) => categories.map((category, index) => {
41
+ var TopListOverlay = ({ categories, bandwidth, innerWidth, innerHeight, getCategoryPosition, getItem, categoryLabelWidth, categoryLabelAlign, valueLabelPadding, hasNegativeValues, onItemPointerMove, onItemPointerOut, onItemActivate }) => categories.map((category, index) => {
41
42
  const item = getItem(category);
42
43
  const position = getCategoryPosition(category);
43
44
  if (item == null || position == null) return null;
@@ -76,7 +77,7 @@ var TopListOverlay = ({ categories, bandwidth, innerWidth, innerHeight, getCateg
76
77
  width: categoryLabelWidth,
77
78
  height: bandwidth,
78
79
  children: /* @__PURE__ */ jsx("div", {
79
- className: "flex size-full min-w-0 items-center justify-end overflow-hidden pr-space-3",
80
+ className: cn("flex size-full min-w-0 items-center overflow-hidden pr-space-3", categoryLabelAlign === "start" ? "justify-start" : "justify-end"),
80
81
  children: /* @__PURE__ */ jsx(Text, {
81
82
  as: "span",
82
83
  variant: "xs",
@@ -90,7 +91,7 @@ var TopListOverlay = ({ categories, bandwidth, innerWidth, innerHeight, getCateg
90
91
  }, `hitbox:${item.id}`);
91
92
  });
92
93
  /** Use for ranked top-K data; adapters own data shaping, tooltips, and navigation. */
93
- var TopList = ({ items, sort = "descending", limit, formatValue = defaultFormatValue, valueAxisLabel, categoryLabelWidth = TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH, activeItemId, getItemAriaLabel, isRendering = true, shouldAnimate = true, onItemPointerMove, onItemPointerOut, onItemActivate, onItemFocus, onItemBlur, className, "aria-label": ariaLabel, ...rest }) => {
94
+ var TopList = ({ items, sort = "descending", limit, formatValue = defaultFormatValue, valueAxisLabel, categoryLabelWidth = TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH, categoryLabelAlign = "end", activeItemId, getItemAriaLabel, isRendering = true, shouldAnimate = true, onItemPointerMove, onItemPointerOut, onItemActivate, onItemFocus, onItemBlur, className, "aria-label": ariaLabel, ...rest }) => {
94
95
  const sortedItems = sortTopListItems(items.filter((item) => Number.isFinite(item.value)), sort);
95
96
  const visibleItems = limitTopListItems(sortedItems, limit);
96
97
  const itemById = new Map(visibleItems.map((item) => [item.id, item]));
@@ -163,6 +164,7 @@ var TopList = ({ items, sort = "descending", limit, formatValue = defaultFormatV
163
164
  ...slotProps,
164
165
  getItem,
165
166
  categoryLabelWidth: resolvedCategoryLabelWidth,
167
+ categoryLabelAlign,
166
168
  valueLabelPadding,
167
169
  hasNegativeValues,
168
170
  onItemPointerMove,
@@ -1 +1 @@
1
- {"version":3,"file":"TopList.js","names":[],"sources":["../../../src/components/TopList/TopList.tsx"],"sourcesContent":["import { animated, useReducedMotion, useSpring } from '@react-spring/web';\nimport { getStringWidth } from '@visx/text';\n\nimport { getFillChartColorForKey } from '../../utils/chartColors';\nimport {\n BarChart,\n type BarChartCategory,\n type BarChartInteractionDatum,\n type BarChartPlotSlotProps,\n type BarChartRenderedBar,\n type BarChartSeries,\n} from '../BarChart';\nimport { Text } from '../Text';\nimport type { TopListItem, TopListProps } from './TopList.types';\nimport { limitTopListItems, sortTopListItems } from './TopList.utils';\n\nconst TOP_LIST_SERIES_ID = 'top-list-values';\nconst TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH = 112;\nconst TOP_LIST_VALUE_LABEL_GAP = 8;\nconst TOP_LIST_VALUE_LABEL_FONT_SIZE = 12;\nconst numberFormatter = new Intl.NumberFormat('en-US', {\n maximumFractionDigits: 2,\n});\n\nconst defaultFormatValue = (value: number) => numberFormatter.format(value);\n\nconst TopListValueLabel = ({\n bar,\n label,\n shouldAnimate,\n}: {\n bar: BarChartRenderedBar;\n label: string;\n shouldAnimate: boolean;\n}) => {\n const isNegative = bar.value < 0;\n const isReducedMotion = (useReducedMotion() ?? false) || !shouldAnimate;\n const targetX = isNegative\n ? bar.x - TOP_LIST_VALUE_LABEL_GAP\n : bar.x + bar.width + TOP_LIST_VALUE_LABEL_GAP;\n const targetY = bar.y + bar.height / 2;\n const { x, y } = useSpring({\n from: {\n x:\n bar.baselinePosition +\n (isNegative ? -TOP_LIST_VALUE_LABEL_GAP : TOP_LIST_VALUE_LABEL_GAP),\n y: targetY,\n },\n to: { x: targetX, y: targetY },\n immediate: isReducedMotion,\n });\n\n return (\n <animated.text\n x={x}\n y={y}\n dominantBaseline=\"middle\"\n textAnchor={isNegative ? 'end' : 'start'}\n className=\"fill-current font-mono text-xs text-secondary\"\n >\n {label}\n </animated.text>\n );\n};\n\ntype TopListOverlayProps<Item extends TopListItem> = BarChartPlotSlotProps &\n Pick<\n TopListProps<Item>,\n 'onItemActivate' | 'onItemPointerMove' | 'onItemPointerOut'\n > & {\n getItem: (category: BarChartCategory) => Item | undefined;\n categoryLabelWidth: number;\n valueLabelPadding: number;\n hasNegativeValues: boolean;\n };\n\nconst TopListOverlay = <Item extends TopListItem>({\n categories,\n bandwidth,\n innerWidth,\n innerHeight,\n getCategoryPosition,\n getItem,\n categoryLabelWidth,\n valueLabelPadding,\n hasNegativeValues,\n onItemPointerMove,\n onItemPointerOut,\n onItemActivate,\n}: TopListOverlayProps<Item>) =>\n categories.map((category, index) => {\n const item = getItem(category);\n const position = getCategoryPosition(category);\n if (item == null || position == null) return null;\n\n const previousPosition =\n index === 0 ? undefined : getCategoryPosition(categories[index - 1]);\n const nextPosition =\n index === categories.length - 1\n ? undefined\n : getCategoryPosition(categories[index + 1]);\n const top =\n previousPosition == null ? 0 : (previousPosition + position) / 2;\n const bottom =\n nextPosition == null ? innerHeight : (position + nextPosition) / 2;\n const leftPadding = hasNegativeValues ? valueLabelPadding : 0;\n const isInteractive = onItemPointerMove != null || onItemActivate != null;\n\n let cursorClassName: string | undefined;\n if (onItemActivate != null) {\n cursorClassName = 'cursor-pointer';\n } else if (onItemPointerMove != null) {\n cursorClassName = 'cursor-crosshair';\n }\n\n return (\n <g\n key={`hitbox:${item.id}`}\n className={cursorClassName}\n onPointerMove={\n onItemPointerMove == null\n ? undefined\n : (event) => {\n event.stopPropagation();\n onItemPointerMove(item, event);\n }\n }\n onPointerLeave={onItemPointerOut}\n onPointerUp={\n onItemActivate == null\n ? undefined\n : (event) => {\n if (event.button !== 0) return;\n event.stopPropagation();\n onItemActivate(item, event);\n }\n }\n >\n {isInteractive && (\n <rect\n x={-categoryLabelWidth - leftPadding}\n y={top}\n width={\n categoryLabelWidth + leftPadding + innerWidth + valueLabelPadding\n }\n height={Math.max(0, bottom - top)}\n fill=\"transparent\"\n pointerEvents=\"all\"\n aria-hidden=\"true\"\n />\n )}\n <foreignObject\n x={-categoryLabelWidth - leftPadding}\n y={position - bandwidth / 2}\n width={categoryLabelWidth}\n height={bandwidth}\n >\n <div className=\"flex size-full min-w-0 items-center justify-end overflow-hidden pr-space-3\">\n <Text\n as=\"span\"\n variant=\"xs\"\n color=\"primary\"\n className=\"block max-w-full truncate\"\n title={item.label}\n >\n {item.label}\n </Text>\n </div>\n </foreignObject>\n </g>\n );\n });\n\n/** Use for ranked top-K data; adapters own data shaping, tooltips, and navigation. */\nexport const TopList = <Item extends TopListItem>({\n items,\n sort = 'descending',\n limit,\n formatValue = defaultFormatValue,\n valueAxisLabel,\n categoryLabelWidth = TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH,\n activeItemId,\n getItemAriaLabel,\n isRendering = true,\n shouldAnimate = true,\n onItemPointerMove,\n onItemPointerOut,\n onItemActivate,\n onItemFocus,\n onItemBlur,\n className,\n 'aria-label': ariaLabel,\n ...rest\n}: TopListProps<Item>) => {\n const sortedItems = sortTopListItems(\n items.filter((item) => Number.isFinite(item.value)),\n sort\n );\n const visibleItems = limitTopListItems(sortedItems, limit);\n const itemById = new Map(visibleItems.map((item) => [item.id, item]));\n const formattedValueById = new Map(\n visibleItems.map((item) => [item.id, formatValue(item.value)])\n );\n const resolvedCategoryLabelWidth = Number.isFinite(categoryLabelWidth)\n ? Math.max(0, categoryLabelWidth)\n : TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH;\n const valueLabelPadding = Math.ceil(\n TOP_LIST_VALUE_LABEL_GAP +\n Math.max(\n 0,\n ...Array.from(formattedValueById.values(), (value) =>\n Math.max(\n getStringWidth(value, {\n fontSize: `${TOP_LIST_VALUE_LABEL_FONT_SIZE}px`,\n }) ?? 0,\n value.length * 7\n )\n )\n )\n );\n const values = sortedItems.map((item) => item.value);\n const valueDomain: readonly [number, number] | undefined =\n values.length === 0\n ? undefined\n : [Math.min(0, ...values), Math.max(0, ...values)];\n const hasNegativeValues = (valueDomain?.[0] ?? 0) < 0;\n const series: readonly BarChartSeries[] = [\n {\n id: TOP_LIST_SERIES_ID,\n label: valueAxisLabel ?? 'Value',\n ariaLabel: valueAxisLabel ?? 'Values',\n data: visibleItems.map((item) => ({\n id: item.id,\n category: item.id,\n value: item.value,\n color: item.color ?? getFillChartColorForKey(item.id),\n })),\n },\n ];\n const getItem = (category: BarChartCategory) =>\n itemById.get(String(category));\n const getDatumItem = (datum: BarChartInteractionDatum) =>\n getItem(datum.category);\n const mapDatumHandlerToItem = <Event,>(\n handler: ((item: Item, event: Event) => void) | undefined\n ) => {\n if (!isRendering || handler == null) return undefined;\n\n return (datum: BarChartInteractionDatum, event: Event) => {\n const item = getDatumItem(datum);\n if (item != null) handler(item, event);\n };\n };\n\n return (\n <BarChart\n {...rest}\n aria-label={ariaLabel}\n className={className}\n orientation=\"horizontal\"\n mode=\"grouped\"\n series={series}\n showLegend={false}\n activeBarId={activeItemId}\n isRendering={isRendering}\n shouldAnimate={shouldAnimate}\n minimumPlotWidth={0}\n categoryPadding={{ inner: 0.24, outer: 0.24 }}\n categoryAxis={{\n domain: visibleItems.map((item) => item.id),\n thickness: resolvedCategoryLabelWidth,\n }}\n valueAxes={[\n {\n id: TOP_LIST_SERIES_ID,\n label: valueAxisLabel,\n domain: valueDomain,\n nice: true,\n formatValue,\n },\n ]}\n grid={{ value: true }}\n plotPadding={{\n left: hasNegativeValues ? valueLabelPadding : 0,\n right: valueLabelPadding,\n }}\n slots={{\n categoryAxis: () => null,\n barLabel: (bar) => (\n <TopListValueLabel\n key={`${bar.id}:${bar.startValue}:${bar.endValue}`}\n bar={bar}\n label={formattedValueById.get(bar.id) ?? formatValue(bar.value)}\n shouldAnimate={shouldAnimate}\n />\n ),\n overlay: (slotProps) =>\n isRendering ? (\n <TopListOverlay\n {...slotProps}\n getItem={getItem}\n categoryLabelWidth={resolvedCategoryLabelWidth}\n valueLabelPadding={valueLabelPadding}\n hasNegativeValues={hasNegativeValues}\n onItemPointerMove={onItemPointerMove}\n onItemPointerOut={onItemPointerOut}\n onItemActivate={onItemActivate}\n />\n ) : null,\n }}\n getCategoryAriaLabel={\n !isRendering\n ? undefined\n : (datum) => {\n const item = getDatumItem(datum);\n if (item == null) return undefined;\n return (\n getItemAriaLabel?.(item) ??\n `${item.label}: ${formattedValueById.get(item.id) ?? formatValue(item.value)}`\n );\n }\n }\n onDatumActivate={mapDatumHandlerToItem(onItemActivate)}\n onDatumFocus={mapDatumHandlerToItem(onItemFocus)}\n onDatumBlur={mapDatumHandlerToItem(onItemBlur)}\n />\n );\n};\n"],"mappings":";;;;;;;;AAgBA,IAAM,qBAAqB;AAC3B,IAAM,wCAAwC;AAC9C,IAAM,2BAA2B;AACjC,IAAM,iCAAiC;AACvC,IAAM,kBAAkB,IAAI,KAAK,aAAa,SAAS,EACrD,uBAAuB,EACzB,CAAC;AAED,IAAM,sBAA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S,KAAK,EAAE;GAC1C,WAAW;EACb;EACA,WAAW,CACT;GACE,IAAI;GACJ,OAAO;GACP,QAAQ;GACR,MAAM;GACN;EACF,CACF;EACA,MAAM,EAAE,OAAO,KAAK;EACpB,aAAa;GACX,MAAM,oBAAoB,oBAAoB;GAC9C,OAAO;EACT;EACA,OAAO;GACL,oBAAoB;GACpB,WAAW,QACT,oBAAC,mBAAD;IAEO;IACL,OAAO,mBAAmB,IAAI,IAAI,EAAE,KAAK,YAAY,IAAI,KAAK;IAC/C;GAChB,GAJM,GAAG,IAAI,GAAG,GAAG,IAAI,WAAW,GAAG,IAAI,UAIzC;GAEH,UAAU,cACR,cACE,oBAAC,gBAAD;IACE,GAAI;IACK;IACT,oBAAoB;IACD;IACA;IACA;IACD;IACF;GACjB,CAAA,IACC;EACR;EACA,sBACE,CAAC,cACG,KAAA,KACC,UAAU;GACT,MAAM,OAAO,aAAa,KAAK;GAC/B,IAAI,QAAQ,MAAM,OAAO,KAAA;GACzB,OACE,mBAAmB,IAAI,KACvB,GAAG,KAAK,MAAM,IAAI,mBAAmB,IAAI,KAAK,EAAE,KAAK,YAAY,KAAK,KAAK;EAE/E;EAEN,iBAAiB,sBAAsB,cAAc;EACrD,cAAc,sBAAsB,WAAW;EAC/C,aAAa,sBAAsB,UAAU;CAC9C,CAAA;AAEL"}
1
+ {"version":3,"file":"TopList.js","names":[],"sources":["../../../src/components/TopList/TopList.tsx"],"sourcesContent":["import { animated, useReducedMotion, useSpring } from '@react-spring/web';\nimport { getStringWidth } from '@visx/text';\n\nimport { getFillChartColorForKey } from '../../utils/chartColors';\nimport { cn } from '../../utils/cn';\nimport {\n BarChart,\n type BarChartCategory,\n type BarChartInteractionDatum,\n type BarChartPlotSlotProps,\n type BarChartRenderedBar,\n type BarChartSeries,\n} from '../BarChart';\nimport { Text } from '../Text';\nimport type { TopListItem, TopListProps } from './TopList.types';\nimport { limitTopListItems, sortTopListItems } from './TopList.utils';\n\nconst TOP_LIST_SERIES_ID = 'top-list-values';\nconst TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH = 112;\nconst TOP_LIST_VALUE_LABEL_GAP = 8;\nconst TOP_LIST_VALUE_LABEL_FONT_SIZE = 12;\nconst numberFormatter = new Intl.NumberFormat('en-US', {\n maximumFractionDigits: 2,\n});\n\nconst defaultFormatValue = (value: number) => numberFormatter.format(value);\n\nconst TopListValueLabel = ({\n bar,\n label,\n shouldAnimate,\n}: {\n bar: BarChartRenderedBar;\n label: string;\n shouldAnimate: boolean;\n}) => {\n const isNegative = bar.value < 0;\n const isReducedMotion = (useReducedMotion() ?? false) || !shouldAnimate;\n const targetX = isNegative\n ? bar.x - TOP_LIST_VALUE_LABEL_GAP\n : bar.x + bar.width + TOP_LIST_VALUE_LABEL_GAP;\n const targetY = bar.y + bar.height / 2;\n const { x, y } = useSpring({\n from: {\n x:\n bar.baselinePosition +\n (isNegative ? -TOP_LIST_VALUE_LABEL_GAP : TOP_LIST_VALUE_LABEL_GAP),\n y: targetY,\n },\n to: { x: targetX, y: targetY },\n immediate: isReducedMotion,\n });\n\n return (\n <animated.text\n x={x}\n y={y}\n dominantBaseline=\"middle\"\n textAnchor={isNegative ? 'end' : 'start'}\n className=\"fill-current font-mono text-xs text-secondary\"\n >\n {label}\n </animated.text>\n );\n};\n\ntype TopListOverlayProps<Item extends TopListItem> = BarChartPlotSlotProps &\n Pick<\n TopListProps<Item>,\n 'onItemActivate' | 'onItemPointerMove' | 'onItemPointerOut'\n > & {\n getItem: (category: BarChartCategory) => Item | undefined;\n categoryLabelWidth: number;\n categoryLabelAlign: 'start' | 'end';\n valueLabelPadding: number;\n hasNegativeValues: boolean;\n };\n\nconst TopListOverlay = <Item extends TopListItem>({\n categories,\n bandwidth,\n innerWidth,\n innerHeight,\n getCategoryPosition,\n getItem,\n categoryLabelWidth,\n categoryLabelAlign,\n valueLabelPadding,\n hasNegativeValues,\n onItemPointerMove,\n onItemPointerOut,\n onItemActivate,\n}: TopListOverlayProps<Item>) =>\n categories.map((category, index) => {\n const item = getItem(category);\n const position = getCategoryPosition(category);\n if (item == null || position == null) return null;\n\n const previousPosition =\n index === 0 ? undefined : getCategoryPosition(categories[index - 1]);\n const nextPosition =\n index === categories.length - 1\n ? undefined\n : getCategoryPosition(categories[index + 1]);\n const top =\n previousPosition == null ? 0 : (previousPosition + position) / 2;\n const bottom =\n nextPosition == null ? innerHeight : (position + nextPosition) / 2;\n const leftPadding = hasNegativeValues ? valueLabelPadding : 0;\n const isInteractive = onItemPointerMove != null || onItemActivate != null;\n\n let cursorClassName: string | undefined;\n if (onItemActivate != null) {\n cursorClassName = 'cursor-pointer';\n } else if (onItemPointerMove != null) {\n cursorClassName = 'cursor-crosshair';\n }\n\n return (\n <g\n key={`hitbox:${item.id}`}\n className={cursorClassName}\n onPointerMove={\n onItemPointerMove == null\n ? undefined\n : (event) => {\n event.stopPropagation();\n onItemPointerMove(item, event);\n }\n }\n onPointerLeave={onItemPointerOut}\n onPointerUp={\n onItemActivate == null\n ? undefined\n : (event) => {\n if (event.button !== 0) return;\n event.stopPropagation();\n onItemActivate(item, event);\n }\n }\n >\n {isInteractive && (\n <rect\n x={-categoryLabelWidth - leftPadding}\n y={top}\n width={\n categoryLabelWidth + leftPadding + innerWidth + valueLabelPadding\n }\n height={Math.max(0, bottom - top)}\n fill=\"transparent\"\n pointerEvents=\"all\"\n aria-hidden=\"true\"\n />\n )}\n <foreignObject\n x={-categoryLabelWidth - leftPadding}\n y={position - bandwidth / 2}\n width={categoryLabelWidth}\n height={bandwidth}\n >\n <div\n className={cn(\n 'flex size-full min-w-0 items-center overflow-hidden pr-space-3',\n categoryLabelAlign === 'start' ? 'justify-start' : 'justify-end'\n )}\n >\n <Text\n as=\"span\"\n variant=\"xs\"\n color=\"primary\"\n className=\"block max-w-full truncate\"\n title={item.label}\n >\n {item.label}\n </Text>\n </div>\n </foreignObject>\n </g>\n );\n });\n\n/** Use for ranked top-K data; adapters own data shaping, tooltips, and navigation. */\nexport const TopList = <Item extends TopListItem>({\n items,\n sort = 'descending',\n limit,\n formatValue = defaultFormatValue,\n valueAxisLabel,\n categoryLabelWidth = TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH,\n categoryLabelAlign = 'end',\n activeItemId,\n getItemAriaLabel,\n isRendering = true,\n shouldAnimate = true,\n onItemPointerMove,\n onItemPointerOut,\n onItemActivate,\n onItemFocus,\n onItemBlur,\n className,\n 'aria-label': ariaLabel,\n ...rest\n}: TopListProps<Item>) => {\n const sortedItems = sortTopListItems(\n items.filter((item) => Number.isFinite(item.value)),\n sort\n );\n const visibleItems = limitTopListItems(sortedItems, limit);\n const itemById = new Map(visibleItems.map((item) => [item.id, item]));\n const formattedValueById = new Map(\n visibleItems.map((item) => [item.id, formatValue(item.value)])\n );\n const resolvedCategoryLabelWidth = Number.isFinite(categoryLabelWidth)\n ? Math.max(0, categoryLabelWidth)\n : TOP_LIST_DEFAULT_CATEGORY_LABEL_WIDTH;\n const valueLabelPadding = Math.ceil(\n TOP_LIST_VALUE_LABEL_GAP +\n Math.max(\n 0,\n ...Array.from(formattedValueById.values(), (value) =>\n Math.max(\n getStringWidth(value, {\n fontSize: `${TOP_LIST_VALUE_LABEL_FONT_SIZE}px`,\n }) ?? 0,\n value.length * 7\n )\n )\n )\n );\n const values = sortedItems.map((item) => item.value);\n const valueDomain: readonly [number, number] | undefined =\n values.length === 0\n ? undefined\n : [Math.min(0, ...values), Math.max(0, ...values)];\n const hasNegativeValues = (valueDomain?.[0] ?? 0) < 0;\n const series: readonly BarChartSeries[] = [\n {\n id: TOP_LIST_SERIES_ID,\n label: valueAxisLabel ?? 'Value',\n ariaLabel: valueAxisLabel ?? 'Values',\n data: visibleItems.map((item) => ({\n id: item.id,\n category: item.id,\n value: item.value,\n color: item.color ?? getFillChartColorForKey(item.id),\n })),\n },\n ];\n const getItem = (category: BarChartCategory) =>\n itemById.get(String(category));\n const getDatumItem = (datum: BarChartInteractionDatum) =>\n getItem(datum.category);\n const mapDatumHandlerToItem = <Event,>(\n handler: ((item: Item, event: Event) => void) | undefined\n ) => {\n if (!isRendering || handler == null) return undefined;\n\n return (datum: BarChartInteractionDatum, event: Event) => {\n const item = getDatumItem(datum);\n if (item != null) handler(item, event);\n };\n };\n\n return (\n <BarChart\n {...rest}\n aria-label={ariaLabel}\n className={className}\n orientation=\"horizontal\"\n mode=\"grouped\"\n series={series}\n showLegend={false}\n activeBarId={activeItemId}\n isRendering={isRendering}\n shouldAnimate={shouldAnimate}\n minimumPlotWidth={0}\n categoryPadding={{ inner: 0.24, outer: 0.24 }}\n categoryAxis={{\n domain: visibleItems.map((item) => item.id),\n thickness: resolvedCategoryLabelWidth,\n }}\n valueAxes={[\n {\n id: TOP_LIST_SERIES_ID,\n label: valueAxisLabel,\n domain: valueDomain,\n nice: true,\n formatValue,\n },\n ]}\n grid={{ value: true }}\n plotPadding={{\n left: hasNegativeValues ? valueLabelPadding : 0,\n right: valueLabelPadding,\n }}\n slots={{\n categoryAxis: () => null,\n barLabel: (bar) => (\n <TopListValueLabel\n key={`${bar.id}:${bar.startValue}:${bar.endValue}`}\n bar={bar}\n label={formattedValueById.get(bar.id) ?? formatValue(bar.value)}\n shouldAnimate={shouldAnimate}\n />\n ),\n overlay: (slotProps) =>\n isRendering ? (\n <TopListOverlay\n {...slotProps}\n getItem={getItem}\n categoryLabelWidth={resolvedCategoryLabelWidth}\n categoryLabelAlign={categoryLabelAlign}\n valueLabelPadding={valueLabelPadding}\n hasNegativeValues={hasNegativeValues}\n onItemPointerMove={onItemPointerMove}\n onItemPointerOut={onItemPointerOut}\n onItemActivate={onItemActivate}\n />\n ) : null,\n }}\n getCategoryAriaLabel={\n !isRendering\n ? undefined\n : (datum) => {\n const item = getDatumItem(datum);\n if (item == null) return undefined;\n return (\n getItemAriaLabel?.(item) ??\n `${item.label}: ${formattedValueById.get(item.id) ?? formatValue(item.value)}`\n );\n }\n }\n onDatumActivate={mapDatumHandlerToItem(onItemActivate)}\n onDatumFocus={mapDatumHandlerToItem(onItemF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C,UAAiB;GACxD,MAAM,OAAO,aAAa,KAAK;GAC/B,IAAI,QAAQ,MAAM,QAAQ,MAAM,KAAK;EACvC;CACF;CAEA,OACE,oBAAC,UAAD;EACE,GAAI;EACJ,cAAY;EACD;EACX,aAAY;EACZ,MAAK;EACG;EACR,YAAY;EACZ,aAAa;EACA;EACE;EACf,kBAAkB;EAClB,iBAAiB;GAAE,OAAO;GAAM,OAAO;EAAK;EAC5C,cAAc;GACZ,QAAQ,aAAa,KAAK,SAAS,KAAK,EAAE;GAC1C,WAAW;EACb;EACA,WAAW,CACT;GACE,IAAI;GACJ,OAAO;GACP,QAAQ;GACR,MAAM;GACN;EACF,CACF;EACA,MAAM,EAAE,OAAO,KAAK;EACpB,aAAa;GACX,MAAM,oBAAoB,oBAAoB;GAC9C,OAAO;EACT;EACA,OAAO;GACL,oBAAoB;GACpB,WAAW,QACT,oBAAC,mBAAD;IAEO;IACL,OAAO,mBAAmB,IAAI,IAAI,EAAE,KAAK,YAAY,IAAI,KAAK;IAC/C;GAChB,GAJM,GAAG,IAAI,GAAG,GAAG,IAAI,WAAW,GAAG,IAAI,UAIzC;GAEH,UAAU,cACR,cACE,oBAAC,gBAAD;IACE,GAAI;IACK;IACT,oBAAoB;IACA;IACD;IACA;IACA;IACD;IACF;GACjB,CAAA,IACC;EACR;EACA,sBACE,CAAC,cACG,KAAA,KACC,UAAU;GACT,MAAM,OAAO,aAAa,KAAK;GAC/B,IAAI,QAAQ,MAAM,OAAO,KAAA;GACzB,OACE,mBAAmB,IAAI,KACvB,GAAG,KAAK,MAAM,IAAI,mBAAmB,IAAI,KAAK,EAAE,KAAK,YAAY,KAAK,KAAK;EAE/E;EAEN,iBAAiB,sBAAsB,cAAc;EACrD,cAAc,sBAAsB,WAAW;EAC/C,aAAa,sBAAsB,UAAU;CAC9C,CAAA;AAEL"}
@@ -18,6 +18,8 @@ export type TopListProps<Item extends TopListItem = TopListItem> = Omit<HTMLAttr
18
18
  valueAxisLabel?: string;
19
19
  /** Reserved width for category labels, in pixels. Defaults to 112. */
20
20
  categoryLabelWidth?: number;
21
+ /** Aligns labels within the category-label column. Defaults to end. */
22
+ categoryLabelAlign?: 'start' | 'end';
21
23
  activeItemId?: string | null;
22
24
  /** Defaults to `<label>: <formatted value>`. */
23
25
  getItemAriaLabel?: (item: Item) => string;
package/docs/DESIGN.md CHANGED
@@ -133,6 +133,8 @@ Choose color by meaning:
133
133
 
134
134
  Chart SVG `fill` and `stroke` are the exception to Tailwind-only color usage. Import semantic values from `@langchain/macaw-components/utils/chartColors`; do not use raw visualization variables or literals.
135
135
 
136
+ Import `LineChart` from `@langchain/macaw-components/LineChart` for series over time. Keep the default `line` variant to compare how individual series change. Use `variant="stacked-area"` to show a total and each series' contribution, and only for same-unit values that add up, such as counts; never stack averages, rates, latency, or percentiles. See **Line Charts** in [`STYLES.md`](./STYLES.md).
137
+
136
138
  Import `SankeyChart` from `@langchain/macaw-components/SankeyChart` for weighted flow diagrams. Compose it with `ChartCard`, `ChartLegend`, and `ChartTooltip`; consumers own data, value formatting, tooltips, and navigation.
137
139
 
138
140
  Import `HeatmapChart` and `HeatmapBlock` from `@langchain/macaw-components/HeatmapChart` for percentage matrices. Supply `getBlockLabel` for meaningful row and column names; cells expose details on hover and keyboard focus. The matrix has one Tab stop; arrow keys move between cells, Home/End move within a row, and Ctrl+Home/End move to the first/last cell.
package/docs/STYLES.md CHANGED
@@ -197,6 +197,34 @@ dashboard visualization shells.
197
197
  reordering; `variant="full-width"` hides the move handle.
198
198
  - Use `expandButtonProps` and `menuItems` for optional, feature-owned actions.
199
199
 
200
+ ## Line Charts
201
+
202
+ Use `LineChart` from `@langchain/macaw-components/LineChart` for numeric series
203
+ over time or ordered buckets. Choose `variant` by the question the chart answers:
204
+
205
+ | `variant` | Answers | Example values |
206
+ | ---------------- | ------------------------------------------------------ | ---------------------------------------------- |
207
+ | `line` (default) | How does each series change, and how do they compare? | Per-category trends, latency, error rate |
208
+ | `stacked-area` | How large is the total, and what does each series add? | Request counts by environment, cost by service |
209
+
210
+ - Stack only series with the same unit whose values add up to a meaningful
211
+ total, such as counts, volumes, or costs. Keep `line` for averages, rates,
212
+ ratios, latency, percentiles, and scores; their stacked height means nothing.
213
+ - Series stack per y axis in input order, with the first series at the bottom.
214
+ Positive values stack up from zero and negative values stack down. A missing
215
+ or null value adds nothing to its stack. Legend selection restacks only the
216
+ selected series.
217
+ - Areas are solid fills from the categorical fill palette. Leave `color` unset
218
+ or pass a categorical line color, and the chart switches it to the
219
+ hue-matched fill. Any other color is used as given. Per-series stroke width
220
+ and dash patterns do not apply.
221
+ - To offer both views of the same data, pass the same series and switch
222
+ `variant`. Each series keeps its hue, and legend, crosshair, points, markers,
223
+ and bands behave the same.
224
+ - Pointer callbacks report each series' own value and its area color, not its
225
+ stacked height. Show those values in the tooltip with one `ChartTooltipRow`
226
+ per series, and add a `variant="total"` row when the total matters.
227
+
200
228
  ## Top Lists
201
229
 
202
230
  Use `TopList` from `@langchain/macaw-components/TopList` for ranked top-K
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/macaw-components",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "React components and usage guidance for the Macaw Design System.",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -582,7 +582,7 @@
582
582
  "@fontsource/fira-code": "^5.2.7",
583
583
  "@fontsource/inter": "^5.2.8",
584
584
  "@headlessui/react": "^2.2.10",
585
- "@langchain/macaw-tokens": "1.2.1",
585
+ "@langchain/macaw-tokens": "1.2.2",
586
586
  "@lezer/common": "^1.2.1",
587
587
  "@lezer/highlight": "^1.2.0",
588
588
  "@lezer/yaml": "^1.0.4",