Detailed technical blog article on using Fluent UI’s Stack component within a SharePoint Framework (SPFx) Web Part. This example uses Fluent UI to create a responsive, full-screen layout with specific regional divisions.
Building Responsive SPFx Web Parts with Fluent UI’s Stack Component
SharePoint Framework (SPFx) development has evolved with support for modern libraries, including Fluent UI. Fluent UI, developed by Microsoft, provides a set of React components optimized for building SharePoint and Office applications, allowing for rapid UI creation while maintaining consistent Office styling. This article will cover the Stack component in Fluent UI, showcasing how to build a SPFx Web Part with complex layout requirements.
Why Fluent UI’s Stack?
The Stack component is highly flexible and is designed to simplify layout management. Instead of handling CSS manually, you can quickly configure Stack to control spacing, alignment, and responsiveness of your components. This is especially useful in SharePoint, where you often need to accommodate various layouts across different screen sizes and configurations.
Example: Building a Full-Screen SPFx Web Part Layout
The following example demonstrates how to use Fluent UI’s Stack component to create a full-screen layout in SPFx with the following structure:
- First Row (25%): Split into two columns, each containing two sub-regions.
- Second Row (50%): Split into two columns.
- Third Row (25%): Divided into a grid layout with two rows and eight columns.
Here’s the complete code for this SPFx Web Part:
import * as React from 'react';
import { Stack, IStackStyles, IStackTokens } from '@fluentui/react';
const CustomLayoutWebPart: React.FC = () => {
// Full-screen layout styles
const fullScreenStackStyles: IStackStyles = {
root: {
width: '100%',
height: '100vh', // Occupies the entire viewport height
boxSizing: 'border-box',
},
};
// Styles for each primary row
const row25Styles: IStackStyles = {
root: {
width: '100%',
height: '25%', // First row takes up 25% height
display: 'flex',
},
};
const row50Styles: IStackStyles = {
root: {
width: '100%',
height: '50%', // Second row takes up 50% height
display: 'flex',
},
};
const row25GridStyles: IStackStyles = {
root: {
width: '100%',
height: '25%', // Third row takes up 25% height
display: 'grid',
gridTemplateColumns: 'repeat(8, 1fr)', // 8 columns
gridTemplateRows: '1fr 1fr', // 2 rows
gap: '2px', // Spacing between items
},
};
// Styles for individual cells
const cellStyles: IStackStyles = {
root: {
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
border: '1px solid #ddd', // Border for visibility
boxSizing: 'border-box',
height: '100%',
width: '100%',
},
};
// Tokens for spacing within each Stack
const stackTokens: IStackTokens = {
childrenGap: 2, // Space between Stack items
};
return (
<Stack styles={fullScreenStackStyles}>
{/* First row: 25% height with 2 main columns, each split into 2 sub-regions */}
<Stack horizontal styles={row25Styles} tokens={stackTokens}>
{/* Region 1.1 with 2 sub-regions */}
<Stack horizontal tokens={stackTokens} styles={cellStyles}>
<Stack styles={cellStyles}><p>Region 1.1.1</p></Stack>
<Stack styles={cellStyles}><p>Region 1.1.2</p></Stack>
</Stack>
{/* Region 1.2 with 2 sub-regions */}
<Stack horizontal tokens={stackTokens} styles={cellStyles}>
<Stack styles={cellStyles}><p>Region 1.2.1</p></Stack>
<Stack styles={cellStyles}><p>Region 1.2.2</p></Stack>
</Stack>
</Stack>
{/* Second row: 50% height with 2 columns */}
<Stack horizontal styles={row50Styles} tokens={stackTokens}>
<Stack styles={cellStyles}><p>Region 2.1</p></Stack>
<Stack styles={cellStyles}><p>Region 2.2</p></Stack>
</Stack>
{/* Third row: 25% height with 2 rows and 8 columns */}
<Stack styles={row25GridStyles} tokens={stackTokens}>
{[...Array(16)].map((_, index) => (
<Stack key={index} styles={cellStyles}>
<p>Region 3.{index + 1}</p>
</Stack>
))}
</Stack>
</Stack>
);
};
export default CustomLayoutWebPart;
Code Explanation
- Full-Screen Layout (
fullScreenStackStyles): The outermostStacktakes the full viewport height usingheight: '100vh'. - Row Layouts (
row25Stylesandrow50Styles): We define individual rows using percentage heights:
- The first row (
row25Styles) is 25% of the height, split into two columns, each subdivided further. - The second row (
row50Styles) is 50% of the height, divided into two equal columns.
- Grid Layout (
row25GridStyles): The third row, taking 25% height, is structured as a grid with two rows and eight columns usinggridTemplateColumnsandgridTemplateRows. - Spacing with
IStackTokens: We useIStackTokensto set thechildrenGapproperty for consistent spacing.
Customizing Stack Properties
- Direction: The
Stackcomponent can arrange items in a row (horizontal) or a column. In this example,horizontalstacks are used for rows and columns. - Alignment: Properties like
alignItemsandjustifyContentcan be set for alignment within each stack. - Gap Control: By using
IStackTokens, you can adjust thechildrenGapbetween elements to ensure a balanced layout.
Cost and Licensing
Fluent UI is open-source, making it free to use within SPFx development projects. Its seamless integration with SharePoint provides a robust solution for developers without additional licensing requirements.
Conclusion
Using Fluent UI’s Stack component with SPFx provides a powerful, efficient way to create responsive layouts with minimal CSS. By leveraging Fluent UI, SPFx developers can create complex and professional layouts that adhere to Microsoft’s design guidelines. This approach reduces the need for extensive custom styling and promotes code reuse, enhancing development speed and maintainability.
For additional information on Fluent UI and SPFx, refer to the official documentation here.
