carbon-addons-iot-react 5.1.11 → 5.1.12

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.
@@ -16,7 +16,7 @@ import 'core-js/modules/es.string.includes.js';
16
16
  import 'core-js/modules/es.string.replace.js';
17
17
  import 'core-js/modules/es.string.split.js';
18
18
  import 'core-js/modules/web.dom-collections.for-each.js';
19
- import React__default, { useRef, useMemo, useEffect, useState, useCallback } from 'react';
19
+ import React__default, { useRef, useMemo, useEffect, useState } from 'react';
20
20
  import ReactDOM__default from 'react-dom';
21
21
  import PropTypes from 'prop-types';
22
22
  import { OrderedList, ListItem, FormGroup, RadioButtonGroup, RadioButton, Layer, NumberInput, Select, SelectItem, DatePicker, DatePickerInput } from '@carbon/react';
@@ -265,7 +265,7 @@ var defaultProps = {
265
265
  hasIconOnly: false,
266
266
  menuOffset: undefined,
267
267
  datePickerType: 'range',
268
- renderInPortal: false,
268
+ renderInPortal: true,
269
269
  useAutoPositioning: false,
270
270
  style: {},
271
271
  closeOnSelect: true
@@ -277,96 +277,148 @@ var CalendarPortal = function CalendarPortal(_ref) {
277
277
  _ref$onClose = _ref.onClose,
278
278
  onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose,
279
279
  children = _ref.children;
280
- var portalContentRef = useRef(null); // Reference to the calendar portal content element
281
- var _useState = useState({
280
+ var _React$useState = React__default.useState(null),
281
+ _React$useState2 = _slicedToArray(_React$useState, 2),
282
+ container = _React$useState2[0],
283
+ setContainer = _React$useState2[1];
284
+ var portalContentRef = React__default.useRef(null);
285
+ var _React$useState3 = React__default.useState({
282
286
  top: 0,
283
287
  left: 0
284
288
  }),
285
- _useState2 = _slicedToArray(_useState, 2),
286
- position = _useState2[0],
287
- setPosition = _useState2[1]; // State to store the position of the calendar portal
288
-
289
- var portalWidth = 316; // Width of the calendar portal
290
-
291
- var updatePosition = useCallback(function () {
292
- var anchor = anchorRef.current; // Reference to the anchor element
293
- var portal = portalContentRef.current; // Reference to the calendar portal content element
294
-
295
- if (!anchor || !portal) return false; // Return early if anchor or portal references are null
289
+ _React$useState4 = _slicedToArray(_React$useState3, 2),
290
+ position = _React$useState4[0],
291
+ setPosition = _React$useState4[1];
292
+ var portalWidth = 316;
296
293
 
297
- var rect = anchor.getBoundingClientRect(); // Get the bounding client rectangle of the anchor element
298
- var scrollY = window.scrollY || document.documentElement.scrollTop; // Get the current scroll position vertically
299
- var scrollX = window.scrollX || document.documentElement.scrollLeft; // Get the current scroll position horizontally
294
+ // Decide container once the anchor exists (modal or body)
295
+ React__default.useEffect(function () {
296
+ var anchor = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current;
297
+ if (!anchor) {
298
+ setContainer(document.body);
299
+ return;
300
+ }
301
+ var modal = anchor.closest('.cds--modal-container');
302
+ setContainer(modal || document.body);
303
+ }, [anchorRef, isOpen]);
304
+ var updatePosition = React__default.useCallback(function () {
305
+ var anchor = anchorRef.current;
306
+ var portal = portalContentRef.current;
307
+ var cont = container;
308
+ if (!anchor || !portal || !cont) return false;
309
+ var rect = anchor.getBoundingClientRect();
300
310
  var _window = window,
301
311
  innerWidth = _window.innerWidth,
302
- innerHeight = _window.innerHeight; // Get the inner width and height of the window
312
+ innerHeight = _window.innerHeight;
313
+ var portalHeight = portal.offsetHeight;
314
+ var inModal = cont !== document.body;
315
+ var top;
316
+ var left;
317
+ if (inModal) {
318
+ // ----- inside modal: absolute, relative to modal rect -----
319
+ var modalRect = cont.getBoundingClientRect();
320
+ var topSpace = rect.top - modalRect.top;
321
+ var bottomSpace = modalRect.bottom - rect.bottom;
322
+ var fitsBelow = bottomSpace >= portalHeight;
323
+ var fitsAbove = topSpace >= portalHeight;
324
+ top = fitsBelow ? rect.bottom - modalRect.top : fitsAbove ? rect.top - portalHeight - modalRect.top : rect.bottom - portalHeight - modalRect.top;
325
+ left = rect.right - portalWidth - modalRect.left;
303
326
 
304
- var portalHeight = portal.offsetHeight; // Get the height of the calendar portal content element
305
-
306
- var topSpace = rect.top; // Top space available above the anchor element
307
- var bottomSpace = innerHeight - rect.bottom; // Bottom space available below the anchor element
327
+ // clamp within modal width
328
+ var modalWidth = cont.clientWidth;
329
+ left = Math.max(0, Math.min(left, modalWidth - portalWidth));
330
+ } else {
331
+ // ----- in body: FIXED, relative to viewport (no scroll offsets) -----
332
+ var _topSpace = rect.top;
333
+ var _bottomSpace = innerHeight - rect.bottom;
334
+ var _fitsBelow = _bottomSpace >= portalHeight;
335
+ var _fitsAbove = _topSpace >= portalHeight;
336
+ top = _fitsBelow ? rect.bottom : _fitsAbove ? rect.top - portalHeight : Math.max(0, innerHeight - portalHeight);
337
+ left = rect.right - portalWidth;
338
+ left = Math.max(0, Math.min(left, innerWidth - portalWidth));
339
+ }
340
+ setPosition(function (prev) {
341
+ return prev.top === top && prev.left === left ? prev : {
342
+ top: top,
343
+ left: left
344
+ };
345
+ });
346
+ return true;
347
+ }, [anchorRef, container]);
308
348
 
309
- var fitsBelow = bottomSpace >= portalHeight; // Check if the calendar portal fits below the anchor element
310
- var fitsAbove = topSpace >= portalHeight; // Check if the calendar portal fits above the anchor element
349
+ // Run after open + after content lays out
350
+ React__default.useLayoutEffect(function () {
351
+ if (!isOpen) return;
352
+ updatePosition();
353
+ }, [isOpen, updatePosition]);
311
354
 
312
- var top = fitsBelow ? rect.bottom + scrollY : fitsAbove ? rect.top + scrollY - portalHeight : Math.max(scrollY, scrollY + rect.bottom - portalHeight); // Calculate the top position of the calendar portal
355
+ // Reposition on resize/scroll. In modal, listen to modal scroller too.
356
+ React__default.useEffect(function () {
357
+ if (!isOpen || !container) return;
358
+ var inModal = container !== document.body;
359
+ var modalScroller = inModal ? container.querySelector('.cds--modal-content') || container : null;
360
+ var onRescroll = function onRescroll() {
361
+ return updatePosition();
362
+ };
363
+ window.addEventListener('resize', onRescroll);
364
+ window.addEventListener('scroll', onRescroll, {
365
+ capture: true
366
+ });
367
+ if (modalScroller) modalScroller.addEventListener('scroll', onRescroll, {
368
+ capture: true
369
+ });
313
370
 
314
- var left = rect.right - portalWidth + scrollX; // Calculate the left position of the calendar portal
315
- left = Math.max(0, Math.min(left, scrollX + innerWidth - portalWidth)); // Ensure the left position is within the viewport
371
+ // If the calendar's size changes (months switch), keep it stuck to the anchor
372
+ var ro = new ResizeObserver(onRescroll);
373
+ if (portalContentRef.current) ro.observe(portalContentRef.current);
316
374
 
317
- setPosition({
318
- top: top,
319
- left: left
320
- }); // Update the position state
321
- return true;
322
- });
323
- useEffect(function () {
324
- if (isOpen) {
325
- var frame = requestAnimationFrame(updatePosition); // Request an animation frame to update the position
326
- return function () {
327
- return cancelAnimationFrame(frame);
328
- }; // Cancel the animation frame when the component unmounts
329
- }
330
- return undefined;
331
- }, [isOpen, updatePosition]); // Depend on the isOpen prop to re-run the effect when it changes
375
+ // eslint-disable-next-line consistent-return
376
+ return function () {
377
+ window.removeEventListener('resize', onRescroll);
378
+ window.removeEventListener('scroll', onRescroll, {
379
+ capture: true
380
+ });
381
+ if (modalScroller) modalScroller.removeEventListener('scroll', onRescroll, {
382
+ capture: true
383
+ });
384
+ ro.disconnect();
385
+ };
386
+ }, [isOpen, container, updatePosition]);
332
387
 
333
- useEffect(function () {
388
+ // Close on outside click
389
+ React__default.useEffect(function () {
390
+ if (!isOpen) return;
334
391
  var handleClickOutside = function handleClickOutside(e) {
335
- var anchor = anchorRef.current; // Reference to the anchor element
392
+ var anchor = anchorRef.current;
336
393
  if (portalContentRef.current && !portalContentRef.current.contains(e.target) && anchor && !anchor.contains(e.target)) {
337
- onClose(); // Call the onClose callback when clicking outside the calendar portal
394
+ onClose();
338
395
  }
339
396
  };
340
- window.addEventListener('scroll', updatePosition, true); // Listen for scroll events and update the position
341
- window.addEventListener('resize', updatePosition); // Listen for resize events and update the position
342
- document.addEventListener('mousedown', handleClickOutside); // Listen for mousedown events and handle click outside the calendar portal
343
-
397
+ document.addEventListener('mousedown', handleClickOutside);
398
+ // eslint-disable-next-line consistent-return
344
399
  return function () {
345
- window.removeEventListener('scroll', updatePosition, true); // Remove the scroll event listener
346
- window.removeEventListener('resize', updatePosition); // Remove the resize event listener
347
- document.removeEventListener('mousedown', handleClickOutside); // Remove the click outside event listener
400
+ return document.removeEventListener('mousedown', handleClickOutside);
348
401
  };
349
- }, [anchorRef, onClose, updatePosition]); // Depend on the anchorRef and onClose props to re-run the effect when they change
350
-
351
- if (!isOpen) return null; // Return null if the calendar portal is not open
352
-
402
+ }, [isOpen, onClose, anchorRef]);
403
+ if (!isOpen || !container) return null;
404
+ var inModal = container !== document.body;
405
+ var stylePosition = inModal ? 'absolute' : 'fixed';
353
406
  return /*#__PURE__*/ReactDOM__default.createPortal(/*#__PURE__*/React__default.createElement("div", {
354
407
  ref: portalContentRef,
355
408
  className: "datetime-picker-portal",
356
409
  style: {
357
- position: 'absolute',
358
- top: "".concat(position.top, "px"),
359
- left: "".concat(position.left, "px"),
360
- width: "".concat(portalWidth, "px"),
410
+ position: stylePosition,
411
+ top: position.top,
412
+ left: position.left,
413
+ width: portalWidth,
361
414
  backgroundColor: 'white',
362
- padding: '10px',
415
+ padding: 10,
363
416
  zIndex: 9999,
364
417
  overflowY: 'auto',
365
418
  overflowX: 'hidden',
366
- maxHeight: '400px'
419
+ maxHeight: 400
367
420
  }
368
- }, children), document.body // Attach the calendar portal to the document body
369
- );
421
+ }, children), container);
370
422
  };
371
423
  var DateTimePicker = function DateTimePicker(_ref2) {
372
424
  var _style$zIndex2;
@@ -398,6 +450,7 @@ var DateTimePicker = function DateTimePicker(_ref2) {
398
450
  style = _ref2.style,
399
451
  closeOnSelect = _ref2.closeOnSelect,
400
452
  others = _objectWithoutProperties(_ref2, _excluded);
453
+ console.log(renderInPortal);
401
454
  var id = useRef(others.id || v4()).current;
402
455
  React__default.useEffect(function () {
403
456
  {
@@ -430,34 +483,34 @@ var DateTimePicker = function DateTimePicker(_ref2) {
430
483
  customRangeKind = _useDateTimePickerRan2[0],
431
484
  setCustomRangeKind = _useDateTimePickerRan2[1],
432
485
  onCustomRangeChange = _useDateTimePickerRan2[2];
433
- var _useState3 = useState(false),
486
+ var _useState = useState(false),
487
+ _useState2 = _slicedToArray(_useState, 2),
488
+ isCustomRange = _useState2[0],
489
+ setIsCustomRange = _useState2[1];
490
+ var _useState3 = useState(null),
434
491
  _useState4 = _slicedToArray(_useState3, 2),
435
- isCustomRange = _useState4[0],
436
- setIsCustomRange = _useState4[1];
492
+ selectedPreset = _useState4[0],
493
+ setSelectedPreset = _useState4[1];
437
494
  var _useState5 = useState(null),
438
495
  _useState6 = _slicedToArray(_useState5, 2),
439
- selectedPreset = _useState6[0],
440
- setSelectedPreset = _useState6[1];
496
+ currentValue = _useState6[0],
497
+ setCurrentValue = _useState6[1];
441
498
  var _useState7 = useState(null),
442
499
  _useState8 = _slicedToArray(_useState7, 2),
443
- currentValue = _useState8[0],
444
- setCurrentValue = _useState8[1];
500
+ lastAppliedValue = _useState8[0],
501
+ setLastAppliedValue = _useState8[1];
445
502
  var _useState9 = useState(null),
446
503
  _useState10 = _slicedToArray(_useState9, 2),
447
- lastAppliedValue = _useState10[0],
448
- setLastAppliedValue = _useState10[1];
449
- var _useState11 = useState(null),
504
+ humanValue = _useState10[0],
505
+ setHumanValue = _useState10[1];
506
+ var _useState11 = useState(false),
450
507
  _useState12 = _slicedToArray(_useState11, 2),
451
- humanValue = _useState12[0],
452
- setHumanValue = _useState12[1];
453
- var _useState13 = useState(false),
508
+ defaultTimeValueUpdate = _useState12[0],
509
+ setDefaultTimeValueUpdate = _useState12[1];
510
+ var _useState13 = useState(invalid),
454
511
  _useState14 = _slicedToArray(_useState13, 2),
455
- defaultTimeValueUpdate = _useState14[0],
456
- setDefaultTimeValueUpdate = _useState14[1];
457
- var _useState15 = useState(invalid),
458
- _useState16 = _slicedToArray(_useState15, 2),
459
- invalidState = _useState16[0],
460
- setInvalidState = _useState16[1];
512
+ invalidState = _useState14[0],
513
+ setInvalidState = _useState14[1];
461
514
  var relativeSelect = useRef(null);
462
515
  var containerRef = useRef();
463
516
  var dropdownRef = useRef();
@@ -498,34 +551,34 @@ var DateTimePicker = function DateTimePicker(_ref2) {
498
551
  onNavigateRadioButton = _useDateTimePickerKey.onNavigateRadioButton,
499
552
  onNavigatePresets = _useDateTimePickerKey.onNavigatePresets,
500
553
  onFieldClick = _useDateTimePickerKey.onFieldClick;
554
+ var _useState15 = useState(null),
555
+ _useState16 = _slicedToArray(_useState15, 2),
556
+ singleDateValue = _useState16[0],
557
+ setSingleDateValue = _useState16[1];
501
558
  var _useState17 = useState(null),
502
559
  _useState18 = _slicedToArray(_useState17, 2),
503
- singleDateValue = _useState18[0],
504
- setSingleDateValue = _useState18[1];
560
+ singleTimeValue = _useState18[0],
561
+ setSingleTimeValue = _useState18[1];
505
562
  var _useState19 = useState(null),
506
563
  _useState20 = _slicedToArray(_useState19, 2),
507
- singleTimeValue = _useState20[0],
508
- setSingleTimeValue = _useState20[1];
564
+ rangeStartTimeValue = _useState20[0],
565
+ setRangeStartTimeValue = _useState20[1];
509
566
  var _useState21 = useState(null),
510
567
  _useState22 = _slicedToArray(_useState21, 2),
511
- rangeStartTimeValue = _useState22[0],
512
- setRangeStartTimeValue = _useState22[1];
513
- var _useState23 = useState(null),
568
+ rangeEndTimeValue = _useState22[0],
569
+ setRangeEndTimeValue = _useState22[1];
570
+ var _useState23 = useState(false),
514
571
  _useState24 = _slicedToArray(_useState23, 2),
515
- rangeEndTimeValue = _useState24[0],
516
- setRangeEndTimeValue = _useState24[1];
572
+ invalidRangeStartTime = _useState24[0],
573
+ setInvalidRangeStartTime = _useState24[1];
517
574
  var _useState25 = useState(false),
518
575
  _useState26 = _slicedToArray(_useState25, 2),
519
- invalidRangeStartTime = _useState26[0],
520
- setInvalidRangeStartTime = _useState26[1];
576
+ invalidRangeEndTime = _useState26[0],
577
+ setInvalidRangeEndTime = _useState26[1];
521
578
  var _useState27 = useState(false),
522
579
  _useState28 = _slicedToArray(_useState27, 2),
523
- invalidRangeEndTime = _useState28[0],
524
- setInvalidRangeEndTime = _useState28[1];
525
- var _useState29 = useState(false),
526
- _useState30 = _slicedToArray(_useState29, 2),
527
- invalidRangeStartDate = _useState30[0],
528
- setInvalidRangeStartDate = _useState30[1];
580
+ invalidRangeStartDate = _useState28[0],
581
+ setInvalidRangeStartDate = _useState28[1];
529
582
  var dateTimePickerBaseValue = {
530
583
  kind: '',
531
584
  preset: {
@@ -1634,7 +1687,7 @@ DateTimePicker.__docgenInfo = {
1634
1687
  },
1635
1688
  "renderInPortal": {
1636
1689
  "defaultValue": {
1637
- "value": "false",
1690
+ "value": "true",
1638
1691
  "computed": false
1639
1692
  },
1640
1693
  "description": "If set to true it will render outside of the current DOM in a portal, otherwise render as a child",
@@ -511,6 +511,10 @@ var TimePickerSpinner = /*#__PURE__*/React__default.forwardRef(function (_ref2,
511
511
  is24hours = _ref2.is24hours,
512
512
  amString = _ref2.amString,
513
513
  pmString = _ref2.pmString;
514
+ var _useState15 = useState(null),
515
+ _useState16 = _slicedToArray(_useState15, 2),
516
+ container = _useState16[0],
517
+ setContainer = _useState16[1];
514
518
  var currentTime = dayjs().format(AVAILABLE_FORMATS);
515
519
  var updatedStyle = useMemo(function () {
516
520
  var _style$zIndex;
@@ -527,20 +531,25 @@ var TimePickerSpinner = /*#__PURE__*/React__default.forwardRef(function (_ref2,
527
531
  var thirdVal = useMemo(function () {
528
532
  return is24hours ? '' : timeUtils.getMeridiem(value, currentTime, amString, pmString) === amString ? 'AM' : 'PM';
529
533
  }, [is24hours, value, currentTime, amString, pmString]);
530
- var _useState15 = useState([firstVal, secondVal, thirdVal]),
531
- _useState16 = _slicedToArray(_useState15, 2),
532
- selected = _useState16[0],
533
- setSelected = _useState16[1];
534
- var _useState17 = useState(value),
534
+ var _useState17 = useState([firstVal, secondVal, thirdVal]),
535
535
  _useState18 = _slicedToArray(_useState17, 2),
536
- callbackValue = _useState18[0],
537
- setCallbackValue = _useState18[1];
538
- var _useState19 = useState(0),
536
+ selected = _useState18[0],
537
+ setSelected = _useState18[1];
538
+ var _useState19 = useState(value),
539
539
  _useState20 = _slicedToArray(_useState19, 2),
540
- setFocusedSpinner = _useState20[1];
540
+ callbackValue = _useState20[0],
541
+ setCallbackValue = _useState20[1];
542
+ var _useState21 = useState(0),
543
+ _useState22 = _slicedToArray(_useState21, 2),
544
+ setFocusedSpinner = _useState22[1];
541
545
  var secondSpinnerRef = useRef();
542
546
  var thirdSpinnerRef = useRef();
543
547
  var numberOfSpinners = is24hours ? 2 : 3;
548
+ useEffect(function () {
549
+ var active = document.activeElement; // currently focused element
550
+ var nearestModal = active === null || active === void 0 ? void 0 : active.closest('.cds--modal');
551
+ setContainer(nearestModal || document.body);
552
+ }, []);
544
553
  useEffect(function () {
545
554
  setSelected([firstVal, secondVal, thirdVal]);
546
555
  }, [firstVal, secondVal, thirdVal]);
@@ -636,6 +645,7 @@ var TimePickerSpinner = /*#__PURE__*/React__default.forwardRef(function (_ref2,
636
645
  }, /* eslint-disable-next-line react-hooks/exhaustive-deps */
637
646
  [selected[2]]);
638
647
  var dropdown = /*#__PURE__*/React__default.createElement("div", {
648
+ ref: ref,
639
649
  "data-testid": testId,
640
650
  className: classnames("".concat(iotPrefix, "--time-picker-spinner"), _defineProperty({}, "".concat(iotPrefix, "--time-picker-spinner--24h"), is24hours)),
641
651
  style: _objectSpread(_objectSpread({}, updatedStyle), {}, {
@@ -643,7 +653,8 @@ var TimePickerSpinner = /*#__PURE__*/React__default.forwardRef(function (_ref2,
643
653
  top: "".concat(position[1], "px")
644
654
  })
645
655
  }, listSpinner1, listSpinner2, is24hours ? null : listSpinner3);
646
- return /*#__PURE__*/ReactDOM__default.createPortal(dropdown, document.body);
656
+ if (!container) return null;
657
+ return /*#__PURE__*/ReactDOM__default.createPortal(dropdown, container);
647
658
  });
648
659
  TimePickerSpinner.propTypes = spinnerPropTypes;
649
660
  TimePickerSpinner.defaultProps = defaultSpinnerProps;
@@ -281,7 +281,7 @@ var defaultProps = {
281
281
  hasIconOnly: false,
282
282
  menuOffset: undefined,
283
283
  datePickerType: 'range',
284
- renderInPortal: false,
284
+ renderInPortal: true,
285
285
  useAutoPositioning: false,
286
286
  style: {},
287
287
  closeOnSelect: true
@@ -293,96 +293,148 @@ var CalendarPortal = function CalendarPortal(_ref) {
293
293
  _ref$onClose = _ref.onClose,
294
294
  onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose,
295
295
  children = _ref.children;
296
- var portalContentRef = React.useRef(null); // Reference to the calendar portal content element
297
- var _useState = React.useState({
296
+ var _React$useState = React__default.default.useState(null),
297
+ _React$useState2 = _slicedToArray__default.default(_React$useState, 2),
298
+ container = _React$useState2[0],
299
+ setContainer = _React$useState2[1];
300
+ var portalContentRef = React__default.default.useRef(null);
301
+ var _React$useState3 = React__default.default.useState({
298
302
  top: 0,
299
303
  left: 0
300
304
  }),
301
- _useState2 = _slicedToArray__default.default(_useState, 2),
302
- position = _useState2[0],
303
- setPosition = _useState2[1]; // State to store the position of the calendar portal
304
-
305
- var portalWidth = 316; // Width of the calendar portal
306
-
307
- var updatePosition = React.useCallback(function () {
308
- var anchor = anchorRef.current; // Reference to the anchor element
309
- var portal = portalContentRef.current; // Reference to the calendar portal content element
310
-
311
- if (!anchor || !portal) return false; // Return early if anchor or portal references are null
305
+ _React$useState4 = _slicedToArray__default.default(_React$useState3, 2),
306
+ position = _React$useState4[0],
307
+ setPosition = _React$useState4[1];
308
+ var portalWidth = 316;
312
309
 
313
- var rect = anchor.getBoundingClientRect(); // Get the bounding client rectangle of the anchor element
314
- var scrollY = window.scrollY || document.documentElement.scrollTop; // Get the current scroll position vertically
315
- var scrollX = window.scrollX || document.documentElement.scrollLeft; // Get the current scroll position horizontally
310
+ // Decide container once the anchor exists (modal or body)
311
+ React__default.default.useEffect(function () {
312
+ var anchor = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current;
313
+ if (!anchor) {
314
+ setContainer(document.body);
315
+ return;
316
+ }
317
+ var modal = anchor.closest('.cds--modal-container');
318
+ setContainer(modal || document.body);
319
+ }, [anchorRef, isOpen]);
320
+ var updatePosition = React__default.default.useCallback(function () {
321
+ var anchor = anchorRef.current;
322
+ var portal = portalContentRef.current;
323
+ var cont = container;
324
+ if (!anchor || !portal || !cont) return false;
325
+ var rect = anchor.getBoundingClientRect();
316
326
  var _window = window,
317
327
  innerWidth = _window.innerWidth,
318
- innerHeight = _window.innerHeight; // Get the inner width and height of the window
328
+ innerHeight = _window.innerHeight;
329
+ var portalHeight = portal.offsetHeight;
330
+ var inModal = cont !== document.body;
331
+ var top;
332
+ var left;
333
+ if (inModal) {
334
+ // ----- inside modal: absolute, relative to modal rect -----
335
+ var modalRect = cont.getBoundingClientRect();
336
+ var topSpace = rect.top - modalRect.top;
337
+ var bottomSpace = modalRect.bottom - rect.bottom;
338
+ var fitsBelow = bottomSpace >= portalHeight;
339
+ var fitsAbove = topSpace >= portalHeight;
340
+ top = fitsBelow ? rect.bottom - modalRect.top : fitsAbove ? rect.top - portalHeight - modalRect.top : rect.bottom - portalHeight - modalRect.top;
341
+ left = rect.right - portalWidth - modalRect.left;
319
342
 
320
- var portalHeight = portal.offsetHeight; // Get the height of the calendar portal content element
321
-
322
- var topSpace = rect.top; // Top space available above the anchor element
323
- var bottomSpace = innerHeight - rect.bottom; // Bottom space available below the anchor element
343
+ // clamp within modal width
344
+ var modalWidth = cont.clientWidth;
345
+ left = Math.max(0, Math.min(left, modalWidth - portalWidth));
346
+ } else {
347
+ // ----- in body: FIXED, relative to viewport (no scroll offsets) -----
348
+ var _topSpace = rect.top;
349
+ var _bottomSpace = innerHeight - rect.bottom;
350
+ var _fitsBelow = _bottomSpace >= portalHeight;
351
+ var _fitsAbove = _topSpace >= portalHeight;
352
+ top = _fitsBelow ? rect.bottom : _fitsAbove ? rect.top - portalHeight : Math.max(0, innerHeight - portalHeight);
353
+ left = rect.right - portalWidth;
354
+ left = Math.max(0, Math.min(left, innerWidth - portalWidth));
355
+ }
356
+ setPosition(function (prev) {
357
+ return prev.top === top && prev.left === left ? prev : {
358
+ top: top,
359
+ left: left
360
+ };
361
+ });
362
+ return true;
363
+ }, [anchorRef, container]);
324
364
 
325
- var fitsBelow = bottomSpace >= portalHeight; // Check if the calendar portal fits below the anchor element
326
- var fitsAbove = topSpace >= portalHeight; // Check if the calendar portal fits above the anchor element
365
+ // Run after open + after content lays out
366
+ React__default.default.useLayoutEffect(function () {
367
+ if (!isOpen) return;
368
+ updatePosition();
369
+ }, [isOpen, updatePosition]);
327
370
 
328
- var top = fitsBelow ? rect.bottom + scrollY : fitsAbove ? rect.top + scrollY - portalHeight : Math.max(scrollY, scrollY + rect.bottom - portalHeight); // Calculate the top position of the calendar portal
371
+ // Reposition on resize/scroll. In modal, listen to modal scroller too.
372
+ React__default.default.useEffect(function () {
373
+ if (!isOpen || !container) return;
374
+ var inModal = container !== document.body;
375
+ var modalScroller = inModal ? container.querySelector('.cds--modal-content') || container : null;
376
+ var onRescroll = function onRescroll() {
377
+ return updatePosition();
378
+ };
379
+ window.addEventListener('resize', onRescroll);
380
+ window.addEventListener('scroll', onRescroll, {
381
+ capture: true
382
+ });
383
+ if (modalScroller) modalScroller.addEventListener('scroll', onRescroll, {
384
+ capture: true
385
+ });
329
386
 
330
- var left = rect.right - portalWidth + scrollX; // Calculate the left position of the calendar portal
331
- left = Math.max(0, Math.min(left, scrollX + innerWidth - portalWidth)); // Ensure the left position is within the viewport
387
+ // If the calendar's size changes (months switch), keep it stuck to the anchor
388
+ var ro = new ResizeObserver(onRescroll);
389
+ if (portalContentRef.current) ro.observe(portalContentRef.current);
332
390
 
333
- setPosition({
334
- top: top,
335
- left: left
336
- }); // Update the position state
337
- return true;
338
- });
339
- React.useEffect(function () {
340
- if (isOpen) {
341
- var frame = requestAnimationFrame(updatePosition); // Request an animation frame to update the position
342
- return function () {
343
- return cancelAnimationFrame(frame);
344
- }; // Cancel the animation frame when the component unmounts
345
- }
346
- return undefined;
347
- }, [isOpen, updatePosition]); // Depend on the isOpen prop to re-run the effect when it changes
391
+ // eslint-disable-next-line consistent-return
392
+ return function () {
393
+ window.removeEventListener('resize', onRescroll);
394
+ window.removeEventListener('scroll', onRescroll, {
395
+ capture: true
396
+ });
397
+ if (modalScroller) modalScroller.removeEventListener('scroll', onRescroll, {
398
+ capture: true
399
+ });
400
+ ro.disconnect();
401
+ };
402
+ }, [isOpen, container, updatePosition]);
348
403
 
349
- React.useEffect(function () {
404
+ // Close on outside click
405
+ React__default.default.useEffect(function () {
406
+ if (!isOpen) return;
350
407
  var handleClickOutside = function handleClickOutside(e) {
351
- var anchor = anchorRef.current; // Reference to the anchor element
408
+ var anchor = anchorRef.current;
352
409
  if (portalContentRef.current && !portalContentRef.current.contains(e.target) && anchor && !anchor.contains(e.target)) {
353
- onClose(); // Call the onClose callback when clicking outside the calendar portal
410
+ onClose();
354
411
  }
355
412
  };
356
- window.addEventListener('scroll', updatePosition, true); // Listen for scroll events and update the position
357
- window.addEventListener('resize', updatePosition); // Listen for resize events and update the position
358
- document.addEventListener('mousedown', handleClickOutside); // Listen for mousedown events and handle click outside the calendar portal
359
-
413
+ document.addEventListener('mousedown', handleClickOutside);
414
+ // eslint-disable-next-line consistent-return
360
415
  return function () {
361
- window.removeEventListener('scroll', updatePosition, true); // Remove the scroll event listener
362
- window.removeEventListener('resize', updatePosition); // Remove the resize event listener
363
- document.removeEventListener('mousedown', handleClickOutside); // Remove the click outside event listener
416
+ return document.removeEventListener('mousedown', handleClickOutside);
364
417
  };
365
- }, [anchorRef, onClose, updatePosition]); // Depend on the anchorRef and onClose props to re-run the effect when they change
366
-
367
- if (!isOpen) return null; // Return null if the calendar portal is not open
368
-
418
+ }, [isOpen, onClose, anchorRef]);
419
+ if (!isOpen || !container) return null;
420
+ var inModal = container !== document.body;
421
+ var stylePosition = inModal ? 'absolute' : 'fixed';
369
422
  return /*#__PURE__*/ReactDOM__default.default.createPortal(/*#__PURE__*/React__default.default.createElement("div", {
370
423
  ref: portalContentRef,
371
424
  className: "datetime-picker-portal",
372
425
  style: {
373
- position: 'absolute',
374
- top: "".concat(position.top, "px"),
375
- left: "".concat(position.left, "px"),
376
- width: "".concat(portalWidth, "px"),
426
+ position: stylePosition,
427
+ top: position.top,
428
+ left: position.left,
429
+ width: portalWidth,
377
430
  backgroundColor: 'white',
378
- padding: '10px',
431
+ padding: 10,
379
432
  zIndex: 9999,
380
433
  overflowY: 'auto',
381
434
  overflowX: 'hidden',
382
- maxHeight: '400px'
435
+ maxHeight: 400
383
436
  }
384
- }, children), document.body // Attach the calendar portal to the document body
385
- );
437
+ }, children), container);
386
438
  };
387
439
  var DateTimePicker = function DateTimePicker(_ref2) {
388
440
  var _style$zIndex2;
@@ -414,6 +466,7 @@ var DateTimePicker = function DateTimePicker(_ref2) {
414
466
  style = _ref2.style,
415
467
  closeOnSelect = _ref2.closeOnSelect,
416
468
  others = _objectWithoutProperties__default.default(_ref2, _excluded);
469
+ console.log(renderInPortal);
417
470
  var id = React.useRef(others.id || uuid.v4()).current;
418
471
  React__default.default.useEffect(function () {
419
472
  {
@@ -446,34 +499,34 @@ var DateTimePicker = function DateTimePicker(_ref2) {
446
499
  customRangeKind = _useDateTimePickerRan2[0],
447
500
  setCustomRangeKind = _useDateTimePickerRan2[1],
448
501
  onCustomRangeChange = _useDateTimePickerRan2[2];
449
- var _useState3 = React.useState(false),
502
+ var _useState = React.useState(false),
503
+ _useState2 = _slicedToArray__default.default(_useState, 2),
504
+ isCustomRange = _useState2[0],
505
+ setIsCustomRange = _useState2[1];
506
+ var _useState3 = React.useState(null),
450
507
  _useState4 = _slicedToArray__default.default(_useState3, 2),
451
- isCustomRange = _useState4[0],
452
- setIsCustomRange = _useState4[1];
508
+ selectedPreset = _useState4[0],
509
+ setSelectedPreset = _useState4[1];
453
510
  var _useState5 = React.useState(null),
454
511
  _useState6 = _slicedToArray__default.default(_useState5, 2),
455
- selectedPreset = _useState6[0],
456
- setSelectedPreset = _useState6[1];
512
+ currentValue = _useState6[0],
513
+ setCurrentValue = _useState6[1];
457
514
  var _useState7 = React.useState(null),
458
515
  _useState8 = _slicedToArray__default.default(_useState7, 2),
459
- currentValue = _useState8[0],
460
- setCurrentValue = _useState8[1];
516
+ lastAppliedValue = _useState8[0],
517
+ setLastAppliedValue = _useState8[1];
461
518
  var _useState9 = React.useState(null),
462
519
  _useState10 = _slicedToArray__default.default(_useState9, 2),
463
- lastAppliedValue = _useState10[0],
464
- setLastAppliedValue = _useState10[1];
465
- var _useState11 = React.useState(null),
520
+ humanValue = _useState10[0],
521
+ setHumanValue = _useState10[1];
522
+ var _useState11 = React.useState(false),
466
523
  _useState12 = _slicedToArray__default.default(_useState11, 2),
467
- humanValue = _useState12[0],
468
- setHumanValue = _useState12[1];
469
- var _useState13 = React.useState(false),
524
+ defaultTimeValueUpdate = _useState12[0],
525
+ setDefaultTimeValueUpdate = _useState12[1];
526
+ var _useState13 = React.useState(invalid),
470
527
  _useState14 = _slicedToArray__default.default(_useState13, 2),
471
- defaultTimeValueUpdate = _useState14[0],
472
- setDefaultTimeValueUpdate = _useState14[1];
473
- var _useState15 = React.useState(invalid),
474
- _useState16 = _slicedToArray__default.default(_useState15, 2),
475
- invalidState = _useState16[0],
476
- setInvalidState = _useState16[1];
528
+ invalidState = _useState14[0],
529
+ setInvalidState = _useState14[1];
477
530
  var relativeSelect = React.useRef(null);
478
531
  var containerRef = React.useRef();
479
532
  var dropdownRef = React.useRef();
@@ -514,34 +567,34 @@ var DateTimePicker = function DateTimePicker(_ref2) {
514
567
  onNavigateRadioButton = _useDateTimePickerKey.onNavigateRadioButton,
515
568
  onNavigatePresets = _useDateTimePickerKey.onNavigatePresets,
516
569
  onFieldClick = _useDateTimePickerKey.onFieldClick;
570
+ var _useState15 = React.useState(null),
571
+ _useState16 = _slicedToArray__default.default(_useState15, 2),
572
+ singleDateValue = _useState16[0],
573
+ setSingleDateValue = _useState16[1];
517
574
  var _useState17 = React.useState(null),
518
575
  _useState18 = _slicedToArray__default.default(_useState17, 2),
519
- singleDateValue = _useState18[0],
520
- setSingleDateValue = _useState18[1];
576
+ singleTimeValue = _useState18[0],
577
+ setSingleTimeValue = _useState18[1];
521
578
  var _useState19 = React.useState(null),
522
579
  _useState20 = _slicedToArray__default.default(_useState19, 2),
523
- singleTimeValue = _useState20[0],
524
- setSingleTimeValue = _useState20[1];
580
+ rangeStartTimeValue = _useState20[0],
581
+ setRangeStartTimeValue = _useState20[1];
525
582
  var _useState21 = React.useState(null),
526
583
  _useState22 = _slicedToArray__default.default(_useState21, 2),
527
- rangeStartTimeValue = _useState22[0],
528
- setRangeStartTimeValue = _useState22[1];
529
- var _useState23 = React.useState(null),
584
+ rangeEndTimeValue = _useState22[0],
585
+ setRangeEndTimeValue = _useState22[1];
586
+ var _useState23 = React.useState(false),
530
587
  _useState24 = _slicedToArray__default.default(_useState23, 2),
531
- rangeEndTimeValue = _useState24[0],
532
- setRangeEndTimeValue = _useState24[1];
588
+ invalidRangeStartTime = _useState24[0],
589
+ setInvalidRangeStartTime = _useState24[1];
533
590
  var _useState25 = React.useState(false),
534
591
  _useState26 = _slicedToArray__default.default(_useState25, 2),
535
- invalidRangeStartTime = _useState26[0],
536
- setInvalidRangeStartTime = _useState26[1];
592
+ invalidRangeEndTime = _useState26[0],
593
+ setInvalidRangeEndTime = _useState26[1];
537
594
  var _useState27 = React.useState(false),
538
595
  _useState28 = _slicedToArray__default.default(_useState27, 2),
539
- invalidRangeEndTime = _useState28[0],
540
- setInvalidRangeEndTime = _useState28[1];
541
- var _useState29 = React.useState(false),
542
- _useState30 = _slicedToArray__default.default(_useState29, 2),
543
- invalidRangeStartDate = _useState30[0],
544
- setInvalidRangeStartDate = _useState30[1];
596
+ invalidRangeStartDate = _useState28[0],
597
+ setInvalidRangeStartDate = _useState28[1];
545
598
  var dateTimePickerBaseValue = {
546
599
  kind: '',
547
600
  preset: {
@@ -1650,7 +1703,7 @@ DateTimePicker.__docgenInfo = {
1650
1703
  },
1651
1704
  "renderInPortal": {
1652
1705
  "defaultValue": {
1653
- "value": "false",
1706
+ "value": "true",
1654
1707
  "computed": false
1655
1708
  },
1656
1709
  "description": "If set to true it will render outside of the current DOM in a portal, otherwise render as a child",
@@ -527,6 +527,10 @@ var TimePickerSpinner = /*#__PURE__*/React__default.default.forwardRef(function
527
527
  is24hours = _ref2.is24hours,
528
528
  amString = _ref2.amString,
529
529
  pmString = _ref2.pmString;
530
+ var _useState15 = React.useState(null),
531
+ _useState16 = _slicedToArray__default.default(_useState15, 2),
532
+ container = _useState16[0],
533
+ setContainer = _useState16[1];
530
534
  var currentTime = dayjs__default.default().format(AVAILABLE_FORMATS);
531
535
  var updatedStyle = React.useMemo(function () {
532
536
  var _style$zIndex;
@@ -543,20 +547,25 @@ var TimePickerSpinner = /*#__PURE__*/React__default.default.forwardRef(function
543
547
  var thirdVal = React.useMemo(function () {
544
548
  return is24hours ? '' : timeUtils.getMeridiem(value, currentTime, amString, pmString) === amString ? 'AM' : 'PM';
545
549
  }, [is24hours, value, currentTime, amString, pmString]);
546
- var _useState15 = React.useState([firstVal, secondVal, thirdVal]),
547
- _useState16 = _slicedToArray__default.default(_useState15, 2),
548
- selected = _useState16[0],
549
- setSelected = _useState16[1];
550
- var _useState17 = React.useState(value),
550
+ var _useState17 = React.useState([firstVal, secondVal, thirdVal]),
551
551
  _useState18 = _slicedToArray__default.default(_useState17, 2),
552
- callbackValue = _useState18[0],
553
- setCallbackValue = _useState18[1];
554
- var _useState19 = React.useState(0),
552
+ selected = _useState18[0],
553
+ setSelected = _useState18[1];
554
+ var _useState19 = React.useState(value),
555
555
  _useState20 = _slicedToArray__default.default(_useState19, 2),
556
- setFocusedSpinner = _useState20[1];
556
+ callbackValue = _useState20[0],
557
+ setCallbackValue = _useState20[1];
558
+ var _useState21 = React.useState(0),
559
+ _useState22 = _slicedToArray__default.default(_useState21, 2),
560
+ setFocusedSpinner = _useState22[1];
557
561
  var secondSpinnerRef = React.useRef();
558
562
  var thirdSpinnerRef = React.useRef();
559
563
  var numberOfSpinners = is24hours ? 2 : 3;
564
+ React.useEffect(function () {
565
+ var active = document.activeElement; // currently focused element
566
+ var nearestModal = active === null || active === void 0 ? void 0 : active.closest('.cds--modal');
567
+ setContainer(nearestModal || document.body);
568
+ }, []);
560
569
  React.useEffect(function () {
561
570
  setSelected([firstVal, secondVal, thirdVal]);
562
571
  }, [firstVal, secondVal, thirdVal]);
@@ -652,6 +661,7 @@ var TimePickerSpinner = /*#__PURE__*/React__default.default.forwardRef(function
652
661
  }, /* eslint-disable-next-line react-hooks/exhaustive-deps */
653
662
  [selected[2]]);
654
663
  var dropdown = /*#__PURE__*/React__default.default.createElement("div", {
664
+ ref: ref,
655
665
  "data-testid": testId,
656
666
  className: classnames__default.default("".concat(iotPrefix, "--time-picker-spinner"), _defineProperty__default.default({}, "".concat(iotPrefix, "--time-picker-spinner--24h"), is24hours)),
657
667
  style: _objectSpread(_objectSpread({}, updatedStyle), {}, {
@@ -659,7 +669,8 @@ var TimePickerSpinner = /*#__PURE__*/React__default.default.forwardRef(function
659
669
  top: "".concat(position[1], "px")
660
670
  })
661
671
  }, listSpinner1, listSpinner2, is24hours ? null : listSpinner3);
662
- return /*#__PURE__*/ReactDOM__default.default.createPortal(dropdown, document.body);
672
+ if (!container) return null;
673
+ return /*#__PURE__*/ReactDOM__default.default.createPortal(dropdown, container);
663
674
  });
664
675
  TimePickerSpinner.propTypes = spinnerPropTypes;
665
676
  TimePickerSpinner.defaultProps = defaultSpinnerProps;
package/package.json CHANGED
@@ -344,11 +344,11 @@
344
344
  "whatwg-fetch": "^3.0.0"
345
345
  },
346
346
  "sideEffects": false,
347
- "version": "5.1.11",
347
+ "version": "5.1.12",
348
348
  "resolutions": {
349
349
  "chokidar": "3.3.1",
350
350
  "react-grid-layout": "1.2.2",
351
351
  "got": "11.8.5"
352
352
  },
353
- "gitHead": "60bb638a38f0961aff808ea54184360160bc7900"
353
+ "gitHead": "261d0506c0139a221abedd8f992fa085cba94f5c"
354
354
  }
@@ -267842,6 +267842,10 @@ ${formatRule(Codicon.menuSubmenu)}
267842
267842
  is24hours = _ref2.is24hours,
267843
267843
  amString = _ref2.amString,
267844
267844
  pmString = _ref2.pmString;
267845
+ var _useState15 = React$1.useState(null),
267846
+ _useState16 = _slicedToArray$9(_useState15, 2),
267847
+ container = _useState16[0],
267848
+ setContainer = _useState16[1];
267845
267849
  var currentTime = dayjs_min().format(AVAILABLE_FORMATS);
267846
267850
  var updatedStyle = React$1.useMemo(function () {
267847
267851
  var _style$zIndex;
@@ -267858,20 +267862,25 @@ ${formatRule(Codicon.menuSubmenu)}
267858
267862
  var thirdVal = React$1.useMemo(function () {
267859
267863
  return is24hours ? '' : timeUtils.getMeridiem(value, currentTime, amString, pmString) === amString ? 'AM' : 'PM';
267860
267864
  }, [is24hours, value, currentTime, amString, pmString]);
267861
- var _useState15 = React$1.useState([firstVal, secondVal, thirdVal]),
267862
- _useState16 = _slicedToArray$9(_useState15, 2),
267863
- selected = _useState16[0],
267864
- setSelected = _useState16[1];
267865
- var _useState17 = React$1.useState(value),
267865
+ var _useState17 = React$1.useState([firstVal, secondVal, thirdVal]),
267866
267866
  _useState18 = _slicedToArray$9(_useState17, 2),
267867
- callbackValue = _useState18[0],
267868
- setCallbackValue = _useState18[1];
267869
- var _useState19 = React$1.useState(0),
267867
+ selected = _useState18[0],
267868
+ setSelected = _useState18[1];
267869
+ var _useState19 = React$1.useState(value),
267870
267870
  _useState20 = _slicedToArray$9(_useState19, 2),
267871
- setFocusedSpinner = _useState20[1];
267871
+ callbackValue = _useState20[0],
267872
+ setCallbackValue = _useState20[1];
267873
+ var _useState21 = React$1.useState(0),
267874
+ _useState22 = _slicedToArray$9(_useState21, 2),
267875
+ setFocusedSpinner = _useState22[1];
267872
267876
  var secondSpinnerRef = React$1.useRef();
267873
267877
  var thirdSpinnerRef = React$1.useRef();
267874
267878
  var numberOfSpinners = is24hours ? 2 : 3;
267879
+ React$1.useEffect(function () {
267880
+ var active = document.activeElement; // currently focused element
267881
+ var nearestModal = active === null || active === void 0 ? void 0 : active.closest('.cds--modal');
267882
+ setContainer(nearestModal || document.body);
267883
+ }, []);
267875
267884
  React$1.useEffect(function () {
267876
267885
  setSelected([firstVal, secondVal, thirdVal]);
267877
267886
  }, [firstVal, secondVal, thirdVal]);
@@ -267967,6 +267976,7 @@ ${formatRule(Codicon.menuSubmenu)}
267967
267976
  }, /* eslint-disable-next-line react-hooks/exhaustive-deps */
267968
267977
  [selected[2]]);
267969
267978
  var dropdown = /*#__PURE__*/React$1.createElement("div", {
267979
+ ref: ref,
267970
267980
  "data-testid": testId,
267971
267981
  className: classnames("".concat(iotPrefix$1g, "--time-picker-spinner"), _defineProperty$c({}, "".concat(iotPrefix$1g, "--time-picker-spinner--24h"), is24hours)),
267972
267982
  style: _objectSpread$1i(_objectSpread$1i({}, updatedStyle), {}, {
@@ -267974,7 +267984,8 @@ ${formatRule(Codicon.menuSubmenu)}
267974
267984
  top: "".concat(position[1], "px")
267975
267985
  })
267976
267986
  }, listSpinner1, listSpinner2, is24hours ? null : listSpinner3);
267977
- return /*#__PURE__*/ReactDOM.createPortal(dropdown, document.body);
267987
+ if (!container) return null;
267988
+ return /*#__PURE__*/ReactDOM.createPortal(dropdown, container);
267978
267989
  });
267979
267990
  TimePickerSpinner.propTypes = spinnerPropTypes;
267980
267991
  TimePickerSpinner.defaultProps = defaultSpinnerProps;
@@ -269996,7 +270007,7 @@ ${formatRule(Codicon.menuSubmenu)}
269996
270007
  hasIconOnly: false,
269997
270008
  menuOffset: undefined,
269998
270009
  datePickerType: 'range',
269999
- renderInPortal: false,
270010
+ renderInPortal: true,
270000
270011
  useAutoPositioning: false,
270001
270012
  style: {},
270002
270013
  closeOnSelect: true
@@ -270008,96 +270019,148 @@ ${formatRule(Codicon.menuSubmenu)}
270008
270019
  _ref$onClose = _ref.onClose,
270009
270020
  onClose = _ref$onClose === void 0 ? function () {} : _ref$onClose,
270010
270021
  children = _ref.children;
270011
- var portalContentRef = React$1.useRef(null); // Reference to the calendar portal content element
270012
- var _useState = React$1.useState({
270022
+ var _React$useState = React$1.useState(null),
270023
+ _React$useState2 = _slicedToArray$9(_React$useState, 2),
270024
+ container = _React$useState2[0],
270025
+ setContainer = _React$useState2[1];
270026
+ var portalContentRef = React$1.useRef(null);
270027
+ var _React$useState3 = React$1.useState({
270013
270028
  top: 0,
270014
270029
  left: 0
270015
270030
  }),
270016
- _useState2 = _slicedToArray$9(_useState, 2),
270017
- position = _useState2[0],
270018
- setPosition = _useState2[1]; // State to store the position of the calendar portal
270019
-
270020
- var portalWidth = 316; // Width of the calendar portal
270031
+ _React$useState4 = _slicedToArray$9(_React$useState3, 2),
270032
+ position = _React$useState4[0],
270033
+ setPosition = _React$useState4[1];
270034
+ var portalWidth = 316;
270021
270035
 
270036
+ // Decide container once the anchor exists (modal or body)
270037
+ React$1.useEffect(function () {
270038
+ var anchor = anchorRef === null || anchorRef === void 0 ? void 0 : anchorRef.current;
270039
+ if (!anchor) {
270040
+ setContainer(document.body);
270041
+ return;
270042
+ }
270043
+ var modal = anchor.closest('.cds--modal-container');
270044
+ setContainer(modal || document.body);
270045
+ }, [anchorRef, isOpen]);
270022
270046
  var updatePosition = React$1.useCallback(function () {
270023
- var anchor = anchorRef.current; // Reference to the anchor element
270024
- var portal = portalContentRef.current; // Reference to the calendar portal content element
270025
-
270026
- if (!anchor || !portal) return false; // Return early if anchor or portal references are null
270027
-
270028
- var rect = anchor.getBoundingClientRect(); // Get the bounding client rectangle of the anchor element
270029
- var scrollY = window.scrollY || document.documentElement.scrollTop; // Get the current scroll position vertically
270030
- var scrollX = window.scrollX || document.documentElement.scrollLeft; // Get the current scroll position horizontally
270047
+ var anchor = anchorRef.current;
270048
+ var portal = portalContentRef.current;
270049
+ var cont = container;
270050
+ if (!anchor || !portal || !cont) return false;
270051
+ var rect = anchor.getBoundingClientRect();
270031
270052
  var _window = window,
270032
270053
  innerWidth = _window.innerWidth,
270033
- innerHeight = _window.innerHeight; // Get the inner width and height of the window
270034
-
270035
- var portalHeight = portal.offsetHeight; // Get the height of the calendar portal content element
270036
-
270037
- var topSpace = rect.top; // Top space available above the anchor element
270038
- var bottomSpace = innerHeight - rect.bottom; // Bottom space available below the anchor element
270054
+ innerHeight = _window.innerHeight;
270055
+ var portalHeight = portal.offsetHeight;
270056
+ var inModal = cont !== document.body;
270057
+ var top;
270058
+ var left;
270059
+ if (inModal) {
270060
+ // ----- inside modal: absolute, relative to modal rect -----
270061
+ var modalRect = cont.getBoundingClientRect();
270062
+ var topSpace = rect.top - modalRect.top;
270063
+ var bottomSpace = modalRect.bottom - rect.bottom;
270064
+ var fitsBelow = bottomSpace >= portalHeight;
270065
+ var fitsAbove = topSpace >= portalHeight;
270066
+ top = fitsBelow ? rect.bottom - modalRect.top : fitsAbove ? rect.top - portalHeight - modalRect.top : rect.bottom - portalHeight - modalRect.top;
270067
+ left = rect.right - portalWidth - modalRect.left;
270068
+
270069
+ // clamp within modal width
270070
+ var modalWidth = cont.clientWidth;
270071
+ left = Math.max(0, Math.min(left, modalWidth - portalWidth));
270072
+ } else {
270073
+ // ----- in body: FIXED, relative to viewport (no scroll offsets) -----
270074
+ var _topSpace = rect.top;
270075
+ var _bottomSpace = innerHeight - rect.bottom;
270076
+ var _fitsBelow = _bottomSpace >= portalHeight;
270077
+ var _fitsAbove = _topSpace >= portalHeight;
270078
+ top = _fitsBelow ? rect.bottom : _fitsAbove ? rect.top - portalHeight : Math.max(0, innerHeight - portalHeight);
270079
+ left = rect.right - portalWidth;
270080
+ left = Math.max(0, Math.min(left, innerWidth - portalWidth));
270081
+ }
270082
+ setPosition(function (prev) {
270083
+ return prev.top === top && prev.left === left ? prev : {
270084
+ top: top,
270085
+ left: left
270086
+ };
270087
+ });
270088
+ return true;
270089
+ }, [anchorRef, container]);
270039
270090
 
270040
- var fitsBelow = bottomSpace >= portalHeight; // Check if the calendar portal fits below the anchor element
270041
- var fitsAbove = topSpace >= portalHeight; // Check if the calendar portal fits above the anchor element
270091
+ // Run after open + after content lays out
270092
+ React$1.useLayoutEffect(function () {
270093
+ if (!isOpen) return;
270094
+ updatePosition();
270095
+ }, [isOpen, updatePosition]);
270042
270096
 
270043
- var top = fitsBelow ? rect.bottom + scrollY : fitsAbove ? rect.top + scrollY - portalHeight : Math.max(scrollY, scrollY + rect.bottom - portalHeight); // Calculate the top position of the calendar portal
270097
+ // Reposition on resize/scroll. In modal, listen to modal scroller too.
270098
+ React$1.useEffect(function () {
270099
+ if (!isOpen || !container) return;
270100
+ var inModal = container !== document.body;
270101
+ var modalScroller = inModal ? container.querySelector('.cds--modal-content') || container : null;
270102
+ var onRescroll = function onRescroll() {
270103
+ return updatePosition();
270104
+ };
270105
+ window.addEventListener('resize', onRescroll);
270106
+ window.addEventListener('scroll', onRescroll, {
270107
+ capture: true
270108
+ });
270109
+ if (modalScroller) modalScroller.addEventListener('scroll', onRescroll, {
270110
+ capture: true
270111
+ });
270044
270112
 
270045
- var left = rect.right - portalWidth + scrollX; // Calculate the left position of the calendar portal
270046
- left = Math.max(0, Math.min(left, scrollX + innerWidth - portalWidth)); // Ensure the left position is within the viewport
270113
+ // If the calendar's size changes (months switch), keep it stuck to the anchor
270114
+ var ro = new ResizeObserver(onRescroll);
270115
+ if (portalContentRef.current) ro.observe(portalContentRef.current);
270047
270116
 
270048
- setPosition({
270049
- top: top,
270050
- left: left
270051
- }); // Update the position state
270052
- return true;
270053
- });
270054
- React$1.useEffect(function () {
270055
- if (isOpen) {
270056
- var frame = requestAnimationFrame(updatePosition); // Request an animation frame to update the position
270057
- return function () {
270058
- return cancelAnimationFrame(frame);
270059
- }; // Cancel the animation frame when the component unmounts
270060
- }
270061
- return undefined;
270062
- }, [isOpen, updatePosition]); // Depend on the isOpen prop to re-run the effect when it changes
270117
+ // eslint-disable-next-line consistent-return
270118
+ return function () {
270119
+ window.removeEventListener('resize', onRescroll);
270120
+ window.removeEventListener('scroll', onRescroll, {
270121
+ capture: true
270122
+ });
270123
+ if (modalScroller) modalScroller.removeEventListener('scroll', onRescroll, {
270124
+ capture: true
270125
+ });
270126
+ ro.disconnect();
270127
+ };
270128
+ }, [isOpen, container, updatePosition]);
270063
270129
 
270130
+ // Close on outside click
270064
270131
  React$1.useEffect(function () {
270132
+ if (!isOpen) return;
270065
270133
  var handleClickOutside = function handleClickOutside(e) {
270066
- var anchor = anchorRef.current; // Reference to the anchor element
270134
+ var anchor = anchorRef.current;
270067
270135
  if (portalContentRef.current && !portalContentRef.current.contains(e.target) && anchor && !anchor.contains(e.target)) {
270068
- onClose(); // Call the onClose callback when clicking outside the calendar portal
270136
+ onClose();
270069
270137
  }
270070
270138
  };
270071
- window.addEventListener('scroll', updatePosition, true); // Listen for scroll events and update the position
270072
- window.addEventListener('resize', updatePosition); // Listen for resize events and update the position
270073
- document.addEventListener('mousedown', handleClickOutside); // Listen for mousedown events and handle click outside the calendar portal
270074
-
270139
+ document.addEventListener('mousedown', handleClickOutside);
270140
+ // eslint-disable-next-line consistent-return
270075
270141
  return function () {
270076
- window.removeEventListener('scroll', updatePosition, true); // Remove the scroll event listener
270077
- window.removeEventListener('resize', updatePosition); // Remove the resize event listener
270078
- document.removeEventListener('mousedown', handleClickOutside); // Remove the click outside event listener
270142
+ return document.removeEventListener('mousedown', handleClickOutside);
270079
270143
  };
270080
- }, [anchorRef, onClose, updatePosition]); // Depend on the anchorRef and onClose props to re-run the effect when they change
270081
-
270082
- if (!isOpen) return null; // Return null if the calendar portal is not open
270083
-
270144
+ }, [isOpen, onClose, anchorRef]);
270145
+ if (!isOpen || !container) return null;
270146
+ var inModal = container !== document.body;
270147
+ var stylePosition = inModal ? 'absolute' : 'fixed';
270084
270148
  return /*#__PURE__*/ReactDOM.createPortal(/*#__PURE__*/React$1.createElement("div", {
270085
270149
  ref: portalContentRef,
270086
270150
  className: "datetime-picker-portal",
270087
270151
  style: {
270088
- position: 'absolute',
270089
- top: "".concat(position.top, "px"),
270090
- left: "".concat(position.left, "px"),
270091
- width: "".concat(portalWidth, "px"),
270152
+ position: stylePosition,
270153
+ top: position.top,
270154
+ left: position.left,
270155
+ width: portalWidth,
270092
270156
  backgroundColor: 'white',
270093
- padding: '10px',
270157
+ padding: 10,
270094
270158
  zIndex: 9999,
270095
270159
  overflowY: 'auto',
270096
270160
  overflowX: 'hidden',
270097
- maxHeight: '400px'
270161
+ maxHeight: 400
270098
270162
  }
270099
- }, children), document.body // Attach the calendar portal to the document body
270100
- );
270163
+ }, children), container);
270101
270164
  };
270102
270165
  var DateTimePicker$2 = function DateTimePicker(_ref2) {
270103
270166
  var _style$zIndex2;
@@ -270129,6 +270192,7 @@ ${formatRule(Codicon.menuSubmenu)}
270129
270192
  style = _ref2.style,
270130
270193
  closeOnSelect = _ref2.closeOnSelect,
270131
270194
  others = _objectWithoutProperties$3(_ref2, _excluded$C);
270195
+ console.log(renderInPortal);
270132
270196
  var id = React$1.useRef(others.id || v4()).current;
270133
270197
  React$1.useEffect(function () {
270134
270198
  {
@@ -270161,34 +270225,34 @@ ${formatRule(Codicon.menuSubmenu)}
270161
270225
  customRangeKind = _useDateTimePickerRan2[0],
270162
270226
  setCustomRangeKind = _useDateTimePickerRan2[1],
270163
270227
  onCustomRangeChange = _useDateTimePickerRan2[2];
270164
- var _useState3 = React$1.useState(false),
270228
+ var _useState = React$1.useState(false),
270229
+ _useState2 = _slicedToArray$9(_useState, 2),
270230
+ isCustomRange = _useState2[0],
270231
+ setIsCustomRange = _useState2[1];
270232
+ var _useState3 = React$1.useState(null),
270165
270233
  _useState4 = _slicedToArray$9(_useState3, 2),
270166
- isCustomRange = _useState4[0],
270167
- setIsCustomRange = _useState4[1];
270234
+ selectedPreset = _useState4[0],
270235
+ setSelectedPreset = _useState4[1];
270168
270236
  var _useState5 = React$1.useState(null),
270169
270237
  _useState6 = _slicedToArray$9(_useState5, 2),
270170
- selectedPreset = _useState6[0],
270171
- setSelectedPreset = _useState6[1];
270238
+ currentValue = _useState6[0],
270239
+ setCurrentValue = _useState6[1];
270172
270240
  var _useState7 = React$1.useState(null),
270173
270241
  _useState8 = _slicedToArray$9(_useState7, 2),
270174
- currentValue = _useState8[0],
270175
- setCurrentValue = _useState8[1];
270242
+ lastAppliedValue = _useState8[0],
270243
+ setLastAppliedValue = _useState8[1];
270176
270244
  var _useState9 = React$1.useState(null),
270177
270245
  _useState10 = _slicedToArray$9(_useState9, 2),
270178
- lastAppliedValue = _useState10[0],
270179
- setLastAppliedValue = _useState10[1];
270180
- var _useState11 = React$1.useState(null),
270246
+ humanValue = _useState10[0],
270247
+ setHumanValue = _useState10[1];
270248
+ var _useState11 = React$1.useState(false),
270181
270249
  _useState12 = _slicedToArray$9(_useState11, 2),
270182
- humanValue = _useState12[0],
270183
- setHumanValue = _useState12[1];
270184
- var _useState13 = React$1.useState(false),
270250
+ defaultTimeValueUpdate = _useState12[0],
270251
+ setDefaultTimeValueUpdate = _useState12[1];
270252
+ var _useState13 = React$1.useState(invalid),
270185
270253
  _useState14 = _slicedToArray$9(_useState13, 2),
270186
- defaultTimeValueUpdate = _useState14[0],
270187
- setDefaultTimeValueUpdate = _useState14[1];
270188
- var _useState15 = React$1.useState(invalid),
270189
- _useState16 = _slicedToArray$9(_useState15, 2),
270190
- invalidState = _useState16[0],
270191
- setInvalidState = _useState16[1];
270254
+ invalidState = _useState14[0],
270255
+ setInvalidState = _useState14[1];
270192
270256
  var relativeSelect = React$1.useRef(null);
270193
270257
  var containerRef = React$1.useRef();
270194
270258
  var dropdownRef = React$1.useRef();
@@ -270229,34 +270293,34 @@ ${formatRule(Codicon.menuSubmenu)}
270229
270293
  onNavigateRadioButton = _useDateTimePickerKey.onNavigateRadioButton,
270230
270294
  onNavigatePresets = _useDateTimePickerKey.onNavigatePresets,
270231
270295
  onFieldClick = _useDateTimePickerKey.onFieldClick;
270296
+ var _useState15 = React$1.useState(null),
270297
+ _useState16 = _slicedToArray$9(_useState15, 2),
270298
+ singleDateValue = _useState16[0],
270299
+ setSingleDateValue = _useState16[1];
270232
270300
  var _useState17 = React$1.useState(null),
270233
270301
  _useState18 = _slicedToArray$9(_useState17, 2),
270234
- singleDateValue = _useState18[0],
270235
- setSingleDateValue = _useState18[1];
270302
+ singleTimeValue = _useState18[0],
270303
+ setSingleTimeValue = _useState18[1];
270236
270304
  var _useState19 = React$1.useState(null),
270237
270305
  _useState20 = _slicedToArray$9(_useState19, 2),
270238
- singleTimeValue = _useState20[0],
270239
- setSingleTimeValue = _useState20[1];
270306
+ rangeStartTimeValue = _useState20[0],
270307
+ setRangeStartTimeValue = _useState20[1];
270240
270308
  var _useState21 = React$1.useState(null),
270241
270309
  _useState22 = _slicedToArray$9(_useState21, 2),
270242
- rangeStartTimeValue = _useState22[0],
270243
- setRangeStartTimeValue = _useState22[1];
270244
- var _useState23 = React$1.useState(null),
270310
+ rangeEndTimeValue = _useState22[0],
270311
+ setRangeEndTimeValue = _useState22[1];
270312
+ var _useState23 = React$1.useState(false),
270245
270313
  _useState24 = _slicedToArray$9(_useState23, 2),
270246
- rangeEndTimeValue = _useState24[0],
270247
- setRangeEndTimeValue = _useState24[1];
270314
+ invalidRangeStartTime = _useState24[0],
270315
+ setInvalidRangeStartTime = _useState24[1];
270248
270316
  var _useState25 = React$1.useState(false),
270249
270317
  _useState26 = _slicedToArray$9(_useState25, 2),
270250
- invalidRangeStartTime = _useState26[0],
270251
- setInvalidRangeStartTime = _useState26[1];
270318
+ invalidRangeEndTime = _useState26[0],
270319
+ setInvalidRangeEndTime = _useState26[1];
270252
270320
  var _useState27 = React$1.useState(false),
270253
270321
  _useState28 = _slicedToArray$9(_useState27, 2),
270254
- invalidRangeEndTime = _useState28[0],
270255
- setInvalidRangeEndTime = _useState28[1];
270256
- var _useState29 = React$1.useState(false),
270257
- _useState30 = _slicedToArray$9(_useState29, 2),
270258
- invalidRangeStartDate = _useState30[0],
270259
- setInvalidRangeStartDate = _useState30[1];
270322
+ invalidRangeStartDate = _useState28[0],
270323
+ setInvalidRangeStartDate = _useState28[1];
270260
270324
  var dateTimePickerBaseValue = {
270261
270325
  kind: '',
270262
270326
  preset: {
@@ -271365,7 +271429,7 @@ ${formatRule(Codicon.menuSubmenu)}
271365
271429
  },
271366
271430
  "renderInPortal": {
271367
271431
  "defaultValue": {
271368
- "value": "false",
271432
+ "value": "true",
271369
271433
  "computed": false
271370
271434
  },
271371
271435
  "description": "If set to true it will render outside of the current DOM in a portal, otherwise render as a child",