Shards React
DocumentationThe alert component can be used to display contextual user messages.
Alerts come in various contextual theme colors.
import React from "react";
import { Alert } from "shards-react";
export default function AlertExample() {
return (
<div className="example">
<Alert theme="primary">
Alert - Primary Theme (default) -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="secondary">
Alert - Secondary Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="success">
Alert - Success Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="danger">
Alert - Danger Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="warning">
Alert - Warning Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="info">
Alert - Info Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="light">
Alert - Light Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
<Alert theme="dark">
Alert - Dark Theme -{" "}
<a className="alert-link" href="#">
Example Link
</a>
</Alert>
</div>
);
}
Dismissible alerts allow you to hide them using an X
button.
import React from "react";
import { Alert } from "shards-react";
export default class DismissibleAlertExample extends React.Component {
constructor(props) {
super(props);
this.dismiss = this.dismiss.bind(this);
this.state = { visible: true };
}
render() {
return (
<Alert dismissible={this.dismiss} open={this.state.visible}>
You can easily dismiss me using the <strong>close</strong> button →
</Alert>
);
}
dismiss() {
this.setState({ visible: false });
}
}
Self-dismissible alerts can also be created using a few state tricks.
import React from "react";
import { Alert, Button } from "shards-react";
export default class SelfDismissingAlertExample extends React.Component {
constructor(props) {
super(props);
this.interval = null;
this.state = {
visible: false,
countdown: 0,
timeUntilDismissed: 5
};
this.showAlert = this.showAlert.bind(this);
this.handleTimeChange = this.handleTimeChange.bind(this);
this.clearInterval = this.clearInterval.bind(this);
}
showAlert() {
this.clearInterval();
this.setState({ visible: true, countdown: 0, timeUntilDismissed: 5 });
this.interval = setInterval(this.handleTimeChange, 1000);
}
handleTimeChange() {
if (this.state.countdown < this.state.timeUntilDismissed - 1) {
this.setState({
...this.state,
...{ countdown: this.state.countdown + 1 }
});
return;
}
this.setState({ ...this.state, ...{ visible: false } });
this.clearInterval();
}
clearInterval() {
clearInterval(this.interval);
this.interval = null;
}
render() {
return (
<div>
<Alert className="mb-3" open={this.state.visible} theme="success">
Success! This alert will will be dismissed in{" "}
{this.state.timeUntilDismissed - this.state.countdown} seconds!
</Alert>
<Button onClick={this.showAlert}>Show Alert!</Button>
</div>
);
}
}
The following props are available for the Alert
component.
Prop | Description | Type | Default | Required |
---|---|---|---|---|
children | The children nodes. | Node | - | No |
className | The class name. | String | - | No |
closeClassName | The close button's class name. | String | - | No |
closeAriaLabel | The close button's aria label. | String | "Close" | No |
theme | The theme color. | String | "primary" | No |
fade | Whether it should fade, or not. | Bool | true | No |
open | Whether is open, or not. | Bool | true | No |
dismissible | Whether is dismissible, or not. | Func | - | No |
transition | The transition config. See `Fade` component for more details. | Shape | {
...Fade.defaultProps,
unmountOnExit: true
} | No |
tag | The component tag type. | Func String | "div" | No |