From d03e792eaeef13d5bbd0729719914b52eb3c3df0 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Fri, 16 Oct 2026 19:48:38 +0300 Subject: [PATCH] APP-3447: Keep alt text drafts inside the dialog (#12042) Co-authored-by: Claude Opus 5.5 --- src/view/com/composer/GifAltText.tsx | 35 ++++++++++++++----- .../composer/photos/ImageAltTextDialog.tsx | 27 +++++++++----- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/src/view/com/composer/GifAltText.tsx b/src/view/com/composer/GifAltText.tsx index 27ac913d3..3fee7afbc 100644 --- a/src/view/com/composer/GifAltText.tsx +++ b/src/view/com/composer/GifAltText.tsx @@ -1,4 +1,4 @@ -import {useState} from 'react' +import {useRef, useState} from 'react' import {TouchableOpacity, View} from 'react-native' import {msg} from '@lingui/core/macro' import {useLingui} from '@lingui/react' @@ -68,7 +68,14 @@ export function GifAltTextDialogLoaded({ const control = Dialog.useDialogControl() const {_} = useLingui() const t = useTheme() - const [altTextDraft, setAltTextDraft] = useState(altText || vendorAltText) + /* + * The text being edited lives in AltTextInner, not here. The sheet's portal + * forwards its children from an effect, so state held above it reaches the + * controlled input a commit late and makes typing jumpy. This only tracks the + * latest edit so it can be saved on close, however the dialog is closed. + */ + const editedAltText = useRef(null) + const initialAltText = altText || vendorAltText return ( <> { - onSubmit(enforceLen(altTextDraft, MAX_ALT_TEXT, true)) + /* + * Closing without editing accepts the vendor alt text, which counts + * towards the alt text requirement. + */ + const next = editedAltText.current ?? initialAltText + editedAltText.current = null + onSubmit(enforceLen(next, MAX_ALT_TEXT, true)) }} nativeOptions={{fullHeight: true}}> { + editedAltText.current = text + }} thumb={thumb} control={control} params={params} @@ -131,19 +146,20 @@ export function GifAltTextDialogLoaded({ function AltTextInner({ vendorAltText, - altText, + initialAltText, onChange, control, params, thumb, }: { vendorAltText: string - altText: string + initialAltText: string onChange: (text: string) => void control: DialogControlProps params: EmbedPlayerParams thumb: string | undefined }) { + const [altText, setAltText] = useState(initialAltText) const t = useTheme() const {_, i18n} = useLingui() @@ -160,7 +176,10 @@ function AltTextInner({ { + setAltText(text) + onChange(text) + }} value={altText} multiline autoFocus diff --git a/src/view/com/composer/photos/ImageAltTextDialog.tsx b/src/view/com/composer/photos/ImageAltTextDialog.tsx index 1f40085d0..24d6748fd 100644 --- a/src/view/com/composer/photos/ImageAltTextDialog.tsx +++ b/src/view/com/composer/photos/ImageAltTextDialog.tsx @@ -1,4 +1,4 @@ -import {useMemo, useState} from 'react' +import {useMemo, useRef, useState} from 'react' import {type ImageStyle, useWindowDimensions, View} from 'react-native' import {Image} from 'expo-image' import {msg} from '@lingui/core/macro' @@ -31,40 +31,48 @@ export const ImageAltTextDialog = ({ onChange, sourceViewTag, }: Props): React.ReactNode => { - const [altText, setAltText] = useState(image.alt) + /* + * The text being edited lives in ImageAltTextInner, not here. The sheet's + * portal forwards its children from an effect, so state held above it reaches + * the controlled input a commit late and makes typing jumpy. This only tracks + * the latest edit so it can be saved on close, however the dialog is closed. + */ + const editedAltText = useRef(null) return ( { + if (editedAltText.current === null) return onChange({ ...image, - alt: enforceLen(altText, MAX_ALT_TEXT, true), + alt: enforceLen(editedAltText.current, MAX_ALT_TEXT, true), }) + editedAltText.current = null }} nativeOptions={{fullHeight: true, sourceViewTag}}> { + editedAltText.current = text + }} /> ) } const ImageAltTextInner = ({ - altText, - setAltText, + onChangeText, control, image, }: { - altText: string - setAltText: (text: string) => void + onChangeText: (text: string) => void control: DialogControlProps image: Props['image'] }): React.ReactNode => { + const [altText, setAltText] = useState(image.alt) const {_, i18n} = useLingui() const t = useTheme() const {width: screenWidth} = useWindowDimensions() @@ -128,6 +136,7 @@ const ImageAltTextInner = ({ label={_(msg`Alt text`)} onChangeText={text => { setAltText(text) + onChangeText(text) }} value={altText} multiline -- knotmirror 0.0.1