Skip to content

Commit

Permalink
Close realm on map click (#864)
Browse files Browse the repository at this point in the history
Resolves #859 

## What has been done
User can now close the realm panel by clicking on the map / outside of
panel

## Testing
- [x] Open and close panel by clicking on "Close button"
- [x] Open and close panel by clicking on map
- [x] Open and close panel by clicking above it
- [x] Open and close panel by clicking below it
- [x] Clicking on panel itself does not trigger realm panel close
  • Loading branch information
jagodarybacka authored Dec 8, 2023
2 parents 07bad03 + 694d0d3 commit f499101
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
7 changes: 6 additions & 1 deletion src/shared/components/Dialogs/Panel.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { ReactNode, CSSProperties } from "react"
import classNames from "classnames"
import { animated } from "@react-spring/web"
import { useRealmPanelTransition } from "shared/hooks"
import { usePanelRealmClose, useRealmPanelTransition } from "shared/hooks"
import Portal from "../Interface/Portal"

type PortalSectionProps = {
Expand Down Expand Up @@ -36,12 +36,17 @@ function Container({
style,
}: PanelContainerProps) {
const containerTransitionStyles = useRealmPanelTransition(position)
const closePanel = usePanelRealmClose()

return (
<Portal>
<animated.div
style={{ ...style, ...containerTransitionStyles }}
className="no_scrollbar"
onClick={(e) => {
if (e.target !== e.currentTarget) return
closePanel()
}}
>
<div
className={classNames("panel column", {
Expand Down
27 changes: 25 additions & 2 deletions src/shared/hooks/realm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { easings, useSpring } from "@react-spring/web"
import { useMemo } from "react"
import { selectRealmPanelVisible, useDappSelector } from "redux-state"
import { useCallback, useMemo } from "react"
import {
selectRealmPanelVisible,
setDisplayedRealmId,
setRealmPanelVisible,
useDappDispatch,
useDappSelector,
} from "redux-state"
import { REALM_PANEL_ANIMATION_TIME } from "shared/constants"
import { useTabletScreen } from "./helpers"

Expand Down Expand Up @@ -71,3 +77,20 @@ export function useRealmCloseButtonTransition() {

return buttonTransition
}

export function usePanelRealmClose() {
const dispatch = useDappDispatch()

const handlePanelClose = useCallback(() => {
dispatch(setRealmPanelVisible(false))

const timeout = setTimeout(
() => dispatch(setDisplayedRealmId(null)),
REALM_PANEL_ANIMATION_TIME
)

return () => clearTimeout(timeout)
}, [dispatch])

return handlePanelClose
}
25 changes: 14 additions & 11 deletions src/ui/Island/RealmPanel/RealmPanelCloseButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import closeIcon from "shared/assets/icons/s/close-black.svg"
import { animated } from "@react-spring/web"
import { useRealmCloseButtonTransition } from "shared/hooks"
import Button from "../../../shared/components/Interface/Button"
import Portal from "shared/components/Interface/Portal"

export default function RealmPanelCloseButton({
onClose,
Expand All @@ -12,16 +13,18 @@ export default function RealmPanelCloseButton({
const buttonTransition = useRealmCloseButtonTransition()

return (
<animated.div style={{ ...buttonTransition, position: "absolute" }}>
<Button
size="medium"
type="close"
iconSrc={closeIcon}
iconPosition="left"
onClick={onClose}
>
Close view
</Button>
</animated.div>
<Portal>
<animated.div style={{ ...buttonTransition, position: "absolute" }}>
<Button
size="medium"
type="close"
iconSrc={closeIcon}
iconPosition="left"
onClick={onClose}
>
Close view
</Button>
</animated.div>
</Portal>
)
}
7 changes: 7 additions & 0 deletions src/ui/Island/RealmPanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import React, { useEffect } from "react"
import {
useAssistant,
useLocalStorageChange,
usePanelRealmClose,
useTabletScreen,
} from "shared/hooks"
import { LOCAL_STORAGE_VISITED_REALM } from "shared/constants"
import { selectRealmPanelVisible, useDappSelector } from "redux-state"
import ClickableModalOverlay from "shared/components/Dialogs/ClickableModalOverlay"
import RealmDetailsPanel from "./RealmDetailsPanel"
import RealmLeaderboardPanel from "./RealmLeaderboardPanel"
import RealmPanelCountdown from "./RealmPanelCountdown"
Expand All @@ -18,6 +21,9 @@ export default function RealmPanel({ onClose }: { onClose: () => void }) {
LOCAL_STORAGE_VISITED_REALM
)

const realmPanelVisible = useDappSelector(selectRealmPanelVisible)
const handlePanelClose = usePanelRealmClose()

useEffect(() => {
if (value) return
updateStorage(true)
Expand All @@ -37,6 +43,7 @@ export default function RealmPanel({ onClose }: { onClose: () => void }) {
</Panel.Container>
)}
<RealmPanelCountdown />
{realmPanelVisible && <ClickableModalOverlay close={handlePanelClose} />}
</>
)
}
17 changes: 4 additions & 13 deletions src/ui/Island/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { memo, useCallback, useEffect } from "react"
import React, { memo, useEffect } from "react"
import backgroundImg from "public/dapp_island_bg.webp"
import {
useValueRef,
IslandContext,
useAssistant,
useAssets,
usePanelRealmClose,
} from "shared/hooks"
import {
selectDisplayedRealmId,
Expand All @@ -17,7 +18,6 @@ import {
import FullPageLoader from "shared/components/Loaders/FullPageLoader"
import { usePostHog } from "posthog-js/react"
import RealmPanel from "ui/Island/RealmPanel"
import { REALM_PANEL_ANIMATION_TIME } from "shared/constants"
import InteractiveIsland from "./Background/InteractiveIsland"
import IslandPresence from "./Reflect/IslandPresence"

Expand Down Expand Up @@ -52,16 +52,7 @@ function IslandWrapper() {
},
}))

const handleClose = useCallback(() => {
dispatch(setRealmPanelVisible(false))

const timeout = setTimeout(
() => dispatch(setDisplayedRealmId(null)),
REALM_PANEL_ANIMATION_TIME
)

return () => clearTimeout(timeout)
}, [dispatch])
const handlePanelClose = usePanelRealmClose()

return (
<>
Expand All @@ -80,7 +71,7 @@ function IslandWrapper() {
<IslandContext.Provider value={contextRef}>
<InteractiveIsland />
{process.env.DISABLE_REFLECT === "true" ? null : <IslandPresence />}
<RealmPanel onClose={handleClose} />
<RealmPanel onClose={handlePanelClose} />
</IslandContext.Provider>
</div>
</>
Expand Down

0 comments on commit f499101

Please sign in to comment.