Skip to content

Commit 1deb041

Browse files
committed
refactor(CToaster): rename onDismiss to onClose, improve transition
1 parent 9747440 commit 1deb041

File tree

1 file changed

+29
-23
lines changed

1 file changed

+29
-23
lines changed

src/components/alert/CAlert.tsx

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { forwardRef, HTMLAttributes, useState } from 'react'
1+
import React, { forwardRef, HTMLAttributes, useEffect, useState } from 'react'
22
import PropTypes from 'prop-types'
33
import classNames from 'classnames'
44
import { CSSTransition } from 'react-transition-group'
@@ -22,13 +22,9 @@ export interface CAlertProps extends HTMLAttributes<HTMLDivElement> {
2222
*/
2323
dismissible?: boolean
2424
/**
25-
* Method called before the dissmiss animation has started.
25+
* Callback fired when the component requests to be closed.
2626
*/
27-
onDismiss?: () => void
28-
/**
29-
* Method called after the dissmiss animation has completed and the component is removed from the dom.
30-
*/
31-
onDismissed?: () => void
27+
onClose?: () => void
3228
/**
3329
* Set the alert variant to a solid.
3430
*/
@@ -48,36 +44,47 @@ export const CAlert = forwardRef<HTMLDivElement, CAlertProps>(
4844
dismissible,
4945
variant,
5046
visible = true,
51-
onDismiss,
52-
onDismissed,
47+
onClose,
5348
...rest
5449
},
5550
ref,
5651
) => {
5752
const [_visible, setVisible] = useState(visible)
5853

54+
useEffect(() => {
55+
setVisible(visible)
56+
}, [visible])
57+
5958
const _className = classNames(
6059
'alert',
6160
variant === 'solid' ? `bg-${color} text-white` : `alert-${color}`,
6261
{
6362
'alert-dismissible fade': dismissible,
64-
show: _visible && dismissible,
63+
// show: _visible,
6564
},
6665
className,
6766
)
6867

68+
const getTransitionClass = (state: string) => {
69+
return state === 'entered' && 'show'
70+
}
71+
6972
return (
70-
<CSSTransition
71-
in={_visible}
72-
timeout={150}
73-
onExit={onDismiss}
74-
onExited={onDismissed}
75-
unmountOnExit
76-
>
77-
<div className={_className} role="alert" {...rest} ref={ref}>
78-
{children}
79-
{dismissible && <CCloseButton onClick={() => setVisible(false)} />}
80-
</div>
73+
<CSSTransition in={_visible} timeout={150} onExit={onClose} mountOnEnter unmountOnExit>
74+
{(state) => {
75+
const transitionClass = getTransitionClass(state)
76+
return (
77+
<div
78+
className={classNames(_className, transitionClass)}
79+
role="alert"
80+
{...rest}
81+
ref={ref}
82+
>
83+
{children}
84+
{dismissible && <CCloseButton onClick={() => setVisible(false)} />}
85+
</div>
86+
)
87+
}}
8188
</CSSTransition>
8289
)
8390
},
@@ -88,8 +95,7 @@ CAlert.propTypes = {
8895
className: PropTypes.string,
8996
color: colorPropType.isRequired,
9097
dismissible: PropTypes.bool,
91-
onDismiss: PropTypes.func,
92-
onDismissed: PropTypes.func,
98+
onClose: PropTypes.func,
9399
variant: PropTypes.string,
94100
visible: PropTypes.bool,
95101
}

0 commit comments

Comments
 (0)