-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Popovers now register as dialogs and can be flagged as non-modal
- Loading branch information
1 parent
07e283c
commit ba1f167
Showing
16 changed files
with
222 additions
and
74 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,86 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
import React, { useCallback, useRef } from 'react'; | ||
import { | ||
Popover as AriaPopover, | ||
Dialog as AriaDialog, | ||
DialogTrigger as AriaDialogTrigger, | ||
// OverlayArrow as AriaOverlayArrow | ||
OverlayArrow as AriaOverlayArrow | ||
} from 'react-aria-components'; | ||
import { fillStyles, cn } from '@do-ob/ui/utility'; | ||
// import { ArrowUpIcon } from '@do-ob/ui/icons-hero-solid'; | ||
|
||
export interface PopoverProps { | ||
/** | ||
* The content of the popover. | ||
*/ | ||
content: React.ReactNode; | ||
/** | ||
* The trigger element of the popover. | ||
*/ | ||
trigger: React.ReactNode; | ||
/** | ||
* The placement of the popover. | ||
*/ | ||
placement?: 'top' | 'right' | 'bottom' | 'left'; | ||
/** | ||
* The offset of the popover. | ||
*/ | ||
offset?: number; | ||
} | ||
import { PopoverProps } from './Popover.types'; | ||
import { useDialogState, useDialogControl, useDialogRegister, useOutsidePress } from '@do-ob/ui/hooks'; | ||
|
||
export function Popover({ | ||
content, | ||
trigger, | ||
id, | ||
placement = 'bottom', | ||
offset = 8, | ||
}: PopoverProps) { | ||
nonmodal = false, | ||
hideArrow = false, | ||
children, | ||
}: React.PropsWithChildren<PopoverProps>) { | ||
|
||
const popoverRef = useRef<HTMLDivElement>(null); | ||
|
||
// Get the popover dialog state. | ||
const popover = useDialogState(id); | ||
|
||
// Get the popover dialog control. | ||
const controls = useDialogControl(id); | ||
|
||
const handleOutsidePress = useCallback(() => { | ||
if(nonmodal) { | ||
controls.close(); | ||
} | ||
}, [ controls, nonmodal ]); | ||
|
||
useOutsidePress(popoverRef, handleOutsidePress); | ||
|
||
// Register the popover dialog. | ||
useDialogRegister(id); | ||
|
||
const handleOpenChange = useCallback((next: boolean) => { | ||
if (!next) { | ||
controls.close(); | ||
} | ||
}, [ controls ]); | ||
|
||
return ( | ||
<AriaDialogTrigger> | ||
{trigger} | ||
<AriaPopover | ||
placement={placement} | ||
offset={offset} | ||
className="min-w-56 origin-top-left overflow-auto rounded bg-background p-1 shadow-lg ring-1 ring-background-fg/30 fill-mode-forwards entering:animate-in entering:fade-in entering:zoom-in-95 exiting:animate-out exiting:fade-out exiting:zoom-out-95 dark:bg-background-dark dark:ring-background-dark-fg/30" | ||
<AriaPopover | ||
ref={popoverRef} | ||
isOpen={popover.open && !!(popover.triggerRef?.current)} | ||
triggerRef={popover.triggerRef ?? { current: null }} | ||
onOpenChange={handleOpenChange} | ||
placement={placement} | ||
offset={offset} | ||
isNonModal={nonmodal} | ||
shouldCloseOnInteractOutside={(element) => { | ||
return !!popover.triggerRef && !element.contains(popover.triggerRef?.current); | ||
}} | ||
className="min-w-56 origin-top-left rounded bg-background p-1 shadow-lg ring-1 ring-background-fg/30 fill-mode-forwards entering:animate-in entering:fade-in entering:zoom-in-95 exiting:animate-out exiting:fade-out exiting:zoom-out-95 dark:bg-background-dark dark:ring-background-dark-fg/30" | ||
> | ||
<AriaOverlayArrow | ||
className={({ placement }) => cn( | ||
'absolute left-1/2 z-10 size-[12px] -translate-x-1/2 [&_svg]:fill-background-fg/50 dark:[&_svg]:fill-background-dark-fg/30', | ||
hideArrow && 'hidden', | ||
placement === 'top' && '[&_svg]:rotate-0', | ||
placement === 'bottom' && '[&_svg]:rotate-180', | ||
placement === 'left' && '[&_svg]:rotate-90', | ||
placement === 'right' && '[&_svg]:-rotate-90', | ||
)} | ||
> | ||
<svg width={12} height={12} viewBox="0 0 12 12" fill="gray"> | ||
<path d="M0 0 L6 6 L12 0" /> | ||
</svg> | ||
</AriaOverlayArrow> | ||
<AriaDialog | ||
aria-label={id} | ||
className={cn( | ||
'p-2 focus-visible:outline-none', | ||
fillStyles.background, | ||
)} | ||
> | ||
{/* <AriaOverlayArrow> | ||
<ArrowUpIcon className="block size-4 bg-red-500 fill-black" /> | ||
</AriaOverlayArrow> */} | ||
<AriaDialog | ||
className={cn( | ||
'p-2 focus-visible:outline-none', | ||
fillStyles.background, | ||
)} | ||
> | ||
{content} | ||
</AriaDialog> | ||
</AriaPopover> | ||
</AriaDialogTrigger> | ||
{children} | ||
</AriaDialog> | ||
</AriaPopover> | ||
); | ||
} |
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,26 @@ | ||
export interface PopoverProps { | ||
/** | ||
* Required identifier of the popover. | ||
*/ | ||
id: string; | ||
|
||
/** | ||
* The placement of the popover. | ||
*/ | ||
placement?: 'top' | 'right' | 'bottom' | 'left'; | ||
|
||
/** | ||
* The offset of the popover. | ||
*/ | ||
offset?: number; | ||
|
||
/** | ||
* Allows elements outside the popover to be interactive. | ||
*/ | ||
nonmodal?: boolean; | ||
|
||
/** | ||
* Hide the overlay arrow with the popup. | ||
*/ | ||
hideArrow?: boolean; | ||
} |
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 |
---|---|---|
@@ -1,16 +1,18 @@ | ||
import { dialogActions } from '@do-ob/ui/reducer'; | ||
import { use, useCallback } from 'react'; | ||
import { use, useCallback, useRef } from 'react'; | ||
import { DialogDispatchContext } from '@do-ob/ui/context'; | ||
|
||
export function useDialogControlButtonProps(name?: string) { | ||
const triggerRef = useRef<HTMLButtonElement>(null); | ||
const dispatch = use(DialogDispatchContext); | ||
|
||
const onPress = useCallback(() => { | ||
if(!name) return; | ||
dispatch(dialogActions.toggle(name)); | ||
}, [ dispatch, name ]); | ||
dispatch(dialogActions.toggle(name, triggerRef)); | ||
}, [ dispatch, name, triggerRef ]); | ||
|
||
return { | ||
ref: triggerRef, | ||
onPress, | ||
}; | ||
}; |
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,15 @@ | ||
|
||
import { dialogActions } from '@do-ob/ui/reducer'; | ||
import { use, useEffect } from 'react'; | ||
import { DialogDispatchContext } from '@do-ob/ui/context'; | ||
|
||
export function useDialogRegister(id: string) { | ||
const dispatch = use(DialogDispatchContext); | ||
|
||
useEffect(() => { | ||
dispatch(dialogActions.register(id)); | ||
return () => { | ||
dispatch(dialogActions.unregister(id)); | ||
}; | ||
}, [ dispatch, id ]); | ||
}; |
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,9 @@ | ||
import { use } from 'react'; | ||
import { DialogContext } from '@do-ob/ui/context'; | ||
|
||
/** | ||
* Gets the dialog state for the given id | ||
*/ | ||
export function useDialogState(id: string) { | ||
return use(DialogContext).items[id] ?? { id, open: 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,24 @@ | ||
import { useEffect, RefObject } from 'react'; | ||
|
||
export function useOutsidePress<T extends HTMLElement>( | ||
ref: RefObject<T | null>, | ||
handler: (event: MouseEvent | TouchEvent) => void | ||
): void { | ||
useEffect(() => { | ||
const listener = (event: MouseEvent | TouchEvent) => { | ||
// Do nothing if clicking ref's element or its descendants | ||
if (!ref.current || ref.current.contains(event.target as Node)) { | ||
return; | ||
} | ||
handler(event); | ||
}; | ||
|
||
document.addEventListener('mousedown', listener); | ||
document.addEventListener('touchstart', listener); | ||
|
||
return () => { | ||
document.removeEventListener('mousedown', listener); | ||
document.removeEventListener('touchstart', listener); | ||
}; | ||
}, [ ref, handler ]); | ||
} |
Oops, something went wrong.