@activecollab/components 2.0.574 → 2.0.576

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 (121) hide show
  1. package/dist/cjs/components/MainMenu/MainMenu.js +44 -40
  2. package/dist/cjs/components/MainMenu/MainMenu.js.map +1 -1
  3. package/dist/cjs/components/MainMenu/MainMenuSettings.js +112 -0
  4. package/dist/cjs/components/MainMenu/MainMenuSettings.js.map +1 -0
  5. package/dist/cjs/components/MainMenu/Styles.js +8 -3
  6. package/dist/cjs/components/MainMenu/Styles.js.map +1 -1
  7. package/dist/cjs/components/MainMenu/hooks.js +1 -9
  8. package/dist/cjs/components/MainMenu/hooks.js.map +1 -1
  9. package/dist/cjs/components/MainMenu/types.js +4 -2
  10. package/dist/cjs/components/MainMenu/types.js.map +1 -1
  11. package/dist/cjs/components/Popper/Popper.js +14 -0
  12. package/dist/cjs/components/Popper/Popper.js.map +1 -1
  13. package/dist/cjs/components/Popper/anchorMove.js +42 -0
  14. package/dist/cjs/components/Popper/anchorMove.js.map +1 -0
  15. package/dist/cjs/components/ScrollArea/ScrollArea.js +5 -3
  16. package/dist/cjs/components/ScrollArea/ScrollArea.js.map +1 -1
  17. package/dist/cjs/components/ViewSettings/Styles.js +26 -0
  18. package/dist/cjs/components/ViewSettings/Styles.js.map +1 -0
  19. package/dist/cjs/components/ViewSettings/ViewSettings.js +96 -0
  20. package/dist/cjs/components/ViewSettings/ViewSettings.js.map +1 -0
  21. package/dist/cjs/components/ViewSettings/index.js +17 -0
  22. package/dist/cjs/components/ViewSettings/index.js.map +1 -0
  23. package/dist/cjs/components/ViewSwitcher/ApplyToAllTab.js +199 -0
  24. package/dist/cjs/components/ViewSwitcher/ApplyToAllTab.js.map +1 -0
  25. package/dist/cjs/components/ViewSwitcher/Styles.js +73 -61
  26. package/dist/cjs/components/ViewSwitcher/Styles.js.map +1 -1
  27. package/dist/cjs/components/ViewSwitcher/{ViewSwitcherMenu.js → SubViewSwitcher.js} +70 -22
  28. package/dist/cjs/components/ViewSwitcher/SubViewSwitcher.js.map +1 -0
  29. package/dist/cjs/components/ViewSwitcher/ViewSwitcher.js +46 -184
  30. package/dist/cjs/components/ViewSwitcher/ViewSwitcher.js.map +1 -1
  31. package/dist/cjs/components/ViewSwitcher/index.js +4 -4
  32. package/dist/cjs/components/ViewSwitcher/index.js.map +1 -1
  33. package/dist/cjs/components/ViewSwitcher/useApplyToAllOffer.js +179 -0
  34. package/dist/cjs/components/ViewSwitcher/useApplyToAllOffer.js.map +1 -0
  35. package/dist/cjs/components/index.js +12 -0
  36. package/dist/cjs/components/index.js.map +1 -1
  37. package/dist/cjs/presentation/shared/ActivityFeed.js +1 -2
  38. package/dist/cjs/presentation/shared/ActivityFeed.js.map +1 -1
  39. package/dist/esm/components/MainMenu/MainMenu.d.ts +6 -4
  40. package/dist/esm/components/MainMenu/MainMenu.d.ts.map +1 -1
  41. package/dist/esm/components/MainMenu/MainMenu.js +39 -40
  42. package/dist/esm/components/MainMenu/MainMenu.js.map +1 -1
  43. package/dist/esm/components/MainMenu/MainMenuSettings.d.ts +14 -0
  44. package/dist/esm/components/MainMenu/MainMenuSettings.d.ts.map +1 -0
  45. package/dist/esm/components/MainMenu/MainMenuSettings.js +84 -0
  46. package/dist/esm/components/MainMenu/MainMenuSettings.js.map +1 -0
  47. package/dist/esm/components/MainMenu/Styles.d.ts +11 -0
  48. package/dist/esm/components/MainMenu/Styles.d.ts.map +1 -1
  49. package/dist/esm/components/MainMenu/Styles.js +7 -2
  50. package/dist/esm/components/MainMenu/Styles.js.map +1 -1
  51. package/dist/esm/components/MainMenu/hooks.d.ts.map +1 -1
  52. package/dist/esm/components/MainMenu/hooks.js +1 -8
  53. package/dist/esm/components/MainMenu/hooks.js.map +1 -1
  54. package/dist/esm/components/MainMenu/types.d.ts +4 -2
  55. package/dist/esm/components/MainMenu/types.d.ts.map +1 -1
  56. package/dist/esm/components/MainMenu/types.js +4 -2
  57. package/dist/esm/components/MainMenu/types.js.map +1 -1
  58. package/dist/esm/components/Popper/Popper.d.ts.map +1 -1
  59. package/dist/esm/components/Popper/Popper.js +12 -0
  60. package/dist/esm/components/Popper/Popper.js.map +1 -1
  61. package/dist/esm/components/Popper/anchorMove.d.ts +4 -0
  62. package/dist/esm/components/Popper/anchorMove.d.ts.map +1 -0
  63. package/dist/esm/components/Popper/anchorMove.js +33 -0
  64. package/dist/esm/components/Popper/anchorMove.js.map +1 -0
  65. package/dist/esm/components/ScrollArea/ScrollArea.d.ts +2 -0
  66. package/dist/esm/components/ScrollArea/ScrollArea.d.ts.map +1 -1
  67. package/dist/esm/components/ScrollArea/ScrollArea.js +5 -3
  68. package/dist/esm/components/ScrollArea/ScrollArea.js.map +1 -1
  69. package/dist/esm/components/ViewSettings/Styles.d.ts +4 -0
  70. package/dist/esm/components/ViewSettings/Styles.d.ts.map +1 -0
  71. package/dist/esm/components/ViewSettings/Styles.js +20 -0
  72. package/dist/esm/components/ViewSettings/Styles.js.map +1 -0
  73. package/dist/esm/components/ViewSettings/ViewSettings.d.ts +20 -0
  74. package/dist/esm/components/ViewSettings/ViewSettings.d.ts.map +1 -0
  75. package/dist/esm/components/ViewSettings/ViewSettings.js +87 -0
  76. package/dist/esm/components/ViewSettings/ViewSettings.js.map +1 -0
  77. package/dist/esm/components/ViewSettings/index.d.ts +2 -0
  78. package/dist/esm/components/ViewSettings/index.d.ts.map +1 -0
  79. package/dist/esm/components/ViewSettings/index.js +2 -0
  80. package/dist/esm/components/ViewSettings/index.js.map +1 -0
  81. package/dist/esm/components/ViewSwitcher/ApplyToAllTab.d.ts +24 -0
  82. package/dist/esm/components/ViewSwitcher/ApplyToAllTab.d.ts.map +1 -0
  83. package/dist/esm/components/ViewSwitcher/ApplyToAllTab.js +179 -0
  84. package/dist/esm/components/ViewSwitcher/ApplyToAllTab.js.map +1 -0
  85. package/dist/esm/components/ViewSwitcher/Styles.d.ts +15 -15
  86. package/dist/esm/components/ViewSwitcher/Styles.d.ts.map +1 -1
  87. package/dist/esm/components/ViewSwitcher/Styles.js +72 -60
  88. package/dist/esm/components/ViewSwitcher/Styles.js.map +1 -1
  89. package/dist/esm/components/ViewSwitcher/SubViewSwitcher.d.ts +50 -0
  90. package/dist/esm/components/ViewSwitcher/SubViewSwitcher.d.ts.map +1 -0
  91. package/dist/esm/components/ViewSwitcher/SubViewSwitcher.js +149 -0
  92. package/dist/esm/components/ViewSwitcher/SubViewSwitcher.js.map +1 -0
  93. package/dist/esm/components/ViewSwitcher/ViewSwitcher.d.ts +8 -8
  94. package/dist/esm/components/ViewSwitcher/ViewSwitcher.d.ts.map +1 -1
  95. package/dist/esm/components/ViewSwitcher/ViewSwitcher.js +48 -163
  96. package/dist/esm/components/ViewSwitcher/ViewSwitcher.js.map +1 -1
  97. package/dist/esm/components/ViewSwitcher/index.d.ts +1 -1
  98. package/dist/esm/components/ViewSwitcher/index.d.ts.map +1 -1
  99. package/dist/esm/components/ViewSwitcher/index.js +1 -1
  100. package/dist/esm/components/ViewSwitcher/index.js.map +1 -1
  101. package/dist/esm/components/ViewSwitcher/useApplyToAllOffer.d.ts +27 -0
  102. package/dist/esm/components/ViewSwitcher/useApplyToAllOffer.d.ts.map +1 -0
  103. package/dist/esm/components/ViewSwitcher/useApplyToAllOffer.js +159 -0
  104. package/dist/esm/components/ViewSwitcher/useApplyToAllOffer.js.map +1 -0
  105. package/dist/esm/components/index.d.ts +1 -0
  106. package/dist/esm/components/index.d.ts.map +1 -1
  107. package/dist/esm/components/index.js +1 -0
  108. package/dist/esm/components/index.js.map +1 -1
  109. package/dist/esm/presentation/shared/ActivityFeed.d.ts.map +1 -1
  110. package/dist/esm/presentation/shared/ActivityFeed.js +1 -2
  111. package/dist/esm/presentation/shared/ActivityFeed.js.map +1 -1
  112. package/dist/index.js +1679 -1174
  113. package/dist/index.js.map +1 -1
  114. package/dist/index.min.js +1 -1
  115. package/dist/index.min.js.map +1 -1
  116. package/package.json +1 -1
  117. package/dist/cjs/components/ViewSwitcher/ViewSwitcherMenu.js.map +0 -1
  118. package/dist/esm/components/ViewSwitcher/ViewSwitcherMenu.d.ts +0 -39
  119. package/dist/esm/components/ViewSwitcher/ViewSwitcherMenu.d.ts.map +0 -1
  120. package/dist/esm/components/ViewSwitcher/ViewSwitcherMenu.js +0 -101
  121. package/dist/esm/components/ViewSwitcher/ViewSwitcherMenu.js.map +0 -1
@@ -1,18 +1,19 @@
1
1
  import _extends from "@babel/runtime/helpers/esm/extends";
2
2
  import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/esm/objectWithoutPropertiesLoose";
3
- const _excluded = ["views", "value", "onChange", "aria-label", "onApplyToAll", "applyToAllLabel", "applyToAllTitle", "offerTitle", "dismissLabel", "appliedLabel", "offerKey", "className", "onKeyDown", "onBlur"];
4
- import React, { forwardRef, useCallback, useEffect, useId, useRef, useState } from "react";
3
+ const _excluded = ["views", "value", "onChange", "aria-label", "onApplyToAll", "applyToAllTitle", "offerTitle", "appliedLabel", "applyToAllLabel", "dismissLabel", "offerKey", "className", "onKeyDown", "onBlur"];
4
+ import React, { forwardRef, useCallback, useEffect, useRef } from "react";
5
5
  import classnames from "classnames";
6
- import { StyledViewSwitcher, StyledViewSwitcherAction, StyledViewSwitcherApplied, StyledViewSwitcherDismiss, StyledViewSwitcherLine, StyledViewSwitcherOption, StyledViewSwitcherOptions, StyledViewSwitcherPrompt, StyledViewSwitcherPromptClip, StyledViewSwitcherPromptPill, StyledViewSwitcherPromptTitle, StyledViewSwitcherTrack, VIEW_SWITCHER_APPLIED_MS, VIEW_SWITCHER_LEAVE_MS, VIEW_SWITCHER_PROMPT_DELAY_MS, VIEW_SWITCHER_PROMPT_MS } from "./Styles";
6
+ import { ApplyToAllTab } from "./ApplyToAllTab";
7
+ import { StyledViewSwitcher, StyledViewSwitcherOption, StyledViewSwitcherOptions, StyledViewSwitcherTrack } from "./Styles";
8
+ import { useApplyToAllOffer } from "./useApplyToAllOffer";
7
9
  import useForkRef from "../../utils/useForkRef";
8
- import { CheckIcon, CloseSmallIcon } from "../Icons";
9
10
  import { Tooltip } from "../Tooltip";
10
11
  /**
11
12
  * @component ViewSwitcher
12
13
  * @description
13
- * Icon-only radiogroup that switches the view of a page. A tooltip shows the view name on hover.
14
- * With onApplyToAll, a switch or a click on the checked view opens a compact extra action beside the buttons
15
- * after a short delay, for five seconds.
14
+ * The main view switcher: an icon-only radiogroup that switches the view of a page. A tooltip shows the view name on hover.
15
+ * With onApplyToAll, a switch or a click on the checked view slides a check button out from under the right end
16
+ * after a short delay. It applies the view to all projects, and a click outside closes it.
16
17
  *
17
18
  * @example
18
19
  * <ViewSwitcher
@@ -34,15 +35,15 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
34
35
  _ref$ariaLabel = _ref["aria-label"],
35
36
  ariaLabel = _ref$ariaLabel === void 0 ? "View" : _ref$ariaLabel,
36
37
  onApplyToAll = _ref.onApplyToAll,
37
- _ref$applyToAllLabel = _ref.applyToAllLabel,
38
- applyToAllLabel = _ref$applyToAllLabel === void 0 ? "All Projects" : _ref$applyToAllLabel,
39
38
  _ref$applyToAllTitle = _ref.applyToAllTitle,
40
39
  applyToAllTitle = _ref$applyToAllTitle === void 0 ? "Apply to All Projects" : _ref$applyToAllTitle,
41
40
  offerTitle = _ref.offerTitle,
42
- _ref$dismissLabel = _ref.dismissLabel,
43
- dismissLabel = _ref$dismissLabel === void 0 ? "Dismiss" : _ref$dismissLabel,
44
41
  _ref$appliedLabel = _ref.appliedLabel,
45
42
  appliedLabel = _ref$appliedLabel === void 0 ? "Applied" : _ref$appliedLabel,
43
+ _ref$applyToAllLabel = _ref.applyToAllLabel,
44
+ applyToAllLabel = _ref$applyToAllLabel === void 0 ? "All Projects?" : _ref$applyToAllLabel,
45
+ _ref$dismissLabel = _ref.dismissLabel,
46
+ dismissLabel = _ref$dismissLabel === void 0 ? "Dismiss" : _ref$dismissLabel,
46
47
  offerKey = _ref.offerKey,
47
48
  className = _ref.className,
48
49
  onKeyDown = _ref.onKeyDown,
@@ -51,50 +52,40 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
51
52
  const rootRef = useRef(null);
52
53
  const handleRootRef = useForkRef(rootRef, ref);
53
54
  const optionRefs = useRef({});
54
- const promptRef = useRef(null);
55
- const _useState = useState(null),
56
- offerId = _useState[0],
57
- setOfferId = _useState[1];
58
- const _useState2 = useState("waiting"),
59
- offerPhase = _useState2[0],
60
- setOfferPhase = _useState2[1];
61
- const _useState3 = useState(false),
62
- isConfirmed = _useState3[0],
63
- setIsConfirmed = _useState3[1];
64
- const offerTitleId = useId();
65
- const offerIdRef = useRef(offerId);
66
- const offerPhaseRef = useRef(offerPhase);
67
- const restoreFocusRef = useRef(false);
68
- offerIdRef.current = offerId;
69
- offerPhaseRef.current = offerPhase;
70
55
  const checkedIndex = views.findIndex(_ref2 => {
71
56
  let id = _ref2.id;
72
57
  return id === value;
73
58
  });
74
59
  const tabStopId = (_views = views[checkedIndex >= 0 ? checkedIndex : 0]) == null ? void 0 : _views.id;
60
+ const focusChecked = useCallback(() => {
61
+ if (tabStopId) {
62
+ var _optionRefs$current$t;
63
+ (_optionRefs$current$t = optionRefs.current[tabStopId]) == null || _optionRefs$current$t.focus();
64
+ }
65
+ }, [tabStopId]);
66
+ const offer = useApplyToAllOffer({
67
+ rootRef,
68
+ onApplyToAll,
69
+ onRestoreFocus: focusChecked
70
+ });
71
+ const show = offer.show,
72
+ isUp = offer.isUp;
75
73
  const offerView = views.find(_ref3 => {
74
+ var _offer$offer;
76
75
  let id = _ref3.id;
77
- return id === offerId;
76
+ return id === ((_offer$offer = offer.offer) == null ? void 0 : _offer$offer.value);
78
77
  });
79
78
  const select = useCallback(id => {
80
79
  // A click on the checked view also offers the extra action.
81
80
  if (id === value) {
82
- const isOfferUp = offerIdRef.current && ["open", "applying", "applied"].includes(offerPhaseRef.current);
83
- if (onApplyToAll && !isOfferUp) {
84
- setOfferPhase("waiting");
85
- setIsConfirmed(false);
86
- setOfferId(id);
81
+ if (!isUp) {
82
+ show(id);
87
83
  }
88
84
  return;
89
85
  }
90
86
  onChange(id);
91
- if (onApplyToAll) {
92
- // An open prompt stays for the next switch, and only a new one waits.
93
- setOfferPhase(phase => offerIdRef.current && phase === "open" ? "open" : "waiting");
94
- setIsConfirmed(false);
95
- setOfferId(id);
96
- }
97
- }, [onApplyToAll, onChange, value]);
87
+ show(id);
88
+ }, [isUp, onChange, show, value]);
98
89
  const seenOfferKeyRef = useRef(offerKey);
99
90
  useEffect(() => {
100
91
  if (seenOfferKeyRef.current === offerKey) {
@@ -105,58 +96,6 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
105
96
  select(value);
106
97
  }
107
98
  }, [offerKey, select, value]);
108
- const isInside = node => {
109
- var _rootRef$current;
110
- return Boolean(node && ((_rootRef$current = rootRef.current) == null ? void 0 : _rootRef$current.contains(node)));
111
- };
112
- const focusChecked = useCallback(() => {
113
- if (tabStopId) {
114
- var _optionRefs$current$t;
115
- (_optionRefs$current$t = optionRefs.current[tabStopId]) == null || _optionRefs$current$t.focus();
116
- }
117
- }, [tabStopId]);
118
- const leave = useCallback(() => {
119
- var _promptRef$current;
120
- restoreFocusRef.current = Boolean((_promptRef$current = promptRef.current) == null ? void 0 : _promptRef$current.contains(document.activeElement));
121
- if (offerPhaseRef.current === "waiting") {
122
- setOfferId(null);
123
- return;
124
- }
125
- setOfferPhase("leaving");
126
- }, []);
127
- useEffect(() => {
128
- if (!offerId && restoreFocusRef.current) {
129
- restoreFocusRef.current = false;
130
- focusChecked();
131
- }
132
- }, [focusChecked, offerId]);
133
- useEffect(() => {
134
- if (!offerId || offerPhase === "applying") {
135
- return undefined;
136
- }
137
- if (offerPhase === "waiting") {
138
- const timer = setTimeout(() => setOfferPhase("open"), VIEW_SWITCHER_PROMPT_DELAY_MS);
139
- return () => clearTimeout(timer);
140
- }
141
- if (offerPhase === "leaving") {
142
- const timer = setTimeout(() => setOfferId(null), VIEW_SWITCHER_LEAVE_MS);
143
- return () => clearTimeout(timer);
144
- }
145
- const timer = setTimeout(leave, offerPhase === "applied" ? VIEW_SWITCHER_APPLIED_MS : VIEW_SWITCHER_PROMPT_MS);
146
- return () => clearTimeout(timer);
147
- }, [leave, offerId, offerPhase]);
148
- useEffect(() => {
149
- if (!offerId) {
150
- return undefined;
151
- }
152
- const leaveOutside = event => {
153
- if (!isInside(event.target)) {
154
- leave();
155
- }
156
- };
157
- document.addEventListener("mousedown", leaveOutside);
158
- return () => document.removeEventListener("mousedown", leaveOutside);
159
- }, [leave, offerId]);
160
99
  const handleOptionKeyDown = (event, index) => {
161
100
  var _optionRefs$current$i;
162
101
  const last = views.length - 1;
@@ -179,46 +118,11 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
179
118
  };
180
119
  const handleRootKeyDown = event => {
181
120
  onKeyDown == null || onKeyDown(event);
182
- if (event.key !== "Escape" || !offerId) {
183
- return;
184
- }
185
- if (offerPhase === "waiting") {
186
- leave();
187
- return;
188
- }
189
-
190
- // A native dialog around the switcher would take an Escape that is not prevented as its own close request.
191
- event.preventDefault();
192
- event.stopPropagation();
193
- leave();
121
+ offer.handleKeyDown(event);
194
122
  };
195
123
  const handleRootBlur = event => {
196
124
  onBlur == null || onBlur(event);
197
- const next = event.relatedTarget;
198
- if (offerId && next && !isInside(next)) {
199
- leave();
200
- }
201
- };
202
- const applyToAll = async () => {
203
- var _promptRef$current2;
204
- if (!offerId || !onApplyToAll || offerPhase !== "open") {
205
- return;
206
- }
207
- let isApplied = false;
208
-
209
- // The action hides while it confirms, so the focus goes back to the views now.
210
- if ((_promptRef$current2 = promptRef.current) != null && _promptRef$current2.contains(document.activeElement)) {
211
- focusChecked();
212
- }
213
- setOfferPhase("applying");
214
- try {
215
- isApplied = (await onApplyToAll(offerId)) !== false;
216
- } finally {
217
- if (offerIdRef.current === offerId) {
218
- setIsConfirmed(isApplied);
219
- setOfferPhase(isApplied ? "applied" : "leaving");
220
- }
221
- }
125
+ offer.handleBlur(event);
222
126
  };
223
127
  return /*#__PURE__*/React.createElement(StyledViewSwitcher, _extends({
224
128
  ref: handleRootRef,
@@ -227,38 +131,7 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
227
131
  onBlur: handleRootBlur
228
132
  }, rest), /*#__PURE__*/React.createElement(StyledViewSwitcherTrack, {
229
133
  className: "c-view-switcher__track"
230
- }, offerView && offerPhase !== "waiting" ? /*#__PURE__*/React.createElement(StyledViewSwitcherPrompt, {
231
- ref: promptRef,
232
- role: "group",
233
- "aria-labelledby": offerTitleId,
234
- className: "c-view-switcher__prompt",
235
- "data-phase": offerPhase,
236
- "data-confirmed": isConfirmed || undefined
237
- }, /*#__PURE__*/React.createElement(StyledViewSwitcherPromptTitle, {
238
- id: offerTitleId
239
- }, offerTitle ? offerTitle(offerView) : "Now in " + offerView.label), /*#__PURE__*/React.createElement(StyledViewSwitcherPromptClip, null, /*#__PURE__*/React.createElement(StyledViewSwitcherPromptPill, {
240
- className: "c-view-switcher__prompt-pill"
241
- }, /*#__PURE__*/React.createElement(Tooltip, {
242
- title: applyToAllTitle,
243
- placement: "bottom"
244
- }, /*#__PURE__*/React.createElement(StyledViewSwitcherAction, {
245
- type: "button",
246
- "aria-label": applyToAllTitle,
247
- className: "c-view-switcher__action",
248
- onClick: applyToAll
249
- }, /*#__PURE__*/React.createElement(CheckIcon, null), applyToAllLabel)), /*#__PURE__*/React.createElement(StyledViewSwitcherLine, {
250
- className: "c-view-switcher__line"
251
- }), /*#__PURE__*/React.createElement(Tooltip, {
252
- title: dismissLabel,
253
- placement: "bottom"
254
- }, /*#__PURE__*/React.createElement(StyledViewSwitcherDismiss, {
255
- type: "button",
256
- "aria-label": dismissLabel,
257
- className: "c-view-switcher__dismiss",
258
- onClick: leave
259
- }, /*#__PURE__*/React.createElement(CloseSmallIcon, null))), isConfirmed ? /*#__PURE__*/React.createElement(StyledViewSwitcherApplied, {
260
- role: "status"
261
- }, /*#__PURE__*/React.createElement(CheckIcon, null), appliedLabel) : null))) : null, /*#__PURE__*/React.createElement(StyledViewSwitcherOptions, {
134
+ }, /*#__PURE__*/React.createElement(StyledViewSwitcherOptions, {
262
135
  role: "radiogroup",
263
136
  "aria-label": ariaLabel
264
137
  }, views.map((view, index) => {
@@ -266,7 +139,7 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
266
139
  return /*#__PURE__*/React.createElement(Tooltip, {
267
140
  key: view.id,
268
141
  title: view.label,
269
- placement: "bottom"
142
+ placement: "top"
270
143
  }, /*#__PURE__*/React.createElement(StyledViewSwitcherOption, {
271
144
  ref: node => {
272
145
  optionRefs.current[view.id] = node;
@@ -280,7 +153,19 @@ export const ViewSwitcher = /*#__PURE__*/forwardRef((_ref, ref) => {
280
153
  onClick: () => select(view.id),
281
154
  onKeyDown: event => handleOptionKeyDown(event, index)
282
155
  }, view.icon));
283
- }))));
156
+ }))), offerView && offer.isShown ? /*#__PURE__*/React.createElement(ApplyToAllTab, {
157
+ ref: offer.tabRef,
158
+ phase: offer.phase,
159
+ isConfirmed: offer.isConfirmed,
160
+ title: applyToAllTitle,
161
+ label: offerTitle ? offerTitle(offerView) : "Now in " + offerView.label,
162
+ appliedLabel: appliedLabel,
163
+ onApply: offer.apply,
164
+ onDismiss: offer.leave,
165
+ onHold: offer.hold,
166
+ dismissLabel: dismissLabel,
167
+ applyToAllLabel: applyToAllLabel
168
+ }) : null);
284
169
  });
285
170
  ViewSwitcher.displayName = "ViewSwitcher";
286
171
  //# sourceMappingURL=ViewSwitcher.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"ViewSwitcher.js","names":["React","forwardRef","useCallback","useEffect","useId","useRef","useState","classnames","StyledViewSwitcher","StyledViewSwitcherAction","StyledViewSwitcherApplied","StyledViewSwitcherDismiss","StyledViewSwitcherLine","StyledViewSwitcherOption","StyledViewSwitcherOptions","StyledViewSwitcherPrompt","StyledViewSwitcherPromptClip","StyledViewSwitcherPromptPill","StyledViewSwitcherPromptTitle","StyledViewSwitcherTrack","VIEW_SWITCHER_APPLIED_MS","VIEW_SWITCHER_LEAVE_MS","VIEW_SWITCHER_PROMPT_DELAY_MS","VIEW_SWITCHER_PROMPT_MS","useForkRef","CheckIcon","CloseSmallIcon","Tooltip","ViewSwitcher","_ref","ref","_views","views","value","onChange","_ref$ariaLabel","ariaLabel","onApplyToAll","_ref$applyToAllLabel","applyToAllLabel","_ref$applyToAllTitle","applyToAllTitle","offerTitle","_ref$dismissLabel","dismissLabel","_ref$appliedLabel","appliedLabel","offerKey","className","onKeyDown","onBlur","rest","_objectWithoutPropertiesLoose","_excluded","rootRef","handleRootRef","optionRefs","promptRef","_useState","offerId","setOfferId","_useState2","offerPhase","setOfferPhase","_useState3","isConfirmed","setIsConfirmed","offerTitleId","offerIdRef","offerPhaseRef","restoreFocusRef","current","checkedIndex","findIndex","_ref2","id","tabStopId","offerView","find","_ref3","select","isOfferUp","includes","phase","seenOfferKeyRef","isInside","node","_rootRef$current","Boolean","contains","focusChecked","_optionRefs$current$t","focus","leave","_promptRef$current","document","activeElement","undefined","timer","setTimeout","clearTimeout","leaveOutside","event","target","addEventListener","removeEventListener","handleOptionKeyDown","index","_optionRefs$current$i","last","length","targets","ArrowRight","ArrowDown","ArrowLeft","ArrowUp","Home","End","key","preventDefault","handleRootKeyDown","stopPropagation","handleRootBlur","next","relatedTarget","applyToAll","_promptRef$current2","isApplied","createElement","_extends","role","label","title","placement","type","onClick","map","view","checked","tabIndex","icon","displayName"],"sources":["../../../../src/components/ViewSwitcher/ViewSwitcher.tsx"],"sourcesContent":["import React, {\n FocusEvent,\n forwardRef,\n HTMLAttributes,\n KeyboardEvent,\n ReactNode,\n useCallback,\n useEffect,\n useId,\n useRef,\n useState,\n} from \"react\";\n\nimport classnames from \"classnames\";\n\nimport {\n StyledViewSwitcher,\n StyledViewSwitcherAction,\n StyledViewSwitcherApplied,\n StyledViewSwitcherDismiss,\n StyledViewSwitcherLine,\n StyledViewSwitcherOption,\n StyledViewSwitcherOptions,\n StyledViewSwitcherPrompt,\n StyledViewSwitcherPromptClip,\n StyledViewSwitcherPromptPill,\n StyledViewSwitcherPromptTitle,\n StyledViewSwitcherTrack,\n VIEW_SWITCHER_APPLIED_MS,\n VIEW_SWITCHER_LEAVE_MS,\n VIEW_SWITCHER_PROMPT_DELAY_MS,\n VIEW_SWITCHER_PROMPT_MS,\n} from \"./Styles\";\nimport useForkRef from \"../../utils/useForkRef\";\nimport { CheckIcon, CloseSmallIcon } from \"../Icons\";\nimport { Tooltip } from \"../Tooltip\";\n\nexport interface ViewSwitcherView {\n id: string;\n /** View name: the radio's accessible name and its tooltip. */\n label: string;\n /** DS icon element. */\n icon: ReactNode;\n}\n\nexport interface ViewSwitcherProps\n extends Omit<HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n views: ViewSwitcherView[];\n /** Id of the checked view. */\n value?: string;\n onChange: (id: string) => void;\n /** Accessible name of the radiogroup. */\n \"aria-label\"?: string;\n /** Turns on the extra action offered after a switch. A result of false shows no confirmation. */\n onApplyToAll?: (id: string) => unknown;\n /** Short visible text of the extra action. */\n applyToAllLabel?: string;\n /** Tooltip and accessible name of the extra action. */\n applyToAllTitle?: string;\n /** Accessible name of the extra action group. Defaults to \"Now in {label}\". */\n offerTitle?: (view: ViewSwitcherView) => ReactNode;\n /** Accessible name of the button that dismisses the extra action. */\n dismissLabel?: string;\n /** Text that confirms the extra action. */\n appliedLabel?: string;\n /** A new value opens the extra action for the checked view, for a change made outside the buttons. */\n offerKey?: string | number;\n}\n\n/**\n * @component ViewSwitcher\n * @description\n * Icon-only radiogroup that switches the view of a page. A tooltip shows the view name on hover.\n * With onApplyToAll, a switch or a click on the checked view opens a compact extra action beside the buttons\n * after a short delay, for five seconds.\n *\n * @example\n * <ViewSwitcher\n * aria-label=\"View\"\n * views={[\n * { id: \"list\", label: \"List View\", icon: <ViewListIcon /> },\n * { id: \"grid\", label: \"Grid View\", icon: <ViewGridIcon /> },\n * ]}\n * value={view}\n * onChange={setView}\n * onApplyToAll={applyToAllProjects}\n * />\n */\nexport const ViewSwitcher = forwardRef<HTMLDivElement, ViewSwitcherProps>(\n (\n {\n views,\n value,\n onChange,\n \"aria-label\": ariaLabel = \"View\",\n onApplyToAll,\n applyToAllLabel = \"All Projects\",\n applyToAllTitle = \"Apply to All Projects\",\n offerTitle,\n dismissLabel = \"Dismiss\",\n appliedLabel = \"Applied\",\n offerKey,\n className,\n onKeyDown,\n onBlur,\n ...rest\n },\n ref\n ) => {\n const rootRef = useRef<HTMLDivElement | null>(null);\n const handleRootRef = useForkRef(rootRef, ref);\n const optionRefs = useRef<Record<string, HTMLButtonElement | null>>({});\n const promptRef = useRef<HTMLDivElement | null>(null);\n const [offerId, setOfferId] = useState<string | null>(null);\n const [offerPhase, setOfferPhase] = useState<\n \"waiting\" | \"open\" | \"applying\" | \"applied\" | \"leaving\"\n >(\"waiting\");\n const [isConfirmed, setIsConfirmed] = useState(false);\n const offerTitleId = useId();\n const offerIdRef = useRef(offerId);\n const offerPhaseRef = useRef(offerPhase);\n const restoreFocusRef = useRef(false);\n\n offerIdRef.current = offerId;\n offerPhaseRef.current = offerPhase;\n\n const checkedIndex = views.findIndex(({ id }) => id === value);\n const tabStopId = views[checkedIndex >= 0 ? checkedIndex : 0]?.id;\n const offerView = views.find(({ id }) => id === offerId);\n\n const select = useCallback(\n (id: string) => {\n // A click on the checked view also offers the extra action.\n if (id === value) {\n const isOfferUp =\n offerIdRef.current &&\n [\"open\", \"applying\", \"applied\"].includes(offerPhaseRef.current);\n\n if (onApplyToAll && !isOfferUp) {\n setOfferPhase(\"waiting\");\n setIsConfirmed(false);\n setOfferId(id);\n }\n\n return;\n }\n\n onChange(id);\n\n if (onApplyToAll) {\n // An open prompt stays for the next switch, and only a new one waits.\n setOfferPhase((phase) =>\n offerIdRef.current && phase === \"open\" ? \"open\" : \"waiting\"\n );\n setIsConfirmed(false);\n setOfferId(id);\n }\n },\n [onApplyToAll, onChange, value]\n );\n\n const seenOfferKeyRef = useRef(offerKey);\n\n useEffect(() => {\n if (seenOfferKeyRef.current === offerKey) {\n return;\n }\n\n seenOfferKeyRef.current = offerKey;\n\n if (value) {\n select(value);\n }\n }, [offerKey, select, value]);\n\n const isInside = (node: Node | null): boolean =>\n Boolean(node && rootRef.current?.contains(node));\n\n const focusChecked = useCallback(() => {\n if (tabStopId) {\n optionRefs.current[tabStopId]?.focus();\n }\n }, [tabStopId]);\n\n const leave = useCallback((): void => {\n restoreFocusRef.current = Boolean(\n promptRef.current?.contains(document.activeElement)\n );\n\n if (offerPhaseRef.current === \"waiting\") {\n setOfferId(null);\n\n return;\n }\n\n setOfferPhase(\"leaving\");\n }, []);\n\n useEffect(() => {\n if (!offerId && restoreFocusRef.current) {\n restoreFocusRef.current = false;\n focusChecked();\n }\n }, [focusChecked, offerId]);\n\n useEffect(() => {\n if (!offerId || offerPhase === \"applying\") {\n return undefined;\n }\n\n if (offerPhase === \"waiting\") {\n const timer = setTimeout(\n () => setOfferPhase(\"open\"),\n VIEW_SWITCHER_PROMPT_DELAY_MS\n );\n\n return () => clearTimeout(timer);\n }\n\n if (offerPhase === \"leaving\") {\n const timer = setTimeout(\n () => setOfferId(null),\n VIEW_SWITCHER_LEAVE_MS\n );\n\n return () => clearTimeout(timer);\n }\n\n const timer = setTimeout(\n leave,\n offerPhase === \"applied\"\n ? VIEW_SWITCHER_APPLIED_MS\n : VIEW_SWITCHER_PROMPT_MS\n );\n\n return () => clearTimeout(timer);\n }, [leave, offerId, offerPhase]);\n\n useEffect(() => {\n if (!offerId) {\n return undefined;\n }\n\n const leaveOutside = (event: MouseEvent): void => {\n if (!isInside(event.target as Node)) {\n leave();\n }\n };\n\n document.addEventListener(\"mousedown\", leaveOutside);\n\n return () => document.removeEventListener(\"mousedown\", leaveOutside);\n }, [leave, offerId]);\n\n const handleOptionKeyDown = (\n event: KeyboardEvent<HTMLButtonElement>,\n index: number\n ): void => {\n const last = views.length - 1;\n const targets: Record<string, number> = {\n ArrowRight: index === last ? 0 : index + 1,\n ArrowDown: index === last ? 0 : index + 1,\n ArrowLeft: index === 0 ? last : index - 1,\n ArrowUp: index === 0 ? last : index - 1,\n Home: 0,\n End: last,\n };\n const target = targets[event.key];\n\n if (target === undefined) {\n return;\n }\n\n event.preventDefault();\n\n const { id } = views[target];\n\n optionRefs.current[id]?.focus();\n select(id);\n };\n\n const handleRootKeyDown = (event: KeyboardEvent<HTMLDivElement>): void => {\n onKeyDown?.(event);\n\n if (event.key !== \"Escape\" || !offerId) {\n return;\n }\n\n if (offerPhase === \"waiting\") {\n leave();\n\n return;\n }\n\n // A native dialog around the switcher would take an Escape that is not prevented as its own close request.\n event.preventDefault();\n event.stopPropagation();\n leave();\n };\n\n const handleRootBlur = (event: FocusEvent<HTMLDivElement>): void => {\n onBlur?.(event);\n\n const next = event.relatedTarget as Node | null;\n\n if (offerId && next && !isInside(next)) {\n leave();\n }\n };\n\n const applyToAll = async (): Promise<void> => {\n if (!offerId || !onApplyToAll || offerPhase !== \"open\") {\n return;\n }\n\n let isApplied = false;\n\n // The action hides while it confirms, so the focus goes back to the views now.\n if (promptRef.current?.contains(document.activeElement)) {\n focusChecked();\n }\n\n setOfferPhase(\"applying\");\n\n try {\n isApplied = (await onApplyToAll(offerId)) !== false;\n } finally {\n if (offerIdRef.current === offerId) {\n setIsConfirmed(isApplied);\n setOfferPhase(isApplied ? \"applied\" : \"leaving\");\n }\n }\n };\n\n return (\n <StyledViewSwitcher\n ref={handleRootRef}\n className={classnames(\"c-view-switcher\", className)}\n onKeyDown={handleRootKeyDown}\n onBlur={handleRootBlur}\n {...rest}\n >\n <StyledViewSwitcherTrack className=\"c-view-switcher__track\">\n {offerView && offerPhase !== \"waiting\" ? (\n <StyledViewSwitcherPrompt\n ref={promptRef}\n role=\"group\"\n aria-labelledby={offerTitleId}\n className=\"c-view-switcher__prompt\"\n data-phase={offerPhase}\n data-confirmed={isConfirmed || undefined}\n >\n <StyledViewSwitcherPromptTitle id={offerTitleId}>\n {offerTitle\n ? offerTitle(offerView)\n : `Now in ${offerView.label}`}\n </StyledViewSwitcherPromptTitle>\n <StyledViewSwitcherPromptClip>\n <StyledViewSwitcherPromptPill className=\"c-view-switcher__prompt-pill\">\n <Tooltip title={applyToAllTitle} placement=\"bottom\">\n <StyledViewSwitcherAction\n type=\"button\"\n aria-label={applyToAllTitle}\n className=\"c-view-switcher__action\"\n onClick={applyToAll}\n >\n <CheckIcon />\n {applyToAllLabel}\n </StyledViewSwitcherAction>\n </Tooltip>\n <StyledViewSwitcherLine className=\"c-view-switcher__line\" />\n <Tooltip title={dismissLabel} placement=\"bottom\">\n <StyledViewSwitcherDismiss\n type=\"button\"\n aria-label={dismissLabel}\n className=\"c-view-switcher__dismiss\"\n onClick={leave}\n >\n <CloseSmallIcon />\n </StyledViewSwitcherDismiss>\n </Tooltip>\n {isConfirmed ? (\n <StyledViewSwitcherApplied role=\"status\">\n <CheckIcon />\n {appliedLabel}\n </StyledViewSwitcherApplied>\n ) : null}\n </StyledViewSwitcherPromptPill>\n </StyledViewSwitcherPromptClip>\n </StyledViewSwitcherPrompt>\n ) : null}\n <StyledViewSwitcherOptions role=\"radiogroup\" aria-label={ariaLabel}>\n {views.map((view, index) => {\n const checked = view.id === value;\n\n return (\n <Tooltip key={view.id} title={view.label} placement=\"bottom\">\n <StyledViewSwitcherOption\n ref={(node: HTMLButtonElement | null) => {\n optionRefs.current[view.id] = node;\n }}\n type=\"button\"\n role=\"radio\"\n aria-checked={checked}\n aria-label={view.label}\n tabIndex={view.id === tabStopId ? 0 : -1}\n className=\"c-view-switcher__option\"\n onClick={() => select(view.id)}\n onKeyDown={(event: KeyboardEvent<HTMLButtonElement>) =>\n handleOptionKeyDown(event, index)\n }\n >\n {view.icon}\n </StyledViewSwitcherOption>\n </Tooltip>\n );\n })}\n </StyledViewSwitcherOptions>\n </StyledViewSwitcherTrack>\n </StyledViewSwitcher>\n );\n }\n);\n\nViewSwitcher.displayName = \"ViewSwitcher\";\n"],"mappings":";;;AAAA,OAAOA,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","forwardRef","useCallback","useEffect","useRef","classnames","ApplyToAllTab","StyledViewSwitcher","StyledViewSwitcherOption","StyledViewSwitcherOptions","StyledViewSwitcherTrack","useApplyToAllOffer","useForkRef","Tooltip","ViewSwitcher","_ref","ref","_views","views","value","onChange","_ref$ariaLabel","ariaLabel","onApplyToAll","_ref$applyToAllTitle","applyToAllTitle","offerTitle","_ref$appliedLabel","appliedLabel","_ref$applyToAllLabel","applyToAllLabel","_ref$dismissLabel","dismissLabel","offerKey","className","onKeyDown","onBlur","rest","_objectWithoutPropertiesLoose","_excluded","rootRef","handleRootRef","optionRefs","checkedIndex","findIndex","_ref2","id","tabStopId","focusChecked","_optionRefs$current$t","current","focus","offer","onRestoreFocus","show","isUp","offerView","find","_ref3","_offer$offer","select","seenOfferKeyRef","handleOptionKeyDown","event","index","_optionRefs$current$i","last","length","targets","ArrowRight","ArrowDown","ArrowLeft","ArrowUp","Home","End","target","key","undefined","preventDefault","handleRootKeyDown","handleKeyDown","handleRootBlur","handleBlur","createElement","_extends","role","map","view","checked","title","label","placement","node","type","tabIndex","onClick","icon","isShown","tabRef","phase","isConfirmed","onApply","apply","onDismiss","leave","onHold","hold","displayName"],"sources":["../../../../src/components/ViewSwitcher/ViewSwitcher.tsx"],"sourcesContent":["import React, {\n FocusEvent,\n forwardRef,\n HTMLAttributes,\n KeyboardEvent,\n ReactNode,\n useCallback,\n useEffect,\n useRef,\n} from \"react\";\n\nimport classnames from \"classnames\";\n\nimport { ApplyToAllTab } from \"./ApplyToAllTab\";\nimport {\n StyledViewSwitcher,\n StyledViewSwitcherOption,\n StyledViewSwitcherOptions,\n StyledViewSwitcherTrack,\n} from \"./Styles\";\nimport { useApplyToAllOffer } from \"./useApplyToAllOffer\";\nimport useForkRef from \"../../utils/useForkRef\";\nimport { Tooltip } from \"../Tooltip\";\n\nexport interface ViewSwitcherView {\n id: string;\n /** View name: the radio's accessible name and its tooltip. */\n label: string;\n /** DS icon element. */\n icon: ReactNode;\n}\n\nexport interface ViewSwitcherProps\n extends Omit<HTMLAttributes<HTMLDivElement>, \"onChange\"> {\n views: ViewSwitcherView[];\n /** Id of the checked view. */\n value?: string;\n onChange: (id: string) => void;\n /** Accessible name of the radiogroup. */\n \"aria-label\"?: string;\n /** Turns on the extra action offered after a switch. A result of false shows no confirmation. */\n onApplyToAll?: (id: string) => unknown;\n /** Tooltip and accessible name of the extra action. */\n applyToAllTitle?: string;\n /** Accessible name of the extra action group. Defaults to \"Now in {label}\". */\n offerTitle?: (view: ViewSwitcherView) => ReactNode;\n /** Text that a screen reader reads after the extra action. */\n appliedLabel?: string;\n /** Tooltip and accessible name of the button that closes the extra action. */\n /** Visible text of the apply button, shown where it has room. */\n applyToAllLabel?: string;\n dismissLabel?: string;\n /** A new value opens the extra action for the checked view, for a change made outside the buttons. */\n offerKey?: string | number;\n}\n\n/**\n * @component ViewSwitcher\n * @description\n * The main view switcher: an icon-only radiogroup that switches the view of a page. A tooltip shows the view name on hover.\n * With onApplyToAll, a switch or a click on the checked view slides a check button out from under the right end\n * after a short delay. It applies the view to all projects, and a click outside closes it.\n *\n * @example\n * <ViewSwitcher\n * aria-label=\"View\"\n * views={[\n * { id: \"list\", label: \"List View\", icon: <ViewListIcon /> },\n * { id: \"grid\", label: \"Grid View\", icon: <ViewGridIcon /> },\n * ]}\n * value={view}\n * onChange={setView}\n * onApplyToAll={applyToAllProjects}\n * />\n */\nexport const ViewSwitcher = forwardRef<HTMLDivElement, ViewSwitcherProps>(\n (\n {\n views,\n value,\n onChange,\n \"aria-label\": ariaLabel = \"View\",\n onApplyToAll,\n applyToAllTitle = \"Apply to All Projects\",\n offerTitle,\n appliedLabel = \"Applied\",\n applyToAllLabel = \"All Projects?\",\n dismissLabel = \"Dismiss\",\n offerKey,\n className,\n onKeyDown,\n onBlur,\n ...rest\n },\n ref\n ) => {\n const rootRef = useRef<HTMLDivElement | null>(null);\n const handleRootRef = useForkRef(rootRef, ref);\n const optionRefs = useRef<Record<string, HTMLButtonElement | null>>({});\n\n const checkedIndex = views.findIndex(({ id }) => id === value);\n const tabStopId = views[checkedIndex >= 0 ? checkedIndex : 0]?.id;\n\n const focusChecked = useCallback(() => {\n if (tabStopId) {\n optionRefs.current[tabStopId]?.focus();\n }\n }, [tabStopId]);\n\n const offer = useApplyToAllOffer<string>({\n rootRef,\n onApplyToAll,\n onRestoreFocus: focusChecked,\n });\n const { show, isUp } = offer;\n const offerView = views.find(({ id }) => id === offer.offer?.value);\n\n const select = useCallback(\n (id: string) => {\n // A click on the checked view also offers the extra action.\n if (id === value) {\n if (!isUp) {\n show(id);\n }\n\n return;\n }\n\n onChange(id);\n show(id);\n },\n [isUp, onChange, show, value]\n );\n\n const seenOfferKeyRef = useRef(offerKey);\n\n useEffect(() => {\n if (seenOfferKeyRef.current === offerKey) {\n return;\n }\n\n seenOfferKeyRef.current = offerKey;\n\n if (value) {\n select(value);\n }\n }, [offerKey, select, value]);\n\n const handleOptionKeyDown = (\n event: KeyboardEvent<HTMLButtonElement>,\n index: number\n ): void => {\n const last = views.length - 1;\n const targets: Record<string, number> = {\n ArrowRight: index === last ? 0 : index + 1,\n ArrowDown: index === last ? 0 : index + 1,\n ArrowLeft: index === 0 ? last : index - 1,\n ArrowUp: index === 0 ? last : index - 1,\n Home: 0,\n End: last,\n };\n const target = targets[event.key];\n\n if (target === undefined) {\n return;\n }\n\n event.preventDefault();\n\n const { id } = views[target];\n\n optionRefs.current[id]?.focus();\n select(id);\n };\n\n const handleRootKeyDown = (event: KeyboardEvent<HTMLDivElement>): void => {\n onKeyDown?.(event);\n offer.handleKeyDown(event);\n };\n\n const handleRootBlur = (event: FocusEvent<HTMLDivElement>): void => {\n onBlur?.(event);\n offer.handleBlur(event);\n };\n\n return (\n <StyledViewSwitcher\n ref={handleRootRef}\n className={classnames(\"c-view-switcher\", className)}\n onKeyDown={handleRootKeyDown}\n onBlur={handleRootBlur}\n {...rest}\n >\n <StyledViewSwitcherTrack className=\"c-view-switcher__track\">\n <StyledViewSwitcherOptions role=\"radiogroup\" aria-label={ariaLabel}>\n {views.map((view, index) => {\n const checked = view.id === value;\n\n return (\n <Tooltip key={view.id} title={view.label} placement=\"top\">\n <StyledViewSwitcherOption\n ref={(node: HTMLButtonElement | null) => {\n optionRefs.current[view.id] = node;\n }}\n type=\"button\"\n role=\"radio\"\n aria-checked={checked}\n aria-label={view.label}\n tabIndex={view.id === tabStopId ? 0 : -1}\n className=\"c-view-switcher__option\"\n onClick={() => select(view.id)}\n onKeyDown={(event: KeyboardEvent<HTMLButtonElement>) =>\n handleOptionKeyDown(event, index)\n }\n >\n {view.icon}\n </StyledViewSwitcherOption>\n </Tooltip>\n );\n })}\n </StyledViewSwitcherOptions>\n </StyledViewSwitcherTrack>\n {offerView && offer.isShown ? (\n <ApplyToAllTab\n ref={offer.tabRef}\n phase={offer.phase}\n isConfirmed={offer.isConfirmed}\n title={applyToAllTitle}\n label={\n offerTitle ? offerTitle(offerView) : `Now in ${offerView.label}`\n }\n appliedLabel={appliedLabel}\n onApply={offer.apply}\n onDismiss={offer.leave}\n onHold={offer.hold}\n dismissLabel={dismissLabel}\n applyToAllLabel={applyToAllLabel}\n />\n ) : null}\n </StyledViewSwitcher>\n );\n }\n);\n\nViewSwitcher.displayName = \"ViewSwitcher\";\n"],"mappings":";;;AAAA,OAAOA,KAAK,IAEVC,UAAU,EAIVC,WAAW,EACXC,SAAS,EACTC,MAAM,QACD,OAAO;AAEd,OAAOC,UAAU,MAAM,YAAY;AAEnC,SAASC,aAAa,QAAQ,iBAAiB;AAC/C,SACEC,kBAAkB,EAClBC,wBAAwB,EACxBC,yBAAyB,EACzBC,uBAAuB,QAClB,UAAU;AACjB,SAASC,kBAAkB,QAAQ,sBAAsB;AACzD,OAAOC,UAAU,MAAM,wBAAwB;AAC/C,SAASC,OAAO,QAAQ,YAAY;AAkCpC;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,OAAO,MAAMC,YA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AAEP,IAAI,CAACzC,EAAE,KAAKC,SAAS,GAAG,CAAC,GAAG,CAAC,CAAE;MACzCb,SAAS,EAAC,yBAAyB;MACnC6D,OAAO,EAAEA,CAAA,KAAMnC,MAAM,CAAC2B,IAAI,CAACzC,EAAE,CAAE;MAC/BX,SAAS,EAAG4B,KAAuC,IACjDD,mBAAmB,CAACC,KAAK,EAAEC,KAAK;IACjC,GAEAuB,IAAI,CAACS,IACkB,CACnB,CAAC;EAEd,CAAC,CACwB,CACJ,CAAC,EACzBxC,SAAS,IAAIJ,KAAK,CAAC6C,OAAO,gBACzBjG,KAAA,CAAAmF,aAAA,CAAC7E,aAAa;IACZU,GAAG,EAAEoC,KAAK,CAAC8C,MAAO;IAClBC,KAAK,EAAE/C,KAAK,CAAC+C,KAAM;IACnBC,WAAW,EAAEhD,KAAK,CAACgD,WAAY;IAC/BX,KAAK,EAAEhE,eAAgB;IACvBiE,KAAK,EACHhE,UAAU,GAAGA,UAAU,CAAC8B,SAAS,CAAC,eAAaA,SAAS,CAACkC,KAC1D;IACD9D,YAAY,EAAEA,YAAa;IAC3ByE,OAAO,EAAEjD,KAAK,CAACkD,KAAM;IACrBC,SAAS,EAAEnD,KAAK,CAACoD,KAAM;IACvBC,MAAM,EAAErD,KAAK,CAACsD,IAAK;IACnB1E,YAAY,EAAEA,YAAa;IAC3BF,eAAe,EAAEA;EAAgB,CAClC,CAAC,GACA,IACc,CAAC;AAEzB,CACF,CAAC;AAEDhB,YAAY,CAAC6F,WAAW,GAAG,cAAc","ignoreList":[]}
@@ -1,3 +1,3 @@
1
1
  export * from "./ViewSwitcher";
2
- export * from "./ViewSwitcherMenu";
2
+ export * from "./SubViewSwitcher";
3
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/ViewSwitcher/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/components/ViewSwitcher/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC"}
@@ -1,3 +1,3 @@
1
1
  export * from "./ViewSwitcher";
2
- export * from "./ViewSwitcherMenu";
2
+ export * from "./SubViewSwitcher";
3
3
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":[],"sources":["../../../../src/components/ViewSwitcher/index.ts"],"sourcesContent":["export * from \"./ViewSwitcher\";\nexport * from \"./ViewSwitcherMenu\";\n"],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,oBAAoB","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":[],"sources":["../../../../src/components/ViewSwitcher/index.ts"],"sourcesContent":["export * from \"./ViewSwitcher\";\nexport * from \"./SubViewSwitcher\";\n"],"mappings":"AAAA,cAAc,gBAAgB;AAC9B,cAAc,mBAAmB","ignoreList":[]}
@@ -0,0 +1,27 @@
1
+ import { FocusEvent, KeyboardEvent, RefObject } from "react";
2
+ export type ApplyToAllPhase = "waiting" | "open" | "applying" | "applied" | "leaving";
3
+ interface Options<T> {
4
+ rootRef: RefObject<HTMLElement | null>;
5
+ onApplyToAll?: (value: T) => unknown;
6
+ /** Puts the focus back on the control after the offer closes with the focus in it. */
7
+ onRestoreFocus: () => void;
8
+ }
9
+ /** The state of the apply to all offer beside a view control: it opens after a change and closes on a click outside. */
10
+ export declare const useApplyToAllOffer: <T>({ rootRef, onApplyToAll, onRestoreFocus, }: Options<T>) => {
11
+ offer: {
12
+ value: T;
13
+ } | null;
14
+ phase: ApplyToAllPhase;
15
+ isUp: boolean;
16
+ isConfirmed: boolean;
17
+ hold: import("react").Dispatch<import("react").SetStateAction<boolean>>;
18
+ isShown: boolean;
19
+ tabRef: import("react").MutableRefObject<HTMLDivElement | null>;
20
+ show: (value: T) => void;
21
+ leave: () => void;
22
+ apply: () => Promise<void>;
23
+ handleKeyDown: (event: KeyboardEvent<HTMLElement>) => void;
24
+ handleBlur: (event: FocusEvent<HTMLElement>) => void;
25
+ };
26
+ export {};
27
+ //# sourceMappingURL=useApplyToAllOffer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useApplyToAllOffer.d.ts","sourceRoot":"","sources":["../../../../src/components/ViewSwitcher/useApplyToAllOffer.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,aAAa,EACb,SAAS,EAKV,MAAM,OAAO,CAAC;AASf,MAAM,MAAM,eAAe,GACvB,SAAS,GACT,MAAM,GACN,UAAU,GACV,SAAS,GACT,SAAS,CAAC;AASd,UAAU,OAAO,CAAC,CAAC;IACjB,OAAO,EAAE,SAAS,CAAC,WAAW,GAAG,IAAI,CAAC,CAAC;IACvC,YAAY,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC;IACrC,sFAAsF;IACtF,cAAc,EAAE,MAAM,IAAI,CAAC;CAC5B;AAED,wHAAwH;AACxH,eAAO,MAAM,kBAAkB,GAAI,CAAC,EAAE,4CAInC,OAAO,CAAC,CAAC,CAAC;;eACiC,CAAC;;;;;;;;kBAgBnC,CAAC;iBAemB,IAAI;iBAkHV,OAAO,CAAC,IAAI,CAAC;2BAvBP,aAAa,CAAC,WAAW,CAAC,KAAG,IAAI;wBAcpC,UAAU,CAAC,WAAW,CAAC,KAAG,IAAI;CAiD1D,CAAC"}
@@ -0,0 +1,159 @@
1
+ import { useCallback, useEffect, useRef, useState } from "react";
2
+ import { VIEW_SWITCHER_APPLIED_MS, VIEW_SWITCHER_LEAVE_MS, VIEW_SWITCHER_OPEN_MS, VIEW_SWITCHER_PROMPT_DELAY_MS } from "./Styles";
3
+ // A choice in an open DS menu does not close the offer, and the first click outside only closes the menu.
4
+ const isInMenu = node => {
5
+ var _node$parentElement;
6
+ return node instanceof Element && (Boolean(node.closest(".c-simple-menu__paper")) || node.classList.contains("c-overlay") && Boolean((_node$parentElement = node.parentElement) == null ? void 0 : _node$parentElement.querySelector(".c-simple-menu__paper")));
7
+ };
8
+ /** The state of the apply to all offer beside a view control: it opens after a change and closes on a click outside. */
9
+ export const useApplyToAllOffer = _ref => {
10
+ let rootRef = _ref.rootRef,
11
+ onApplyToAll = _ref.onApplyToAll,
12
+ onRestoreFocus = _ref.onRestoreFocus;
13
+ const _useState = useState(null),
14
+ offer = _useState[0],
15
+ setOffer = _useState[1];
16
+ const _useState2 = useState("waiting"),
17
+ phase = _useState2[0],
18
+ setPhase = _useState2[1];
19
+ const _useState3 = useState(false),
20
+ isConfirmed = _useState3[0],
21
+ setIsConfirmed = _useState3[1];
22
+ const _useState4 = useState(false),
23
+ isHeld = _useState4[0],
24
+ setIsHeld = _useState4[1];
25
+ const tabRef = useRef(null);
26
+ const offerRef = useRef(offer);
27
+ const phaseRef = useRef(phase);
28
+ const restoreFocusRef = useRef(false);
29
+ offerRef.current = offer;
30
+ phaseRef.current = phase;
31
+ const isUp = Boolean(offer) && ["open", "applying", "applied"].includes(phase);
32
+ const show = useCallback(value => {
33
+ if (!onApplyToAll) {
34
+ return;
35
+ }
36
+
37
+ // An open offer stays for the next change, and only a new one waits.
38
+ setPhase(current => offerRef.current && current === "open" ? "open" : "waiting");
39
+ setIsConfirmed(false);
40
+ setOffer({
41
+ value
42
+ });
43
+ }, [onApplyToAll]);
44
+ const leave = useCallback(() => {
45
+ var _tabRef$current;
46
+ restoreFocusRef.current = Boolean((_tabRef$current = tabRef.current) == null ? void 0 : _tabRef$current.contains(document.activeElement));
47
+ if (phaseRef.current === "waiting") {
48
+ setOffer(null);
49
+ return;
50
+ }
51
+ setPhase("leaving");
52
+ }, []);
53
+ useEffect(() => {
54
+ if (!offer && restoreFocusRef.current) {
55
+ restoreFocusRef.current = false;
56
+ onRestoreFocus();
57
+ }
58
+ }, [offer, onRestoreFocus]);
59
+ useEffect(() => {
60
+ // The offer goes by itself, but not while the pointer is over it.
61
+ if (!offer || phase === "applying" || phase === "open" && isHeld) {
62
+ return undefined;
63
+ }
64
+ const timer = setTimeout(() => {
65
+ if (phase === "waiting") {
66
+ setPhase("open");
67
+ } else if (phase === "open" || phase === "applied") {
68
+ leave();
69
+ } else {
70
+ setIsHeld(false);
71
+ setOffer(null);
72
+ }
73
+ }, phase === "waiting" ? VIEW_SWITCHER_PROMPT_DELAY_MS : phase === "open" ? VIEW_SWITCHER_OPEN_MS : phase === "applied" ? VIEW_SWITCHER_APPLIED_MS : VIEW_SWITCHER_LEAVE_MS);
74
+ return () => clearTimeout(timer);
75
+ }, [isHeld, leave, offer, phase]);
76
+ const isOwn = useCallback(node => {
77
+ var _rootRef$current, _tabRef$current2;
78
+ return Boolean(node && (((_rootRef$current = rootRef.current) == null ? void 0 : _rootRef$current.contains(node)) || ((_tabRef$current2 = tabRef.current) == null ? void 0 : _tabRef$current2.contains(node)))) || isInMenu(node);
79
+ }, [rootRef]);
80
+ const isPressingRef = useRef(false);
81
+ useEffect(() => {
82
+ if (!offer) {
83
+ return undefined;
84
+ }
85
+ const press = () => {
86
+ isPressingRef.current = true;
87
+ };
88
+ const leaveOutside = event => {
89
+ isPressingRef.current = false;
90
+ const target = event.target;
91
+ if (isOwn(target)) {
92
+ return;
93
+ }
94
+ leave();
95
+ };
96
+ document.addEventListener("pointerdown", press, true);
97
+ document.addEventListener("click", leaveOutside, true);
98
+ return () => {
99
+ document.removeEventListener("pointerdown", press, true);
100
+ document.removeEventListener("click", leaveOutside, true);
101
+ };
102
+ }, [isOwn, leave, offer]);
103
+ const handleKeyDown = event => {
104
+ if (event.key !== "Escape" || !offer) {
105
+ return;
106
+ }
107
+ if (phase !== "waiting") {
108
+ // A native dialog around the control would take an Escape that is not prevented as its own close request.
109
+ event.preventDefault();
110
+ event.stopPropagation();
111
+ }
112
+ leave();
113
+ };
114
+ const handleBlur = event => {
115
+ const next = event.relatedTarget;
116
+
117
+ // A press moves the focus first, and the click that follows decides.
118
+ if (offer && next && !isOwn(next) && !isPressingRef.current) {
119
+ leave();
120
+ }
121
+ };
122
+ const apply = async () => {
123
+ var _tabRef$current3;
124
+ const current = offer;
125
+ if (!current || !onApplyToAll || phase !== "open") {
126
+ return;
127
+ }
128
+ let isApplied = false;
129
+
130
+ // The button stops taking clicks while it confirms, so the focus goes back to the control now.
131
+ if ((_tabRef$current3 = tabRef.current) != null && _tabRef$current3.contains(document.activeElement)) {
132
+ onRestoreFocus();
133
+ }
134
+ setPhase("applying");
135
+ try {
136
+ isApplied = (await onApplyToAll(current.value)) !== false;
137
+ } finally {
138
+ if (offerRef.current === current) {
139
+ setIsConfirmed(isApplied);
140
+ setPhase(isApplied ? "applied" : "leaving");
141
+ }
142
+ }
143
+ };
144
+ return {
145
+ offer,
146
+ phase,
147
+ isUp,
148
+ isConfirmed,
149
+ hold: setIsHeld,
150
+ isShown: Boolean(offer) && phase !== "waiting",
151
+ tabRef,
152
+ show,
153
+ leave,
154
+ apply,
155
+ handleKeyDown,
156
+ handleBlur
157
+ };
158
+ };
159
+ //# sourceMappingURL=useApplyToAllOffer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useApplyToAllOffer.js","names":["useCallback","useEffect","useRef","useState","VIEW_SWITCHER_APPLIED_MS","VIEW_SWITCHER_LEAVE_MS","VIEW_SWITCHER_OPEN_MS","VIEW_SWITCHER_PROMPT_DELAY_MS","isInMenu","node","_node$parentElement","Element","Boolean","closest","classList","contains","parentElement","querySelector","useApplyToAllOffer","_ref","rootRef","onApplyToAll","onRestoreFocus","_useState","offer","setOffer","_useState2","phase","setPhase","_useState3","isConfirmed","setIsConfirmed","_useState4","isHeld","setIsHeld","tabRef","offerRef","phaseRef","restoreFocusRef","current","isUp","includes","show","value","leave","_tabRef$current","document","activeElement","undefined","timer","setTimeout","clearTimeout","isOwn","_rootRef$current","_tabRef$current2","isPressingRef","press","leaveOutside","event","target","addEventListener","removeEventListener","handleKeyDown","key","preventDefault","stopPropagation","handleBlur","next","relatedTarget","apply","_tabRef$current3","isApplied","hold","isShown"],"sources":["../../../../src/components/ViewSwitcher/useApplyToAllOffer.ts"],"sourcesContent":["import {\n FocusEvent,\n KeyboardEvent,\n RefObject,\n useCallback,\n useEffect,\n useRef,\n useState,\n} from \"react\";\n\nimport {\n VIEW_SWITCHER_APPLIED_MS,\n VIEW_SWITCHER_LEAVE_MS,\n VIEW_SWITCHER_OPEN_MS,\n VIEW_SWITCHER_PROMPT_DELAY_MS,\n} from \"./Styles\";\n\nexport type ApplyToAllPhase =\n | \"waiting\"\n | \"open\"\n | \"applying\"\n | \"applied\"\n | \"leaving\";\n\n// A choice in an open DS menu does not close the offer, and the first click outside only closes the menu.\nconst isInMenu = (node: Node | null): boolean =>\n node instanceof Element &&\n (Boolean(node.closest(\".c-simple-menu__paper\")) ||\n (node.classList.contains(\"c-overlay\") &&\n Boolean(node.parentElement?.querySelector(\".c-simple-menu__paper\"))));\n\ninterface Options<T> {\n rootRef: RefObject<HTMLElement | null>;\n onApplyToAll?: (value: T) => unknown;\n /** Puts the focus back on the control after the offer closes with the focus in it. */\n onRestoreFocus: () => void;\n}\n\n/** The state of the apply to all offer beside a view control: it opens after a change and closes on a click outside. */\nexport const useApplyToAllOffer = <T>({\n rootRef,\n onApplyToAll,\n onRestoreFocus,\n}: Options<T>) => {\n const [offer, setOffer] = useState<{ value: T } | null>(null);\n const [phase, setPhase] = useState<ApplyToAllPhase>(\"waiting\");\n const [isConfirmed, setIsConfirmed] = useState(false);\n const [isHeld, setIsHeld] = useState(false);\n const tabRef = useRef<HTMLDivElement | null>(null);\n const offerRef = useRef(offer);\n const phaseRef = useRef(phase);\n const restoreFocusRef = useRef(false);\n\n offerRef.current = offer;\n phaseRef.current = phase;\n\n const isUp =\n Boolean(offer) && [\"open\", \"applying\", \"applied\"].includes(phase);\n\n const show = useCallback(\n (value: T) => {\n if (!onApplyToAll) {\n return;\n }\n\n // An open offer stays for the next change, and only a new one waits.\n setPhase((current) =>\n offerRef.current && current === \"open\" ? \"open\" : \"waiting\"\n );\n setIsConfirmed(false);\n setOffer({ value });\n },\n [onApplyToAll]\n );\n\n const leave = useCallback((): void => {\n restoreFocusRef.current = Boolean(\n tabRef.current?.contains(document.activeElement)\n );\n\n if (phaseRef.current === \"waiting\") {\n setOffer(null);\n\n return;\n }\n\n setPhase(\"leaving\");\n }, []);\n\n useEffect(() => {\n if (!offer && restoreFocusRef.current) {\n restoreFocusRef.current = false;\n onRestoreFocus();\n }\n }, [offer, onRestoreFocus]);\n\n useEffect(() => {\n // The offer goes by itself, but not while the pointer is over it.\n if (!offer || phase === \"applying\" || (phase === \"open\" && isHeld)) {\n return undefined;\n }\n\n const timer = setTimeout(\n () => {\n if (phase === \"waiting\") {\n setPhase(\"open\");\n } else if (phase === \"open\" || phase === \"applied\") {\n leave();\n } else {\n setIsHeld(false);\n setOffer(null);\n }\n },\n phase === \"waiting\"\n ? VIEW_SWITCHER_PROMPT_DELAY_MS\n : phase === \"open\"\n ? VIEW_SWITCHER_OPEN_MS\n : phase === \"applied\"\n ? VIEW_SWITCHER_APPLIED_MS\n : VIEW_SWITCHER_LEAVE_MS\n );\n\n return () => clearTimeout(timer);\n }, [isHeld, leave, offer, phase]);\n\n const isOwn = useCallback(\n (node: Node | null): boolean =>\n Boolean(\n node &&\n (rootRef.current?.contains(node) || tabRef.current?.contains(node))\n ) || isInMenu(node),\n [rootRef]\n );\n\n const isPressingRef = useRef(false);\n\n useEffect(() => {\n if (!offer) {\n return undefined;\n }\n\n const press = (): void => {\n isPressingRef.current = true;\n };\n\n const leaveOutside = (event: MouseEvent): void => {\n isPressingRef.current = false;\n\n const target = event.target as Element | null;\n\n if (isOwn(target)) {\n return;\n }\n\n leave();\n };\n\n document.addEventListener(\"pointerdown\", press, true);\n document.addEventListener(\"click\", leaveOutside, true);\n\n return () => {\n document.removeEventListener(\"pointerdown\", press, true);\n document.removeEventListener(\"click\", leaveOutside, true);\n };\n }, [isOwn, leave, offer]);\n\n const handleKeyDown = (event: KeyboardEvent<HTMLElement>): void => {\n if (event.key !== \"Escape\" || !offer) {\n return;\n }\n\n if (phase !== \"waiting\") {\n // A native dialog around the control would take an Escape that is not prevented as its own close request.\n event.preventDefault();\n event.stopPropagation();\n }\n\n leave();\n };\n\n const handleBlur = (event: FocusEvent<HTMLElement>): void => {\n const next = event.relatedTarget as Node | null;\n\n // A press moves the focus first, and the click that follows decides.\n if (offer && next && !isOwn(next) && !isPressingRef.current) {\n leave();\n }\n };\n\n const apply = async (): Promise<void> => {\n const current = offer;\n\n if (!current || !onApplyToAll || phase !== \"open\") {\n return;\n }\n\n let isApplied = false;\n\n // The button stops taking clicks while it confirms, so the focus goes back to the control now.\n if (tabRef.current?.contains(document.activeElement)) {\n onRestoreFocus();\n }\n\n setPhase(\"applying\");\n\n try {\n isApplied = (await onApplyToAll(current.value)) !== false;\n } finally {\n if (offerRef.current === current) {\n setIsConfirmed(isApplied);\n setPhase(isApplied ? \"applied\" : \"leaving\");\n }\n }\n };\n\n return {\n offer,\n phase,\n isUp,\n isConfirmed,\n hold: setIsHeld,\n isShown: Boolean(offer) && phase !== \"waiting\",\n tabRef,\n show,\n leave,\n apply,\n handleKeyDown,\n handleBlur,\n };\n};\n"],"mappings":"AAAA,SAIEA,WAAW,EACXC,SAAS,EACTC,MAAM,EACNC,QAAQ,QACH,OAAO;AAEd,SACEC,wBAAwB,EACxBC,sBAAsB,EACtBC,qBAAqB,EACrBC,6BAA6B,QACxB,UAAU;AASjB;AACA,MAAMC,QAAQ,GAAIC,IAAiB;EAAA,IAAAC,mBAAA;EAAA,OACjCD,IAAI,YAAYE,OAAO,KACtBC,OAAO,CAACH,IAAI,CAACI,OAAO,CAAC,uBAAuB,CAAC,CAAC,IAC5CJ,IAAI,CAACK,SAAS,CAACC,QAAQ,CAAC,WAAW,CAAC,IACnCH,OAAO,EAAAF,mBAAA,GAACD,IAAI,CAACO,aAAa,qBAAlBN,mBAAA,CAAoBO,aAAa,CAAC,uBAAuB,CAAC,CAAE,CAAC;AAAA;AAS3E;AACA,OAAO,MAAMC,k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,CAAC,SAAS;MACR,IAAIP,QAAQ,CAACG,OAAO,KAAKA,OAAO,EAAE;QAChCR,cAAc,CAACwC,SAAS,CAAC;QACzB3C,QAAQ,CAAC2C,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;MAC7C;IACF;EACF,CAAC;EAED,OAAO;IACL/C,KAAK;IACLG,KAAK;IACLa,IAAI;IACJV,WAAW;IACX0C,IAAI,EAAEtC,SAAS;IACfuC,OAAO,EAAE7D,OAAO,CAACY,KAAK,CAAC,IAAIG,KAAK,KAAK,SAAS;IAC9CQ,MAAM;IACNO,IAAI;IACJE,KAAK;IACLyB,KAAK;IACLP,aAAa;IACbI;EACF,CAAC;AACH,CAAC","ignoreList":[]}
@@ -65,6 +65,7 @@ export * from "./Window";
65
65
  export * from "./ValueButton";
66
66
  export * from "./PillButton";
67
67
  export * from "./PillGroup";
68
+ export * from "./ViewSettings";
68
69
  export * from "./ViewSwitcher";
69
70
  export * from "./Select";
70
71
  export * from "./SelectTrigger";