@navikt/ds-react 3.4.2 → 4.0.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 (234) hide show
  1. package/_docs.json +5615 -3780
  2. package/cjs/date/hooks/index.js +4 -4
  3. package/cjs/date/index.js +6 -6
  4. package/cjs/dropdown/Dropdown.js +57 -0
  5. package/cjs/dropdown/Menu/Divider.js +47 -0
  6. package/cjs/dropdown/Menu/GroupedList/Heading.js +47 -0
  7. package/cjs/dropdown/Menu/GroupedList/Item.js +54 -0
  8. package/cjs/dropdown/Menu/GroupedList/index.js +51 -0
  9. package/cjs/dropdown/Menu/GroupedList/package.json +6 -0
  10. package/cjs/dropdown/Menu/List/Item.js +54 -0
  11. package/cjs/dropdown/Menu/List/index.js +49 -0
  12. package/cjs/dropdown/Menu/List/package.json +6 -0
  13. package/cjs/dropdown/Menu/index.js +64 -0
  14. package/cjs/dropdown/Menu/package.json +6 -0
  15. package/cjs/dropdown/Toggle.js +66 -0
  16. package/cjs/dropdown/index.js +23 -0
  17. package/cjs/dropdown/package.json +6 -0
  18. package/cjs/form/radio/RadioGroup.js +10 -0
  19. package/cjs/index.js +3 -0
  20. package/cjs/internal-header/InternalHeader.js +55 -0
  21. package/cjs/internal-header/InternalHeaderButton.js +47 -0
  22. package/cjs/internal-header/InternalHeaderTitle.js +48 -0
  23. package/cjs/internal-header/InternalHeaderUser.js +51 -0
  24. package/cjs/internal-header/InternalHeaderUserButton.js +54 -0
  25. package/cjs/internal-header/index.js +8 -0
  26. package/cjs/internal-header/package.json +6 -0
  27. package/cjs/read-more/ReadMore.js +17 -0
  28. package/cjs/timeline/AxisLabels.js +90 -0
  29. package/cjs/timeline/Pin.js +106 -0
  30. package/cjs/timeline/Timeline.js +162 -0
  31. package/cjs/timeline/TimelineRow.js +86 -0
  32. package/cjs/timeline/hooks/usePeriodContext.js +16 -0
  33. package/cjs/timeline/hooks/useRowContext.js +18 -0
  34. package/cjs/timeline/hooks/useTimelineContext.js +23 -0
  35. package/cjs/timeline/hooks/useTimelineRows.js +79 -0
  36. package/cjs/timeline/index.js +8 -0
  37. package/cjs/timeline/package.json +6 -0
  38. package/cjs/timeline/period/ClickablePeriod.js +120 -0
  39. package/cjs/timeline/period/NonClickablePeriod.js +18 -0
  40. package/cjs/timeline/period/index.js +46 -0
  41. package/cjs/timeline/period/package.json +6 -0
  42. package/cjs/timeline/utils/calc.js +19 -0
  43. package/cjs/timeline/utils/filter.js +18 -0
  44. package/cjs/timeline/utils/index.js +7 -0
  45. package/cjs/timeline/utils/package.json +6 -0
  46. package/cjs/timeline/utils/period.js +40 -0
  47. package/cjs/timeline/utils/sort.js +10 -0
  48. package/cjs/timeline/utils/timeline.js +79 -0
  49. package/cjs/timeline/utils/types.external.js +2 -0
  50. package/cjs/timeline/utils/types.internal.js +2 -0
  51. package/cjs/timeline/zoom/ZoomButton.js +68 -0
  52. package/cjs/timeline/zoom/index.js +50 -0
  53. package/cjs/timeline/zoom/package.json +6 -0
  54. package/esm/date/hooks/index.d.ts +3 -3
  55. package/esm/date/hooks/index.js +3 -3
  56. package/esm/date/hooks/index.js.map +1 -1
  57. package/esm/date/index.d.ts +3 -3
  58. package/esm/date/index.js +3 -3
  59. package/esm/date/index.js.map +1 -1
  60. package/esm/dropdown/Dropdown.d.ts +37 -0
  61. package/esm/dropdown/Dropdown.js +29 -0
  62. package/esm/dropdown/Dropdown.js.map +1 -0
  63. package/esm/dropdown/Menu/Divider.d.ts +4 -0
  64. package/esm/dropdown/Menu/Divider.js +19 -0
  65. package/esm/dropdown/Menu/Divider.js.map +1 -0
  66. package/esm/dropdown/Menu/GroupedList/Heading.d.ts +10 -0
  67. package/esm/dropdown/Menu/GroupedList/Heading.js +19 -0
  68. package/esm/dropdown/Menu/GroupedList/Heading.js.map +1 -0
  69. package/esm/dropdown/Menu/GroupedList/Item.d.ts +11 -0
  70. package/esm/dropdown/Menu/GroupedList/Item.js +26 -0
  71. package/esm/dropdown/Menu/GroupedList/Item.js.map +1 -0
  72. package/esm/dropdown/Menu/GroupedList/index.d.ts +15 -0
  73. package/esm/dropdown/Menu/GroupedList/index.js +23 -0
  74. package/esm/dropdown/Menu/GroupedList/index.js.map +1 -0
  75. package/esm/dropdown/Menu/List/Item.d.ts +11 -0
  76. package/esm/dropdown/Menu/List/Item.js +26 -0
  77. package/esm/dropdown/Menu/List/Item.js.map +1 -0
  78. package/esm/dropdown/Menu/List/index.d.ts +13 -0
  79. package/esm/dropdown/Menu/List/index.js +21 -0
  80. package/esm/dropdown/Menu/List/index.js.map +1 -0
  81. package/esm/dropdown/Menu/index.d.ts +27 -0
  82. package/esm/dropdown/Menu/index.js +36 -0
  83. package/esm/dropdown/Menu/index.js.map +1 -0
  84. package/esm/dropdown/Toggle.d.ts +10 -0
  85. package/esm/dropdown/Toggle.js +38 -0
  86. package/esm/dropdown/Toggle.js.map +1 -0
  87. package/esm/dropdown/index.d.ts +2 -0
  88. package/esm/dropdown/index.js +3 -0
  89. package/esm/dropdown/index.js.map +1 -0
  90. package/esm/form/radio/RadioGroup.d.ts +10 -0
  91. package/esm/form/radio/RadioGroup.js +10 -0
  92. package/esm/form/radio/RadioGroup.js.map +1 -1
  93. package/esm/index.d.ts +3 -0
  94. package/esm/index.js +3 -0
  95. package/esm/index.js.map +1 -1
  96. package/esm/internal-header/InternalHeader.d.ts +16 -0
  97. package/esm/internal-header/InternalHeader.js +27 -0
  98. package/esm/internal-header/InternalHeader.js.map +1 -0
  99. package/esm/internal-header/InternalHeaderButton.d.ts +11 -0
  100. package/esm/internal-header/InternalHeaderButton.js +19 -0
  101. package/esm/internal-header/InternalHeaderButton.js.map +1 -0
  102. package/esm/internal-header/InternalHeaderTitle.d.ts +11 -0
  103. package/esm/internal-header/InternalHeaderTitle.js +20 -0
  104. package/esm/internal-header/InternalHeaderTitle.js.map +1 -0
  105. package/esm/internal-header/InternalHeaderUser.d.ts +14 -0
  106. package/esm/internal-header/InternalHeaderUser.js +23 -0
  107. package/esm/internal-header/InternalHeaderUser.js.map +1 -0
  108. package/esm/internal-header/InternalHeaderUserButton.d.ts +15 -0
  109. package/esm/internal-header/InternalHeaderUserButton.js +26 -0
  110. package/esm/internal-header/InternalHeaderUserButton.js.map +1 -0
  111. package/esm/internal-header/index.d.ts +5 -0
  112. package/esm/internal-header/index.js +2 -0
  113. package/esm/internal-header/index.js.map +1 -0
  114. package/esm/read-more/ReadMore.d.ts +17 -0
  115. package/esm/read-more/ReadMore.js +17 -0
  116. package/esm/read-more/ReadMore.js.map +1 -1
  117. package/esm/timeline/AxisLabels.d.ts +6 -0
  118. package/esm/timeline/AxisLabels.js +81 -0
  119. package/esm/timeline/AxisLabels.js.map +1 -0
  120. package/esm/timeline/Pin.d.ts +17 -0
  121. package/esm/timeline/Pin.js +81 -0
  122. package/esm/timeline/Pin.js.map +1 -0
  123. package/esm/timeline/Timeline.d.ts +45 -0
  124. package/esm/timeline/Timeline.js +134 -0
  125. package/esm/timeline/Timeline.js.map +1 -0
  126. package/esm/timeline/TimelineRow.d.ts +22 -0
  127. package/esm/timeline/TimelineRow.js +58 -0
  128. package/esm/timeline/TimelineRow.js.map +1 -0
  129. package/esm/timeline/hooks/usePeriodContext.d.ts +9 -0
  130. package/esm/timeline/hooks/usePeriodContext.js +13 -0
  131. package/esm/timeline/hooks/usePeriodContext.js.map +1 -0
  132. package/esm/timeline/hooks/useRowContext.d.ts +11 -0
  133. package/esm/timeline/hooks/useRowContext.js +15 -0
  134. package/esm/timeline/hooks/useRowContext.js.map +1 -0
  135. package/esm/timeline/hooks/useTimelineContext.d.ts +15 -0
  136. package/esm/timeline/hooks/useTimelineContext.js +20 -0
  137. package/esm/timeline/hooks/useTimelineContext.js.map +1 -0
  138. package/esm/timeline/hooks/useTimelineRows.d.ts +4 -0
  139. package/esm/timeline/hooks/useTimelineRows.js +74 -0
  140. package/esm/timeline/hooks/useTimelineRows.js.map +1 -0
  141. package/esm/timeline/index.d.ts +6 -0
  142. package/esm/timeline/index.js +2 -0
  143. package/esm/timeline/index.js.map +1 -0
  144. package/esm/timeline/period/ClickablePeriod.d.ts +9 -0
  145. package/esm/timeline/period/ClickablePeriod.js +93 -0
  146. package/esm/timeline/period/ClickablePeriod.js.map +1 -0
  147. package/esm/timeline/period/NonClickablePeriod.d.ts +7 -0
  148. package/esm/timeline/period/NonClickablePeriod.js +14 -0
  149. package/esm/timeline/period/NonClickablePeriod.js.map +1 -0
  150. package/esm/timeline/period/index.d.ts +57 -0
  151. package/esm/timeline/period/index.js +18 -0
  152. package/esm/timeline/period/index.js.map +1 -0
  153. package/esm/timeline/utils/calc.d.ts +5 -0
  154. package/esm/timeline/utils/calc.js +15 -0
  155. package/esm/timeline/utils/calc.js.map +1 -0
  156. package/esm/timeline/utils/filter.d.ts +10 -0
  157. package/esm/timeline/utils/filter.js +11 -0
  158. package/esm/timeline/utils/filter.js.map +1 -0
  159. package/esm/timeline/utils/index.d.ts +1 -0
  160. package/esm/timeline/utils/index.js +2 -0
  161. package/esm/timeline/utils/index.js.map +1 -0
  162. package/esm/timeline/utils/period.d.ts +2 -0
  163. package/esm/timeline/utils/period.js +33 -0
  164. package/esm/timeline/utils/period.js.map +1 -0
  165. package/esm/timeline/utils/sort.d.ts +4 -0
  166. package/esm/timeline/utils/sort.js +5 -0
  167. package/esm/timeline/utils/sort.js.map +1 -0
  168. package/esm/timeline/utils/timeline.d.ts +12 -0
  169. package/esm/timeline/utils/timeline.js +73 -0
  170. package/esm/timeline/utils/timeline.js.map +1 -0
  171. package/esm/timeline/utils/types.external.d.ts +53 -0
  172. package/esm/timeline/utils/types.external.js +2 -0
  173. package/esm/timeline/utils/types.external.js.map +1 -0
  174. package/esm/timeline/utils/types.internal.d.ts +61 -0
  175. package/esm/timeline/utils/types.internal.js +2 -0
  176. package/esm/timeline/utils/types.internal.js.map +1 -0
  177. package/esm/timeline/zoom/ZoomButton.d.ts +19 -0
  178. package/esm/timeline/zoom/ZoomButton.js +43 -0
  179. package/esm/timeline/zoom/ZoomButton.js.map +1 -0
  180. package/esm/timeline/zoom/index.d.ts +11 -0
  181. package/esm/timeline/zoom/index.js +22 -0
  182. package/esm/timeline/zoom/index.js.map +1 -0
  183. package/package.json +2 -2
  184. package/src/date/datepicker/datepicker.stories.tsx +19 -21
  185. package/src/date/datepicker/datepicker.test.tsx +5 -5
  186. package/src/date/hooks/index.ts +3 -3
  187. package/src/date/hooks/useRangeDatepicker.test.tsx +6 -6
  188. package/src/date/index.ts +5 -5
  189. package/src/date/monthpicker/monthpicker.stories.tsx +5 -5
  190. package/src/date/utils/__tests__/get-dates.test.ts +0 -1
  191. package/src/dropdown/Dropdown.tsx +80 -0
  192. package/src/dropdown/Menu/Divider.tsx +18 -0
  193. package/src/dropdown/Menu/GroupedList/Heading.tsx +31 -0
  194. package/src/dropdown/Menu/GroupedList/Item.tsx +45 -0
  195. package/src/dropdown/Menu/GroupedList/index.tsx +33 -0
  196. package/src/dropdown/Menu/List/Item.tsx +44 -0
  197. package/src/dropdown/Menu/List/index.tsx +27 -0
  198. package/src/dropdown/Menu/index.tsx +85 -0
  199. package/src/dropdown/Toggle.tsx +52 -0
  200. package/src/dropdown/dropdown.stories.tsx +91 -0
  201. package/src/dropdown/index.ts +2 -0
  202. package/src/form/radio/RadioGroup.tsx +10 -0
  203. package/src/index.ts +3 -0
  204. package/src/internal-header/InternalHeader.tsx +44 -0
  205. package/src/internal-header/InternalHeaderButton.tsx +28 -0
  206. package/src/internal-header/InternalHeaderTitle.tsx +35 -0
  207. package/src/internal-header/InternalHeaderUser.tsx +39 -0
  208. package/src/internal-header/InternalHeaderUserButton.tsx +43 -0
  209. package/src/internal-header/header.stories.tsx +225 -0
  210. package/src/internal-header/index.ts +8 -0
  211. package/src/read-more/ReadMore.tsx +17 -0
  212. package/src/timeline/AxisLabels.tsx +143 -0
  213. package/src/timeline/Pin.tsx +169 -0
  214. package/src/timeline/Timeline.tsx +219 -0
  215. package/src/timeline/TimelineRow.tsx +122 -0
  216. package/src/timeline/hooks/usePeriodContext.tsx +22 -0
  217. package/src/timeline/hooks/useRowContext.tsx +26 -0
  218. package/src/timeline/hooks/useTimelineContext.tsx +37 -0
  219. package/src/timeline/hooks/useTimelineRows.ts +161 -0
  220. package/src/timeline/index.ts +6 -0
  221. package/src/timeline/period/ClickablePeriod.tsx +193 -0
  222. package/src/timeline/period/NonClickablePeriod.tsx +46 -0
  223. package/src/timeline/period/index.tsx +130 -0
  224. package/src/timeline/timeline.stories.tsx +444 -0
  225. package/src/timeline/utils/calc.ts +26 -0
  226. package/src/timeline/utils/filter.ts +32 -0
  227. package/src/timeline/utils/index.ts +6 -0
  228. package/src/timeline/utils/period.ts +48 -0
  229. package/src/timeline/utils/sort.ts +11 -0
  230. package/src/timeline/utils/timeline.ts +83 -0
  231. package/src/timeline/utils/types.external.ts +67 -0
  232. package/src/timeline/utils/types.internal.ts +76 -0
  233. package/src/timeline/zoom/ZoomButton.tsx +83 -0
  234. package/src/timeline/zoom/index.tsx +30 -0
@@ -0,0 +1,143 @@
1
+ import { Detail } from "../typography/Detail";
2
+ import {
3
+ addDays,
4
+ addMonths,
5
+ addYears,
6
+ differenceInDays,
7
+ differenceInMonths,
8
+ differenceInYears,
9
+ endOfMonth,
10
+ endOfYear,
11
+ format,
12
+ startOfDay,
13
+ startOfMonth,
14
+ startOfYear,
15
+ subDays,
16
+ } from "date-fns";
17
+ import nbLocale from "date-fns/locale/nb";
18
+ import React from "react";
19
+ import { useTimelineContext } from "./hooks/useTimelineContext";
20
+ import { isVisible } from "./utils";
21
+ import { horizontalPositionAndWidth } from "./utils/calc";
22
+ import { AxisLabel } from "./utils/types.external";
23
+
24
+ export const dayLabels = (
25
+ start: Date,
26
+ end: Date,
27
+ totalDays: number,
28
+ direction: "left" | "right"
29
+ ): AxisLabel[] => {
30
+ const increment = Math.ceil(totalDays / 10);
31
+ const lastDay = startOfDay(end);
32
+ return new Array(totalDays)
33
+ .fill(lastDay)
34
+ .map((thisDay, i) => {
35
+ if (i % increment !== 0) return null;
36
+ const day: Date = subDays(thisDay, i);
37
+ const { horizontalPosition, width } = horizontalPositionAndWidth(
38
+ day,
39
+ addDays(day, 1),
40
+ start,
41
+ end
42
+ );
43
+ return {
44
+ direction: direction,
45
+ horizontalPosition: horizontalPosition,
46
+ label: format(day, "dd.MM", { locale: nbLocale }),
47
+ date: day,
48
+ width: width,
49
+ };
50
+ })
51
+ .filter((label) => label !== null) as AxisLabel[];
52
+ };
53
+
54
+ export const monthLabels = (
55
+ start: Date,
56
+ end: Date,
57
+ direction: "left" | "right"
58
+ ): AxisLabel[] => {
59
+ const startMonth = startOfMonth(start);
60
+ const endMonth = endOfMonth(end);
61
+ const numberOfMonths = differenceInMonths(endMonth, startMonth) + 1;
62
+ return new Array(numberOfMonths).fill(startMonth).map((thisMonth, i) => {
63
+ const month: Date = addMonths(thisMonth, i);
64
+ const { horizontalPosition, width } = horizontalPositionAndWidth(
65
+ month,
66
+ addMonths(month, 1),
67
+ start,
68
+ end
69
+ );
70
+ return {
71
+ direction: direction,
72
+ horizontalPosition: horizontalPosition,
73
+ label: format(month, "MMM yy", { locale: nbLocale }),
74
+ date: month,
75
+ width: width,
76
+ };
77
+ });
78
+ };
79
+
80
+ export const yearLabels = (
81
+ start: Date,
82
+ end: Date,
83
+ direction: "left" | "right"
84
+ ): AxisLabel[] => {
85
+ const firstYear = startOfYear(start);
86
+ const lastYear = endOfYear(end);
87
+ const yearCount = differenceInYears(lastYear, start) + 1;
88
+ return new Array(yearCount).fill(firstYear).map((thisYear, i) => {
89
+ const year: Date = addYears(thisYear, i);
90
+ const { horizontalPosition, width } = horizontalPositionAndWidth(
91
+ year,
92
+ addYears(year, 1),
93
+ start,
94
+ end
95
+ );
96
+ return {
97
+ direction: direction,
98
+ horizontalPosition: horizontalPosition,
99
+ label: year.getFullYear().toString(),
100
+ date: year,
101
+ width: width,
102
+ };
103
+ });
104
+ };
105
+
106
+ const axisLabels = (
107
+ start: Date,
108
+ end: Date,
109
+ direction: "left" | "right"
110
+ ): AxisLabel[] => {
111
+ const totalDays = differenceInDays(end, start);
112
+ if (totalDays < 40) {
113
+ return dayLabels(start, end, totalDays, direction);
114
+ } else if (totalDays < 370) {
115
+ return monthLabels(start, end, direction);
116
+ } else {
117
+ return yearLabels(start, end, direction);
118
+ }
119
+ };
120
+
121
+ export const AxisLabels = () => {
122
+ const { endDate, startDate, direction } = useTimelineContext();
123
+ const labels = axisLabels(startDate, endDate, direction).filter(isVisible);
124
+
125
+ return (
126
+ <div className="navds-timeline__axislabels" aria-hidden="true">
127
+ {labels.map((etikett) => (
128
+ <Detail
129
+ className="navds-timeline__axislabels-label"
130
+ as="div"
131
+ key={etikett.label}
132
+ style={{
133
+ justifyContent: direction === "left" ? "flex-start" : "flex-end",
134
+ [direction]: `${etikett.horizontalPosition}%`,
135
+ width: `${etikett.width}%`,
136
+ }}
137
+ >
138
+ {etikett.label}
139
+ </Detail>
140
+ ))}
141
+ </div>
142
+ );
143
+ };
@@ -0,0 +1,169 @@
1
+ import {
2
+ autoUpdate,
3
+ arrow as flArrow,
4
+ flip,
5
+ offset,
6
+ safePolygon,
7
+ shift,
8
+ useDismiss,
9
+ useFloating,
10
+ useFocus,
11
+ useHover,
12
+ useInteractions,
13
+ } from "@floating-ui/react";
14
+ import { useEventListener, mergeRefs } from "../util";
15
+ import { format } from "date-fns";
16
+ import React, {
17
+ forwardRef,
18
+ useCallback,
19
+ useMemo,
20
+ useRef,
21
+ useState,
22
+ } from "react";
23
+ import { useTimelineContext } from "./hooks/useTimelineContext";
24
+ import { position } from "./utils/calc";
25
+ import { TimelineComponentTypes } from "./utils/types.internal";
26
+
27
+ export interface TimelinePinProps
28
+ extends React.HTMLAttributes<HTMLButtonElement> {
29
+ /**
30
+ * Date position for the pin.
31
+ */
32
+ date: Date;
33
+ /**
34
+ * Content in Pin Popover.
35
+ */
36
+ children?: React.ReactNode;
37
+ }
38
+
39
+ export interface PinType
40
+ extends React.ForwardRefExoticComponent<
41
+ TimelinePinProps & React.RefAttributes<HTMLButtonElement>
42
+ > {
43
+ componentType: TimelineComponentTypes;
44
+ }
45
+
46
+ export const Pin = forwardRef<HTMLButtonElement, TimelinePinProps>(
47
+ ({ date, children, ...rest }, ref) => {
48
+ const { startDate, endDate, direction } = useTimelineContext();
49
+ const [open, setOpen] = useState(false);
50
+ const arrowRef = useRef<HTMLDivElement | null>(null);
51
+
52
+ const {
53
+ context,
54
+ placement,
55
+ middlewareData: { arrow: { x: arrowX, y: arrowY } = {} },
56
+ refs,
57
+ floatingStyles,
58
+ } = useFloating({
59
+ placement: "top",
60
+ open: open,
61
+ onOpenChange: setOpen,
62
+ middleware: [
63
+ offset(16),
64
+ shift(),
65
+ flip({ padding: 5, fallbackPlacements: ["bottom", "top"] }),
66
+ flArrow({ element: arrowRef, padding: 5 }),
67
+ ],
68
+ whileElementsMounted: autoUpdate,
69
+ });
70
+
71
+ const { getFloatingProps, getReferenceProps } = useInteractions([
72
+ useHover(context, {
73
+ handleClose: safePolygon(),
74
+ restMs: 25,
75
+ delay: { open: 1000 },
76
+ }),
77
+ useFocus(context),
78
+ useDismiss(context),
79
+ ]);
80
+
81
+ const mergedRef = useMemo(
82
+ () => mergeRefs([refs.setReference, ref]),
83
+ [ref, refs.setReference]
84
+ );
85
+
86
+ useEventListener(
87
+ "focusin",
88
+ useCallback(
89
+ (e: FocusEvent) => {
90
+ if (
91
+ ![refs.domReference.current, refs?.floating?.current].some(
92
+ (element) => element?.contains(e.target as Node)
93
+ )
94
+ ) {
95
+ open && setOpen(false);
96
+ }
97
+ },
98
+ [open, refs.domReference, refs?.floating]
99
+ )
100
+ );
101
+
102
+ const staticSide = {
103
+ top: "bottom",
104
+ right: "left",
105
+ bottom: "top",
106
+ left: "right",
107
+ }[placement.split("-")[0]];
108
+
109
+ return (
110
+ <>
111
+ <div
112
+ className="navds-timeline__pin-wrapper"
113
+ style={{ [direction]: `${position(date, startDate, endDate)}%` }}
114
+ >
115
+ <button
116
+ {...rest}
117
+ ref={mergedRef}
118
+ className="navds-timeline__pin-button"
119
+ aria-label={`pin:${format(date, "dd.MM.yyyy")}`}
120
+ type="button"
121
+ aria-expanded={children ? open : undefined}
122
+ {...getReferenceProps({
123
+ onKeyDown: (e) => {
124
+ rest?.onKeyDown?.(e as React.KeyboardEvent<HTMLButtonElement>);
125
+ if (e.key === "Enter") {
126
+ setOpen((prev) => !prev);
127
+ } else if (e.key === " ") {
128
+ setOpen(false);
129
+ }
130
+ },
131
+ })}
132
+ />
133
+ </div>
134
+ {children && (
135
+ <div
136
+ className="navds-timeline__popover"
137
+ data-placement={placement}
138
+ aria-hidden={!open}
139
+ ref={refs.setFloating}
140
+ {...getFloatingProps({
141
+ tabIndex: -1,
142
+ })}
143
+ style={{
144
+ ...floatingStyles,
145
+ display: open ? undefined : "none",
146
+ }}
147
+ >
148
+ <div className="navds-timeline__popover-content">{children}</div>
149
+ <div
150
+ ref={(node) => {
151
+ arrowRef.current = node;
152
+ }}
153
+ style={{
154
+ ...(arrowX != null ? { left: arrowX } : {}),
155
+ ...(arrowY != null ? { top: arrowY } : {}),
156
+ ...(staticSide ? { [staticSide]: "-0.5rem" } : {}),
157
+ }}
158
+ className="navds-timeline__popover-arrow"
159
+ />
160
+ </div>
161
+ )}
162
+ </>
163
+ );
164
+ }
165
+ ) as PinType;
166
+
167
+ Pin.componentType = "pin";
168
+
169
+ export default Pin;
@@ -0,0 +1,219 @@
1
+ import { endOfDay, isSameDay, startOfDay } from "date-fns";
2
+ import React, { forwardRef, useMemo, useRef, useState } from "react";
3
+ import { AxisLabels } from "./AxisLabels";
4
+ import { RowContext } from "./hooks/useRowContext";
5
+ import { TimelineContext } from "./hooks/useTimelineContext";
6
+ import {
7
+ useEarliestDate,
8
+ useLatestDate,
9
+ useTimelineRows,
10
+ } from "./hooks/useTimelineRows";
11
+ import Period, { PeriodType } from "./period";
12
+ import Pin, { PinType } from "./Pin";
13
+ import TimelineRow, { TimelineRowType } from "./TimelineRow";
14
+ import { parseRows } from "./utils/timeline";
15
+ import Zoom, { ZoomType } from "./zoom";
16
+
17
+ export interface TimelineProps extends React.HTMLAttributes<HTMLDivElement> {
18
+ children: React.ReactNode;
19
+ /**
20
+ * Decides startingpoint in timeline.
21
+ * Defaults to earliest date among the timeline periods.
22
+ * @note Using this disables use of ZoomButtons. You will need to control zooming yourself.
23
+ */
24
+ startDate?: Date;
25
+ /**
26
+ * Decides end-date for timeline.
27
+ * Defaults to the latest date among the timeline periods.
28
+ * @note Using this disables use of ZoomButtons. You will need to control zooming yourself.
29
+ */
30
+ endDate?: Date;
31
+ /**
32
+ * Decides direction which periods are sorted/displayed. "left" ascends from earliest date on left.
33
+ * @default "left"
34
+ */
35
+ direction?: "left" | "right";
36
+ }
37
+
38
+ interface TimelineComponent
39
+ extends React.ForwardRefExoticComponent<TimelineProps> {
40
+ /**
41
+ * Built-in timeline row
42
+ */
43
+ Row: TimelineRowType;
44
+ /**
45
+ * Built-in row period
46
+ */
47
+ Period: PeriodType;
48
+ /**
49
+ * Built-in timeline pin
50
+ */
51
+ Pin: PinType;
52
+ /**
53
+ * Built-in timeline zoom-component
54
+ */
55
+ Zoom: ZoomType;
56
+ }
57
+
58
+ export const Timeline = forwardRef<HTMLDivElement, TimelineProps>(
59
+ ({ children, startDate, endDate, direction = "left", ...rest }, ref) => {
60
+ const isMultipleRows = Array.isArray(children);
61
+
62
+ const firstFocusabled = useRef<
63
+ { ref: HTMLButtonElement | null; id: number }[]
64
+ >([]);
65
+
66
+ if (!isMultipleRows) {
67
+ children = [children];
68
+ }
69
+ const rowChildren = React.Children.toArray(children).filter(
70
+ (c: any) => c?.type?.componentType === "row"
71
+ );
72
+
73
+ const pins = React.Children.toArray(children).filter(
74
+ (c: any) => c?.type?.componentType === "pin"
75
+ );
76
+
77
+ const zoomComponent = React.Children.toArray(children).find(
78
+ (c: any) => c?.type?.componentType === "zoom"
79
+ );
80
+
81
+ const rowsRaw = useMemo(() => {
82
+ return parseRows(rowChildren);
83
+ }, [rowChildren]);
84
+
85
+ const rows = rowsRaw.map((r) => {
86
+ if (r?.periods) {
87
+ return r.periods;
88
+ }
89
+ return [];
90
+ });
91
+
92
+ const initialStartDate = startOfDay(useEarliestDate({ startDate, rows }));
93
+ const [start, setStart] = useState(initialStartDate);
94
+ const [activeRow, setActiveRow] = useState<number | null>(null);
95
+ const [endInclusive, setEndInclusive] = useState(
96
+ endOfDay(useLatestDate({ endDate, rows }))
97
+ );
98
+
99
+ const initialEndDate = endOfDay(useLatestDate({ endDate, rows }));
100
+ const processedRows = useTimelineRows(
101
+ rowsRaw,
102
+ startDate ?? start,
103
+ endDate ?? endInclusive,
104
+ direction
105
+ );
106
+
107
+ const handleZoomChange = (zoomStart: Date) => {
108
+ if (startDate || endDate) {
109
+ if (process.env.NODE_ENV !== "production") {
110
+ console.warn(
111
+ "Zooming is not supported when startDate or endDate is set"
112
+ );
113
+ }
114
+ return;
115
+ }
116
+ if (direction === "left") {
117
+ if (isSameDay(zoomStart, start)) {
118
+ setStart(initialStartDate);
119
+ return;
120
+ }
121
+ setStart(zoomStart);
122
+ } else {
123
+ if (isSameDay(zoomStart, endInclusive)) {
124
+ setEndInclusive(initialEndDate);
125
+ return;
126
+ }
127
+ setEndInclusive(zoomStart);
128
+ }
129
+ };
130
+
131
+ const handleActiveRowChange = (key: string) => {
132
+ if (activeRow !== null && key === "ArrowDown") {
133
+ for (let i = activeRow + 1; i < processedRows.length; i++) {
134
+ const row = processedRows[i];
135
+ if (row.periods.find((p) => !!p.children || !!p.onSelectPeriod)) {
136
+ setActiveRow(i);
137
+ firstFocusabled.current.find((x) => x.id === i)?.ref?.focus();
138
+ break;
139
+ }
140
+ }
141
+ return;
142
+ }
143
+ if (activeRow !== null && key === "ArrowUp") {
144
+ for (let i = activeRow - 1; i >= 0; i--) {
145
+ const row = processedRows[i];
146
+ if (row.periods.find((p) => !!p.children || !!p.onSelectPeriod)) {
147
+ setActiveRow(i);
148
+ firstFocusabled.current.find((x) => x.id === i)?.ref?.focus();
149
+ break;
150
+ }
151
+ }
152
+ return;
153
+ }
154
+ };
155
+
156
+ const addFocusable = (ref: HTMLButtonElement | null, id: number) => {
157
+ let items = firstFocusabled.current;
158
+ items = items.filter((x) => x.id !== id);
159
+ items.push({ ref, id });
160
+ firstFocusabled.current = items;
161
+ };
162
+
163
+ return (
164
+ <TimelineContext.Provider
165
+ value={{
166
+ startDate: startDate ?? start,
167
+ endDate: endDate ?? endInclusive,
168
+ direction: direction,
169
+ setStart: (d) => handleZoomChange(d),
170
+ setEndInclusive: (d) => setEndInclusive(d),
171
+ activeRow: activeRow,
172
+ setActiveRow: (key) => handleActiveRowChange(key),
173
+ initiate: (i) => setActiveRow(i),
174
+ addFocusable,
175
+ }}
176
+ >
177
+ <div {...rest} ref={ref}>
178
+ <div className="navds-timeline">
179
+ <AxisLabels />
180
+
181
+ {pins.map((pin) => {
182
+ return pin;
183
+ })}
184
+
185
+ {processedRows.map((row, i) => {
186
+ return (
187
+ <RowContext.Provider
188
+ key={`row-${row.id}`}
189
+ value={{
190
+ periods: row.periods,
191
+ id: row.id,
192
+ active: activeRow === i,
193
+ index: i,
194
+ }}
195
+ >
196
+ <TimelineRow
197
+ {...row?.restProps}
198
+ ref={row?.ref}
199
+ label={row.label}
200
+ icon={row.icon}
201
+ headingTag={row.headingTag}
202
+ />
203
+ </RowContext.Provider>
204
+ );
205
+ })}
206
+ </div>
207
+ {zoomComponent && zoomComponent}
208
+ </div>
209
+ </TimelineContext.Provider>
210
+ );
211
+ }
212
+ ) as TimelineComponent;
213
+
214
+ Timeline.Row = TimelineRow;
215
+ Timeline.Period = Period;
216
+ Timeline.Pin = Pin;
217
+ Timeline.Zoom = Zoom;
218
+
219
+ export default Timeline;
@@ -0,0 +1,122 @@
1
+ import { BodyShort } from "../typography/BodyShort";
2
+ import { format } from "date-fns";
3
+ import React, { forwardRef } from "react";
4
+ import { PeriodContext } from "./hooks/usePeriodContext";
5
+ import { useRowContext } from "./hooks/useRowContext";
6
+ import { useTimelineContext } from "./hooks/useTimelineContext";
7
+ import Period from "./period";
8
+ import {
9
+ PositionedPeriod,
10
+ TimelineComponentTypes,
11
+ } from "./utils/types.internal";
12
+ import cl from "clsx";
13
+
14
+ export interface TimelineRowProps
15
+ extends React.HTMLAttributes<HTMLOListElement> {
16
+ /**
17
+ * Label for the timeline row
18
+ */
19
+ label: string;
20
+ /**
21
+ * Heading level for the label e.g h2, h3...
22
+ * @default "h3"
23
+ */
24
+ headingTag?: "h2" | "h3" | "h4" | "h5" | "h6";
25
+ /**
26
+ * Icon next to label
27
+ */
28
+ icon?: React.ReactNode;
29
+ }
30
+
31
+ export interface TimelineRowType
32
+ extends React.ForwardRefExoticComponent<
33
+ TimelineRowProps & React.RefAttributes<HTMLOListElement>
34
+ > {
35
+ componentType: TimelineComponentTypes;
36
+ }
37
+
38
+ export const TimelineRow = forwardRef<HTMLOListElement, TimelineRowProps>(
39
+ ({ label, className, headingTag = "h3", icon, ...rest }, ref) => {
40
+ const { periods, id, active } = useRowContext();
41
+ const { setActiveRow } = useTimelineContext();
42
+
43
+ const latest = periods.reduce((a, b) => {
44
+ return a.end > b.end ? a : b;
45
+ }, {} as PositionedPeriod);
46
+
47
+ const earliest = periods.reduce((a, b) => {
48
+ return a.end < b.end ? a : b;
49
+ }, {} as PositionedPeriod);
50
+
51
+ const firstFocusable = periods.find(
52
+ (p) => !!p.children || !!p.onSelectPeriod
53
+ );
54
+
55
+ return (
56
+ <>
57
+ {label && (
58
+ <BodyShort
59
+ as={headingTag}
60
+ id={`timeline-row-${id}`}
61
+ className="navds-timeline__row-label"
62
+ size="small"
63
+ >
64
+ {icon}
65
+ {label}
66
+ </BodyShort>
67
+ )}
68
+ <div
69
+ className={cl("navds-timeline__row", {
70
+ "navds-timeline__row--active": active,
71
+ })}
72
+ >
73
+ <ol
74
+ {...rest}
75
+ ref={ref}
76
+ aria-label={
77
+ periods.length === 0
78
+ ? "Ingen perioder"
79
+ : `${format(earliest.start, "dd.MM.yyyy")} til ${format(
80
+ latest.end,
81
+ "dd.MM.yyyy"
82
+ )}`
83
+ }
84
+ className={cl("navds-timeline__row-periods", className)}
85
+ onKeyDown={(e) => {
86
+ if (e.key === "ArrowDown" || e.key === "ArrowUp") {
87
+ e.preventDefault();
88
+ setActiveRow(e.key);
89
+ }
90
+ }}
91
+ >
92
+ {periods &&
93
+ periods.map((period) => {
94
+ return (
95
+ <li key={`period-${period.id}`}>
96
+ <PeriodContext.Provider
97
+ value={{
98
+ periodId: period.id,
99
+ firstFocus: firstFocusable?.id === period.id,
100
+ restProps: period?.restProps,
101
+ }}
102
+ >
103
+ <Period
104
+ start={period.start}
105
+ end={period.endInclusive}
106
+ icon={period.icon}
107
+ ref={period?.ref}
108
+ />
109
+ </PeriodContext.Provider>
110
+ </li>
111
+ );
112
+ })}
113
+ </ol>
114
+ </div>
115
+ </>
116
+ );
117
+ }
118
+ ) as TimelineRowType;
119
+
120
+ TimelineRow.componentType = "row";
121
+
122
+ export default TimelineRow;
@@ -0,0 +1,22 @@
1
+ import { createContext, useContext } from "react";
2
+
3
+ interface PeriodContextProps {
4
+ periodId: String;
5
+ firstFocus: boolean;
6
+ restProps?: any;
7
+ }
8
+
9
+ export const PeriodContext = createContext<PeriodContextProps>({
10
+ periodId: "",
11
+ firstFocus: false,
12
+ });
13
+
14
+ export const usePeriodContext = () => {
15
+ const context = useContext(PeriodContext);
16
+
17
+ if (!context) {
18
+ console.warn("usePeriodContext must be used with PeriodContext");
19
+ }
20
+
21
+ return context;
22
+ };
@@ -0,0 +1,26 @@
1
+ import { createContext, useContext } from "react";
2
+ import { PositionedPeriod } from "../utils/types.internal";
3
+
4
+ interface RowContextProps {
5
+ periods: PositionedPeriod[];
6
+ id: string;
7
+ active: boolean;
8
+ index: number;
9
+ }
10
+
11
+ export const RowContext = createContext<RowContextProps>({
12
+ periods: [],
13
+ id: "",
14
+ active: false,
15
+ index: 0,
16
+ });
17
+
18
+ export const useRowContext = () => {
19
+ const context = useContext(RowContext);
20
+
21
+ if (!context) {
22
+ console.warn("useRowContext must be used with RowContext");
23
+ }
24
+
25
+ return context;
26
+ };