-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/notification-stack
- Loading branch information
Showing
9 changed files
with
245 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import type { CSSProperties, FC } from 'react'; | ||
import React, { useContext, useRef, useState } from 'react'; | ||
import clsx from 'classnames'; | ||
import type { CSSMotionProps } from 'rc-motion'; | ||
import { CSSMotionList } from 'rc-motion'; | ||
import type { InnerOpenConfig, NoticeConfig, OpenConfig, Placement } from './interface'; | ||
import Notice from './Notice'; | ||
import { NotificationContext } from './NotificationProvider'; | ||
|
||
export interface NoticeListProps { | ||
configList?: OpenConfig[]; | ||
placement?: Placement; | ||
prefixCls?: string; | ||
motion?: CSSMotionProps | ((placement: Placement) => CSSMotionProps); | ||
stack?: | ||
| boolean | ||
| { | ||
threshold?: number; | ||
}; | ||
|
||
// Events | ||
onAllNoticeRemoved?: (placement: Placement) => void; | ||
onNoticeClose?: (key: React.Key) => void; | ||
|
||
// Common | ||
className?: string; | ||
style?: CSSProperties; | ||
} | ||
|
||
const NoticeList: FC<NoticeListProps> = (props) => { | ||
const { | ||
configList, | ||
placement, | ||
prefixCls, | ||
className, | ||
style, | ||
motion, | ||
onAllNoticeRemoved, | ||
onNoticeClose, | ||
stack, | ||
} = props; | ||
|
||
const { classNames: ctxCls } = useContext(NotificationContext); | ||
|
||
const listRef = useRef<HTMLDivElement[]>([]); | ||
const [latestNotice, setLatestNotice] = useState<HTMLDivElement>(null); | ||
const [hoverCount, setHoverCount] = useState(0); | ||
|
||
const keys = configList.map((config) => ({ | ||
config, | ||
key: config.key, | ||
})); | ||
|
||
const expanded = | ||
!!stack && | ||
(hoverCount > 0 || | ||
keys.length <= (typeof stack === 'object' && 'threshold' in stack ? stack.threshold : 3)); | ||
|
||
const placementMotion = typeof motion === 'function' ? motion(placement) : motion; | ||
|
||
return ( | ||
<CSSMotionList | ||
key={placement} | ||
className={clsx(prefixCls, `${prefixCls}-${placement}`, ctxCls?.list, className)} | ||
style={style} | ||
keys={keys} | ||
motionAppear | ||
{...placementMotion} | ||
onAllRemoved={() => { | ||
onAllNoticeRemoved(placement); | ||
}} | ||
onAppearPrepare={async (element) => { | ||
if (element.parentNode.lastElementChild === element) { | ||
setLatestNotice(element as HTMLDivElement); | ||
} | ||
}} | ||
> | ||
{({ config, className: motionClassName, style: motionStyle }, nodeRef) => { | ||
const { key, times } = config as InnerOpenConfig; | ||
const { className: configClassName, style: configStyle } = config as NoticeConfig; | ||
|
||
const index = keys.length - 1 - keys.findIndex((item) => item.key === key); | ||
const stackStyle: CSSProperties = {}; | ||
if (stack) { | ||
if (index > 0) { | ||
stackStyle.height = expanded ? '' : latestNotice.offsetHeight; | ||
stackStyle.transform = `translateY(${ | ||
index * 8 + | ||
(expanded | ||
? listRef.current.reduce( | ||
(acc, item, refIndex) => acc + (refIndex < index ? item.offsetHeight : 0), | ||
0, | ||
) | ||
: 0) | ||
}px)`; | ||
} | ||
} | ||
|
||
return ( | ||
<Notice | ||
{...config} | ||
ref={(node) => { | ||
nodeRef(node); | ||
listRef.current[index] = node; | ||
}} | ||
prefixCls={prefixCls} | ||
className={clsx(motionClassName, configClassName, ctxCls?.notice)} | ||
style={{ | ||
...motionStyle, | ||
...configStyle, | ||
...stackStyle, | ||
}} | ||
times={times} | ||
key={key} | ||
eventKey={key} | ||
onNoticeClose={onNoticeClose} | ||
props={{ | ||
onMouseEnter: () => setHoverCount((c) => c + 1), | ||
onMouseLeave: () => setHoverCount((c) => c - 1), | ||
}} | ||
/> | ||
); | ||
}} | ||
</CSSMotionList> | ||
); | ||
}; | ||
|
||
if (process.env.NODE_ENV !== 'production') { | ||
NoticeList.displayName = 'NoticeList'; | ||
} | ||
|
||
export default NoticeList; |
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,23 @@ | ||
import type { FC } from 'react'; | ||
import React from 'react'; | ||
|
||
export interface NotificationContextProps { | ||
classNames?: { | ||
notice?: string; | ||
list?: string; | ||
}; | ||
} | ||
|
||
export const NotificationContext = React.createContext<NotificationContextProps>({}); | ||
|
||
export interface NotificationProviderProps extends NotificationContextProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
const NotificationProvider: FC<NotificationProviderProps> = ({ children, classNames }) => { | ||
return ( | ||
<NotificationContext.Provider value={{ classNames }}>{children}</NotificationContext.Provider> | ||
); | ||
}; | ||
|
||
export default NotificationProvider; |
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,6 +1,7 @@ | ||
import useNotification from './useNotification'; | ||
import Notice from './Notice'; | ||
import type { NotificationAPI, NotificationConfig } from './useNotification'; | ||
import NotificationProvider from './NotificationProvider'; | ||
|
||
export { useNotification, Notice }; | ||
export { useNotification, Notice, NotificationProvider }; | ||
export type { NotificationAPI, NotificationConfig }; |
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,28 @@ | ||
import type React from 'react'; | ||
|
||
export type Placement = 'top' | 'topLeft' | 'topRight' | 'bottom' | 'bottomLeft' | 'bottomRight'; | ||
|
||
export interface NoticeConfig { | ||
content?: React.ReactNode; | ||
duration?: number | null; | ||
closeIcon?: React.ReactNode; | ||
closable?: boolean; | ||
className?: string; | ||
style?: React.CSSProperties; | ||
/** @private Internal usage. Do not override in your code */ | ||
props?: React.HTMLAttributes<HTMLDivElement> & Record<string, any>; | ||
|
||
onClose?: VoidFunction; | ||
onClick?: React.MouseEventHandler<HTMLDivElement>; | ||
} | ||
|
||
export interface OpenConfig extends NoticeConfig { | ||
key: React.Key; | ||
placement?: Placement; | ||
content?: React.ReactNode; | ||
duration?: number | null; | ||
} | ||
|
||
export type InnerOpenConfig = OpenConfig & { times?: number }; | ||
|
||
export type Placements = Partial<Record<Placement, OpenConfig[]>>; |
Oops, something went wrong.