Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close popover when dragging over #31154

Merged
merged 26 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9614668
fix: close popover when dragging over
ishpaul777 Nov 9, 2023
a1fb060
early return when platform is not web or desktop
ishpaul777 Nov 9, 2023
b9b011e
fix lint
ishpaul777 Nov 9, 2023
b2eb920
only call close if popover is open
ishpaul777 Nov 9, 2023
6bdd24e
Merge branch 'Expensify:main' into fix/close-popover-on-drag
ishpaul777 Nov 12, 2023
5be0aa1
final changes in RHP
ishpaul777 Nov 12, 2023
ca5e94a
fix linter
ishpaul777 Nov 12, 2023
09427f1
cleaning up
ishpaul777 Nov 12, 2023
5d19455
fixed crash on ios app
ishpaul777 Nov 12, 2023
9fbd0cf
added requested changes
ishpaul777 Nov 13, 2023
f9be717
clean up code
ishpaul777 Nov 15, 2023
2e88c1b
Merge branch 'main' into fix/close-popover-on-drag
ishpaul777 Nov 20, 2023
466f1d8
fix native drag and drop provider
ishpaul777 Nov 20, 2023
df59165
fix prettier diffs
ishpaul777 Nov 20, 2023
18dc027
added requested changes
ishpaul777 Nov 20, 2023
133204c
remove unrelated change
ishpaul777 Nov 20, 2023
157312a
fix prettier diffs
ishpaul777 Nov 20, 2023
0a4016f
Merge branch 'Expensify:main' into fix/close-popover-on-drag
ishpaul777 Nov 28, 2023
8cc04fd
fix typescript error
ishpaul777 Nov 28, 2023
8614c45
Merge branch 'fix/close-popover-on-drag' of https://github.com/ishpau…
ishpaul777 Nov 28, 2023
142212f
added requested changes
ishpaul777 Nov 29, 2023
b312223
fix prettier diffs
ishpaul777 Nov 29, 2023
4795390
add requested changes
ishpaul777 Dec 1, 2023
20da9bb
added requested changes
ishpaul777 Dec 1, 2023
88bb658
fix early return for usefffect in suggestions
ishpaul777 Dec 1, 2023
4a8478e
Merge branch 'Expensify:main' into fix/close-popover-on-drag
ishpaul777 Dec 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/components/DragAndDrop/Provider/index.native.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';
import type {DragAndDropContextParams, DragAndDropProviderProps} from './types';

const DragAndDropContext: DragAndDropContextParams = {};
const DragAndDropContext = React.createContext<DragAndDropContextParams>({});

function DragAndDropProvider({children}: DragAndDropProviderProps) {
return children;
Expand Down
22 changes: 15 additions & 7 deletions src/hooks/useDragAndDrop.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {useIsFocused} from '@react-navigation/native';
import React, {useCallback, useEffect, useRef, useState} from 'react';
import React, {useCallback, useContext, useEffect, useRef, useState} from 'react';
import {View} from 'react-native';
import {PopoverContext} from '@components/PopoverProvider';

const COPY_DROP_EFFECT = 'copy';
const NONE_DROP_EFFECT = 'none';
Expand All @@ -27,6 +28,7 @@ type DragAndDropOptions = {
export default function useDragAndDrop({dropZone, onDrop = () => {}, shouldAllowDrop = true, isDisabled = false, shouldAcceptDrop = () => true}: DragAndDropParams): DragAndDropOptions {
const isFocused = useIsFocused();
const [isDraggingOver, setIsDraggingOver] = useState(false);
const {close: closePopover} = useContext(PopoverContext);

// This solution is borrowed from this SO: https://stackoverflow.com/questions/7110353/html5-dragleave-fired-when-hovering-a-child-element
// This is necessary because dragging over children will cause dragleave to execute on the parent.
Expand All @@ -43,9 +45,15 @@ export default function useDragAndDrop({dropZone, onDrop = () => {}, shouldAllow
setIsDraggingOver(false);
}, [isFocused, isDisabled]);

const setDropEffect = useCallback(
const handleDragEvent = useCallback(
(event: DragEvent) => {
const effect = shouldAllowDrop && shouldAcceptDrop(event) ? COPY_DROP_EFFECT : NONE_DROP_EFFECT;
const shouldAcceptThisDrop = shouldAllowDrop && shouldAcceptDrop(event);

if (shouldAcceptThisDrop && event.type === DRAG_ENTER_EVENT) {
closePopover();
}

const effect = shouldAcceptThisDrop ? COPY_DROP_EFFECT : NONE_DROP_EFFECT;

if (event.dataTransfer) {
// eslint-disable-next-line no-param-reassign
Expand All @@ -54,7 +62,7 @@ export default function useDragAndDrop({dropZone, onDrop = () => {}, shouldAllow
event.dataTransfer.effectAllowed = effect;
}
},
[shouldAllowDrop, shouldAcceptDrop],
[shouldAllowDrop, shouldAcceptDrop, closePopover],
);

/**
Expand All @@ -71,11 +79,11 @@ export default function useDragAndDrop({dropZone, onDrop = () => {}, shouldAllow

switch (event.type) {
case DRAG_OVER_EVENT:
setDropEffect(event);
handleDragEvent(event);
break;
case DRAG_ENTER_EVENT:
dragCounter.current++;
setDropEffect(event);
handleDragEvent(event);
if (isDraggingOver) {
return;
}
Expand All @@ -98,7 +106,7 @@ export default function useDragAndDrop({dropZone, onDrop = () => {}, shouldAllow
break;
}
},
[isFocused, isDisabled, shouldAcceptDrop, setDropEffect, isDraggingOver, onDrop],
[isFocused, isDisabled, shouldAcceptDrop, isDraggingOver, onDrop, handleDragEvent],
);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function SuggestionEmoji({
}) {
const [suggestionValues, setSuggestionValues] = useState(defaultSuggestionsValues);

const isEmojiSuggestionsMenuVisible = !_.isEmpty(suggestionValues.suggestedEmojis) && suggestionValues.shouldShowEmojiSuggestionMenu;
const isEmojiSuggestionsMenuVisible = !_.isEmpty(suggestionValues.suggestedEmojis) && suggestionValues.shouldShowSuggestionMenu;

const [highlightedEmojiIndex, setHighlightedEmojiIndex] = useArrowKeyFocusManager({
isActive: isEmojiSuggestionsMenuVisible,
Expand Down Expand Up @@ -166,13 +166,13 @@ function SuggestionEmoji({
const nextState = {
suggestedEmojis: [],
colonIndex,
shouldShowEmojiSuggestionMenu: false,
shouldShowSuggestionMenu: false,
};
const newSuggestedEmojis = EmojiUtils.suggestEmojis(leftString, preferredLocale);

if (newSuggestedEmojis.length && isCurrentlyShowingEmojiSuggestion) {
nextState.suggestedEmojis = newSuggestedEmojis;
nextState.shouldShowEmojiSuggestionMenu = !_.isEmpty(newSuggestedEmojis);
nextState.shouldShowSuggestionMenu = !_.isEmpty(newSuggestedEmojis);
}

setSuggestionValues((prevState) => ({...prevState, ...nextState}));
Expand Down
13 changes: 12 additions & 1 deletion src/pages/home/report/ReportActionCompose/Suggestions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import PropTypes from 'prop-types';
import React, {useCallback, useImperativeHandle, useRef} from 'react';
import React, {useCallback, useContext, useEffect, useImperativeHandle, useRef} from 'react';
import {View} from 'react-native';
import {DragAndDropContext} from '@components/DragAndDrop/Provider';
import usePrevious from '@hooks/usePrevious';
import SuggestionEmoji from './SuggestionEmoji';
import SuggestionMention from './SuggestionMention';
import * as SuggestionProps from './suggestionProps';
Expand Down Expand Up @@ -45,6 +47,8 @@ function Suggestions({
}) {
const suggestionEmojiRef = useRef(null);
const suggestionMentionRef = useRef(null);
const {isDraggingOver} = useContext(DragAndDropContext);
const prevIsDraggingOver = usePrevious(isDraggingOver);

const getSuggestions = useCallback(() => {
if (suggestionEmojiRef.current && suggestionEmojiRef.current.getSuggestions) {
Expand Down Expand Up @@ -111,6 +115,13 @@ function Suggestions({
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions],
);

useEffect(() => {
if (!(!prevIsDraggingOver && isDraggingOver)) {
return;
}
updateShouldShowSuggestionMenuToFalse();
}, [isDraggingOver, prevIsDraggingOver, updateShouldShowSuggestionMenuToFalse]);

const baseProps = {
value,
setValue,
Expand Down
Loading