Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ props details:
<thead>
<tr>
<th style="width: 100px;">name</th>
<th style="width: 50px;">type</th>
<th style="width: 200px;">type</th>
<th style="width: 50px;">default</th>
<th>description</th>
</tr>
Expand Down Expand Up @@ -129,9 +129,9 @@ props details:
</tr>
<tr>
<td>duration</td>
<td>number</td>
<td>1.5</td>
<td>after duration of time, this notice will disappear.(seconds)</td>
<td>number | false</td>
<td>4.5</td>
<td>The delay for automatic closing in seconds. Set to 0 or false to not close automatically.</td>
</tr>
<tr>
<td>showProgress</td>
Expand Down
13 changes: 7 additions & 6 deletions src/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
const [percent, setPercent] = React.useState(0);
const [spentTime, setSpentTime] = React.useState(0);
const mergedHovering = forcedHovering || hovering;
const mergedShowProgress = duration > 0 && showProgress;
const mergedDuration: number = typeof duration === 'number' ? duration : 0;
const mergedShowProgress = mergedDuration > 0 && showProgress;

// ======================== Close =========================
const onInternalClose = () => {
Expand All @@ -53,13 +54,13 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }

// ======================== Effect ========================
React.useEffect(() => {
if (!mergedHovering && duration > 0) {
if (!mergedHovering && mergedDuration > 0) {
const start = Date.now() - spentTime;
const timeout = setTimeout(
() => {
onInternalClose();
},
duration * 1000 - spentTime,
mergedDuration * 1000 - spentTime,
);

return () => {
Expand All @@ -70,7 +71,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [duration, mergedHovering, times]);
}, [mergedDuration, mergedHovering, times]);

React.useEffect(() => {
if (!mergedHovering && mergedShowProgress && (pauseOnHover || spentTime === 0)) {
Expand All @@ -81,7 +82,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
cancelAnimationFrame(animationFrame);
animationFrame = requestAnimationFrame((timestamp) => {
const runtime = timestamp + spentTime - start;
const progress = Math.min(runtime / (duration * 1000), 1);
const progress = Math.min(runtime / (mergedDuration * 1000), 1);
setPercent(progress * 100);
if (progress < 1) {
calculate();
Expand All @@ -98,7 +99,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
};
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [duration, spentTime, mergedHovering, mergedShowProgress, times]);
}, [mergedDuration, spentTime, mergedHovering, mergedShowProgress, times]);

// ======================== Closable ========================
const closableObj = React.useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface NotificationConfig {
| boolean
| ({ closeIcon?: React.ReactNode; onClose?: VoidFunction } & React.AriaAttributes);
maxCount?: number;
duration?: number;
duration?: number | false | null;
showProgress?: boolean;
pauseOnHover?: boolean;
/** @private. Config for notification holder style. Safe to remove if refactor */
Expand Down
4 changes: 2 additions & 2 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type NoticeSemanticProps = 'wrapper';

export interface NoticeConfig {
content?: React.ReactNode;
duration?: number | null;
duration?: number | false | null;
showProgress?: boolean;
pauseOnHover?: boolean;

Expand All @@ -32,7 +32,7 @@ export interface OpenConfig extends NoticeConfig {
key: React.Key;
placement?: Placement;
content?: React.ReactNode;
duration?: number | null;
duration?: number | false | null;
}

export type InnerOpenConfig = OpenConfig & { times?: number };
Expand Down
8 changes: 4 additions & 4 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ describe('Notification.Basic', () => {
</p>
),
key,
duration: null,
duration: false,
});
});

Expand Down Expand Up @@ -192,7 +192,7 @@ describe('Notification.Basic', () => {
{notUpdatableValue}
</p>
),
duration: null,
duration: false,
});
});

Expand All @@ -204,7 +204,7 @@ describe('Notification.Basic', () => {
</p>
),
key,
duration: null,
duration: false,
});
});

Expand Down Expand Up @@ -490,7 +490,7 @@ describe('Notification.Basic', () => {
</p>
),
key,
duration: null,
duration: false,
onClick: () => {
clicked += 1;
},
Expand Down
4 changes: 2 additions & 2 deletions tests/stack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('stack', () => {
onClick={() => {
api.open({
content: <div className="context-content">Test</div>,
duration: null,
duration: false,
});
}}
/>
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('stack', () => {
onClick={() => {
api.open({
content: <div className="context-content">Test</div>,
duration: null,
duration: false,
closable: true,
});
}}
Expand Down