Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Progress bar with customizable colors #363

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions assets/index.less
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,19 @@
inline-size: 100%;
block-size: 2px;
border: 0;
--progress-color: #31afff;

&,
&::-webkit-progress-bar {
background-color: rgba(0, 0, 0, 0.04);
}

&::-moz-progress-bar {
background-color: #31afff;
background: var(--progress-color);
}

&::-webkit-progress-value {
background-color: #31afff;
background: var(--progress-color);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions docs/examples/showProgress.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ export default () => {
>
Not Pause On Hover
</button>
<button
onClick={() => {
notice.open({
content: `${new Date().toISOString()}`,
progressBarColor: 'yellow',
});
}}
>
Show Progress Bar with custom color
</button>
{contextHolder}
</>
);
Expand Down
16 changes: 15 additions & 1 deletion src/Notice.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
duration = 4.5,
showProgress,
pauseOnHover = true,
progressBarColor,

eventKey,
content,
Expand All @@ -40,6 +41,8 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
const [spentTime, setSpentTime] = React.useState(0);
const mergedHovering = forcedHovering || hovering;
const mergedShowProgress = duration > 0 && showProgress;
const mergedProgressBarColor = mergedShowProgress && progressBarColor;
const progressRef = React.useRef<HTMLProgressElement>(null);

// ======================== Close =========================
const onInternalClose = () => {
Expand Down Expand Up @@ -101,6 +104,12 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [duration, spentTime, mergedHovering, mergedShowProgress, times]);

React.useEffect(() => {
if (mergedProgressBarColor) {
progressRef.current!.style.setProperty('--progress-color', mergedProgressBarColor);
}
}, [mergedProgressBarColor]);

// ======================== Closable ========================
const closableObj = React.useMemo(() => {
if (typeof closable === 'object' && closable !== null) {
Expand Down Expand Up @@ -161,7 +170,12 @@ const Notify = React.forwardRef<HTMLDivElement, NoticeProps & { times?: number }

{/* Progress Bar */}
{mergedShowProgress && (
<progress className={`${noticePrefixCls}-progress`} max="100" value={validPercent}>
<progress
ref={progressRef}
className={`${noticePrefixCls}-progress`}
max="100"
value={validPercent}
>
{validPercent + '%'}
</progress>
)}
Expand Down
1 change: 1 addition & 0 deletions src/hooks/useNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export interface NotificationConfig {
maxCount?: number;
duration?: number;
showProgress?: boolean;
progressBarColor?: string;
pauseOnHover?: boolean;
/** @private. Config for notification holder style. Safe to remove if refactor */
className?: (placement: Placement) => string;
Expand Down
1 change: 1 addition & 0 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface NoticeConfig {
content?: React.ReactNode;
duration?: number | null;
showProgress?: boolean;
progressBarColor?: string;
pauseOnHover?: boolean;
closeIcon?: React.ReactNode;
closable?: boolean | ({ closeIcon?: React.ReactNode } & React.AriaAttributes);
Expand Down
40 changes: 40 additions & 0 deletions tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -849,4 +849,44 @@ describe('Notification.Basic', () => {
expect(document.querySelector('.rc-notification-notice-progress')).toBeFalsy();
});
});

describe('custom progress bar color', () => {
it('should display progress bar with custom color', () => {
const { instance } = renderDemo({
duration: 1,
showProgress: true,
progressBarColor: 'red',
});

act(() => {
instance.open({
content: <p className="test">1</p>,
});
});

expect(document.querySelector('.rc-notification-notice-progress')).toHaveStyle({
'--progress-color': 'red',
});
});

it('should display progress bar with line-gradient color', () => {
const { instance } = renderDemo({
duration: 1,
showProgress: true,
progressBarColor:
'linear-gradient(90deg, #ff0000 0%, #ff0000 50%, #00ff00 50%, #00ff00 100%)',
});

act(() => {
instance.open({
content: <p className="test">1</p>,
});
});

expect(document.querySelector('.rc-notification-notice-progress')).toHaveStyle({
'--progress-color':
'linear-gradient(90deg, #ff0000 0%, #ff0000 50%, #00ff00 50%, #00ff00 100%)',
});
});
});
});
Loading