@jekrch/react-viewport-lightbox 0.3.0 → 0.3.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/dist/index.js CHANGED
@@ -739,6 +739,7 @@ function NavButton({ direction, enabled, onClick, icon, className }) {
739
739
  }
740
740
  var ANIM_MS = 250;
741
741
  var IMG_PADDING = 44;
742
+ var GHOST_CLICK_MS = 700;
742
743
  var ZOOM_EASE = "cubic-bezier(0.22, 1, 0.36, 1)";
743
744
  function prefersReducedMotion() {
744
745
  if (typeof window === "undefined" || !window.matchMedia) return false;
@@ -792,6 +793,11 @@ function ImageViewer({
792
793
  const [collapsing, setCollapsing] = useState(false);
793
794
  const [isTouchDevice, setIsTouchDevice] = useState(false);
794
795
  const [contentShift, setContentShiftState] = useState({ transform: null, animate: true });
796
+ const openedAtRef = useRef(Date.now());
797
+ const isGhostMouseEvent = useCallback(
798
+ () => Date.now() - openedAtRef.current < GHOST_CLICK_MS,
799
+ []
800
+ );
795
801
  const containerRef = useRef(null);
796
802
  const imgWrapperRef = useRef(null);
797
803
  const topBarRef = useRef(null);
@@ -939,6 +945,33 @@ function ImageViewer({
939
945
  }
940
946
  setTimeout(onClose, reduce ? 0 : ANIM_MS);
941
947
  }, [onClose, getOriginRect, index, isZoomed, imgRef, imgWrapperRef]);
948
+ const handleBackdropTouchEnd = useCallback(
949
+ (e) => {
950
+ if (e.target !== e.currentTarget) return;
951
+ e.preventDefault();
952
+ handleClose();
953
+ },
954
+ [handleClose]
955
+ );
956
+ const handleDoubleClickGuarded = useCallback(
957
+ (e) => {
958
+ if (isGhostMouseEvent()) return;
959
+ handleDoubleClick(e);
960
+ },
961
+ [handleDoubleClick, isGhostMouseEvent]
962
+ );
963
+ const handleBackdropClick = useCallback(() => {
964
+ if (isGhostMouseEvent()) return;
965
+ handleClose();
966
+ }, [handleClose, isGhostMouseEvent]);
967
+ const handleStageClick = useCallback(
968
+ (e) => {
969
+ if (e.target !== e.currentTarget) return;
970
+ if (isGhostMouseEvent()) return;
971
+ handleClose();
972
+ },
973
+ [handleClose, isGhostMouseEvent]
974
+ );
942
975
  const navigate = useCallback(
943
976
  (dir) => {
944
977
  if (dir === "prev") {
@@ -1040,7 +1073,8 @@ function ImageViewer({
1040
1073
  "div",
1041
1074
  {
1042
1075
  className: cx("rvl-backdrop", cn("backdrop")),
1043
- onClick: closeOnBackdropClick ? handleClose : void 0,
1076
+ onClick: closeOnBackdropClick ? handleBackdropClick : void 0,
1077
+ onTouchEnd: closeOnBackdropClick ? handleBackdropTouchEnd : void 0,
1044
1078
  "aria-hidden": "true"
1045
1079
  }
1046
1080
  ),
@@ -1113,9 +1147,8 @@ function ImageViewer({
1113
1147
  "div",
1114
1148
  {
1115
1149
  className: "rvl-stage",
1116
- onClick: closeOnBackdropClick ? (e) => {
1117
- if (e.target === e.currentTarget) handleClose();
1118
- } : void 0,
1150
+ onClick: closeOnBackdropClick ? handleStageClick : void 0,
1151
+ onTouchEnd: closeOnBackdropClick ? handleBackdropTouchEnd : void 0,
1119
1152
  style: {
1120
1153
  transform: contentShift.transform ?? "translateY(0)",
1121
1154
  // animate=false snaps with no transition (overrides the CSS transition)
@@ -1158,7 +1191,7 @@ function ImageViewer({
1158
1191
  ref: imgWrapperRef,
1159
1192
  className: "rvl-img-wrapper",
1160
1193
  onClick: (e) => e.stopPropagation(),
1161
- onDoubleClick: handleDoubleClick,
1194
+ onDoubleClick: handleDoubleClickGuarded,
1162
1195
  onPointerDown: gestures.handlePointerDown,
1163
1196
  onPointerMove: gestures.handlePointerMove,
1164
1197
  onPointerUp: gestures.handlePointerUp,