This article provides steps to implement the Fluent UI Message bar in the SharePoint Framework (SPFx) web part, generally a MessageBar is an area at the top of a primary view that displays relevant status information. You can use a MessageBar to tell the user about a situation that does not require their immediate attention and therefore does not need to block other activities.

Create a new web part project
Open power shell and run following comment to create a new web part by running the Yeoman SharePoint Generator
yo @microsoft/sharepoint
When prompted:
Enter the webpart name as your solution name, and then select Enter.
Select Create a subfolder with solution name for where to place the files.
Select Y to allow the solution to be deployed to all sites immediately.
Select N on the question if solution contains unique permissions.
Select WebPart as the client-side component type to be created.
The next set of prompts ask for specific information about your web part:
Enter your web part name, and then select Enter.
Enter your web part description, and then select Enter.
Select React framework as the framework you would like to use, and then select Enter.
Start Visual Studio Code (or your favorite code editor) within the context of the newly created project folder.
cd .\web part name\
code .
Configure the custom properties
Create a new source code file under the src\webparts\<Webpart name>\components\ folder of the solution. Create the new file I<web part name>State.ts and use it to create a TypeScript Interface
export interface ISpfxFluentuiMessagebarState {
InfoMessage: boolean;
ErrorMessage:boolean;
AccessMessage:boolean;
WarningMessage:boolean;
SuccessQuestion:boolean;
WarningQuestion:boolean;
WarningLongMessage:boolean;
}
Update the tsx file under the components. First, add some import statements to import the types you defined earlier. Notice the import for I<web part name>Props and I<web part name>State. There are also some imports for the PnP components used to render the UI of the PnP React component and pnp sp imports.
import * as React from 'react';
import styles from './SpfxFluentuiMessagebar.module.scss';
import { ISpfxFluentuiMessagebarProps } from './ISpfxFluentuiMessagebarProps';
import { ISpfxFluentuiMessagebarState } from './ISpfxFluentuiMessagebarState';
import {
MessageBarButton,
Link,
Stack,
MessageBar,
MessageBarType,
DefaultButton, PrimaryButton, IStackTokens
} from 'office-ui-fabric-react';
import { autobind } from 'office-ui-fabric-react/lib/Utilities';
Replace this render function with the following code.
public render(): React.ReactElement<ISpfxFluentuiMessagebarProps> {
return (
<div className={styles.spfxFluentuiMessagebar}>
{(this.state.InfoMessage) ? <div><InfoMessage /><br /></div> : ''}
{(this.state.ErrorMessage) ? <div><ErrorMessage /><br /></div> : ''}
{(this.state.AccessMessage) ? <div><AccessMessage /><br /></div> : ''}
{(this.state.WarningMessage) ? <div><WarningMessage /><br /></div> : ''}
{(this.state.SuccessQuestion) ? <div><SuccessQuestion /><br /></div> : ''}
{(this.state.WarningQuestion) ? <div><WarningQuestion /><br /></div> : ''}
{(this.state.WarningLongMessage) ? <div><WarningLongMessage /><br /></div> : ''}
<br />
<br />
<Stack horizontal tokens={stackTokens}>
<PrimaryButton text="Show Info Message" onClick={() => this._showMessageClicked('InfoMessage')} />
<PrimaryButton text="Show Error Message" onClick={() => this._showMessageClicked('ErrorMessage')} />
<PrimaryButton text="Show Access Message" onClick={() => this._showMessageClicked('AccessMessage')} />
</Stack>
<br />
<br />
<Stack horizontal tokens={stackTokens}>
<PrimaryButton text="Show Warning Message" onClick={() => this._showMessageClicked('WarningMessage')} />
<PrimaryButton text="Show Success with Question Message" onClick={() => this._showMessageClicked('SuccessQuestion')} />
<PrimaryButton text="Show Warning with Question Message" onClick={() => this._showMessageClicked('WarningQuestion')} />
</Stack>
<br />
<br />
<Stack horizontal tokens={stackTokens}>
<PrimaryButton text="Show Long Message" onClick={() => this._showMessageClicked('WarningLongMessage')} />
<PrimaryButton text="Show info message and hide after 5 sec" onClick={this._showandhideMessageClicked} />
<DefaultButton text="Hide All Message" onClick={this._hideMessageClicked} />
</Stack>
</div>
);
}
Update the React component type declaration and add a constructor, as shown in the following example.
export default class SpfxFluentuiMessagebar extends React.Component<ISpfxFluentuiMessagebarProps, ISpfxFluentuiMessagebarState> {
constructor(props: ISpfxFluentuiMessagebarProps, state: ISpfxFluentuiMessagebarState) {
super(props);
this.state = ({
InfoMessage: false,
ErrorMessage: false,
AccessMessage: false,
WarningMessage: false,
SuccessQuestion: false,
WarningQuestion: false,
WarningLongMessage: false,
})
}
place the below code inside the react component class, these functions for message bar the events
@autobind
private _showMessageClicked(messagetype: string): void {
if (messagetype == 'InfoMessage')
this.setState({ InfoMessage: true })
else if (messagetype == 'ErrorMessage')
this.setState({ ErrorMessage: true })
else if (messagetype == 'AccessMessage')
this.setState({ AccessMessage: true })
else if (messagetype == 'WarningMessage')
this.setState({ WarningMessage: true })
else if (messagetype == 'SuccessQuestion')
this.setState({ SuccessQuestion: true })
else if (messagetype == 'WarningQuestion')
this.setState({ WarningQuestion: true })
else if (messagetype == 'WarningLongMessage')
this.setState({ WarningLongMessage: true })
}
@autobind
private _hideMessageClicked(): void {
this.setState({
InfoMessage: false, ErrorMessage: false, AccessMessage: false, WarningMessage: false,
SuccessQuestion: false, WarningQuestion: false, WarningLongMessage: false
})
}
@autobind
private _showandhideMessageClicked(): void {
this.setState({ InfoMessage: true })
setTimeout(function () {
this.setState({ InfoMessage: false })
}.bind(this), 5000);
}
These all have to next to imports, but inside the react component class
const InfoMessage = () => (
<MessageBar>
Info/Default MessageBar.
<Link href="www.bing.com" target="_blank">
Visit our website.
</Link>
</MessageBar>
);
const ErrorMessage = () => (
<MessageBar
messageBarType={MessageBarType.error}
isMultiline={false}
dismissButtonAriaLabel="Close"
>
Error MessageBar with single line, with dismiss button.
<Link href="www.bing.com" target="_blank">
Visit our website.
</Link>
</MessageBar>
);
const AccessMessage = () => (
<MessageBar
messageBarType={MessageBarType.blocked}
isMultiline={false}
dismissButtonAriaLabel="Close"
truncated={true}
overflowButtonAriaLabel="See more"
>
<b>Blocked MessageBar - single line, with dismiss button and truncated text.</b> Truncation is not available if you
use action buttons or multiline and should be used sparingly. Lorem ipsum dolor sit amet, consectetur adipiscing
elit. Morbi luctus, purus a lobortis tristique, odio augue pharetra metus, ac placerat nunc mi nec dui. Vestibulum
aliquam et nunc semper scelerisque. Curabitur vitae orci nec quam condimentum porttitor et sed lacus. Vivamus ac
efficitur leo. Cras faucibus mauris libero, ac placerat erat euismod et. Donec pulvinar commodo odio sit amet
faucibus. In hac habitasse platea dictumst. Duis eu ante commodo, condimentum nibh pellentesque, laoreet enim. Fusce
massa lorem, ultrices eu mi a, fermentum suscipit magna. Integer porta purus pulvinar, hendrerit felis eget,
condimentum mauris.
</MessageBar>
);
const WarningMessage = () => (
<MessageBar
messageBarType={MessageBarType.severeWarning}
actions={
<div>
<MessageBarButton onClick={() => console.log('Yes clicked')}>Yes</MessageBarButton>
<MessageBarButton onClick={() => console.log('No clicked')}>No</MessageBarButton>
</div>
}
>
SevereWarning MessageBar with action buttons which defaults to multiline.
<Link href="www.bing.com" target="_blank">
Visit our website.
</Link>
</MessageBar>
);
const SuccessQuestion = () => (
<MessageBar
actions={
<div>
<MessageBarButton onClick={() => console.log('Yes clicked')}>Yes</MessageBarButton>
<MessageBarButton onClick={() => console.log('No clicked')}>No</MessageBarButton>
</div>
}
messageBarType={MessageBarType.success}
isMultiline={false}
>
Success MessageBar with single line and action buttons.
<Link href="www.bing.com" target="_blank">
Visit our website.
</Link>
</MessageBar>
);
const WarningQuestion = () => (
<MessageBar
messageBarType={MessageBarType.warning}
isMultiline={false}
dismissButtonAriaLabel="Close"
actions={
<div>
<MessageBarButton onClick={() => console.log('Action clicked')}>Action</MessageBarButton>
</div>
}
>
Warning MessageBar content.
<Link href="www.bing.com" target="_blank">
Visit our website.
</Link>
</MessageBar>
);
const WarningLongMessage = () => (
<MessageBar
dismissButtonAriaLabel="Close"
messageBarType={MessageBarType.warning}
actions={
<div>
<MessageBarButton>Yes</MessageBarButton>
<MessageBarButton>No</MessageBarButton>
</div>
}
>
<b>Warning defaults to multiline</b>. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi luctus, purus a
lobortis tristique, odio augue pharetra metus, ac placerat nunc mi nec dui. Vestibulum aliquam et nunc semper
scelerisque. Curabitur vitae orci nec quam condimentum porttitor et sed lacus. Vivamus ac efficitur leo. Cras
faucibus mauris libero, ac placerat erat euismod et. Donec pulvinar commodo odio sit amet faucibus. In hac habitasse
platea dictumst. Duis eu ante commodo, condimentum nibh pellentesque, laoreet enim. Fusce massa lorem, ultrices eu
mi a, fermentum suscipit magna. Integer porta purus pulvinar, hendrerit felis eget, condimentum mauris.
<Link href="www.bing.com" target="_blank">
Visit our website.
</Link>
</MessageBar>
);
const stackTokens: IStackTokens = { childrenGap: 40 };
Deploy the solution
You’re now ready to build, bundle, package, and deploy the solution.
Run the gulp commands to verify that the solution builds correctly.
gulp build
Use the following command to bundle and package the solution.
gulp bundle --ship
gulp package-solution --ship
Browse to the app catalog of your target tenant and upload the solution package. You can find the solution package under the sharepoint/solution folder of your solution. It is the .sppkg file. After you upload the solution package in the app catalog. you can find and the web part anywhere across the tenant.
Sharing is caring!
If you have any questions, feel free to let me know in the comments section.
Happy coding!!!