Skip to content

Commit

Permalink
chore: nextFrame
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <i@innei.in>
  • Loading branch information
Innei committed Jun 22, 2024
1 parent 1c79755 commit 800706a
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
18 changes: 8 additions & 10 deletions src/renderer/src/components/ui/auto-completion/AutoCompletion.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useRefValue } from "@renderer/hooks"
import { stopPropagation } from "@renderer/lib/dom"
import { nextFrame, stopPropagation } from "@renderer/lib/dom"
import { cn } from "@renderer/lib/utils"
import clsx from "clsx"
import { AnimatePresence } from "framer-motion"
Expand Down Expand Up @@ -83,11 +83,11 @@ export const Autocomplete = forwardRef<HTMLInputElement, AutocompleteProps>(
useEffect(() => {
const $input = inputRef.current
if (!$input) return
if (document.activeElement !== $input) { return }
if (document.activeElement !== $input) {
return
}

setIsOpen(
filterableSuggestions.length > 0,
)
setIsOpen(filterableSuggestions.length > 0)
}, [filterableSuggestions])

const [isOpen, setIsOpen] = useState(false)
Expand Down Expand Up @@ -145,11 +145,9 @@ export const Autocomplete = forwardRef<HTMLInputElement, AutocompleteProps>(
onSuggestionSelected(filterableSuggestions[currentActiveIndex])
setInputValue(filterableSuggestions[currentActiveIndex].name)

requestAnimationFrame((() => {
requestAnimationFrame(() => {
setIsOpen(false)
})
}))
nextFrame(() => {
setIsOpen(false)
})
}
}

Expand Down
8 changes: 2 additions & 6 deletions src/renderer/src/components/user-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useSignOut } from "@renderer/hooks"
import { loginHandler } from "@renderer/lib/auth"
import { tipcClient } from "@renderer/lib/client"
import { APP_NAME } from "@renderer/lib/constants"
import { stopPropagation } from "@renderer/lib/dom"
import { nextFrame, stopPropagation } from "@renderer/lib/dom"
import { cn } from "@renderer/lib/utils"
import { useSettingModal } from "@renderer/modules/settings/modal/hooks"
import { useSession } from "@renderer/queries/auth"
Expand Down Expand Up @@ -140,11 +140,7 @@ export const ProfileButton: FC<LoginProps> = memo((props) => {
// Here we need to delay one frame, so it's two raf,
// in order to have `point-event: none` recorded by RadixOverlay after modal is invoked in a certain scenario,
// and the page freezes after modal is turned off.
requestAnimationFrame(() => {
requestAnimationFrame(() => {
settingModalPresent()
})
})
nextFrame(settingModalPresent)
}
}}
>
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/src/lib/dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ export const stopPropagation: ReactEventHandler<any> = (e) =>
e.stopPropagation()

export const preventDefault: ReactEventHandler<any> = (e) => e.preventDefault()

export const nextFrame = (fn: (...args: any[]) => any) => {
requestAnimationFrame(() => {
requestAnimationFrame(() => {
fn()
})
})
}
7 changes: 3 additions & 4 deletions src/renderer/src/modules/feed-column/item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useRouteParamsSelector } from "@renderer/hooks/biz/useRouteParams"
import { apiClient } from "@renderer/lib/api-fetch"
import { levels } from "@renderer/lib/constants"
import dayjs from "@renderer/lib/dayjs"
import { nextFrame } from "@renderer/lib/dom"
import { showNativeMenu } from "@renderer/lib/native-menu"
import { cn } from "@renderer/lib/utils"
import { Queries } from "@renderer/queries"
Expand Down Expand Up @@ -49,10 +50,8 @@ const FeedItemImpl = ({
category: null,
})
// focus to main container in order to let keyboard can navigate entry items by arrow keys
requestAnimationFrame(() => {
requestAnimationFrame(() => {
getMainContainerElement()?.focus()
})
nextFrame(() => {
getMainContainerElement()?.focus()
})
},
[subscription.feedId, navigate, view],
Expand Down
7 changes: 3 additions & 4 deletions src/renderer/src/providers/context-menu-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ContextMenuSeparator,
ContextMenuTrigger,
} from "@renderer/components/ui/context-menu"
import { nextFrame } from "@renderer/lib/dom"
import type { NativeMenuItem } from "@renderer/lib/native-menu"
import { CONTEXT_MENU_SHOW_EVENT_KEY } from "@renderer/lib/native-menu"
import type { ReactNode } from "react"
Expand Down Expand Up @@ -59,10 +60,8 @@ const Handler = () => {
// Here we need to delay one frame,
// so it's two raf's, in order to have `point-event: none` recorded by RadixOverlay after modal is invoked in a certain scenario,
// and the page freezes after modal is turned off.
requestAnimationFrame(() => {
requestAnimationFrame(() => {
item.click?.()
})
nextFrame(() => {
item.click?.()
})
}}
>
Expand Down

0 comments on commit 800706a

Please sign in to comment.