import { createStyledHOC } from '@tamagui/core'
// forked from radix-ui
// https://github.com/radix-ui/primitives/blob/main/packages/react/alert-dialog/src/AlertDialog.tsx
import { useComposedRefs } from '@tamagui/compose-refs'
import { isWeb, useIsomorphicLayoutEffect } from '@tamagui/constants'
import type { TamaguiElement } from '@tamagui/core'
import { Slottable, View, createStyledContext, styled } from '@tamagui/core'
import type {
DialogCloseProps,
DialogContentProps,
DialogDescriptionProps,
DialogOverlayExtraProps,
DialogOverlayProps,
DialogPortalProps,
DialogProps,
DialogTitleProps,
DialogTriggerProps,
} from '@tamagui/dialog'
import {
Dialog,
DialogClose,
DialogContent,
DialogContentFrame,
DialogDescription,
DialogOverlay,
DialogOverlayFrame,
DialogPortal,
DialogTitle,
DialogTrigger,
DialogWarningProvider,
} from '@tamagui/dialog'
import { composeEventHandlers, withStaticProperties } from '@tamagui/helpers'
import * as React from 'react'
import { markAlertDialogPart } from './alertDialogPart'
import { useNativeAlertDialog } from './useNativeAlertDialog'
const getAlertDialogScope = (scope?: string) => scope
/* -------------------------------------------------------------------------------------------------
* AlertDialog
* -----------------------------------------------------------------------------------------------*/
const ROOT_NAME = 'AlertDialog'
export type AlertDialogScopes = string
type ScopedProps
= Omit
& { scope?: AlertDialogScopes }
type AlertDialogProps = ScopedProps & {
native?: boolean
}
/* -------------------------------------------------------------------------------------------------
* AlertDialogTrigger
* -----------------------------------------------------------------------------------------------*/
const TRIGGER_NAME = 'AlertDialogTrigger'
type AlertDialogTriggerProps = ScopedProps
const NativeAlertDialogTriggerFrame = styled(View, {
displayName: TRIGGER_NAME,
})
markAlertDialogPart(NativeAlertDialogTriggerFrame, 'trigger')
const AlertDialogTrigger = createStyledHOC(
NativeAlertDialogTriggerFrame,
function AlertDialogTrigger(props: AlertDialogTriggerProps, forwardedRef) {
if (props['__native']) {
const { __native, onPress, __onPress, ...rest } = props as any
return (
)
}
const { scope, ...triggerProps } = props
return (
)
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogPortal
* -----------------------------------------------------------------------------------------------*/
const PORTAL_NAME = 'AlertDialogPortal'
type AlertDialogPortalProps = ScopedProps
const AlertDialogPortal: React.FC = function AlertDialogPortal(
props: ScopedProps
) {
const { scope, ...portalProps } = props
return
}
/* -------------------------------------------------------------------------------------------------
* AlertDialogOverlay
* -----------------------------------------------------------------------------------------------*/
const OVERLAY_NAME = 'AlertDialogOverlay'
const AlertDialogOverlayFrame = styled(DialogOverlayFrame, {
displayName: OVERLAY_NAME,
})
type AlertDialogOverlayExtraProps = ScopedProps<{}> & DialogOverlayExtraProps
type AlertDialogOverlayProps = AlertDialogOverlayExtraProps & DialogOverlayProps
const AlertDialogOverlay = createStyledHOC(
AlertDialogOverlayFrame,
function AlertDialogOverlay(props: AlertDialogOverlayProps, forwardedRef) {
const { scope, ...overlayProps } = props
return (
)
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogContent
* -----------------------------------------------------------------------------------------------*/
const CONTENT_NAME = 'AlertDialogContent'
type AlertDialogContentContextValue = {
cancelRef?: React.RefObject
destructiveRef?: React.RefObject
}
const {
Provider: AlertDialogContextProvider,
useStyledContext: useAlertDialogContentContext,
} = createStyledContext({}, 'AlertDialogContext')
type AlertDialogContentProps = ScopedProps<
Omit
>
// a styled HOC, not a plain component wrapping DialogContent: `styled()` on a
// plain component would land the animation props on this outer frame, which
// renders above the presence boundary DialogContent creates, and an exiting
// frame that never joins presence finishes its exit instantly
const AlertDialogContentFrame = styled(DialogContentFrame, {
displayName: CONTENT_NAME,
})
const AlertDialogContent = createStyledHOC(
AlertDialogContentFrame,
function AlertDialogContent(props: AlertDialogContentProps, forwardedRef) {
const { scope, children, ...contentProps } = props
const dialogScope = getAlertDialogScope(scope)
const contentRef = React.useRef(null)
const composedRefs = useComposedRefs(forwardedRef, contentRef)
const cancelRef = React.useRef(null)
const destructiveRef = React.useRef(null)
return (
{
event.cancel()
if (isWeb) {
cancelRef.current?.focus({ preventScroll: true })
}
}
)}
onPointerDownOutside={(event) => event.cancel()}
onInteractOutside={(event) => event.cancel()}
>
{/**
* We have to use `Slottable` here as we cannot wrap the `AlertDialogContentProvider`
* around everything, otherwise the `DescriptionWarning` would be rendered straight away.
* This is because we want the accessibility checks to run only once the content is actually
* open and that behaviour is already encapsulated in `DialogContent`.
*/}
{children}
{process.env.NODE_ENV === 'development' && (
)}
)
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogTitle
* -----------------------------------------------------------------------------------------------*/
const TITLE_NAME = 'AlertDialogTitle'
type AlertDialogTitleProps = ScopedProps
const AlertDialogTitleFrame = styled(View, {
displayName: TITLE_NAME,
})
markAlertDialogPart(AlertDialogTitleFrame, 'title')
const AlertDialogTitle = createStyledHOC(
AlertDialogTitleFrame,
function AlertDialogTitle(props: AlertDialogTitleProps, forwardedRef) {
const { scope, ...titleProps } = props
return (
)
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogDescription
* -----------------------------------------------------------------------------------------------*/
const DESCRIPTION_NAME = 'AlertDialogDescription'
type AlertDialogDescriptionProps = ScopedProps
const AlertDialogDescriptionFrame = styled(View, {
displayName: DESCRIPTION_NAME,
})
markAlertDialogPart(AlertDialogDescriptionFrame, 'description')
const AlertDialogDescription = createStyledHOC(
AlertDialogDescriptionFrame,
function AlertDialogDescription(props: AlertDialogDescriptionProps, forwardedRef) {
const { scope, ...descriptionProps } = props
return (
)
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogAction
* -----------------------------------------------------------------------------------------------*/
const ACTION_NAME = 'AlertDialogAction'
type AlertDialogActionProps = ScopedProps
const AlertDialogActionFrame = styled(View, {
displayName: ACTION_NAME,
})
markAlertDialogPart(AlertDialogActionFrame, 'action')
const AlertDialogAction = createStyledHOC(
AlertDialogActionFrame,
function AlertDialogAction(props: AlertDialogActionProps, forwardedRef) {
const { scope, ...actionProps } = props
return (
)
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogCancel
* -----------------------------------------------------------------------------------------------*/
const CANCEL_NAME = 'AlertDialogCancel'
type AlertDialogCancelProps = ScopedProps
const AlertDialogCancelFrame = styled(View, {
displayName: CANCEL_NAME,
})
markAlertDialogPart(AlertDialogCancelFrame, 'cancel')
const AlertDialogCancel = createStyledHOC(
AlertDialogCancelFrame,
function AlertDialogCancel(props: AlertDialogCancelProps, forwardedRef) {
const { scope, ...cancelProps } = props
const { cancelRef } = useAlertDialogContentContext(scope)
const ref = useComposedRefs(forwardedRef, cancelRef)
return
}
)
/* -------------------------------------------------------------------------------------------------
* AlertDialogDestructive
* -----------------------------------------------------------------------------------------------*/
const DESTRUCTIVE_NAME = 'AlertDialogDestructive'
type AlertDialogDestructiveProps = ScopedProps
const AlertDialogDestructiveFrame = styled(View, {
displayName: DESTRUCTIVE_NAME,
})
markAlertDialogPart(AlertDialogDestructiveFrame, 'destructive')
const AlertDialogDestructive = createStyledHOC(
AlertDialogDestructiveFrame,
function AlertDialogDestructive(props: AlertDialogDestructiveProps, forwardedRef) {
const { scope, ...destructiveProps } = props
const { destructiveRef } = useAlertDialogContentContext(scope)
const ref = useComposedRefs(forwardedRef, destructiveRef)
return (
)
}
)
/* ---------------------------------------------------------------------------------------------- */
type DescriptionWarningProps = {
contentRef: React.RefObject
}
const DescriptionWarning: React.FC = ({ contentRef }) => {
if (process.env.NODE_ENV === 'development') {
React.useEffect(() => {
if (!isWeb) return
const hasDescription = document.getElementById(
// @ts-ignore
contentRef.current?.getAttribute('aria-describedby')
)
if (!hasDescription) {
console.warn(`\`${CONTENT_NAME}\` requires a description for the component to be accessible for screen reader users.
You can add a description to the \`${CONTENT_NAME}\` by passing a \`${DESCRIPTION_NAME}\` component as a child, which also benefits sighted users by adding visible context to the dialog.
Alternatively, you can use your own component as a description by assigning it an \`id\` and passing the same value to the \`aria-describedby\` prop in \`${CONTENT_NAME}\`. If the description is confusing or duplicative for sighted users, you can use the \`@radix-ui/react-visually-hidden\` primitive as a wrapper around your description component.
For more information, see https://tamagui.dev/docs/components/alert-dialog`)
}
}, [contentRef])
}
return null
}
const AlertDialogInner: React.FC = (props) => {
const { scope, native, ...alertDialogProps } = props
const dialogScope = getAlertDialogScope(scope)
const nativeAlert = useNativeAlertDialog(props)
if (nativeAlert) return nativeAlert
return
}
const AlertDialog = withStaticProperties(AlertDialogInner, {
Trigger: AlertDialogTrigger,
Portal: AlertDialogPortal,
Overlay: AlertDialogOverlay,
Content: AlertDialogContent,
Action: AlertDialogAction,
Cancel: AlertDialogCancel,
Destructive: AlertDialogDestructive,
Title: AlertDialogTitle,
Description: AlertDialogDescription,
})
AlertDialog.displayName = ROOT_NAME
export {
//
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogDestructive,
AlertDialogContent,
AlertDialogDescription,
AlertDialogOverlay,
AlertDialogPortal,
AlertDialogTitle,
AlertDialogTrigger,
}
export type {
AlertDialogActionProps,
AlertDialogCancelProps,
AlertDialogDestructiveProps,
AlertDialogContentProps,
AlertDialogDescriptionProps,
AlertDialogOverlayProps,
AlertDialogPortalProps,
AlertDialogProps,
AlertDialogTitleProps,
AlertDialogTriggerProps,
}