-
Notifications
You must be signed in to change notification settings - Fork 21
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
Showing
4 changed files
with
83 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { type AriaToastRegionProps, useToastRegion } from '@react-aria/toast' | ||
import { | ||
cloneElement, | ||
type ComponentPropsWithoutRef, | ||
forwardRef, | ||
type ReactElement, | ||
useRef, | ||
} from 'react' | ||
|
||
import { SnackbarItem, type SnackbarItemProps } from './SnackbarItem' | ||
import { SnackbarItemContext, type SnackbarItemState } from './SnackbarItemContext' | ||
import { snackbarRegionVariant, type SnackbarRegionVariantProps } from './SnackbarRegion.styles' | ||
|
||
export interface SnackbarRegionProps | ||
extends ComponentPropsWithoutRef<'div'>, | ||
AriaToastRegionProps, | ||
SnackbarRegionVariantProps, | ||
Pick<SnackbarItemState, 'state'> { | ||
/** | ||
* An accessibility label for the snackbar region. | ||
* @default 'Notifications' | ||
*/ | ||
'aria-label'?: string | ||
/** | ||
* Identifies the element (or elements) that labels the current element. | ||
*/ | ||
'aria-labelledby'?: string | ||
/** | ||
* Identifies the element (or elements) that describes the object. | ||
*/ | ||
'aria-describedby'?: string | ||
/** | ||
* Identifies the element (or elements) that provide a detailed, extended description for the object. | ||
*/ | ||
'aria-details'?: string | ||
/** | ||
* The component/template used to display each snackbar from the queue | ||
* @default 'Snackbar.Item' | ||
*/ | ||
children?: ReactElement<SnackbarItemProps, typeof SnackbarItem> | ||
} | ||
|
||
export const SnackbarRegion = forwardRef<HTMLDivElement, SnackbarRegionProps>( | ||
( | ||
{ children = <SnackbarItem />, state, position = 'bottom', className, ...rest }, | ||
forwardedRef | ||
): ReactElement => { | ||
const innerRef = useRef<HTMLDivElement>(null) | ||
const ref = forwardedRef && typeof forwardedRef !== 'function' ? forwardedRef : innerRef | ||
|
||
const { regionProps } = useToastRegion(rest, state, ref) | ||
|
||
return ( | ||
<div | ||
{...regionProps} | ||
ref={ref} | ||
data-position={position} | ||
className={snackbarRegionVariant({ position, className })} | ||
> | ||
{state.visibleToasts.map(toast => ( | ||
<SnackbarItemContext.Provider key={toast.key} value={{ toast, state }}> | ||
{cloneElement(children, { key: toast.key })} | ||
</SnackbarItemContext.Provider> | ||
))} | ||
</div> | ||
) | ||
} | ||
) |