feat: add modal dismiss setter (#144)
Signed-off-by: Innei <i@innei.in>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { FC, RefObject } from "react"
|
||||
import { createContext } from "react"
|
||||
|
||||
export type CurrentModalContentProps = ModalContentPropsInternal & {
|
||||
export type CurrentModalContentProps = ModalActionsInternal & {
|
||||
ref: RefObject<HTMLElement | null>
|
||||
}
|
||||
|
||||
@@ -9,7 +9,8 @@ export const CurrentModalContext = createContext<CurrentModalContentProps>(
|
||||
null as any,
|
||||
)
|
||||
|
||||
export type ModalContentComponent<T> = FC<ModalContentPropsInternal & T>
|
||||
export type ModalContentPropsInternal = {
|
||||
export type ModalContentComponent<T> = FC<ModalActionsInternal & T>
|
||||
export type ModalActionsInternal = {
|
||||
dismiss: () => void
|
||||
setClickOutSideToDismiss: (value: boolean) => void
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import { ErrorComponentType } from "@renderer/components/errors"
|
||||
import { nextFrame, stopPropagation } from "@renderer/lib/dom"
|
||||
import { cn } from "@renderer/lib/utils"
|
||||
import { useAnimationControls, useDragControls } from "framer-motion"
|
||||
import { produce } from "immer"
|
||||
import { useSetAtom } from "jotai"
|
||||
import type { PointerEventHandler, SyntheticEvent } from "react"
|
||||
import {
|
||||
@@ -24,10 +25,7 @@ import { useEventCallback } from "usehooks-ts"
|
||||
import { Divider } from "../../divider"
|
||||
import { modalStackAtom } from "./atom"
|
||||
import { MODAL_STACK_Z_INDEX, modalMontionConfig } from "./constants"
|
||||
import type {
|
||||
CurrentModalContentProps,
|
||||
ModalContentPropsInternal,
|
||||
} from "./context"
|
||||
import type { CurrentModalContentProps, ModalActionsInternal } from "./context"
|
||||
import { CurrentModalContext } from "./context"
|
||||
import type { ModalProps } from "./types"
|
||||
|
||||
@@ -42,11 +40,27 @@ export const ModalInternal: Component<{
|
||||
{ item, index, onClose: onPropsClose, children, isTop },
|
||||
ref: any,
|
||||
) {
|
||||
const {
|
||||
CustomModalComponent,
|
||||
modalClassName,
|
||||
content,
|
||||
title,
|
||||
clickOutsideToDismiss,
|
||||
modalContainerClassName,
|
||||
wrapper: Wrapper = Fragment,
|
||||
max,
|
||||
icon,
|
||||
canClose = true,
|
||||
|
||||
draggable = false,
|
||||
} = item
|
||||
|
||||
const setStack = useSetAtom(modalStackAtom)
|
||||
|
||||
const [currentIsClosing, setCurrentIsClosing] = useState(false)
|
||||
|
||||
const close = useEventCallback(() => {
|
||||
const close = useEventCallback((forceClose = false) => {
|
||||
if (!canClose && !forceClose) return
|
||||
setCurrentIsClosing(true)
|
||||
nextFrame(() => {
|
||||
setStack((p) => p.filter((modal) => modal.id !== item.id))
|
||||
@@ -65,19 +79,6 @@ export const ModalInternal: Component<{
|
||||
|
||||
const opaque = useUISettingKey("modalOpaque")
|
||||
|
||||
const {
|
||||
CustomModalComponent,
|
||||
modalClassName,
|
||||
content,
|
||||
title,
|
||||
clickOutsideToDismiss,
|
||||
modalContainerClassName,
|
||||
wrapper: Wrapper = Fragment,
|
||||
max,
|
||||
icon,
|
||||
|
||||
draggable = false,
|
||||
} = item
|
||||
const zIndexStyle = useMemo(
|
||||
() => ({ zIndex: MODAL_STACK_Z_INDEX + index + 1 }),
|
||||
[index],
|
||||
@@ -86,7 +87,7 @@ export const ModalInternal: Component<{
|
||||
(e: SyntheticEvent) => {
|
||||
e.stopPropagation()
|
||||
|
||||
close()
|
||||
close(true)
|
||||
},
|
||||
[close],
|
||||
)
|
||||
@@ -142,11 +143,19 @@ export const ModalInternal: Component<{
|
||||
}, [isTop])
|
||||
|
||||
const modalContentRef = useRef<HTMLDivElement>(null)
|
||||
const ModalProps: ModalContentPropsInternal = useMemo(
|
||||
const ModalProps: ModalActionsInternal = useMemo(
|
||||
() => ({
|
||||
dismiss: close,
|
||||
setClickOutSideToDismiss: (v) => {
|
||||
setStack((state) => produce(state, (draft) => {
|
||||
const model = draft.find((modal) => modal.id === item.id)
|
||||
if (!model) return
|
||||
if (model.clickOutsideToDismiss === v) return
|
||||
model.clickOutsideToDismiss = v
|
||||
}))
|
||||
},
|
||||
}),
|
||||
[close],
|
||||
[close, item.id, setStack],
|
||||
)
|
||||
|
||||
const ModalContextProps = useMemo<CurrentModalContentProps>(
|
||||
@@ -156,13 +165,16 @@ export const ModalInternal: Component<{
|
||||
}),
|
||||
[ModalProps],
|
||||
)
|
||||
const finalChildren = useMemo(() => (
|
||||
<CurrentModalContext.Provider value={ModalContextProps}>
|
||||
<AppErrorBoundary errorType={ErrorComponentType.Modal}>
|
||||
{children ?? createElement(content, ModalProps)}
|
||||
</AppErrorBoundary>
|
||||
</CurrentModalContext.Provider>
|
||||
), [ModalContextProps, ModalProps, children, content])
|
||||
const finalChildren = useMemo(
|
||||
() => (
|
||||
<CurrentModalContext.Provider value={ModalContextProps}>
|
||||
<AppErrorBoundary errorType={ErrorComponentType.Modal}>
|
||||
{children ?? createElement(content, ModalProps)}
|
||||
</AppErrorBoundary>
|
||||
</CurrentModalContext.Provider>
|
||||
),
|
||||
[ModalContextProps, ModalProps, children, content],
|
||||
)
|
||||
|
||||
useEffect(() => {
|
||||
if (currentIsClosing) {
|
||||
@@ -188,7 +200,9 @@ export const ModalInternal: Component<{
|
||||
currentIsClosing && "!pointer-events-none",
|
||||
modalContainerClassName,
|
||||
)}
|
||||
onClick={clickOutsideToDismiss ? dismiss : undefined}
|
||||
onClick={
|
||||
clickOutsideToDismiss && canClose ? dismiss : undefined
|
||||
}
|
||||
style={zIndexStyle}
|
||||
>
|
||||
<div
|
||||
@@ -219,7 +233,9 @@ export const ModalInternal: Component<{
|
||||
currentIsClosing && "!pointer-events-none",
|
||||
modalContainerClassName,
|
||||
)}
|
||||
onClick={clickOutsideToDismiss ? dismiss : noticeModal}
|
||||
onClick={
|
||||
clickOutsideToDismiss && canClose ? dismiss : noticeModal
|
||||
}
|
||||
>
|
||||
<m.div
|
||||
ref={ref}
|
||||
@@ -259,13 +275,15 @@ export const ModalInternal: Component<{
|
||||
|
||||
<span>{title}</span>
|
||||
</Dialog.Title>
|
||||
<Dialog.DialogClose
|
||||
className="center p-2"
|
||||
tabIndex={1}
|
||||
onClick={close}
|
||||
>
|
||||
<i className="i-mgc-close-cute-re" />
|
||||
</Dialog.DialogClose>
|
||||
{canClose && (
|
||||
<Dialog.DialogClose
|
||||
className="center p-2"
|
||||
tabIndex={1}
|
||||
onClick={close}
|
||||
>
|
||||
<i className="i-mgc-close-cute-re" />
|
||||
</Dialog.DialogClose>
|
||||
)}
|
||||
</div>
|
||||
<Divider className="my-2 shrink-0 border-slate-200 opacity-80 dark:border-neutral-800" />
|
||||
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { FC, PropsWithChildren, ReactNode } from "react"
|
||||
|
||||
import type { ModalContentPropsInternal } from "./context"
|
||||
import type { ModalActionsInternal } from "./context"
|
||||
|
||||
export interface ModalProps {
|
||||
title: ReactNode
|
||||
icon?: ReactNode
|
||||
|
||||
CustomModalComponent?: FC<PropsWithChildren>
|
||||
content: FC<ModalContentPropsInternal>
|
||||
content: FC<ModalActionsInternal>
|
||||
clickOutsideToDismiss?: boolean
|
||||
modalClassName?: string
|
||||
modalContainerClassName?: string
|
||||
@@ -18,6 +18,7 @@ export interface ModalProps {
|
||||
|
||||
overlay?: boolean
|
||||
draggable?: boolean
|
||||
canClose?: boolean
|
||||
}
|
||||
export interface ModalStackOptions {
|
||||
wrapper?: FC
|
||||
|
||||
@@ -47,9 +47,7 @@ export const LoginButton: FC<LoginProps> = (props) => {
|
||||
title: "Login",
|
||||
id: "login",
|
||||
content: () => (
|
||||
<LoginModalContent
|
||||
runtime={ELECTRON ? "app" : "browser"}
|
||||
/>
|
||||
<LoginModalContent runtime={ELECTRON ? "app" : "browser"} />
|
||||
),
|
||||
clickOutsideToDismiss: true,
|
||||
})
|
||||
@@ -87,14 +85,19 @@ export const ProfileButton: FC<LoginProps> = memo((props) => {
|
||||
<UserAvatar className="h-7 shrink-0 p-0" hideName />
|
||||
<div className="max-w-40 leading-none">
|
||||
<div className="truncate">{user?.name}</div>
|
||||
<div className="truncate text-xs font-medium text-zinc-500">{user?.handle}</div>
|
||||
<div className="truncate text-xs font-medium text-zinc-500">
|
||||
{user?.handle}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</DropdownMenuLabel>
|
||||
<DropdownMenuSeparator />
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
window.open(`${WEB_URL}/profile/${user?.handle || user?.id}`, "_blank")
|
||||
window.open(
|
||||
`${WEB_URL}/profile/${user?.handle || user?.id}`,
|
||||
"_blank",
|
||||
)
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-user-3-cute-re mr-1.5" />
|
||||
@@ -125,7 +128,7 @@ export const ProfileButton: FC<LoginProps> = memo((props) => {
|
||||
<>
|
||||
<DropdownMenuItem
|
||||
onClick={() => {
|
||||
// TODO
|
||||
// TODO
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-gift-cute-re mr-1.5" />
|
||||
@@ -158,14 +161,17 @@ export function UserAvatar({
|
||||
enabled: !userId,
|
||||
})
|
||||
|
||||
const profile = useAuthQuery(defineQuery(["profiles", userId], async () => {
|
||||
const res = await apiClient.profiles.$get({
|
||||
query: { id: userId! },
|
||||
})
|
||||
return res.data
|
||||
}), {
|
||||
enabled: !!userId,
|
||||
})
|
||||
const profile = useAuthQuery(
|
||||
defineQuery(["profiles", userId], async () => {
|
||||
const res = await apiClient.profiles.$get({
|
||||
query: { id: userId! },
|
||||
})
|
||||
return res.data
|
||||
}),
|
||||
{
|
||||
enabled: !!userId,
|
||||
},
|
||||
)
|
||||
|
||||
if (!userId && status !== "authenticated") {
|
||||
return <LoginButton {...props} />
|
||||
@@ -179,8 +185,12 @@ export function UserAvatar({
|
||||
)}
|
||||
>
|
||||
<Avatar className="aspect-square h-full w-auto">
|
||||
<AvatarImage src={(profile.data || session?.user)?.image || undefined} />
|
||||
<AvatarFallback>{(profile.data || session?.user)?.name?.slice(0, 2)}</AvatarFallback>
|
||||
<AvatarImage
|
||||
src={(profile.data || session?.user)?.image || undefined}
|
||||
/>
|
||||
<AvatarFallback>
|
||||
{(profile.data || session?.user)?.name?.slice(0, 2)}
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
{!hideName && <div>{(profile.data || session?.user)?.name}</div>}
|
||||
</div>
|
||||
|
||||
@@ -9,11 +9,18 @@ import { m } from "framer-motion"
|
||||
|
||||
interface LoginModalContentProps {
|
||||
runtime?: LoginRuntime
|
||||
canClose?: boolean
|
||||
}
|
||||
export const LoginModalContent = (props: LoginModalContentProps) => {
|
||||
const modal = useCurrentModal()
|
||||
|
||||
const { canClose = true, runtime } = props
|
||||
|
||||
return (
|
||||
<div className="center flex h-full" onClick={modal.dismiss}>
|
||||
<div
|
||||
className="center flex h-full"
|
||||
onClick={canClose ? modal.dismiss : undefined}
|
||||
>
|
||||
<m.div
|
||||
className="shadow-modal rounded-lg border border-border bg-theme-background p-4 px-8 pb-8"
|
||||
onClick={stopPropagation}
|
||||
@@ -31,7 +38,7 @@ export const LoginModalContent = (props: LoginModalContentProps) => {
|
||||
className="h-[48px] w-[320px] rounded-[8px] !bg-black font-sans text-base text-white hover:!bg-black/80"
|
||||
size="lg"
|
||||
onClick={() => {
|
||||
loginHandler("github", props.runtime)
|
||||
loginHandler("github", runtime)
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-github-cute-fi mr-2 text-xl" />
|
||||
@@ -43,7 +50,7 @@ export const LoginModalContent = (props: LoginModalContentProps) => {
|
||||
className="h-[48px] w-[320px] rounded-[8px] bg-blue-500 font-sans text-base text-white hover:bg-blue-500/90"
|
||||
size="xl"
|
||||
onClick={() => {
|
||||
loginHandler("google", props.runtime)
|
||||
loginHandler("google", runtime)
|
||||
}}
|
||||
>
|
||||
<i className="i-mgc-google-cute-fi mr-2 text-xl" />
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
} from "@renderer/components/ui/code-highlighter"
|
||||
import { useShikiDefaultTheme } from "@renderer/components/ui/code-highlighter/shiki/hooks"
|
||||
import { LoadingCircle } from "@renderer/components/ui/loading"
|
||||
import { useCurrentModal } from "@renderer/components/ui/modal"
|
||||
import {
|
||||
Tabs,
|
||||
TabsContent,
|
||||
@@ -19,6 +20,7 @@ import { Queries } from "@renderer/queries"
|
||||
import { useClaimFeedMutation } from "@renderer/queries/feed"
|
||||
import { useFeedById } from "@renderer/store/feed"
|
||||
import type { FC } from "react"
|
||||
import { useEffect } from "react"
|
||||
|
||||
export const FeedClaimModalContent: FC<{
|
||||
feedId: string
|
||||
@@ -31,6 +33,7 @@ export const FeedClaimModalContent: FC<{
|
||||
} = useAuthQuery(Queries.feed.claimMessage({ feedId }), {
|
||||
enabled: !!feed,
|
||||
})
|
||||
const { setClickOutSideToDismiss } = useCurrentModal()
|
||||
|
||||
const {
|
||||
mutateAsync: claim,
|
||||
@@ -38,6 +41,10 @@ export const FeedClaimModalContent: FC<{
|
||||
isSuccess,
|
||||
} = useClaimFeedMutation(feedId)
|
||||
|
||||
useEffect(() => {
|
||||
setClickOutSideToDismiss(isPending)
|
||||
}, [isPending, setClickOutSideToDismiss])
|
||||
|
||||
const shikiTheme = useShikiDefaultTheme()
|
||||
|
||||
if (!feed) return null
|
||||
@@ -81,7 +88,9 @@ export const FeedClaimModalContent: FC<{
|
||||
<TabsContent className="mt-0 pt-3" value="description">
|
||||
<p className="mb-2 leading-none">
|
||||
Current description:
|
||||
<span className="ml-2 text-xs text-zinc-500">{feed.description}</span>
|
||||
<span className="ml-2 text-xs text-zinc-500">
|
||||
{feed.description}
|
||||
</span>
|
||||
</p>
|
||||
<p>
|
||||
Copy the following content and paste it into the
|
||||
|
||||
@@ -5,7 +5,7 @@ import { CopyButton } from "@renderer/components/ui/code-highlighter"
|
||||
import { Form, FormItem, FormLabel } from "@renderer/components/ui/form"
|
||||
import { Input } from "@renderer/components/ui/input"
|
||||
import { Markdown } from "@renderer/components/ui/markdown"
|
||||
import { useModalStack } from "@renderer/components/ui/modal"
|
||||
import { useCurrentModal, useModalStack } from "@renderer/components/ui/modal"
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -22,7 +22,13 @@ import {
|
||||
} from "@renderer/lib/path-parser"
|
||||
import { omit } from "lodash-es"
|
||||
import type { FC } from "react"
|
||||
import { useCallback, useLayoutEffect, useMemo, useRef } from "react"
|
||||
import {
|
||||
useCallback,
|
||||
useEffect,
|
||||
useLayoutEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
} from "react"
|
||||
import type { UseFormReturn } from "react-hook-form"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { toast } from "sonner"
|
||||
@@ -169,6 +175,12 @@ export const DiscoverFeedForm = ({
|
||||
if (!$form) return
|
||||
$form.querySelectorAll("input")[0]?.focus()
|
||||
}, [formElRef])
|
||||
|
||||
const { setClickOutSideToDismiss } = useCurrentModal()
|
||||
|
||||
useEffect(() => {
|
||||
setClickOutSideToDismiss(!form.formState.isDirty)
|
||||
}, [form.formState.isDirty, setClickOutSideToDismiss])
|
||||
return (
|
||||
<Form {...form}>
|
||||
<PreviewUrl
|
||||
@@ -220,7 +232,11 @@ export const DiscoverFeedForm = ({
|
||||
) : (
|
||||
<Input
|
||||
{...formRegister}
|
||||
placeholder={parameters?.default ?? formPlaceholder[keyItem.name] ? `e.g. ${formPlaceholder[keyItem.name]}` : void 0}
|
||||
placeholder={
|
||||
parameters?.default ?? formPlaceholder[keyItem.name] ?
|
||||
`e.g. ${formPlaceholder[keyItem.name]}` :
|
||||
void 0
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{!!parameters && (
|
||||
@@ -237,9 +253,7 @@ export const DiscoverFeedForm = ({
|
||||
<FeedMaintainers maintainers={route.maintainers} />
|
||||
|
||||
<div className="sticky bottom-0 -mt-4 flex w-full translate-y-3 justify-end bg-theme-modal-background-opaque py-3">
|
||||
<StyledButton type="submit">
|
||||
Preview
|
||||
</StyledButton>
|
||||
<StyledButton type="submit">Preview</StyledButton>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
FormMessage,
|
||||
} from "@renderer/components/ui/form"
|
||||
import { LoadingCircle } from "@renderer/components/ui/loading"
|
||||
import { useCurrentModal } from "@renderer/components/ui/modal"
|
||||
import {
|
||||
Select,
|
||||
SelectTrigger,
|
||||
@@ -71,6 +72,14 @@ export const FeedForm: Component<{
|
||||
},
|
||||
})
|
||||
|
||||
const { setClickOutSideToDismiss } = useCurrentModal()
|
||||
|
||||
useEffect(() => {
|
||||
if (form.formState.isDirty) {
|
||||
setClickOutSideToDismiss(false)
|
||||
}
|
||||
}, [form.formState.isDirty])
|
||||
|
||||
useEffect(() => {
|
||||
if (feed.data?.subscription) {
|
||||
form.setValue("view", `${feed.data?.subscription?.view}`)
|
||||
|
||||
@@ -8,9 +8,11 @@ import {
|
||||
FormMessage,
|
||||
} from "@renderer/components/ui/form"
|
||||
import { Input } from "@renderer/components/ui/input"
|
||||
import { useCurrentModal } from "@renderer/components/ui/modal"
|
||||
import { apiClient } from "@renderer/lib/api-fetch"
|
||||
import { Queries } from "@renderer/queries"
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { useEffect } from "react"
|
||||
import { useForm } from "react-hook-form"
|
||||
import { z } from "zod"
|
||||
|
||||
@@ -55,6 +57,13 @@ export function CategoryRenameContent({
|
||||
renameMutation.mutate(values)
|
||||
}
|
||||
|
||||
const { setClickOutSideToDismiss } = useCurrentModal()
|
||||
|
||||
useEffect(() => {
|
||||
if (form.formState.isDirty) {
|
||||
setClickOutSideToDismiss(false)
|
||||
}
|
||||
}, [form.formState.isDirty])
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4">
|
||||
|
||||
@@ -41,7 +41,8 @@ function FeedCategoryImpl({
|
||||
const subscription = useSubscriptionByFeedId(ids[0])
|
||||
const folderName = subscription?.category || subscription.defaultCategory
|
||||
|
||||
const showCollapse = sortByUnreadFeedList.length > 1 || subscription?.category
|
||||
const showCollapse =
|
||||
sortByUnreadFeedList.length > 1 || subscription?.category
|
||||
const [open, setOpen] = useState(!showCollapse)
|
||||
useEffect(() => {
|
||||
if (showCollapse) {
|
||||
@@ -95,6 +96,7 @@ function FeedCategoryImpl({
|
||||
click: () => {
|
||||
present({
|
||||
title: "Rename Category",
|
||||
clickOutsideToDismiss: true,
|
||||
content: ({ dismiss }) => (
|
||||
<CategoryRenameContent
|
||||
feedIdList={ids}
|
||||
|
||||
@@ -24,7 +24,9 @@ export function Component() {
|
||||
<div className="pointer-events-none absolute bottom-3 w-full text-center text-xs opacity-20">
|
||||
Early Access
|
||||
{" "}
|
||||
{GIT_COMMIT_SHA ? `(${GIT_COMMIT_SHA.slice(0, 7).toUpperCase()})` : ""}
|
||||
{GIT_COMMIT_SHA ?
|
||||
`(${GIT_COMMIT_SHA.slice(0, 7).toUpperCase()})` :
|
||||
""}
|
||||
</div>
|
||||
)}
|
||||
<AutoUpdater />
|
||||
@@ -50,8 +52,13 @@ export function Component() {
|
||||
CustomModalComponent={NoopChildren}
|
||||
open
|
||||
title="Login"
|
||||
canClose={false}
|
||||
clickOutsideToDismiss={false}
|
||||
>
|
||||
<LoginModalContent runtime={ELECTRON ? "app" : "browser"} />
|
||||
<LoginModalContent
|
||||
canClose={false}
|
||||
runtime={ELECTRON ? "app" : "browser"}
|
||||
/>
|
||||
</DeclarativeModal>
|
||||
</RootPortal>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user