frappe-ui 1.0.2 → 1.1.0

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.
Files changed (96) hide show
  1. package/experimental/Calendar/calendarUtils.ts +33 -21
  2. package/experimental/MultiEmailInput/MultiEmailInput.vue +64 -12
  3. package/experimental/MultiEmailInput/types.ts +12 -3
  4. package/experimental/Stepper/StepIndicator.vue +129 -0
  5. package/experimental/Stepper/Stepper.api.md +117 -0
  6. package/experimental/Stepper/Stepper.md +95 -0
  7. package/experimental/Stepper/Stepper.vue +82 -0
  8. package/experimental/Stepper/StepperHorizontal.vue +93 -0
  9. package/experimental/Stepper/StepperVertical.vue +291 -0
  10. package/experimental/Stepper/index.ts +11 -0
  11. package/experimental/Stepper/resolve.ts +87 -0
  12. package/experimental/Stepper/types.ts +89 -0
  13. package/experimental.ts +2 -0
  14. package/package.json +1 -1
  15. package/src/charts/CalendarHeatmapChart.vue +353 -0
  16. package/src/charts/DonutChart.vue +34 -58
  17. package/src/charts/HeatmapChart.vue +26 -64
  18. package/src/charts/NumberCard.vue +62 -7
  19. package/src/charts/PercentageBarChart.vue +243 -0
  20. package/src/charts/SankeyChart.vue +14 -11
  21. package/src/charts/ScatterChart.vue +24 -20
  22. package/src/charts/axisChartCommon.ts +182 -18
  23. package/src/charts/axisFormat.ts +8 -3
  24. package/src/charts/calendarHeatmapOptions.ts +604 -0
  25. package/src/charts/colorMath.ts +26 -0
  26. package/src/charts/components/ChartContainer.vue +63 -53
  27. package/src/charts/components/ChartLegend.vue +216 -24
  28. package/src/charts/components/ChartScale.vue +85 -0
  29. package/src/charts/core/dataLabelFit.ts +45 -6
  30. package/src/charts/core/useAxisChart.ts +36 -19
  31. package/src/charts/core/useChart.ts +6 -1
  32. package/src/charts/core/usePlotKeyboard.ts +13 -2
  33. package/src/charts/docs/BarChart.md +11 -3
  34. package/src/charts/docs/CalendarHeatmapChart.api.md +165 -0
  35. package/src/charts/docs/CalendarHeatmapChart.md +33 -0
  36. package/src/charts/docs/ChartContainer.api.md +6 -0
  37. package/src/charts/docs/ChartLegend.api.md +1 -1
  38. package/src/charts/docs/ChartLegend.md +25 -3
  39. package/src/charts/docs/DonutChart.api.md +12 -0
  40. package/src/charts/docs/DonutChart.md +17 -0
  41. package/src/charts/docs/HeatmapChart.api.md +1 -1
  42. package/src/charts/docs/LineChart.md +5 -3
  43. package/src/charts/docs/NumberCard.api.md +6 -0
  44. package/src/charts/docs/NumberCard.md +6 -0
  45. package/src/charts/docs/Overview.md +13 -6
  46. package/src/charts/docs/PercentageBarChart.api.md +148 -0
  47. package/src/charts/docs/PercentageBarChart.md +42 -0
  48. package/src/charts/docs/ScatterChart.md +2 -3
  49. package/src/charts/donutChartOptions.ts +22 -130
  50. package/src/charts/format.ts +4 -1
  51. package/src/charts/heatmapOptions.ts +17 -12
  52. package/src/charts/hiddenSeries.ts +8 -3
  53. package/src/charts/index.ts +12 -0
  54. package/src/charts/legendOverflow.ts +40 -0
  55. package/src/charts/numberCardFormat.ts +19 -13
  56. package/src/charts/referenceLines.ts +2 -1
  57. package/src/charts/scatterOptions.ts +16 -3
  58. package/src/charts/seriesData.ts +8 -3
  59. package/src/charts/slices.ts +228 -0
  60. package/src/charts/tokens.ts +99 -0
  61. package/src/charts/types.ts +318 -9
  62. package/src/components/Avatar/Avatar.md +27 -2
  63. package/src/components/Avatar/Avatar.vue +29 -12
  64. package/src/components/Combobox/Combobox.vue +9 -2
  65. package/src/components/Combobox/ComboboxResults.vue +6 -1
  66. package/src/components/Combobox/utils.ts +2 -1
  67. package/src/components/Dialog/Dialog.md +3 -1
  68. package/src/components/Duration/Duration.api.md +16 -3
  69. package/src/components/Duration/Duration.md +21 -0
  70. package/src/components/Duration/Duration.vue +29 -8
  71. package/src/components/Duration/duration.ts +105 -44
  72. package/src/components/Duration/index.ts +1 -0
  73. package/src/components/Duration/types.ts +30 -1
  74. package/src/components/Menu/utils.ts +3 -2
  75. package/src/components/MultiSelect/MultiSelect.api.md +5 -0
  76. package/src/components/MultiSelect/MultiSelect.vue +34 -0
  77. package/src/components/MultiSelect/MultiSelectResults.vue +3 -4
  78. package/src/components/MultiSelect/types.ts +6 -0
  79. package/src/components/Select/Select.vue +4 -4
  80. package/src/components/Select/utils.ts +1 -0
  81. package/src/components/Switch/Switch.vue +3 -3
  82. package/src/components/shared/selection/utils.ts +8 -1
  83. package/src/data-fetching/useCall/useCall.ts +25 -26
  84. package/src/data-fetching/useFrappeFetch.ts +48 -2
  85. package/src/data-fetching/useList/useList.ts +11 -3
  86. package/src/data-fetching/utils.ts +15 -1
  87. package/src/molecules/editor/components/MediaNodeView.vue +10 -1
  88. package/src/molecules/editor/components/MediaResizeGuide.vue +71 -0
  89. package/src/molecules/editor/components/media-node-view-utils.ts +7 -0
  90. package/src/molecules/editor/composables/useNodeViewResize.ts +142 -10
  91. package/src/molecules/editor/extensions/iframe/IframeNodeView.vue +3 -1
  92. package/src/molecules/editor/useEditor.ts +12 -6
  93. package/src/utils/dialog.ts +37 -14
  94. package/tailwind/content.js +1 -0
  95. package/vite/README.md +5 -3
  96. package/vite/codeLanguages.js +41 -17
@@ -2,7 +2,6 @@ import { dayjs } from '#utils/dayjs'
2
2
  import {
3
3
  estimateTextWidth,
4
4
  formatAxisValue,
5
- formatLabel,
6
5
  formatTimeAxisLabel,
7
6
  formatValue,
8
7
  inferTimeGrain,
@@ -23,6 +22,7 @@ import type {
23
22
  ChartPalette,
24
23
  ChartPaletteName,
25
24
  ChartYAxisConfig,
25
+ ReferenceLine,
26
26
  } from './types'
27
27
 
28
28
  export type AxisChartOptionContext = {
@@ -98,7 +98,7 @@ const MIN_CATEGORY_LABEL_WIDTH = 56
98
98
  const TILT_ANGLE = 45
99
99
  const TILT_SIN = Math.sin((TILT_ANGLE * Math.PI) / 180)
100
100
  /** Clear air either side of a flat label, under which two of them read as one. */
101
- const CATEGORY_LABEL_GAP = 8
101
+ export const CATEGORY_LABEL_GAP = 8
102
102
  /**
103
103
  * How far below its tick a tilted label may reach, which is how much plot height
104
104
  * the axis is allowed to spend on saying more.
@@ -409,14 +409,22 @@ export function buildXAxis(
409
409
  type === 'time' && opts.inset
410
410
  ? timeInset(config, categories, width)
411
411
  : undefined
412
- const { rotate, labelWidth } = categoryLabelFit(config, { ...opts, type })
412
+ const { rotate, labelWidth, step } = categoryLabelFit(config, {
413
+ ...opts,
414
+ type,
415
+ })
413
416
 
414
417
  const axis = {
415
418
  type,
416
419
  // A quantity read along the plot is a position, not a magnitude: there is
417
420
  // no baseline to anchor to, so the scale follows the data the way both of
418
421
  // a scatter's do.
419
- ...(type === 'value' ? { scale: true } : {}),
422
+ ...(type === 'value'
423
+ ? {
424
+ scale: true,
425
+ ...referenceLineExtent(config.referenceLines, 'x', false),
426
+ }
427
+ : {}),
420
428
  // RTL reads right-to-left along the horizontal axis; the vertical axis of a
421
429
  // horizontal bar chart is inverted so the first category sits on top.
422
430
  inverse: horizontal ? true : isRTL,
@@ -425,7 +433,7 @@ export function buildXAxis(
425
433
  // like the time axis' is out: echarts rounds a category `min` and `max`.
426
434
  ...(type === 'category' ? { data: categories, boundaryGap: true } : {}),
427
435
  ...(inset ? inset.extent : {}),
428
- name: config.xAxis.title ? formatLabel(config.xAxis.title) : undefined,
436
+ name: config.xAxis.title || undefined,
429
437
  nameLocation: 'end',
430
438
  nameGap: 8,
431
439
  nameTextStyle: { color: tokens.axisTitle, fontSize: AXIS_LABEL_FONT_SIZE },
@@ -457,6 +465,7 @@ export function buildXAxis(
457
465
  // Left out entirely when the labels fit, so a flat axis carries no
458
466
  // rotation key at all.
459
467
  ...(rotate ? { rotate } : {}),
468
+ ...(step ? { interval: thinnedInterval(categories.length, step) } : {}),
460
469
  ...xAxisLabelFormat(type, {
461
470
  tokens,
462
471
  timeGrain,
@@ -466,7 +475,35 @@ export function buildXAxis(
466
475
  },
467
476
  }
468
477
 
469
- return mergeDeep(axis, config.xAxis.echartOptions)
478
+ return withShortenedFormat(
479
+ mergeDeep(axis, config.xAxis.echartOptions),
480
+ config,
481
+ type,
482
+ labelWidth,
483
+ )
484
+ }
485
+
486
+ /**
487
+ * An axis `format` reaches echarts as an `axisLabel.formatter` and replaces the
488
+ * one built here, which would leave its labels to echarts' end ellipsis. The
489
+ * caller's text is kept and shortened the way the built one is.
490
+ */
491
+ function withShortenedFormat<A extends { axisLabel: Record<string, any> }>(
492
+ axis: A,
493
+ config: AxisChartBaseConfig,
494
+ type: ResolvedXAxis['type'],
495
+ labelWidth: number | undefined,
496
+ ): A {
497
+ const formatter = config.xAxis.echartOptions?.axisLabel?.formatter
498
+ if (type !== 'category' || !labelWidth || typeof formatter !== 'function')
499
+ return axis
500
+ const label = drawnLabel(config, type)
501
+ return mergeDeep(axis, {
502
+ axisLabel: {
503
+ formatter: (value: any, index: number) =>
504
+ categoryLabel(label(value, index), labelWidth),
505
+ },
506
+ })
470
507
  }
471
508
 
472
509
  /**
@@ -558,6 +595,8 @@ type CategoryLabelFit = {
558
595
  rotate: number
559
596
  /** What a label is shortened to, when anything caps it. */
560
597
  labelWidth?: number
598
+ /** Every how many categories a label is drawn. Unset labels every one. */
599
+ step?: number
561
600
  }
562
601
 
563
602
  /**
@@ -570,8 +609,13 @@ type CategoryLabelFit = {
570
609
  * whichever layout carries more text: flat and shortened to the slot, or
571
610
  * tilted, which trades the slot for the depth below the axis. Wide slots keep
572
611
  * flat labels, because reading across beats reading up a diagonal; crowded ones
573
- * tilt, where a flat label would be down to a syllable. Whatever still collides
574
- * after that is thinned out by `hideOverlap`, as it always was.
612
+ * tilt, where a flat label would be down to a syllable.
613
+ *
614
+ * A tilt is bought to name every category, so it is only spent when it does:
615
+ * the slot has to hold a tilted line and the same clear air a flat label keeps.
616
+ * Past that, labels are dropped whichever way they lie, and a thinned axis
617
+ * reads better flat. It then labels every few categories from the first to
618
+ * the last, each label whole up to what a tilted one could carry.
575
619
  *
576
620
  * The whole axis takes one layout. Labels leaning two ways read as two axes.
577
621
  */
@@ -600,23 +644,62 @@ function categoryLabelFit(
600
644
  if (horizontal) return { rotate: 0, labelWidth: categoryLabelWidth(width) }
601
645
 
602
646
  const slot = categorySlotWidth(config, opts)
603
- if (!slot || fitsFlat(opts.categories, drawnLabel(config, type), slot))
604
- return { rotate: 0 }
647
+ const label = drawnLabel(config, type)
648
+ if (!slot || fitsFlat(opts.categories, label, slot)) return { rotate: 0 }
605
649
 
606
650
  const flat = Math.floor(slot - CATEGORY_LABEL_GAP)
607
651
  const tilted = tiltedLabelWidth()
608
652
  if (flat >= tilted) return { rotate: 0, labelWidth: flat }
609
653
 
654
+ // Tilted labels run parallel, so what separates two of them is the slot
655
+ // measured across the diagonal.
656
+ if (slot * TILT_SIN >= LABEL_LINE_HEIGHT + CATEGORY_LABEL_GAP) {
657
+ return {
658
+ // The tilt leans towards the value axis. Its column of ticks is width the
659
+ // grid holds open, and nothing occupies it below the plot — which is
660
+ // exactly where a tilted label reaches. RTL puts that column on the other
661
+ // side, so the labels lean the other way.
662
+ rotate: isRTL ? -TILT_ANGLE : TILT_ANGLE,
663
+ labelWidth: tilted,
664
+ }
665
+ }
666
+
667
+ const widest = opts.categories.reduce(
668
+ (max: number, value, index) =>
669
+ Math.max(
670
+ max,
671
+ estimateTextWidth(label(value, index), AXIS_LABEL_FONT_SIZE),
672
+ ),
673
+ 0,
674
+ )
675
+ const drawn = Math.min(widest, tilted)
676
+ // The labels at either end reach past their slots by half a label less half
677
+ // a slot, and echarts narrows the plot to keep them on the canvas. Measured
678
+ // against the slot before that, neighbours would collide and echarts would
679
+ // drop the first label.
680
+ const narrowed = slot - Math.max(0, drawn - slot) / opts.categories.length
610
681
  return {
611
- // The tilt leans towards the value axis. Its column of ticks is width the
612
- // grid holds open, and nothing occupies it below the plot — which is
613
- // exactly where a tilted label reaches. RTL puts that column on the other
614
- // side, so the labels lean the other way.
615
- rotate: isRTL ? -TILT_ANGLE : TILT_ANGLE,
616
- labelWidth: tilted,
682
+ rotate: 0,
683
+ labelWidth: widest > tilted ? tilted : undefined,
684
+ step: Math.ceil((drawn + CATEGORY_LABEL_GAP) / narrowed),
617
685
  }
618
686
  }
619
687
 
688
+ /**
689
+ * Which categories a thinned axis labels: the first, the last, and as many as
690
+ * fit between them at least a step apart, spread evenly. echarts' own
691
+ * `interval` counts from the first and leaves the last to chance, which reads
692
+ * as a clipped axis, and pinning the last to it leaves a hole before it.
693
+ */
694
+ function thinnedInterval(count: number, step: number) {
695
+ const last = count - 1
696
+ const gaps = Math.max(1, Math.floor(last / step))
697
+ const labelled = new Set(
698
+ Array.from({ length: gaps + 1 }, (_, i) => Math.round((i * last) / gaps)),
699
+ )
700
+ return (index: number) => labelled.has(index)
701
+ }
702
+
620
703
  /** The room the x axis has: the chart less its edge pad and the value axes. */
621
704
  function plotWidth(config: AxisChartBaseConfig, width: number) {
622
705
  return width - 2 * EDGE_PAD - valueAxisReserve(config)
@@ -796,12 +879,21 @@ export function buildValueAxes(
796
879
  const onY2 = (series: AxisChartSeriesConfig) =>
797
880
  secondary && series.axis === 'y2'
798
881
 
882
+ const lines = (axis: 'y' | 'y2') =>
883
+ referenceLineExtent(config.referenceLines, axis, secondary)
884
+
799
885
  const primary = buildValueAxis(config.yAxis, tokens, {
800
886
  ...opts,
887
+ lines: lines('y'),
888
+ integer: holdsWholeNumbers(
889
+ config.data ?? [],
890
+ config.series.filter((s) => !onY2(s)).map((s) => s.name),
891
+ ),
801
892
  empty: emptyAxisExtent(
802
893
  config,
803
894
  config.series.filter((s) => !onY2(s)),
804
895
  hidden,
896
+ lines('y'),
805
897
  ),
806
898
  })
807
899
  if (!secondary) return primary
@@ -810,7 +902,17 @@ export function buildValueAxes(
810
902
  buildValueAxis(config.y2Axis, tokens, {
811
903
  ...opts,
812
904
  secondary: true,
813
- empty: emptyAxisExtent(config, config.series.filter(onY2), hidden),
905
+ lines: lines('y2'),
906
+ integer: holdsWholeNumbers(
907
+ config.data ?? [],
908
+ config.series.filter(onY2).map((s) => s.name),
909
+ ),
910
+ empty: emptyAxisExtent(
911
+ config,
912
+ config.series.filter(onY2),
913
+ hidden,
914
+ lines('y2'),
915
+ ),
814
916
  }),
815
917
  ]
816
918
  }
@@ -832,17 +934,70 @@ function emptyAxisExtent(
832
934
  config: AxisChartBaseConfig,
833
935
  series: AxisChartSeriesConfig[],
834
936
  hidden: string[],
937
+ lines: { dataMin?: number; dataMax?: number },
835
938
  ): ValueExtent | null {
836
939
  if (!series.length) return null
837
940
  if (series.some((one) => !hidden.includes(one.name))) return null
838
941
  const { min, max } = valueExtent(config.data ?? [], series)
839
942
  // A value axis holds 0 unless told to `scale`, and none of these are, so the
840
943
  // ends have to reach it the way the live axis did.
841
- return niceExtent(Math.min(0, min), Math.max(0, max))
944
+ return niceExtent(
945
+ Math.min(0, min, lines.dataMin ?? 0),
946
+ Math.max(0, max, lines.dataMax ?? 0),
947
+ )
842
948
  }
843
949
 
844
950
  export type ValueExtent = { min: number; max: number }
845
951
 
952
+ /**
953
+ * Whether every value in these columns is a whole number: a count, which an
954
+ * axis should not split into halves. Read over every series rather than the
955
+ * visible ones, so switching one off in the legend does not re-tick the axis.
956
+ */
957
+ export function holdsWholeNumbers(
958
+ rows: Record<string, any>[],
959
+ keys: string[],
960
+ ): boolean {
961
+ return rows.every((row) =>
962
+ keys.every((key) => {
963
+ const value = toNumber(row[key])
964
+ return value === null || Number.isInteger(value)
965
+ }),
966
+ )
967
+ }
968
+
969
+ /**
970
+ * The axis a reference line is read against. `'y2'` falls back to `'y'` on a
971
+ * chart that draws one value axis, the same rule a series follows.
972
+ */
973
+ export function referenceLineAxis(
974
+ line: ReferenceLine,
975
+ hasSecondaryValueAxis: boolean,
976
+ ): 'x' | 'y' | 'y2' {
977
+ if (line.axis === 'x') return 'x'
978
+ return hasSecondaryValueAxis && line.axis === 'y2' ? 'y2' : 'y'
979
+ }
980
+
981
+ /**
982
+ * The span the reference lines on one axis reach, as echarts' `dataMin` and
983
+ * `dataMax`. echarts unions both into the extent the data reaches, so the axis
984
+ * stretches to every line and still rounds its ends as it does for data.
985
+ */
986
+ export function referenceLineExtent(
987
+ lines: ReferenceLine[] | undefined,
988
+ axis: 'x' | 'y' | 'y2',
989
+ hasSecondaryValueAxis: boolean,
990
+ ): { dataMin?: number; dataMax?: number } {
991
+ const values: number[] = []
992
+ for (const line of lines ?? []) {
993
+ if (referenceLineAxis(line, hasSecondaryValueAxis) !== axis) continue
994
+ const value = toNumber(line.value)
995
+ if (value !== null) values.push(value)
996
+ }
997
+ if (!values.length) return {}
998
+ return { dataMin: Math.min(...values), dataMax: Math.max(...values) }
999
+ }
1000
+
846
1001
  /**
847
1002
  * The low and the high a set of series reaches across the rows. Rows with
848
1003
  * nothing numeric in them read as 0 to 0, which is the scale echarts draws for
@@ -906,10 +1061,15 @@ export function buildValueAxis(
906
1061
  secondary?: boolean
907
1062
  /** The ends to pin it to while nothing is drawn on it. See `emptyAxisExtent`. */
908
1063
  empty?: ValueExtent | null
1064
+ /** The span its reference lines reach. See `referenceLineExtent`. */
1065
+ lines?: { dataMin?: number; dataMax?: number }
1066
+ /** Whether the values plotted on it are whole numbers. See `holdsWholeNumbers`. */
1067
+ integer?: boolean
909
1068
  },
910
1069
  ) {
911
1070
  const { horizontal, isRTL, empty } = opts
912
1071
  const secondary = Boolean(opts.secondary)
1072
+ const integer = axisConfig?.integer ?? opts.integer
913
1073
 
914
1074
  const axis = {
915
1075
  type: 'value',
@@ -923,6 +1083,10 @@ export function buildValueAxis(
923
1083
  // title whichever way the bars run.
924
1084
  min: axisConfig?.min ?? empty?.min,
925
1085
  max: axisConfig?.max ?? empty?.max,
1086
+ ...opts.lines,
1087
+ // A floor on the tick interval, so it only reaches a scale echarts would
1088
+ // otherwise split below one.
1089
+ ...(integer ? { minInterval: 1 } : {}),
926
1090
  // Gridlines carry the reading of the plot; the axis line itself is noise.
927
1091
  // Only the primary draws them: with aligned ticks the second set lands on
928
1092
  // the same rows, so it adds nothing but a doubled line.
@@ -5,6 +5,7 @@ import type {
5
5
  ChartValueFormatter,
6
6
  EchartOptionsOverride,
7
7
  } from './types'
8
+ import { resolveXAxis } from './axisChartCommon'
8
9
  import { mergeDeep } from './utils'
9
10
 
10
11
  /**
@@ -12,18 +13,22 @@ import { mergeDeep } from './utils'
12
13
  * the same per-axis `echartOptions` the builders already apply. Going through
13
14
  * that path rather than a builder argument keeps the precedence honest: an
14
15
  * explicit `echartOptions.axisLabel.formatter` still wins over `format`.
16
+ *
17
+ * `x` stays off a time axis: it prints the value where one is read, and the
18
+ * ticks keep the levels (`2025`, `Feb`, …) one fixed format would flatten.
15
19
  */
16
20
  export function applyAxisFormatters<C extends AxisChartBaseConfig>(
17
21
  config: C,
18
22
  format: AxisChartFormatters,
19
23
  ): C {
20
- if (!format.x && !format.y && !format.y2) return config
24
+ const formatX = resolveXAxis(config).type === 'time' ? undefined : format.x
25
+ if (!formatX && !format.y && !format.y2) return config
21
26
 
22
27
  const next = { ...config }
23
- if (format.x) {
28
+ if (formatX) {
24
29
  next.xAxis = {
25
30
  ...config.xAxis,
26
- echartOptions: withFormatter(config.xAxis.echartOptions, format.x),
31
+ echartOptions: withFormatter(config.xAxis.echartOptions, formatX),
27
32
  }
28
33
  }
29
34
  if (format.y) {