@primer/components 0.0.0-20219254849 → 0.0.0-20219256296

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 (114) hide show
  1. package/CHANGELOG.md +4 -8
  2. package/dist/browser.esm.js +3 -3
  3. package/dist/browser.esm.js.map +1 -1
  4. package/dist/browser.umd.js +3 -3
  5. package/dist/browser.umd.js.map +1 -1
  6. package/lib/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  7. package/lib/Autocomplete/Autocomplete.d.ts +11 -13
  8. package/lib/Autocomplete/AutocompleteInput.d.ts +11 -13
  9. package/lib/Button/Button.d.ts +11 -11
  10. package/lib/Button/ButtonBase.d.ts +1 -1
  11. package/lib/Button/ButtonClose.d.ts +29 -29
  12. package/lib/Button/ButtonDanger.d.ts +11 -11
  13. package/lib/Button/ButtonInvisible.d.ts +11 -11
  14. package/lib/Button/ButtonOutline.d.ts +11 -11
  15. package/lib/Button/ButtonPrimary.d.ts +11 -11
  16. package/lib/Button/ButtonStyles.js +1 -1
  17. package/lib/CircleBadge.d.ts +2 -2
  18. package/lib/CircleOcticon.d.ts +33 -33
  19. package/lib/Dialog.d.ts +35 -35
  20. package/lib/Dropdown.d.ts +110 -110
  21. package/lib/DropdownMenu/DropdownButton.d.ts +30 -30
  22. package/lib/FilterList.d.ts +26 -26
  23. package/lib/Label.d.ts +1 -1
  24. package/lib/NewButton/button.d.ts +18 -0
  25. package/lib/NewButton/button.js +275 -0
  26. package/lib/NewButton/index.d.ts +4 -0
  27. package/lib/NewButton/index.js +21 -0
  28. package/lib/NewButton/visual.d.ts +6 -0
  29. package/lib/NewButton/visual.js +28 -0
  30. package/lib/Position.d.ts +4 -4
  31. package/lib/ProgressBar.d.ts +1 -1
  32. package/lib/SelectMenu/SelectMenu.d.ts +151 -153
  33. package/lib/SelectMenu/SelectMenuItem.d.ts +1 -1
  34. package/lib/SelectMenu/SelectMenuModal.d.ts +1 -1
  35. package/lib/TextInputWithTokens.d.ts +11 -13
  36. package/lib/Timeline.d.ts +33 -33
  37. package/lib/Token/AvatarToken.d.ts +1 -1
  38. package/lib/Token/IssueLabelToken.d.ts +1 -1
  39. package/lib/Token/Token.d.ts +1 -1
  40. package/lib/_TextInputWrapper.d.ts +1 -1
  41. package/lib/constants.js +3 -1
  42. package/lib/theme-preval.js +2 -2
  43. package/lib/utils/testing.d.ts +1 -1
  44. package/lib-esm/AnchoredOverlay/AnchoredOverlay.d.ts +2 -4
  45. package/lib-esm/Autocomplete/Autocomplete.d.ts +11 -13
  46. package/lib-esm/Autocomplete/AutocompleteInput.d.ts +11 -13
  47. package/lib-esm/Button/Button.d.ts +11 -11
  48. package/lib-esm/Button/ButtonBase.d.ts +1 -1
  49. package/lib-esm/Button/ButtonClose.d.ts +29 -29
  50. package/lib-esm/Button/ButtonDanger.d.ts +11 -11
  51. package/lib-esm/Button/ButtonInvisible.d.ts +11 -11
  52. package/lib-esm/Button/ButtonOutline.d.ts +11 -11
  53. package/lib-esm/Button/ButtonPrimary.d.ts +11 -11
  54. package/lib-esm/Button/ButtonStyles.js +1 -1
  55. package/lib-esm/CircleBadge.d.ts +2 -2
  56. package/lib-esm/CircleOcticon.d.ts +33 -33
  57. package/lib-esm/Dialog.d.ts +35 -35
  58. package/lib-esm/Dropdown.d.ts +110 -110
  59. package/lib-esm/DropdownMenu/DropdownButton.d.ts +30 -30
  60. package/lib-esm/FilterList.d.ts +26 -26
  61. package/lib-esm/Label.d.ts +1 -1
  62. package/lib-esm/NewButton/button.d.ts +18 -0
  63. package/lib-esm/NewButton/button.js +257 -0
  64. package/lib-esm/NewButton/index.d.ts +4 -0
  65. package/lib-esm/NewButton/index.js +3 -0
  66. package/lib-esm/NewButton/visual.d.ts +6 -0
  67. package/lib-esm/NewButton/visual.js +17 -0
  68. package/lib-esm/Position.d.ts +4 -4
  69. package/lib-esm/ProgressBar.d.ts +1 -1
  70. package/lib-esm/SelectMenu/SelectMenu.d.ts +151 -153
  71. package/lib-esm/SelectMenu/SelectMenuItem.d.ts +1 -1
  72. package/lib-esm/SelectMenu/SelectMenuModal.d.ts +1 -1
  73. package/lib-esm/TextInputWithTokens.d.ts +11 -13
  74. package/lib-esm/Timeline.d.ts +33 -33
  75. package/lib-esm/Token/AvatarToken.d.ts +1 -1
  76. package/lib-esm/Token/IssueLabelToken.d.ts +1 -1
  77. package/lib-esm/Token/Token.d.ts +1 -1
  78. package/lib-esm/_TextInputWrapper.d.ts +1 -1
  79. package/lib-esm/constants.js +3 -1
  80. package/lib-esm/theme-preval.js +2 -2
  81. package/lib-esm/utils/testing.d.ts +1 -1
  82. package/package.json +8 -9
  83. package/lib/DatePicker/DatePicker.d.ts +0 -48
  84. package/lib/DatePicker/DatePicker.js +0 -88
  85. package/lib/DatePicker/DatePickerAnchor.d.ts +0 -5
  86. package/lib/DatePicker/DatePickerAnchor.js +0 -88
  87. package/lib/DatePicker/DatePickerOverlay.d.ts +0 -3
  88. package/lib/DatePicker/DatePickerOverlay.js +0 -39
  89. package/lib/DatePicker/DatePickerPanel.d.ts +0 -2
  90. package/lib/DatePicker/DatePickerPanel.js +0 -126
  91. package/lib/DatePicker/Day.d.ts +0 -14
  92. package/lib/DatePicker/Day.js +0 -174
  93. package/lib/DatePicker/Month.d.ts +0 -9
  94. package/lib/DatePicker/Month.js +0 -97
  95. package/lib/DatePicker/index.d.ts +0 -2
  96. package/lib/DatePicker/index.js +0 -13
  97. package/lib/DatePicker/useDatePicker.d.ts +0 -81
  98. package/lib/DatePicker/useDatePicker.js +0 -371
  99. package/lib-esm/DatePicker/DatePicker.d.ts +0 -48
  100. package/lib-esm/DatePicker/DatePicker.js +0 -71
  101. package/lib-esm/DatePicker/DatePickerAnchor.d.ts +0 -5
  102. package/lib-esm/DatePicker/DatePickerAnchor.js +0 -63
  103. package/lib-esm/DatePicker/DatePickerOverlay.d.ts +0 -3
  104. package/lib-esm/DatePicker/DatePickerOverlay.js +0 -24
  105. package/lib-esm/DatePicker/DatePickerPanel.d.ts +0 -2
  106. package/lib-esm/DatePicker/DatePickerPanel.js +0 -100
  107. package/lib-esm/DatePicker/Day.d.ts +0 -14
  108. package/lib-esm/DatePicker/Day.js +0 -151
  109. package/lib-esm/DatePicker/Month.d.ts +0 -9
  110. package/lib-esm/DatePicker/Month.js +0 -74
  111. package/lib-esm/DatePicker/index.d.ts +0 -2
  112. package/lib-esm/DatePicker/index.js +0 -1
  113. package/lib-esm/DatePicker/useDatePicker.d.ts +0 -81
  114. package/lib-esm/DatePicker/useDatePicker.js +0 -343
@@ -1,343 +0,0 @@
1
- import { format, isEqual, isAfter, isBefore, addMonths, subMonths } from 'date-fns';
2
- import deepmerge from 'deepmerge';
3
- import React, { createContext, useCallback, useContext, useMemo, useEffect, useState } from 'react';
4
- const DatePickerContext = /*#__PURE__*/createContext(null);
5
-
6
- const useDatePicker = date => {
7
- const value = useContext(DatePickerContext);
8
- const [selected, setSelected] = useState(false);
9
-
10
- if (!value) {
11
- throw new Error('useDatePicker must be used inside a DatePickerProvider');
12
- }
13
-
14
- useEffect(() => {
15
- if (date) {
16
- if (value.hoverRange) {
17
- if (isRangeSelection(value.hoverRange)) {
18
- if (isEqual(date, value.hoverRange.from)) {
19
- setSelected('start');
20
- } else if (value.hoverRange.to && isEqual(date, value.hoverRange.to)) {
21
- setSelected('end');
22
- } else if (isAfter(date, value.hoverRange.from) && value.hoverRange.to && isBefore(date, value.hoverRange.to)) {
23
- setSelected('middle');
24
- } else {
25
- setSelected(false);
26
- }
27
- }
28
- } else if (value.selection) {
29
- if (isMultiSelection(value.selection)) {
30
- setSelected(!!value.selection.find(d => isEqual(d, date)));
31
- } else if (isRangeSelection(value.selection)) {
32
- if (isEqual(date, value.selection.from)) {
33
- setSelected('start');
34
- } else if (value.selection.to && isEqual(date, value.selection.to)) {
35
- setSelected('end');
36
- } else if (isAfter(date, value.selection.from) && value.selection.to && isBefore(date, value.selection.to)) {
37
- setSelected('middle');
38
- } else {
39
- setSelected(false);
40
- }
41
- } else {
42
- setSelected(isEqual(date, value.selection));
43
- }
44
- }
45
- }
46
- }, [date, value.hoverRange, value.selection]);
47
- let blocked,
48
- disabled = false;
49
-
50
- if (date) {
51
- // Determine if date is blocked out
52
- if (value.configuration.blockedDates) {
53
- blocked = !!value.configuration.blockedDates.find(d => isEqual(d, date));
54
- } // Determine if date is disabled
55
-
56
-
57
- if (value.configuration.minDate || value.configuration.maxDate) {
58
- disabled = (value.configuration.minDate ? isBefore(date, value.configuration.minDate) : false) || (value.configuration.maxDate ? isAfter(date, value.configuration.maxDate) : false);
59
- }
60
- }
61
-
62
- return { ...value,
63
- blocked,
64
- disabled,
65
- selected
66
- };
67
- };
68
-
69
- export default useDatePicker;
70
- export function isSingleSelection(selection) {
71
- return selection instanceof Date;
72
- }
73
- export function isMultiSelection(selection) {
74
- return Array.isArray(selection);
75
- }
76
- export function isRangeSelection(selection) {
77
- return !!selection.from;
78
- }
79
- export function isStringRangeSelection(selection) {
80
- return !!selection.from;
81
- }
82
-
83
- function parseSelection(selection, variant) {
84
- if (!selection) return;
85
-
86
- if (variant === 'multi') {
87
- if (isMultiSelection(selection)) {
88
- const parsedSelection = [];
89
-
90
- for (const d of selection) {
91
- parsedSelection.push(new Date(new Date(d).toDateString()));
92
- }
93
-
94
- return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
95
- } else if (selection instanceof Date) {
96
- return [new Date(new Date(selection).toDateString())];
97
- } else if (isRangeSelection(selection)) {
98
- const parsedSelection = [];
99
- parsedSelection.push(new Date(new Date(selection.from).toDateString()));
100
-
101
- if (selection.to) {
102
- parsedSelection.push(new Date(new Date(selection.to).toDateString()));
103
- }
104
-
105
- return parsedSelection.sort((a, b) => a.getTime() - b.getTime());
106
- }
107
- } else if (variant === 'range') {
108
- if (isRangeSelection(selection)) {
109
- return {
110
- from: new Date(new Date(selection.from).toDateString()),
111
- to: selection.to ? new Date(new Date(selection.to).toDateString()) : null
112
- };
113
- } else if (isMultiSelection(selection)) {
114
- return {
115
- from: new Date(new Date(selection[0]).toDateString()),
116
- to: selection[1] ? new Date(new Date(selection[1]).toDateString()) : null
117
- };
118
- } else if (selection instanceof Date) {
119
- return {
120
- from: new Date(new Date(selection).toDateString()),
121
- to: null
122
- };
123
- }
124
- } else {
125
- if (selection instanceof Date) {
126
- return new Date(new Date(selection).toDateString());
127
- } else if (isMultiSelection(selection)) {
128
- return new Date(new Date(selection[0]).toDateString());
129
- } else if (isRangeSelection(selection)) {
130
- return new Date(new Date(selection.from).toDateString());
131
- } else {
132
- return;
133
- }
134
- }
135
- }
136
-
137
- const defaultConfiguration = {
138
- anchorVariant: 'button',
139
- confirmation: false,
140
- contiguousSelection: false,
141
- dimWeekends: false,
142
- placeholder: 'Select a Date...',
143
- selection: 'single',
144
- view: '2-month'
145
- };
146
- export const DatePickerProvider = ({
147
- configuration: externalConfig = {},
148
- children,
149
- closePicker,
150
- value
151
- }) => {
152
- const [configuration, setConfiguration] = useState(deepmerge(defaultConfiguration, externalConfig));
153
- const [previousSelection, setPreviousSelection] = useState(parseSelection(value, configuration.selection));
154
- const [selection, setSelection] = useState(parseSelection(value, configuration.selection));
155
- const [hoverRange, setHoverRange] = useState(null);
156
- const [currentViewingDate, setCurrentViewingDate] = useState(new Date());
157
- useEffect(() => {
158
- setConfiguration(deepmerge(defaultConfiguration, externalConfig));
159
- setSelection(parseSelection(selection, configuration.selection)); // Don't want this to run every time selection gets updated
160
- // eslint-disable-next-line react-hooks/exhaustive-deps
161
- }, [configuration.selection, externalConfig]);
162
- const goToMonth = useCallback(date => {
163
- setCurrentViewingDate(new Date(new Date(date).toDateString()));
164
- }, []);
165
- const nextMonth = useCallback(() => {
166
- setCurrentViewingDate(addMonths(currentViewingDate, 1));
167
- }, [currentViewingDate]);
168
- const previousMonth = useCallback(() => {
169
- setCurrentViewingDate(subMonths(currentViewingDate, 1));
170
- }, [currentViewingDate]);
171
- const getFormattedDate = useMemo(() => {
172
- if (!selection) {
173
- return configuration.placeholder;
174
- }
175
-
176
- let template = 'MMM d';
177
-
178
- if (configuration.dateFormat) {
179
- switch (configuration.dateFormat) {
180
- case 'short':
181
- template = 'MMM d';
182
- break;
183
-
184
- case 'long':
185
- template = 'MMM d, yyyy';
186
- break;
187
-
188
- default:
189
- template = configuration.dateFormat;
190
- break;
191
- }
192
- }
193
-
194
- switch (configuration.selection) {
195
- case 'single':
196
- {
197
- if (selection instanceof Date) {
198
- return format(selection, template);
199
- } else if (Array.isArray(selection)) {
200
- return format(selection[0], template);
201
- } else if (isRangeSelection(selection)) {
202
- return format(selection.from, template);
203
- } else {
204
- return 'Invalid Selection';
205
- }
206
- }
207
-
208
- case 'multi':
209
- {
210
- if (Array.isArray(selection)) {
211
- if (selection.length > 3) return `${selection.length} Selected`;
212
- const formatted = selection.map(d => format(d, template)).join(', ');
213
- return formatted;
214
- } else if (selection instanceof Date) {
215
- return [selection].map(d => format(d, template)).join(', ');
216
- } else if (isRangeSelection(selection)) {
217
- return [selection.to, selection.from].map(d => d ? format(d, template) : '').join(', ');
218
- } else {
219
- return 'Invalid Selection';
220
- }
221
- }
222
-
223
- case 'range':
224
- {
225
- if (isRangeSelection(selection)) {
226
- return Object.entries(selection).map(([_, date]) => date ? format(date, template) : '').join(' - ');
227
- } else if (selection instanceof Date) {
228
- return Object.entries({
229
- from: selection,
230
- to: null
231
- }).map(([_, date]) => date ? format(date, template) : '').join(' - ');
232
- } else if (Array.isArray(selection)) {
233
- return Object.entries({
234
- from: selection[0],
235
- to: selection[1]
236
- }) // to date can still be null
237
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
238
- .map(([_, date]) => date ? format(date, template) : '').join(' - ');
239
- } else {
240
- return 'Invalid Selection';
241
- }
242
- }
243
-
244
- default:
245
- {
246
- return 'Invalid Configuration';
247
- }
248
- }
249
- }, [configuration.dateFormat, configuration.placeholder, configuration.selection, selection]);
250
- const saveValue = useCallback(updatedSelection => {
251
- setPreviousSelection(updatedSelection !== null && updatedSelection !== void 0 ? updatedSelection : selection);
252
- closePicker === null || closePicker === void 0 ? void 0 : closePicker();
253
- }, [closePicker, selection]);
254
- const selectionHandler = useCallback(date => {
255
- if (configuration.selection === 'multi') {
256
- const selections = [...selection];
257
- const existingIndex = selections.findIndex(s => isEqual(s, date));
258
-
259
- if (existingIndex > -1) {
260
- selections.splice(existingIndex, 1);
261
- setSelection(selections.sort((a, b) => a.getTime() - b.getTime()));
262
- } else {
263
- setSelection([...selections, date].sort((a, b) => a.getTime() - b.getTime()));
264
- }
265
- } else if (configuration.selection === 'range') {
266
- if (selection && isRangeSelection(selection) && !selection.to) {
267
- const updatedSelection = isBefore(date, selection.from) ? {
268
- from: date,
269
- to: selection.from
270
- } : {
271
- from: selection.from,
272
- to: date
273
- };
274
- setSelection(updatedSelection);
275
- setHoverRange(null);
276
-
277
- if (!configuration.confirmation) {
278
- saveValue(updatedSelection);
279
- }
280
- } else {
281
- setHoverRange({
282
- from: date,
283
- to: date
284
- });
285
- setSelection({
286
- from: date,
287
- to: null
288
- });
289
- }
290
- } else {
291
- setSelection(date);
292
-
293
- if (!configuration.confirmation) {
294
- saveValue(date);
295
- }
296
- }
297
- }, [configuration.confirmation, configuration.selection, saveValue, selection]);
298
- const focusHnadler = useCallback(date => {
299
- if (!selection) return;
300
-
301
- if (configuration.selection === 'range' && isRangeSelection(selection) && hoverRange) {
302
- setHoverRange(isBefore(date, selection.from) ? {
303
- from: date,
304
- to: selection.from
305
- } : {
306
- from: selection.from,
307
- to: date
308
- });
309
- }
310
- }, [configuration.selection, hoverRange, selection]);
311
- const blurHnadler = useCallback(date => {
312
- if (!selection || !hoverRange) return;
313
-
314
- if (configuration.selection === 'range' && isRangeSelection(selection) && (hoverRange.from === date || hoverRange.to === date)) {// setHoverRange({from: hoverRange.from, to: hoverRange.from})
315
- }
316
- }, [configuration.selection, hoverRange, selection]);
317
- const revertValue = useCallback(() => {
318
- setSelection(previousSelection);
319
- }, [previousSelection]);
320
- const datePickerCtx = useMemo(() => {
321
- return {
322
- configuration,
323
- currentViewingDate,
324
- disabled: false,
325
- formattedDate: getFormattedDate,
326
- goToMonth,
327
- hoverRange,
328
- nextMonth,
329
- onDayBlur: blurHnadler,
330
- onDayFocus: focusHnadler,
331
- onSelection: selectionHandler,
332
- previousMonth,
333
- revertValue,
334
- saveValue,
335
- selectionActive: false,
336
- selection
337
- };
338
- }, [blurHnadler, configuration, currentViewingDate, focusHnadler, getFormattedDate, goToMonth, hoverRange, nextMonth, previousMonth, revertValue, saveValue, selection, selectionHandler]);
339
- return /*#__PURE__*/React.createElement(DatePickerContext.Provider, {
340
- value: datePickerCtx
341
- }, children);
342
- };
343
- DatePickerProvider.displayName = "DatePickerProvider";