Why Site Header Toggler?
In SharePoint Modern Sites, the Suite Bar and the Site Header often occupy a lot of the viewport, and the actual content for a user to read is not immediately visible. Also as the Site Header is always positioned at the top, even though users scroll through the content, the viewport is much less. The Site Header occupies a significant amount in the viewport which really doesn't show any content of the intranet.
A Site Header Toggler would allow the users to hide the Site Header and gives more space to content which is easily readable. And users can always see the Site Header by toggling it again. Here is a sample of how it would work.
The Solution
This needs 2 buttons, one to close the header and one to open the header. And some CSS to hide/show the Site Header. The tricky part here is to define the CSS class to hide the Site Header section. The Site Header section on the modern pages doesn't have a CSS class name which we can override, and it is recommended to not change the DOM elements of a SharePoint Modern page. Here is a modern CSS construct which allows us to create a CSS definition based on the HTML element's custom attributes.
- // Sample HTML of the outer element of a Site Header
- <div class="mainHeader-48" data-automationid="SiteHeader">
Css class definition using the custom attribute,
- div[data-automationid=SiteHeader]{
- display: none;
- }
The toggle buttons when clicked will add/remove a CSS class to the body element of the page, using which we can define CSS to show/hide the header.
Developing the SPFx Application Customizer
An Application Customizer of type of SPFx Extension adds some custom HTML in the top placeholder, where the HTML for showing toggling buttons can be added to every modern page on a SharePoint site.
STEP 1 - CREATE THE PROJECT
- Create a project with the name - 'js-application-header-toggler'
- Open a command prompt in the new folder
- Run 'yo @microsoft/sharepoint'
- Provide the necessary values for the yeoman generator as shown below,
- What is your solution name? js-application-header-toggler
- Which baseline packages do you want to target for your component(s)? SharePoint Online only (latest)
- Where do you want to place the files? Use the current folder
- Do you want to allow the tenant admin the choice of being able to deploy the solution to all sites immediately without running any feature deployment or adding apps in sites? No
- Will the components in the solution require permissions to access web APIs that are unique and not shared with other components in the tenant? No
- Which type of client-side component to create? Extension
- Which type of client-side extension to create? Application Customizer
- What is your Application Customizer name? HeaderToggler
- What is your Application Customizer description? Adds a toggle button to the Site Header
STEP 2 - DEVELOP THE APPLICATION CUSTOMIZER
Open the ' js-application-header-toggler/src/extensions/headerToggler/HeaderTogglerApplicationCustomizer.ts' file and make sure you have the following code.
- import { override } from '@microsoft/decorators';
- import { Log } from '@microsoft/sp-core-library';
- import {
- BaseApplicationCustomizer, ApplicationCustomizerContext,
- PlaceholderContent, PlaceholderName
- } from '@microsoft/sp-application-base';
-
- import * as strings from 'HeaderTogglerApplicationCustomizerStrings';
- require('./HeaderTogglerStyles.css');
-
- const LOG_SOURCE: string = 'HeaderTogglerApplicationCustomizer';
-
-
-
-
-
-
- export interface IHeaderTogglerApplicationCustomizerProperties {
-
- testMessage: string;
- }
-
-
- export default class HeaderTogglerApplicationCustomizer
- extends BaseApplicationCustomizer<IHeaderTogglerApplicationCustomizerProperties> {
-
- private topPlaceHolder: PlaceholderContent | undefined;
- private appContext: ApplicationCustomizerContext = null;
-
- @override
- public onInit(): Promise<void> {
- Log.info(LOG_SOURCE, `Initialized ${strings.Title}`);
- this.appContext = this.context;
-
- this.context.placeholderProvider.changedEvent.add(this, this.renderPlaceholders);
-
- this.registerClickHandlers();
-
- return Promise.resolve();
- }
-
- private renderPlaceholders(){
- if (!this.topPlaceHolder) {
- this.topPlaceHolder = this.appContext.placeholderProvider.tryCreateContent(PlaceholderName.Top, { onDispose: this.onDispose });
- }
-
- if (this.topPlaceHolder.domElement) {
- let html: string = `<div class="toggleButton o365cs-base">
- <a class='toggleButtonClose' href='javascript:void(0);'>
- <span class="ms-Icon--ChevronUpMed _2e1Nb05SSaoOLYsNgNyvkr" role="presentation" style="display: inline-block;font-size: 20px;color: #fff;font-weight: bold;line-height: 30px;"></span>
- </a>
- <a class='toggleButtonOpen' href='javascript:void(0);'>
- <span class="ms-Icon--ChevronDownMed _2e1Nb05SSaoOLYsNgNyvkr" role="presentation" style="display: inline-block;font-size: 20px;color: #fff;font-weight: bold;line-height: 30px;"></span>
- </a>
- </div>`;
- this.topPlaceHolder.domElement.innerHTML = html;
- }
- }
-
- private registerClickHandlers(){
- let btnClose = document.getElementsByClassName("toggleButtonClose")[0];
- let btnOpen = document.getElementsByClassName("toggleButtonOpen")[0];
-
- btnClose.addEventListener('click', (e) => {
- document.body.classList.add("menuClosed");
- });
-
- btnOpen.addEventListener('click', (e) => {
- document.body.classList.remove("menuClosed");
- });
- }
- }
Add a CSS file named HeaderTogglerStyles.css in the folder js-application-header-toggler/src/extensions/headerToggler/. Copy the following content to this file.
- .toggleButton{
- background: #666;
- position: absolute;
- z-index: 9999;
- width: 30px;
- height: 30px;
- right: 0px;
- font-size: 0px;
- text-align: center;
- }
-
- a.toggleButtonOpen{
- display:none;
- }
-
- body.menuClosed a.toggleButtonOpen{
- display:inline-block !important;
- }
- body.menuClosed a.toggleButtonClose{
- display:none !important;
- }
-
- body.menuClosed div[data-automationid='SiteHeader']{
- display:none !important;
- }
STEP 3 - BUILD, TEST AND DEPLOY
- Update the SharePoint site URL in the 'js-application-header-toggler/config/serve.json' file
- In the command prompt run 'gulp serve'. This should open your SharePoint site with the SPFx extension from your local workbench
Follow the below steps to publish and deploy the SPFx solution,
- In the comment prompt run the following command,
- npm run build
- npm run bundle --ship
- npm run package-solution --ship
- Upload the generated sppkg file to your App Catalog
- Add the app to your SharePoint modern site.
Conclusion
By providing a Site Header Toggler users can hide the header to create more reading space on the SharePoint modern pages and toggle the header anytime to make it visible again. It's a simple solution which would be very useful for creating a better viewport for the main content on the modern intranet pages.
This SPFx project is also available on
GitHub.