UNPKG

2.21 kBJavaScriptView Raw
1import classNames from 'classnames';
2import * as React from 'react';
3import { useUncontrolled } from 'uncontrollable';
4import useEventCallback from '@restart/hooks/useEventCallback';
5import Anchor from '@restart/ui/Anchor';
6import { useBootstrapPrefix } from './ThemeProvider';
7import Fade from './Fade';
8import CloseButton from './CloseButton';
9import divWithClassName from './divWithClassName';
10import createWithBsPrefix from './createWithBsPrefix';
11import { jsx as _jsx } from "react/jsx-runtime";
12import { jsxs as _jsxs } from "react/jsx-runtime";
13const DivStyledAsH4 = divWithClassName('h4');
14DivStyledAsH4.displayName = 'DivStyledAsH4';
15const AlertHeading = createWithBsPrefix('alert-heading', {
16 Component: DivStyledAsH4
17});
18const AlertLink = createWithBsPrefix('alert-link', {
19 Component: Anchor
20});
21const defaultProps = {
22 variant: 'primary',
23 show: true,
24 transition: Fade,
25 closeLabel: 'Close alert'
26};
27const Alert = /*#__PURE__*/React.forwardRef((uncontrolledProps, ref) => {
28 const {
29 bsPrefix,
30 show,
31 closeLabel,
32 closeVariant,
33 className,
34 children,
35 variant,
36 onClose,
37 dismissible,
38 transition,
39 ...props
40 } = useUncontrolled(uncontrolledProps, {
41 show: 'onClose'
42 });
43 const prefix = useBootstrapPrefix(bsPrefix, 'alert');
44 const handleClose = useEventCallback(e => {
45 if (onClose) {
46 onClose(false, e);
47 }
48 });
49 const Transition = transition === true ? Fade : transition;
50
51 const alert = /*#__PURE__*/_jsxs("div", {
52 role: "alert",
53 ...(!Transition ? props : undefined),
54 ref: ref,
55 className: classNames(className, prefix, variant && `${prefix}-${variant}`, dismissible && `${prefix}-dismissible`),
56 children: [dismissible && /*#__PURE__*/_jsx(CloseButton, {
57 onClick: handleClose,
58 "aria-label": closeLabel,
59 variant: closeVariant
60 }), children]
61 });
62
63 if (!Transition) return show ? alert : null;
64 return /*#__PURE__*/_jsx(Transition, {
65 unmountOnExit: true,
66 ...props,
67 ref: undefined,
68 in: show,
69 children: alert
70 });
71});
72Alert.displayName = 'Alert';
73Alert.defaultProps = defaultProps;
74export default Object.assign(Alert, {
75 Link: AlertLink,
76 Heading: AlertHeading
77});
\No newline at end of file