@oods/components-react 0.4.0 → 0.4.1

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.
package/README.md CHANGED
@@ -32,4 +32,4 @@ Use matching versions of the OODS packages. These packages are separate from the
32
32
 
33
33
  ## License
34
34
 
35
- Apache License 2.0. See [LICENSE](https://cdn.jsdelivr.net/npm/@oods/components-react@0.4.0/LICENSE) and [NOTICE](https://cdn.jsdelivr.net/npm/@oods/components-react@0.4.0/NOTICE). Generated output belongs to its user; installed package dependencies retain their own licenses.
35
+ Apache License 2.0. See [LICENSE](https://cdn.jsdelivr.net/npm/@oods/components-react@0.4.1/LICENSE) and [NOTICE](https://cdn.jsdelivr.net/npm/@oods/components-react@0.4.1/NOTICE). Generated output belongs to its user; installed package dependencies retain their own licenses.
@@ -636,6 +636,31 @@ var noSubscription = () => () => void 0;
636
636
  var useIsomorphicLayoutEffect = typeof window === "undefined" ? React3.useEffect : React3.useLayoutEffect;
637
637
  var canShowModal = () => typeof HTMLDialogElement !== "undefined" && typeof HTMLDialogElement.prototype.showModal === "function";
638
638
  var hasContent = (node) => node !== void 0 && node !== null && node !== false && node !== "";
639
+ var pageLocks = 0;
640
+ var pageStyle;
641
+ function lockPageScroll() {
642
+ const page = document.documentElement;
643
+ if (pageLocks++ === 0) {
644
+ const scrollbar = window.innerWidth - page.clientWidth;
645
+ pageStyle = { overflow: page.style.overflow, paddingRight: page.style.paddingRight };
646
+ if (scrollbar > 0) page.style.paddingRight = `${parseFloat(getComputedStyle(page).paddingRight) + scrollbar}px`;
647
+ page.style.overflow = "hidden";
648
+ }
649
+ let held = true;
650
+ return () => {
651
+ if (!held) return;
652
+ held = false;
653
+ if (--pageLocks === 0 && pageStyle) {
654
+ page.style.overflow = pageStyle.overflow;
655
+ page.style.paddingRight = pageStyle.paddingRight;
656
+ pageStyle = void 0;
657
+ }
658
+ };
659
+ }
660
+ var outsideBox = (event, dialog) => {
661
+ const box = dialog.getBoundingClientRect();
662
+ return event.clientX < box.left || event.clientX > box.right || event.clientY < box.top || event.clientY > box.bottom;
663
+ };
639
664
  var Dialog = React3.forwardRef(
640
665
  ({
641
666
  id,
@@ -650,12 +675,16 @@ var Dialog = React3.forwardRef(
650
675
  className,
651
676
  onClose,
652
677
  onKeyDown,
678
+ onMouseDown,
679
+ onClick,
653
680
  ...rest
654
681
  }, ref) => {
655
682
  const generatedId = React3.useId().replace(/[^\w-]/g, "");
656
683
  const dialogId = id ?? `oods-dialog-${generatedId}`;
657
684
  const element = React3.useRef(null);
658
685
  const openRef = React3.useRef(open);
686
+ const scrollLock = React3.useRef(null);
687
+ const pressedBackdrop = React3.useRef(false);
659
688
  const serverMarkup = React3.useSyncExternalStore(noSubscription, () => false, () => true);
660
689
  const managed = modal && !serverMarkup && canShowModal();
661
690
  const setRef = React3.useCallback((node) => {
@@ -663,13 +692,19 @@ var Dialog = React3.forwardRef(
663
692
  if (typeof ref === "function") ref(node);
664
693
  else if (ref) ref.current = node;
665
694
  }, [ref]);
695
+ const releaseScroll = React3.useCallback(() => {
696
+ scrollLock.current?.();
697
+ scrollLock.current = null;
698
+ }, []);
666
699
  useIsomorphicLayoutEffect(() => {
667
700
  openRef.current = open;
668
701
  const dialog = element.current;
669
702
  if (!dialog || !managed) return;
670
703
  if (open && !dialog.open) dialog.showModal();
671
704
  else if (!open && dialog.open) dialog.close();
705
+ if (open && dialog.open) scrollLock.current ??= lockPageScroll();
672
706
  }, [open, managed]);
707
+ React3.useEffect(() => releaseScroll, [releaseScroll]);
673
708
  const requestClose = (event) => {
674
709
  event.preventDefault();
675
710
  onClose?.();
@@ -691,7 +726,19 @@ var Dialog = React3.forwardRef(
691
726
  },
692
727
  onCancel: requestClose,
693
728
  onClose: () => {
694
- if (openRef.current && !element.current?.open) onClose?.();
729
+ if (element.current?.open) return;
730
+ releaseScroll();
731
+ if (openRef.current) onClose?.();
732
+ },
733
+ onMouseDown: (event) => {
734
+ onMouseDown?.(event);
735
+ pressedBackdrop.current = managed && event.target === event.currentTarget && outsideBox(event, event.currentTarget);
736
+ },
737
+ onClick: (event) => {
738
+ onClick?.(event);
739
+ const backdrop = pressedBackdrop.current && event.target === event.currentTarget && outsideBox(event, event.currentTarget);
740
+ pressedBackdrop.current = false;
741
+ if (backdrop && !event.defaultPrevented) onClose?.();
695
742
  },
696
743
  ...rest,
697
744
  children: [
@@ -1353,4 +1400,4 @@ var RelativeTimestamp = React3.forwardRef(
1353
1400
  );
1354
1401
  RelativeTimestamp.displayName = "OODS.RelativeTimestamp";
1355
1402
 
1356
- export { AuditTimeline, Badge, Banner, Button, CancellationSummary, Card, Checkbox, DatePicker, Dialog, Input, PaginationBar, PriceBadge, RelativeTimestamp, SearchInput, Select, StatusBadge, StatusTimeline, Switch, Text, Textarea };
1403
+ export { AuditTimeline, Badge, Banner, Button, CancellationSummary, Card, Checkbox, DatePicker, Dialog, Input, PaginationBar, PriceBadge, RelativeTimestamp, SearchInput, Select, StatusBadge, StatusIcon, StatusTimeline, Switch, Text, Textarea };