Introduction

This post will demonstrate another SharePoint UI hack that is typically seen on many modern sites today. Back to top is a button that will position itself on the right hand bottom corner of the screen once you scroll a bit down. It’s very useful on long pages where your users might want to scroll up quickly and not get mouse scroll rash on their fingers in the process. This is another client request that I hear infrequently but I found this as something that the general population can implement and learn from.

Before you continue, I must mention that SharePoint UI hacks are unsupported and frowned upon by Microsoft as we cannot rely on the UI [html, classes and DOM] to remain the same over time. With that said you are free to implement and learn more about SPFX and the current DOM given this example. When Microsoft implements a change this would potentially stop working and that is all. Post an issue on github and I would be happy to review and make a change.

Overview

I’m not going to go into the nitty gritty details of the code in this article, you can look at the code for that. This customization is based on an SPFX application customizer. This application customizer injects a button when the users scrolls past a certain point. The button would then jump the user back to the top of the scroll region.

The scroll region might change at some point for SharePoint, rendering this customization ineffective.

Implementation

In the SPFX application customizer there is some code to detect the scroll event. There is also some code to detect the page navigation event as it’s somewhat tricky. When users navigate away from a page application customizers are not re-rendered. There is also some code to detect a navigation event and refresh the scroll area so that this SharePoint UI hack continues to work.

The current scroll region is defined in the BackToTop.tsx. The current scroll region that is the target can be seen below.

The customization is also theme aware and will respect the primary color of the theme.

Installation

As with many github solution you can always clone the repo and make any changes you desire to the solution, check out the REPO. Alternatively you can take the latest build and install it into your app catalog.

Once you install the solution into your app catalog it will be available as an App on your site. It’s as simple as that.

Summary

I hope you enjoy this, yet another SharePoint UI Hack. It’s a great little enhancement if you have long content and want to scroll back up to the top of the page quickly. Feel free to use the github code to inspect how it’s implemented, make changes or enhancements or just implement it as the package is available for you. I’ll mention again that SharePoint UI hacks are not supported by Microsoft so don’t ask them for support. If you implement this you should always remove it before troubleshooting other issues. Generally it’s safe to apply as it won’t adjust or change anything permanently within your site, but take note that if there is a change with the SharePoint UI it could stop working as some point in the future. Please feel free to reach out or comment if your feedback or experience.

Comments
  • Dave Johnson
    Posted at 4:30 am August 18, 2022
    Dave Johnson
    Reply
    Author

    This is really good. Just a few notes to ask.
    Are there ways to customize the icon. Also when you click it is there a way to make go slowly rather than directly on top.

    • Tom Daly
      Posted at 6:37 pm August 26, 2022
      Tom Daly
      Reply
      Author

      I’ll look into the smooth scrolling. You can change the icon but it would require you to rebuild the package

      you can replace the “Up” with anything from here. https://www.flicon.io/

  • D
    Posted at 2:16 am August 18, 2022
    D
    Reply
    Author

    By any chance can you do change the icon and also when clicked, it will slowly move to top rather than instantly.

  • João Victor
    Posted at 2:14 pm May 23, 2022
    João Victor
    Reply
    Author

    I installed it but it doesn’t work.
    After instalation on the site content it does not appear at the webpart add menu, i’ve been struggling for days to get it working, any ideas?

    • Tom Daly
      Posted at 4:01 am August 2, 2022
      Tom Daly
      Reply
      Author

      it’s not a web part, it’s automatic. It’s been a while since I’ve validated that it still works. I’ll take a note to do that this week.

  • Leave a Reply to João Victor
    Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.