diff --git a/packages/kg-unsplash-selector/src/UnsplashSearchModal.tsx b/packages/kg-unsplash-selector/src/UnsplashSearchModal.tsx index 3edd012bc3..1d9aedb16f 100644 --- a/packages/kg-unsplash-selector/src/UnsplashSearchModal.tsx +++ b/packages/kg-unsplash-selector/src/UnsplashSearchModal.tsx @@ -16,6 +16,9 @@ interface UnsplashModalProps { } export const UnsplashSearchModal : React.FC = ({onClose, onImageInsert, unsplashProviderConfig}) => { + const ONE_COLUMN_WIDTH = 540; + const TWO_COLUMN_WIDTH = 1024; + const unsplashProvider = useMemo(() => { if (!unsplashProviderConfig) { return new InMemoryUnsplashProvider(); @@ -73,6 +76,37 @@ export const UnsplashSearchModal : React.FC = ({onClose, onI } }, [galleryRef, zoomedImg]); + const calculateColumnCountForViewport = () => { + if (window.matchMedia(`(max-width: ${ONE_COLUMN_WIDTH}px)`).matches) { + return 1; + } + + if (window.matchMedia(`(max-width: ${TWO_COLUMN_WIDTH}px)`).matches) { + return 2; + } + + return 3; + }; + + React.useEffect(() => { + const handleResize = () => { + const newColumnCount = calculateColumnCountForViewport(); + masonryService.changeColumnCount(newColumnCount); + UnsplashLib.layoutPhotos(); + const columns = UnsplashLib.getColumns(); + setDataset(columns || []); + }; + + window.addEventListener('resize', handleResize); + + // Call once to make sure state is in sync on mount + handleResize(); + + return () => { + window.removeEventListener('resize', handleResize); + }; + }, [UnsplashLib, masonryService]); + const loadInitPhotos = React.useCallback(async () => { if (initLoadRef.current === false || searchTerm.length === 0) { setDataset([]); @@ -155,6 +189,15 @@ export const UnsplashSearchModal : React.FC = ({onClose, onI }, [galleryRef, loadMorePhotos, zoomedImg]); const selectImg = (payload:Photo) => { + const isMobileViewport = window.matchMedia('(max-width: 540px)').matches; + const isTouchDevice = window.matchMedia('(pointer: coarse)').matches; + const shouldNotZoom = isMobileViewport || isTouchDevice; + + if (shouldNotZoom) { + setZoomedImg(null); + return; + } + if (payload) { setZoomedImg(payload); setLastScrollPos(scrollPos); @@ -176,10 +219,12 @@ export const UnsplashSearchModal : React.FC = ({onClose, onI } return ( ; isLoading?: boolean; zoomed?: Photo | null; } interface UnsplashGalleryProps extends GalleryLayoutProps { + columnCount?: number; error?: string | null; dataset?: Photo[][] | []; selectImg?: any; @@ -78,9 +80,18 @@ const UnsplashGalleryColumns: React.FC = (props) => }; const GalleryLayout: React.FC = (props) => { + const columnCount = props?.columnCount ?? 0; + const classNames = [ + 'flex', + 'size-full', + 'justify-center', + 'overflow-auto', + props?.zoomed ? 'pb-10' : '', + columnCount < 3 ? 'px-5' : 'px-20' + ].filter(Boolean).join(' '); return ( -
-
+
+
{props.children} {props?.isLoading && }
@@ -89,6 +100,7 @@ const GalleryLayout: React.FC = (props) => { }; const UnsplashGallery: React.FC = ({zoomed, + columnCount, error, galleryRef, isLoading, @@ -133,6 +145,7 @@ const UnsplashGallery: React.FC = ({zoomed, return ( diff --git a/packages/kg-unsplash-selector/src/ui/UnsplashImage.tsx b/packages/kg-unsplash-selector/src/ui/UnsplashImage.tsx index f4b151d49d..00e0719d49 100644 --- a/packages/kg-unsplash-selector/src/ui/UnsplashImage.tsx +++ b/packages/kg-unsplash-selector/src/ui/UnsplashImage.tsx @@ -32,7 +32,7 @@ const UnsplashImage: FC = ({payload, srcUrl, links, likes, u return (
@@ -61,10 +61,10 @@ const UnsplashImage: FC = ({payload, srcUrl, links, likes, u icon="download" />
-
-
- author -
{user.name}
+
+
+ author +
{user.name}
{ e.stopPropagation(); diff --git a/packages/kg-unsplash-selector/src/ui/UnsplashSelector.tsx b/packages/kg-unsplash-selector/src/ui/UnsplashSelector.tsx index 8f49534a08..5f7f286901 100644 --- a/packages/kg-unsplash-selector/src/ui/UnsplashSelector.tsx +++ b/packages/kg-unsplash-selector/src/ui/UnsplashSelector.tsx @@ -5,11 +5,12 @@ import {ChangeEvent, FunctionComponent, ReactNode} from 'react'; interface UnsplashSelectorProps { closeModal: () => void; + columnCount: number; handleSearch: (e: ChangeEvent) => void; children: ReactNode; } -const UnsplashSelector: FunctionComponent = ({closeModal, handleSearch, children}) => { +const UnsplashSelector: FunctionComponent = ({closeModal, columnCount, handleSearch, children}) => { return ( <>
@@ -22,12 +23,12 @@ const UnsplashSelector: FunctionComponent = ({closeModal, />
-
+

- + Unsplash

-
+
diff --git a/packages/kg-unsplash-selector/src/ui/UnsplashZoomed.tsx b/packages/kg-unsplash-selector/src/ui/UnsplashZoomed.tsx index 88860d3b18..9de6be8ac0 100644 --- a/packages/kg-unsplash-selector/src/ui/UnsplashZoomed.tsx +++ b/packages/kg-unsplash-selector/src/ui/UnsplashZoomed.tsx @@ -9,7 +9,7 @@ interface UnsplashZoomedProps extends Omit { const UnsplashZoomed: FC = ({payload, insertImage, selectImg, zoomed}) => { return ( -
selectImg(null)}> +
selectImg(null)}>