diff --git a/app/components/Sidebar/components/CollectionLink.tsx b/app/components/Sidebar/components/CollectionLink.tsx index f4642d7b0d..d476f1106d 100644 --- a/app/components/Sidebar/components/CollectionLink.tsx +++ b/app/components/Sidebar/components/CollectionLink.tsx @@ -13,6 +13,7 @@ import DocumentReparent from "~/scenes/DocumentReparent"; import CollectionIcon from "~/components/CollectionIcon"; import Modal from "~/components/Modal"; import useBoolean from "~/hooks/useBoolean"; +import usePrevious from "~/hooks/usePrevious"; import useStores from "~/hooks/useStores"; import CollectionMenu from "~/menus/CollectionMenu"; import CollectionSortMenu from "~/menus/CollectionSortMenu"; @@ -71,8 +72,9 @@ function CollectionLink({ const [expanded, setExpanded] = React.useState( collection.id === ui.activeCollectionId ); - const [expandedViaDisclosure, setExpandedViaDisclosure] = React.useState( - false + + const prevActiveCollectionId = usePrevious( + ui.activeCollectionId ); const manualSort = collection.sort.field === "index"; @@ -209,13 +211,20 @@ function CollectionLink({ return; } - if (isDraggingAnyCollection) { + const wasUIActiveCollection = + collection.id === prevActiveCollectionId && + collection.id !== ui.activeCollectionId; + + if (isDraggingAnyCollection || wasUIActiveCollection) { setExpanded(false); - setExpandedViaDisclosure(false); - } else { - setExpanded(collection.id === ui.activeCollectionId); } - }, [isDraggingAnyCollection, collection.id, ui.activeCollectionId, search]); + }, [ + isDraggingAnyCollection, + collection.id, + ui.activeCollectionId, + search, + prevActiveCollectionId, + ]); return ( <> @@ -237,10 +246,10 @@ function CollectionLink({ icon={ <> { event.preventDefault(); - setExpandedViaDisclosure((prev) => !prev); + setExpanded((prev) => !prev); }} /> @@ -290,12 +299,10 @@ function CollectionLink({ )} {collectionDocuments.map((node, index) => ( {nodeChildren.map((childNode, index) => ( ` + max-height: ${(props) => props.$maxHeight}; + transition: ${(props) => + props.$expanded ? "max-height 300ms ease-in" : "max-height 400ms ease-out"}; overflow: hidden; `;