Skip to content

Commit

Permalink
feat(Toast): add bg property (#5877)
Browse files Browse the repository at this point in the history
* feat(v5): Added bg property to Toast Component

* added test
  • Loading branch information
golota60 committed Jun 12, 2021
1 parent ec74ef3 commit ff8f1d0
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
BsPrefixRefForwardingComponent,
TransitionComponent,
} from './helpers';
import { Variant } from './types';

export interface ToastProps
extends BsPrefixProps,
Expand All @@ -24,6 +25,7 @@ export interface ToastProps
onClose?: () => void;
show?: boolean;
transition?: TransitionComponent;
bg?: Variant;
}

const propTypes = {
Expand Down Expand Up @@ -61,6 +63,13 @@ const propTypes = {
* A `react-transition-group` Transition component used to animate the Toast on dismissal.
*/
transition: PropTypes.elementType,

/**
* Sets Toast background
*
* @type {('primary'|'secondary'|'success'|'danger'|'warning'|'info'|'dark'|'light')}
*/
bg: PropTypes.string,
};

const Toast: BsPrefixRefForwardingComponent<'div', ToastProps> =
Expand All @@ -75,6 +84,7 @@ const Toast: BsPrefixRefForwardingComponent<'div', ToastProps> =
delay = 5000,
autohide = false,
onClose,
bg,
...props
},
ref,
Expand Down Expand Up @@ -121,6 +131,7 @@ const Toast: BsPrefixRefForwardingComponent<'div', ToastProps> =
className={classNames(
bsPrefix,
className,
bg && `bg-${bg}`,
!hasAnimation && (show ? 'show' : 'hide'),
)}
role="alert"
Expand Down
4 changes: 4 additions & 0 deletions test/ToastSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('<Toast>', () => {
clock.restore();
});

it('should apply bg prop', () => {
mount(<Toast bg="primary">Card</Toast>).assertSingle('.toast.bg-primary');
});

it('should render an entire toast', () => {
mount(
<Toast>
Expand Down
21 changes: 21 additions & 0 deletions www/src/examples/Toast/Contextual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[
'Primary',
'Secondary',
'Success',
'Danger',
'Warning',
'Info',
'Light',
'Dark',
].map((variant, idx) => (
<Toast className="d-inline-block m-1" bg={variant.toLowerCase()} key={idx}>
<Toast.Header>
<img src="holder.js/20x20?text=%20" className="rounded me-2" alt="" />
<strong className="me-auto">Bootstrap</strong>
<small>11 mins ago</small>
</Toast.Header>
<Toast.Body className={variant === 'Dark' && 'text-white'}>
Hello, world! This is a toast message.
</Toast.Body>
</Toast>
));
7 changes: 7 additions & 0 deletions www/src/pages/components/toasts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import ToastStacking from '../../examples/Toast/Stacking';
import ToastPlacement from '../../examples/Toast/Placement';
import ToastPlacementMulti from '../../examples/Toast/PlacementMulti';
import ToastAutohide from '../../examples/Toast/Autohide';
import ToastContextual from '../../examples/Toast/Contextual';

# Toasts

Expand Down Expand Up @@ -55,6 +56,12 @@ A Toast can also automatically hide after X milliseconds. For that, use the `aut

<ReactPlayground codeText={ToastAutohide} />

### Contextual variations

Add any of the below mentioned modifier classes to change the appearance of a toast.

<ReactPlayground codeText={ToastContextual} />

## API

<ComponentApi metadata={props.data.Toast} />
Expand Down

0 comments on commit ff8f1d0

Please sign in to comment.