-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ata Shaikh
committed
Mar 20, 2024
1 parent
1a49018
commit 7beb846
Showing
14 changed files
with
290 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { useState } from 'react'; | ||
|
||
import { Adapt, Button, AlertDialog, Sheet } from 'tamagui'; | ||
|
||
export const Alert = ({ title, description, trigger, children }) => { | ||
const [open, setOpen] = useState(false); | ||
|
||
return ( | ||
<AlertDialog | ||
modal | ||
onOpenChange={(open) => { | ||
setOpen(open); | ||
}} | ||
> | ||
<AlertDialog.Trigger asChild> | ||
<Button>{trigger}</Button> | ||
</AlertDialog.Trigger> | ||
<Adapt when="sm" platform="touch"> | ||
<Sheet zIndex={200000} modal dismissOnSnapToBottom> | ||
<Sheet.Frame padding="$4" gap> | ||
<Adapt.Contents /> | ||
</Sheet.Frame> | ||
|
||
<Sheet.Overlay | ||
animation="lazy" | ||
enterStyle={{ opacity: 0 }} | ||
exitStyle={{ opacity: 0 }} | ||
/> | ||
</Sheet> | ||
</Adapt> | ||
<AlertDialog.Portal> | ||
<AlertDialog.Overlay | ||
key="overlay" | ||
animation="quick" | ||
opacity={0.5} | ||
enterStyle={{ opacity: 0 }} | ||
exitStyle={{ opacity: 0 }} | ||
/> | ||
<AlertDialog.Content | ||
bordered | ||
elevate | ||
key="content" | ||
animateOnly={['transform', 'opacity']} | ||
animation={[ | ||
'quick', | ||
{ | ||
opacity: { | ||
overshootClamping: true, | ||
}, | ||
}, | ||
]} | ||
enterStyle={{ x: 0, y: -20, opacity: 0, scale: 0.9 }} | ||
exitStyle={{ x: 0, y: 10, opacity: 0, scale: 0.95 }} | ||
gap | ||
> | ||
<AlertDialog.Title>{title}</AlertDialog.Title> | ||
|
||
<AlertDialog.Description>{description}</AlertDialog.Description> | ||
{children} | ||
</AlertDialog.Content> | ||
</AlertDialog.Portal> | ||
</AlertDialog> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,168 @@ | ||
import React, { useEffect, useMemo, useState } from 'react'; | ||
import { Button, AlertDialog } from 'tamagui'; | ||
import { X } from '@tamagui/lucide-icons'; | ||
import RButton from '@packrat/ui/src/RButton'; | ||
import RStack from '@packrat/ui/src/RStack'; | ||
import { useAlert, AlertProvider } from './provider'; | ||
|
||
export interface BaseAlertProps { | ||
id?: string; | ||
title: string; | ||
trigger?: string; | ||
children: React.ReactNode; | ||
buttonColor?: string; | ||
footerButtons?: any[]; | ||
triggerComponent?: React.DetailedReactHTMLElement<any, HTMLElement>; | ||
footerComponent: React.DetailedReactHTMLElement<any, HTMLElement>; | ||
hideIcon?: boolean; | ||
isOpen?: boolean | undefined; | ||
toggle?: any; | ||
} | ||
|
||
export const BaseAlert = ({ | ||
triggerComponent, | ||
title, | ||
trigger, | ||
footerButtons, | ||
footerComponent, | ||
children, | ||
hideIcon = false, | ||
isOpen = undefined, | ||
toggle, | ||
}: BaseAlertProps) => { | ||
const [isAlertOpen, setIsAlertOpen] = useState(false); | ||
|
||
useEffect(() => { | ||
if(isOpen !== undefined) { | ||
setIsAlertOpen(isOpen) | ||
} | ||
}, [isOpen]) | ||
|
||
const triggerElement = useMemo(() => { | ||
return triggerComponent ? ( | ||
<RButton | ||
onPress={() => setIsAlertOpen(true)} | ||
style={{ backgroundColor: 'transparent' }} | ||
backgroundColor={'transparent'} | ||
> | ||
{React.cloneElement(triggerComponent, { setIsAlertOpen })} | ||
</RButton> | ||
) : ( | ||
<RButton | ||
top={5} | ||
alignSelf={'center'} | ||
onPress={() => setIsAlertOpen(true)} | ||
> | ||
{trigger} | ||
</RButton> | ||
); | ||
}, [triggerComponent]); | ||
|
||
const memoFooterButtons = useMemo(() => { | ||
if (!Array.isArray(footerButtons)) return null; | ||
|
||
return footerButtons.map(({ color, label, onClick, ...button }, index) => ( | ||
<RButton | ||
key={index} | ||
onPress={withAlertCloseHandler(onClick, () => { | ||
setIsAlertOpen(false) | ||
if(toggle) { | ||
toggle() | ||
} | ||
})} | ||
backgroundColor={color} | ||
disabled={button.disabled} | ||
color="white" | ||
{...button} | ||
> | ||
{label} | ||
</RButton> | ||
)); | ||
}, [footerButtons]); | ||
|
||
const footerElement = useMemo(() => { | ||
return ( | ||
footerComponent && React.cloneElement(footerComponent, { setIsAlertOpen }) | ||
); | ||
}, [footerComponent]); | ||
|
||
return ( | ||
<AlertDialog | ||
modal | ||
open={isAlertOpen} | ||
onOpenChange={(open) => { | ||
setIsAlertOpen(open); | ||
}} | ||
> | ||
{!hideIcon && <AlertDialog.Trigger asChild>{triggerElement}</AlertDialog.Trigger>} | ||
<AlertDialog.Portal> | ||
<AlertDialog.Overlay | ||
key="overlay" | ||
animation="quick" | ||
opacity={0.5} | ||
enterStyle={{ opacity: 0 }} | ||
exitStyle={{ opacity: 0 }} | ||
minWidth={400} | ||
/> | ||
|
||
<AlertDialog.Content | ||
bordered | ||
elevate | ||
key="content" | ||
animateOnly={['transform', 'opacity']} | ||
animation={[ | ||
'quick', | ||
{ | ||
opacity: { | ||
overshootClamping: true, | ||
}, | ||
}, | ||
]} | ||
enterStyle={{ x: 0, y: -20, opacity: 0, scale: 0.9 }} | ||
exitStyle={{ x: 0, y: 10, opacity: 0, scale: 0.95 }} | ||
gap="$4" | ||
> | ||
<AlertDialog.Title>{title}</AlertDialog.Title> | ||
<AlertDialog.Description> | ||
<AlertProvider | ||
isAlertOpen={isAlertOpen} | ||
setIsAlertOpen={setIsAlertOpen} | ||
> | ||
{children} | ||
</AlertProvider> | ||
</AlertDialog.Description> | ||
|
||
<RStack | ||
style={{ alignSelf: 'flex-end', flexDirection: 'row' }} | ||
gap="$4" | ||
> | ||
{memoFooterButtons} | ||
</RStack> | ||
{footerElement} | ||
<AlertDialog.Cancel asChild> | ||
<Button | ||
onPress={() => { | ||
setIsAlertOpen(false); | ||
if(toggle) { | ||
toggle() | ||
} | ||
}} | ||
position="absolute" | ||
backgroundColor="transparent" | ||
top="$3" | ||
right="$3" | ||
size="$2" | ||
circular | ||
icon={X} | ||
/> | ||
</AlertDialog.Cancel> | ||
</AlertDialog.Content> | ||
</AlertDialog.Portal> | ||
</AlertDialog> | ||
); | ||
}; | ||
|
||
const withAlertCloseHandler = | ||
(fn, closeHandler) => | ||
(...args) => | ||
fn?.(...args, closeHandler.bind(null, false)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { BaseAlert } from './BaseAlert'; | ||
export { useAlert, AlertProvider } from './provider'; |
Oops, something went wrong.