@adecore/charts 0.39.0 → 0.40.1

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/src/timeline.tsx CHANGED
@@ -103,9 +103,11 @@ const TimelineContext = createContext<TimelineContextValue | null>(null);
103
103
 
104
104
  const useTimeline = (part: string): TimelineContextValue => {
105
105
  const context = useContext(TimelineContext);
106
+
106
107
  if (context === null) {
107
108
  throw new Error(`${part} draws inside a Timeline.Root.`);
108
109
  }
110
+
109
111
  return context;
110
112
  };
111
113
 
@@ -134,10 +136,12 @@ export function TimelineRoot<Span extends TimelineSpan>({
134
136
  }: TimelineRootProps<Span>) {
135
137
  const rows = timelineTree(data);
136
138
  const parents = parentIds(rows);
139
+
137
140
  // Uncontrolled, the root keeps what was folded rather than what is open, so a span that arrives later opens.
138
141
  const [folded, setFolded] = useState<ReadonlySet<string>>(() =>
139
142
  defaultExpanded === undefined ? new Set() : new Set(parents.filter((id) => !defaultExpanded.includes(id)))
140
143
  );
144
+
141
145
  const [ownValue, setOwnValue] = useState<string | null>(defaultValue);
142
146
  const expanded = new Set(expandedProp ?? parents.filter((id) => !folded.has(id)));
143
147
  const value = valueProp === undefined ? ownValue : valueProp;
@@ -146,9 +150,11 @@ export function TimelineRoot<Span extends TimelineSpan>({
146
150
 
147
151
  const colorOfKind = (kind: string | undefined): string => {
148
152
  const index = kind === undefined ? undefined : kindIndex.get(kind);
153
+
149
154
  if (index === undefined) {
150
155
  return colorOf('accent')!;
151
156
  }
157
+
152
158
  return colorOf(kinds[index]!.tone) ?? seriesColor(index);
153
159
  };
154
160
 
@@ -156,22 +162,27 @@ export function TimelineRoot<Span extends TimelineSpan>({
156
162
  if (expandedProp === undefined) {
157
163
  setFolded((current) => {
158
164
  const next = new Set(current);
165
+
159
166
  if (open) {
160
167
  next.delete(id);
161
168
  } else {
162
169
  next.add(id);
163
170
  }
171
+
164
172
  return next;
165
173
  });
166
174
  }
175
+
167
176
  onExpandedChange?.(open ? [...expanded, id] : [...expanded].filter((entry) => entry !== id));
168
177
  };
169
178
 
170
179
  const select = (id: string): void => {
171
180
  const next = value === id ? null : id;
181
+
172
182
  if (valueProp === undefined) {
173
183
  setOwnValue(next);
174
184
  }
185
+
175
186
  onValueChange?.(next);
176
187
  };
177
188
 
@@ -204,9 +215,11 @@ export function TimelineLegend({ className, ref }: TimelineLegendProps) {
204
215
  const timeline = useTimeline('Timeline.Legend');
205
216
  const times = kindTimes(timeline.rows);
206
217
  const shown = timeline.kinds.filter((kind) => (times.get(kind.id) ?? 0) > 0);
218
+
207
219
  if (shown.length === 0) {
208
220
  return null;
209
221
  }
222
+
210
223
  return (
211
224
  <div ref={ref} className={clsx('flex flex-wrap gap-x-4 gap-y-1 border-b border-border-soft px-4 py-2.5 text-xs text-text-muted', className)}>
212
225
  {shown.map((kind) => (
@@ -224,12 +237,14 @@ export function TimelineLegend({ className, ref }: TimelineLegendProps) {
224
237
  export function TimelineAxis({ heading, children, className, ref }: TimelineAxisProps) {
225
238
  const timeline = useTimeline('Timeline.Axis');
226
239
  const { origin, total, ticks } = timeline;
240
+
227
241
  return (
228
242
  <div ref={ref} className={clsx(COLUMNS, className)}>
229
243
  <div className="hidden border-b border-border-soft px-4 py-2 text-xs text-text-muted @xl:block">{heading}</div>
230
244
  <div className="relative mx-4 h-8 border-b border-border-soft @xl:ml-0">
231
245
  {ticks.map((tick, index) => {
232
246
  const left = percentOf(origin + tick, origin, total);
247
+
233
248
  // The first label starts at its tick and one near the end ends at it, so neither hangs past the axis.
234
249
  return (
235
250
  <span
@@ -256,6 +271,7 @@ export function TimelineMoment({ at, label, tone = 'error' }: TimelineMomentProp
256
271
  const timeline = useTimeline('Timeline.Moment');
257
272
  const { t } = useTranslation('charts');
258
273
  const text = t('timeline.moment', { label, offset: timeline.format(Math.max(0, at - timeline.origin)) });
274
+
259
275
  return (
260
276
  <Tooltip label={text} name>
261
277
  <span
@@ -279,21 +295,26 @@ export function TimelineRows<Span extends TimelineSpan>({ label, detail, classNa
279
295
 
280
296
  // The row that takes the tab stop: the one last focused, or the open row that hides it, else the selected or the first.
281
297
  const indexOf = (target: string | null): number => shown.findIndex((row) => row.span.id === target);
298
+
282
299
  let current = indexOf(focusId);
300
+
283
301
  if (current < 0 && focusId !== null) {
284
302
  const hidden = rows.find((row) => row.span.id === focusId);
285
303
  const ancestor = hidden?.ancestors.find((ancestorId) => !expanded.has(ancestorId));
286
304
  current = indexOf(ancestor ?? null);
287
305
  }
306
+
288
307
  if (current < 0) {
289
308
  current = Math.max(0, indexOf(timeline.value));
290
309
  }
291
310
 
292
311
  const focusRow = (box: HTMLElement, index: number): void => {
293
312
  const target = shown[index];
313
+
294
314
  if (target === undefined) {
295
315
  return;
296
316
  }
317
+
297
318
  setFocusId(target.span.id);
298
319
  const node = [...box.querySelectorAll<HTMLElement>('[role="treeitem"]')].find((item) => item.dataset.span === target.span.id);
299
320
  node?.focus();
@@ -304,11 +325,15 @@ export function TimelineRows<Span extends TimelineSpan>({ label, detail, classNa
304
325
  if ((e.target as HTMLElement).getAttribute('role') !== 'treeitem') {
305
326
  return;
306
327
  }
328
+
307
329
  const move = timelineMove(e.key, shown, current, expanded);
330
+
308
331
  if (move === undefined) {
309
332
  return;
310
333
  }
334
+
311
335
  e.preventDefault();
336
+
312
337
  if ('focus' in move) {
313
338
  focusRow(e.currentTarget, move.focus);
314
339
  } else if ('expand' in move) {
@@ -345,6 +370,7 @@ export function TimelineRows<Span extends TimelineSpan>({ label, detail, classNa
345
370
  const color = colorOf(span.tone) ?? timeline.colorOfKind(span.kind);
346
371
  const bar = barOf(span, origin, total);
347
372
  const kind = timeline.kinds.find((entry) => entry.id === span.kind);
373
+
348
374
  const name = [
349
375
  span.label,
350
376
  kind?.label,
@@ -353,8 +379,10 @@ export function TimelineRows<Span extends TimelineSpan>({ label, detail, classNa
353
379
  ]
354
380
  .filter(Boolean)
355
381
  .join(', ');
382
+
356
383
  const detailId = `${id}-${index}-detail`;
357
384
  const shownDetail = selected && detail !== undefined;
385
+
358
386
  return (
359
387
  <div
360
388
  key={span.id}
package/src/use-chart.ts CHANGED
@@ -22,6 +22,7 @@ export interface ChartApi<Row> {
22
22
  export function useChart<Row = unknown>(): ChartApi<Row> {
23
23
  const chart = useChartContext('useChart');
24
24
  const active = chart.activeIndex >= 0 ? chart.activeIndex : null;
25
+
25
26
  return {
26
27
  plot: chart.plot,
27
28
  data: chart.data as Row[],
@@ -0,0 +1,12 @@
1
+ /* A table of values as it is shown; hidden, it stays for a screen reader without any of these. */
2
+ const TABLE_CLASSES = {
3
+ box: 'max-h-80 w-full overflow-auto rounded-md border border-border-soft',
4
+ table: 'w-full border-collapse text-xs tabular-nums',
5
+ head: 'sticky top-0 bg-surface-hover',
6
+ column: 'h-8 px-2 text-right font-medium whitespace-nowrap text-text-muted',
7
+ row: 'border-t border-border-soft',
8
+ rowHeading: 'h-8 px-2 text-left font-normal whitespace-nowrap text-text-muted',
9
+ cell: 'h-8 px-2 text-right whitespace-nowrap text-text'
10
+ };
11
+
12
+ export const tableClasses = (shown: boolean): Partial<typeof TABLE_CLASSES> & { box: string } => (shown ? TABLE_CLASSES : { box: 'sr-only' });