// // A button with a long-press action. Long-pressing the button displays a tooltip // import * as React from 'react'; import { themeStyle } from '@joplin/lib/theme'; import { Theme } from '@joplin/lib/themes/type'; import { useState, useMemo, useCallback, useRef } from 'react'; import { Text, Pressable, ViewStyle, StyleSheet, LayoutChangeEvent, LayoutRectangle, Animated, AccessibilityState, AccessibilityRole, TextStyle, GestureResponderEvent, Platform } from 'react-native'; import { Menu, MenuOptions, MenuTrigger, renderers } from 'react-native-popup-menu'; import Icon from './Icon'; import AccessibleView from './accessibility/AccessibleView'; type ButtonClickListener = ()=> void; interface ButtonProps { onPress: ButtonClickListener; // Accessibility label and text shown in a tooltip description: string; iconName: string; iconStyle: TextStyle; themeId: number; // (web only) On web, touching buttons can cause the on-screen keyboard to be dismissed. // Setting preventKeyboardDismiss overrides this behavior. preventKeyboardDismiss?: boolean; containerStyle?: ViewStyle; contentWrapperStyle?: ViewStyle; // Additional accessibility information. See View.accessibilityHint accessibilityHint?: string; // Role of the button. Defaults to 'button'. accessibilityRole?: AccessibilityRole; accessibilityState?: AccessibilityState; disabled?: boolean; } const IconButton = (props: ButtonProps) => { const [tooltipVisible, setTooltipVisible] = useState(false); const [buttonLayout, setButtonLayout] = useState(null); const tooltipStyles = useTooltipStyles(props.themeId); // See https://blog.logrocket.com/react-native-touchable-vs-pressable-components/ // for more about animating Pressable buttons. const fadeAnim = useRef(new Animated.Value(1)).current; const animationDuration = 100; // ms const onPressIn = useCallback(() => { // Fade out. Animated.timing(fadeAnim, { toValue: 0.5, duration: animationDuration, useNativeDriver: true, }).start(); }, [fadeAnim]); const onPressOut = useCallback(() => { // Fade in. Animated.timing(fadeAnim, { toValue: 1, duration: animationDuration, useNativeDriver: true, }).start(); setTooltipVisible(false); }, [fadeAnim]); const onLongPress = useCallback(() => { setTooltipVisible(true); }, []); const onButtonLayout = useCallback((event: LayoutChangeEvent) => { const layoutEvt = event.nativeEvent.layout; // Copy the layout event setButtonLayout({ ...layoutEvt }); }, []); const { onTouchStart, onTouchMove, onTouchEnd } = usePreventKeyboardDismissTouchListeners( props.preventKeyboardDismiss, props.onPress, props.disabled, ); const button = ( ); const renderTooltip = () => { if (!props.description) return null; return ( {props.description} ); }; return ( <> {renderTooltip()} {button} ); }; const useTooltipStyles = (themeId: number) => { return useMemo(() => { const themeData: Theme = themeStyle(themeId); return StyleSheet.create({ text: { color: themeData.raisedColor, padding: 4, }, anchor: { backgroundColor: themeData.raisedBackgroundColor, }, optionsContainer: { backgroundColor: themeData.raisedBackgroundColor, }, }); }, [themeId]); }; // On web, by default, pressing buttons defocuses the active edit control, dismissing the // virtual keyboard. This hook creates listeners that optionally prevent the keyboard from dismissing. const usePreventKeyboardDismissTouchListeners = (preventKeyboardDismiss: boolean, onPress: ()=> void, disabled: boolean) => { const touchStartPointRef = useRef<[number, number]>(); const isTapRef = useRef(); const onTouchStart = useCallback((event: GestureResponderEvent) => { if (Platform.OS === 'web' && preventKeyboardDismiss) { const touch = event.nativeEvent.touches[0]; touchStartPointRef.current = [touch?.pageX, touch?.pageY]; isTapRef.current = true; } }, [preventKeyboardDismiss]); const onTouchMove = useCallback((event: GestureResponderEvent) => { if (Platform.OS === 'web' && preventKeyboardDismiss && isTapRef.current) { // Update isTapRef onTouchMove, rather than onTouchEnd -- the final // touch position is unavailable in onTouchEnd on some devices. const touch = event.nativeEvent.touches[0]; const dx = touch?.pageX - touchStartPointRef.current[0]; const dy = touch?.pageY - touchStartPointRef.current[1]; isTapRef.current = Math.hypot(dx, dy) < 15; } }, [preventKeyboardDismiss]); const onTouchEnd = useCallback((event: GestureResponderEvent) => { if (Platform.OS === 'web' && preventKeyboardDismiss) { if (isTapRef.current && !disabled) { event.preventDefault(); onPress(); } } }, [onPress, disabled, preventKeyboardDismiss]); return { onTouchStart, onTouchMove, onTouchEnd }; }; export default IconButton;