refactor: optimize the user experience of lightbox

This commit is contained in:
lawvs
2025-07-09 18:55:43 +08:00
parent 99f0b2aaeb
commit 777f4caba9
10 changed files with 58 additions and 35 deletions
@@ -6,6 +6,7 @@
*
*/
import type { ImageProps } from "expo-image"
import type { TransformsStyle } from "react-native"
import type { MeasuredDimensions } from "react-native-reanimated"
@@ -19,10 +20,10 @@ export type Position = {
y: number
}
export type ImageSource = {
export type LightboxImageSource = {
uri: string
dimensions: Dimensions | null
thumbUri: string
thumbUri: ImageProps["placeholder"]
thumbDimensions: Dimensions | null
thumbRect: MeasuredDimensions | null
alt?: string
@@ -15,7 +15,7 @@ import Animated, {
import { Image as ProxyImage } from "@/src/components/ui/image/Image"
import type { Dimensions as ImageDimensions, ImageSource, Transform } from "../../@types"
import type { Dimensions as ImageDimensions, LightboxImageSource, Transform } from "../../@types"
import type { TransformMatrix } from "../../transforms"
import {
applyRounding,
@@ -32,7 +32,7 @@ const MAX_ORIGINAL_IMAGE_ZOOM = 2
const initialTransform = createTransform()
type Props = {
imageSrc: ImageSource
imageSrc: LightboxImageSource
onRequestClose: () => void
onTap: () => void
onZoom: (isZoomed: boolean) => void
@@ -350,7 +350,7 @@ const ImageItem = ({
contentFit="contain"
source={{ uri: imageSrc.uri }}
placeholderContentFit="contain"
placeholder={{ uri: imageSrc.thumbUri }}
placeholder={imageSrc.thumbUri}
accessibilityLabel={imageSrc.alt}
onLoad={
hasLoaded
@@ -360,7 +360,7 @@ const ImageItem = ({
onLoad({ width: e.source.width, height: e.source.height })
}
}
style={{ flex: 1, borderRadius, backgroundColor: "transparent" }}
style={{ flex: 1, borderRadius, backgroundColor: "rgba(255, 255, 255, 0.1)" }}
accessibilityHint=""
accessibilityIgnoresInvertColors
cachePolicy="memory"
@@ -25,13 +25,13 @@ import { useSafeAreaFrame } from "react-native-safe-area-context"
import { Image as ProxyImage } from "@/src/components/ui/image/Image"
import type { Dimensions as ImageDimensions, ImageSource, Transform } from "../../@types"
import type { Dimensions as ImageDimensions, LightboxImageSource, Transform } from "../../@types"
const MAX_ORIGINAL_IMAGE_ZOOM = 2
const MIN_SCREEN_ZOOM = 2
type Props = {
imageSrc: ImageSource
imageSrc: LightboxImageSource
onRequestClose: () => void
onTap: () => void
onZoom: (scaled: boolean) => void
@@ -219,13 +219,17 @@ const ImageItem = ({
contentFit="contain"
source={{ uri: imageSrc.uri }}
placeholderContentFit="contain"
placeholder={{ uri: imageSrc.thumbUri }}
style={{ flex: 1, borderRadius, backgroundColor: "transparent" }}
placeholder={imageSrc.thumbUri}
style={{ flex: 1, borderRadius, backgroundColor: "rgba(255, 255, 255, 0.1)" }}
accessibilityLabel={imageSrc.alt}
accessibilityHint=""
enableLiveTextInteraction={showControls && !scaled}
accessibilityIgnoresInvertColors
priority="high"
// Workaround for a bug where the image doesn't retrieve the cache on iOS
// Remove this when updated to Expo
// Learn more https://github.com/expo/expo/issues/37705
blurRadius={0.001}
onLoad={
hasLoaded
? undefined
@@ -8,12 +8,12 @@ import type { SharedValue } from "react-native-reanimated"
import type {
Dimensions,
Dimensions as ImageDimensions,
ImageSource,
LightboxImageSource,
Transform,
} from "../../@types"
type Props = {
imageSrc: ImageSource
imageSrc: LightboxImageSource
onRequestClose: () => void
onTap: () => void
onZoom: (scaled: boolean) => void
@@ -35,7 +35,7 @@ import { useSafeAreaFrame, useSafeAreaInsets } from "react-native-safe-area-cont
import { isIOS } from "@/src/lib/platform"
import type { Lightbox } from "../lightboxState"
import type { Dimensions, ImageSource, Transform } from "./@types"
import type { Dimensions, LightboxImageSource, Transform } from "./@types"
import ImageDefaultHeader from "./components/ImageDefaultHeader"
import ImageItem from "./components/ImageItem/ImageItem"
@@ -377,7 +377,7 @@ function LightboxImage({
openProgress,
dismissSwipeTranslateY,
}: {
imageSrc: ImageSource
imageSrc: LightboxImageSource
onRequestClose: () => void
onTap: () => void
onZoom: (scaled: boolean) => void
@@ -517,7 +517,7 @@ function LightboxFooter({
isAltExpanded,
toggleAltExpanded,
}: {
images: ImageSource[]
images: LightboxImageSource[]
index: number
isAltExpanded: boolean
toggleAltExpanded: () => void
@@ -2,11 +2,11 @@ import { nanoid } from "nanoid/non-secure"
import type { PropsWithChildren } from "react"
import { createContext, use, useCallback, useMemo, useState } from "react"
import type { ImageSource } from "./ImageViewing/@types"
import type { LightboxImageSource } from "./ImageViewing/@types"
export type Lightbox = {
id: string
images: ImageSource[]
images: LightboxImageSource[]
index: number
}
@@ -1,5 +1,6 @@
import type { FeedViewType } from "@follow/constants"
import type { MediaModel } from "@follow/database/schemas/types"
import type { ImageSource } from "expo-image"
import type { Ref } from "react"
import { useEffect, useState } from "react"
import { ScrollView, TouchableOpacity, View } from "react-native"
@@ -14,6 +15,7 @@ import { EntryGridFooter } from "@/src/modules/entry-content/EntryGridFooter"
import { Image } from "../image/Image"
import { ImageContextMenu } from "../image/ImageContextMenu"
import { getAllSources } from "../image/utils"
import { VideoPlayer } from "../video/VideoPlayer"
export const MediaCarousel = ({
@@ -27,7 +29,7 @@ export const MediaCarousel = ({
ref?: Ref<View>
entryId: string
media: MediaModel[]
onPreview?: (index: number) => void
onPreview?: (index: number, placeholder: ImageSource | undefined) => void
aspectRatio: number
view: FeedViewType
}) => {
@@ -64,12 +66,18 @@ export const MediaCarousel = ({
if (!imageUrl) {
return null
}
const proxy = {
height: 400,
}
const ImageItem = (
<TouchableOpacity onPress={() => onPreview?.(index)}>
<TouchableOpacity
onPress={() => {
const [placeholder] = getAllSources({ uri: imageUrl }, proxy)
onPreview?.(index, { blurhash: m.blurhash, ...placeholder })
}}
>
<Image
proxy={{
height: 400,
}}
proxy={proxy}
source={{ uri: imageUrl }}
blurhash={m.blurhash}
className="w-full"
+2 -2
View File
@@ -38,7 +38,7 @@ export const getAllSources = (
},
) => {
if (!isImageSourceWithUri(source)) {
return [undefined, undefined]
return [undefined, undefined] as const
}
// Now TypeScript knows source has a uri property
@@ -67,7 +67,7 @@ export const getAllSources = (
}
})()
return [safeSource, proxiesSafeSource]
return [safeSource, proxiesSafeSource] as const
}
const getImageData = async (imageUrl: string) => {
@@ -5,6 +5,7 @@ import { getFeedById } from "@follow/store/feed/getter"
import { unreadSyncService } from "@follow/store/unread/store"
import { tracker } from "@follow/tracker"
import { uniqBy } from "es-toolkit/compat"
import type { ImageSource } from "expo-image"
import type { Ref } from "react"
import { useMemo } from "react"
import { Text, View } from "react-native"
@@ -44,7 +45,7 @@ export function EntryPictureItem({ id }: { id: string }) {
ref={aviRef}
media={item.media}
entryId={id}
onPreview={(index) => {
onPreview={(index, placeholder) => {
const feed = getFeedById(item.feedId!)
if (!feed) {
return
@@ -60,7 +61,7 @@ export function EntryPictureItem({ id }: { id: string }) {
runOnJS(openLightbox)({
images: (item.media ?? []).map((media) => ({
uri: media.url,
thumbUri: media.url,
thumbUri: placeholder ?? { uri: media.url },
thumbDimensions: null,
thumbRect: rect,
dimensions: rect
@@ -95,7 +96,7 @@ const MediaItems = ({
ref?: Ref<View>
media: MediaModel[]
entryId: string
onPreview?: (index: number) => void
onPreview?: (index: number, placeholder: ImageSource | undefined) => void
aspectRatio?: number
}) => {
const firstMedia = media[0]
@@ -5,17 +5,20 @@ import { useFeedById } from "@follow/store/feed/hooks"
import { useEntryTranslation } from "@follow/store/translation/hooks"
import { unreadSyncService } from "@follow/store/unread/store"
import { tracker } from "@follow/tracker"
import type { ImageSource } from "expo-image"
import { memo, useCallback } from "react"
import { Pressable, Text, View } from "react-native"
import type { MeasuredDimensions } from "react-native-reanimated"
import Animated, { measure, runOnJS, runOnUI, useAnimatedRef } from "react-native-reanimated"
import { useActionLanguage, useGeneralSettingKey } from "@/src/atoms/settings/general"
import type { LightboxImageSource } from "@/src/components/lightbox/ImageViewing/@types"
import { useLightboxControls } from "@/src/components/lightbox/lightboxState"
import { UserAvatar } from "@/src/components/ui/avatar/UserAvatar"
import { RelativeDateTime } from "@/src/components/ui/datetime/RelativeDateTime"
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
import { Image } from "@/src/components/ui/image/Image"
import { getAllSources } from "@/src/components/ui/image/utils"
import { ItemPressableStyle } from "@/src/components/ui/pressable/enum"
import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable"
import { NativePressable } from "@/src/components/ui/pressable/NativePressable"
@@ -72,7 +75,7 @@ export const EntrySocialItem = memo(
}, [entry, navigation])
const onPreviewImage = useCallback(
(index: number, rect: MeasuredDimensions | null) => {
(index: number, rect: MeasuredDimensions | null, placeholder: ImageSource | undefined) => {
"worklet"
runOnJS(openLightbox)({
images: (entry?.media ?? [])
@@ -89,11 +92,11 @@ export const EntrySocialItem = memo(
width: mediaItem.width ?? 0,
height: mediaItem.height ?? 0,
},
thumbUri: imageUrl ?? "",
thumbUri: placeholder ?? { uri: imageUrl },
thumbDimensions: null,
thumbRect: rect,
type: "image" as const,
}
} satisfies LightboxImageSource
})
.filter((i) => !!i.uri),
index,
@@ -181,7 +184,11 @@ interface EntryMediaItemProps {
entryId: string
mediaItem: MediaModel
fullWidth: boolean
onPreviewImage: (index: number, rect: MeasuredDimensions | null) => void
onPreviewImage: (
index: number,
rect: MeasuredDimensions | null,
placeholder: ImageSource | undefined,
) => void
}
const EntryMediaItem = memo(
@@ -196,21 +203,23 @@ const EntryMediaItem = memo(
: undefined
if (!imageUrl) return null
const proxy = {
width: fullWidth ? 400 : 200,
}
const ImageItem = (
<Animated.View ref={aviRef} collapsable={false}>
<NativePressable
onPress={() => {
const [placeholder] = getAllSources({ uri: imageUrl }, proxy)
runOnUI(() => {
"worklet"
const rect = measure(aviRef)
onPreviewImage(index, rect)
onPreviewImage(index, rect, { blurhash: mediaItem.blurhash, ...placeholder })
})()
}}
>
<Image
proxy={{
width: fullWidth ? 400 : 200,
}}
proxy={proxy}
source={{ uri: imageUrl }}
blurhash={mediaItem.blurhash}
className="border-secondary-system-background w-full rounded-lg border"