Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add resizable text area component to mobile app (#2193)
Leendert de Borstbeaae63eac05
diff --git a/apps/mobile-app/app/(tabs)/items/add-edit.tsx b/apps/mobile-app/app/(tabs)/items/add-edit.tsxindex 05c974642..4ad7c985d 100644--- a/apps/mobile-app/app/(tabs)/items/add-edit.tsx+++ b/apps/mobile-app/app/(tabs)/items/add-edit.tsx@@ -34,6 +34,7 @@ import { FormSection } from '@/components/form/FormSection';import { HiddenField } from '@/components/form/HiddenField';import { ItemNameField, ItemNameFieldRef } from '@/components/form/ItemNameField';import { MultiValueField } from '@/components/form/MultiValueField';+import { ResizableTextArea } from '@/components/form/ResizableTextArea';import { AttachmentUploader } from '@/components/items/details/AttachmentUploader';import { TotpEditor } from '@/components/items/details/TotpEditor';import { ItemTypeSelector } from '@/components/items/ItemTypeSelector';@@ -1149,13 +1150,10 @@ export default function AddEditItemScreen(): React.ReactNode {case FieldTypes.TextArea:return (- <FormField+ <ResizableTextAreavalue={stringValue}onChangeText={(val) => handleFieldChange(fieldKey, val)}label={label}- multiline- numberOfLines={4}- textAlignVertical="top"onRemove={onRemove}testID={testID}/>diff --git a/apps/mobile-app/components/form/DraggableCustomFieldsList.tsx b/apps/mobile-app/components/form/DraggableCustomFieldsList.tsxindex ff1679252..2b49c5dfd 100644--- a/apps/mobile-app/components/form/DraggableCustomFieldsList.tsx+++ b/apps/mobile-app/components/form/DraggableCustomFieldsList.tsx@@ -15,6 +15,7 @@ import { AdvancedPasswordField } from './AdvancedPasswordField';import { EditableFieldLabel } from './EditableFieldLabel';import { FormField } from './FormField';import { HiddenField } from './HiddenField';+import { ResizableTextArea } from './ResizableTextArea';/*** Custom field definition type@@ -55,13 +56,10 @@ const CustomFieldItem: React.FC<CustomFieldItemProps> = ({const renderFieldInput = (): React.ReactNode => {if (field.fieldType === FieldTypes.TextArea) {return (- <FormField+ <ResizableTextAreavalue={value}onChangeText={onValueChange}label=""- multiline- numberOfLines={4}- textAlignVertical="top"/>);}diff --git a/apps/mobile-app/components/form/ResizableTextArea.tsx b/apps/mobile-app/components/form/ResizableTextArea.tsxnew file mode 100644index 000000000..f80faca2d--- /dev/null+++ b/apps/mobile-app/components/form/ResizableTextArea.tsx@@ -0,0 +1,217 @@+import { MaterialIcons } from '@expo/vector-icons';+import * as Haptics from 'expo-haptics';+import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react';+import { useTranslation } from 'react-i18next';+import { Animated, LayoutChangeEvent, PanResponder, StyleSheet, TextInput, TextInputProps, TouchableHighlight, View, useWindowDimensions } from 'react-native';++import { HapticsUtility } from '@/utils/HapticsUtility';++import { useColors } from '@/hooks/useColorScheme';++import { ThemedText } from '@/components/themed/ThemedText';++/** Minimum height in pixels. */+const MIN_HEIGHT = 100;++/** Max height as a fraction of the window height. */+const MAX_HEIGHT_RATIO = 0.6;++/** Height change per accessibility increment/decrement action. */+const ACCESSIBILITY_STEP = 40;++type ResizableTextAreaProps = Omit<TextInputProps, 'onChangeText' | 'multiline' | 'numberOfLines'> & {+ label: string;+ value: string;+ onChangeText: (value: string) => void;+ onRemove?: () => void;+ testID?: string;+}++/**+ * Multi-line form field with a drag handle at the bottom to resize it.+ *+ * Until the handle is dragged the field grows with its content (between the minimum+ * and maximum height). After a drag it keeps the height the user dragged it to.+ */+export const ResizableTextArea: React.FC<ResizableTextAreaProps> = ({+ label,+ value,+ onChangeText,+ onRemove,+ testID,+ ...props+}): React.ReactNode => {+ const colors = useColors();+ const { t } = useTranslation();+ const { height: windowHeight } = useWindowDimensions();++ const maxHeight = Math.max(MIN_HEIGHT * 2, Math.round(windowHeight * MAX_HEIGHT_RATIO));+ const maxHeightRef = useRef(maxHeight);++ // Whether the user dragged the handle; until then the field sizes itself to its content.+ const [isResized, setIsResized] = useState(false);+ const heightAnim = useRef(new Animated.Value(MIN_HEIGHT)).current;+ const renderedHeight = useRef(MIN_HEIGHT);+ const dragStartHeight = useRef(MIN_HEIGHT);+ const hasDragged = useRef(false);++ /**+ * Clamp a height between the minimum and the current maximum.+ */+ const clampHeight = useCallback((height: number): number => {+ return Math.min(Math.max(height, MIN_HEIGHT), maxHeightRef.current);+ }, []);++ /**+ * Apply a height to the animated value and remember it as the rendered height.+ */+ const applyHeight = useCallback((height: number): void => {+ renderedHeight.current = height;+ heightAnim.setValue(height);+ }, [heightAnim]);++ // Keep the height within bounds when the window size changes (e.g. rotation).+ useEffect(() => {+ maxHeightRef.current = maxHeight;++ if (isResized && renderedHeight.current > maxHeight) {+ applyHeight(maxHeight);+ }+ }, [maxHeight, isResized, applyHeight]);++ const panResponder = useMemo(() => PanResponder.create({+ onStartShouldSetPanResponder: (): boolean => true,+ onMoveShouldSetPanResponder: (): boolean => true,+ onPanResponderTerminationRequest: (): boolean => false,+ onPanResponderGrant: (): void => {+ hasDragged.current = false;+ dragStartHeight.current = renderedHeight.current;+ heightAnim.setValue(renderedHeight.current);+ HapticsUtility.impact(Haptics.ImpactFeedbackStyle.Light);+ },+ // Resize the field along with the finger, ignoring the jitter of a plain tap.+ onPanResponderMove: (_event, gestureState): void => {+ if (!hasDragged.current && Math.abs(gestureState.dy) < 2) {+ return;+ }++ applyHeight(clampHeight(dragStartHeight.current + gestureState.dy));++ // Switch from auto-growing to the fixed height that follows the finger.+ if (!hasDragged.current) {+ hasDragged.current = true;+ setIsResized(true);+ }+ },+ }), [applyHeight, clampHeight, heightAnim]);++ /**+ * Resize in fixed steps, used by the accessibility increment/decrement actions.+ */+ const stepHeight = useCallback((delta: number): void => {+ applyHeight(clampHeight(renderedHeight.current + delta));+ setIsResized(true);+ }, [applyHeight, clampHeight]);++ /**+ * Track the height the field renders at, so a drag starts from the size the+ * user actually sees while the field is still auto-growing.+ */+ const handleLayout = useCallback((event: LayoutChangeEvent): void => {+ renderedHeight.current = event.nativeEvent.layout.height;+ }, []);++ const styles = StyleSheet.create({+ handle: {+ alignItems: 'center',+ borderTopColor: colors.accentBorder,+ borderTopWidth: 1,+ justifyContent: 'center',+ paddingVertical: 3,+ },+ input: {+ color: colors.text,+ fontSize: 16,+ padding: 10,+ textAlignVertical: 'top',+ },+ inputContainer: {+ backgroundColor: colors.background,+ borderColor: colors.accentBorder,+ borderRadius: 6,+ borderWidth: 1,+ },+ inputFilling: {+ flex: 1,+ },+ inputGroup: {+ marginBottom: 6,+ },+ inputLabel: {+ color: colors.textMuted,+ fontSize: 12,+ },+ labelContainer: {+ alignItems: 'center',+ flexDirection: 'row',+ justifyContent: 'space-between',+ marginBottom: 4,+ },+ removeButton: {+ padding: 4,+ },+ });++ const showLabelContainer = label || onRemove;++ return (+ <View style={styles.inputGroup}>+ {showLabelContainer && (+ <View style={styles.labelContainer}>+ <ThemedText style={styles.inputLabel}>{label}</ThemedText>+ {onRemove && (+ <TouchableHighlight+ style={styles.removeButton}+ onPress={onRemove}+ underlayColor={colors.accentBackground}+ >+ <MaterialIcons name="close" size={18} color={colors.textMuted} />+ </TouchableHighlight>+ )}+ </View>+ )}+ <View style={styles.inputContainer}>+ <Animated.View+ style={isResized ? { height: heightAnim } : undefined}+ onLayout={handleLayout}+ >+ <TextInput+ style={isResized ? [styles.input, styles.inputFilling] : [styles.input, { minHeight: MIN_HEIGHT, maxHeight }]}+ value={value}+ onChangeText={onChangeText}+ placeholderTextColor={colors.textMuted}+ autoCapitalize="none"+ autoComplete="off"+ autoCorrect={false}+ multiline+ testID={testID}+ accessibilityLabel={testID}+ {...props}+ />+ </Animated.View>+ <View+ style={styles.handle}+ hitSlop={{ bottom: 12, left: 24, right: 24, top: 8 }}+ accessibilityRole="adjustable"+ accessibilityLabel={t('common.resizeField')}+ accessibilityActions={[{ name: 'increment' }, { name: 'decrement' }]}+ onAccessibilityAction={(event) => stepHeight(event.nativeEvent.actionName === 'increment' ? ACCESSIBILITY_STEP : -ACCESSIBILITY_STEP)}+ testID={testID ? `${testID}-resize-handle` : undefined}+ {...panResponder.panHandlers}+ >+ <MaterialIcons name="drag-handle" size={18} color={colors.textMuted} />+ </View>+ </View>+ </View>+ );+};diff --git a/apps/mobile-app/i18n/locales/en.json b/apps/mobile-app/i18n/locales/en.jsonindex fdc7f8c6a..3d760ec71 100644--- a/apps/mobile-app/i18n/locales/en.json+++ b/apps/mobile-app/i18n/locales/en.json@@ -29,6 +29,7 @@"attachments": "Attachments","or": "or","retry": "Retry",+ "resizeField": "Drag to resize field","deleteItemConfirmTitle": "Delete Item","deleteItemConfirmDescription": "Are you sure you want to delete this item?","errors": {