forked from aeharding/voyager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
20 changed files
with
416 additions
and
223 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { FocusEvent, KeyboardEvent, useContext, useRef } from "react"; | ||
import "photoswipe/dist/photoswipe.css"; | ||
import { useAppDispatch } from "../../store"; | ||
import { imageLoaded } from "./gallerySlice"; | ||
import { PostView } from "lemmy-js-client"; | ||
import { GalleryContext } from "./GalleryProvider"; | ||
import { PreparedPhotoSwipeOptions } from "photoswipe"; | ||
|
||
export interface GalleryImgProps { | ||
src?: string; | ||
alt?: string; | ||
className?: string; | ||
post?: PostView; | ||
animationType?: PreparedPhotoSwipeOptions["showHideAnimationType"]; | ||
} | ||
|
||
/** | ||
* TODO: photoswipe traps focus, so onFocusCapture and onKeyDown is a hack to prevent it | ||
* from detecting that we're changing focus. It's not great.. but it's what we got | ||
* https://github.com/dimsemenov/PhotoSwipe/issues/1968 | ||
*/ | ||
export const preventPhotoswipeGalleryFocusTrap = { | ||
onFocusCapture: (e: FocusEvent) => e.stopPropagation(), | ||
onKeyDown: (e: KeyboardEvent) => e.stopPropagation(), | ||
}; | ||
|
||
export function GalleryImg({ | ||
src, | ||
alt, | ||
className, | ||
post, | ||
animationType, | ||
}: GalleryImgProps) { | ||
const dispatch = useAppDispatch(); | ||
const loaded = useRef(false); | ||
const imgRef = useRef<HTMLImageElement>(null); | ||
const { open } = useContext(GalleryContext); | ||
|
||
return ( | ||
<img | ||
ref={imgRef} | ||
draggable="false" | ||
alt={alt} | ||
onClick={(e) => { | ||
if (!loaded.current) return; | ||
|
||
e.stopPropagation(); | ||
|
||
open(e.currentTarget, post, animationType); | ||
}} | ||
src={src} | ||
className={className} | ||
onLoad={(e) => { | ||
if (!(e.target instanceof HTMLImageElement)) return; | ||
if (!src) return; | ||
|
||
loaded.current = true; | ||
|
||
dispatch( | ||
imageLoaded({ | ||
src, | ||
width: e.target.naturalWidth, | ||
height: e.target.naturalHeight, | ||
}) | ||
); | ||
}} | ||
/> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.