Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TechDocs navigation bugfix #20287

Merged
merged 11 commits into from
Oct 20, 2023
Merged

TechDocs navigation bugfix #20287

merged 11 commits into from
Oct 20, 2023

Conversation

squid-ney
Copy link
Contributor

@squid-ney squid-ney commented Sep 29, 2023

Fix TechDocs page scroll. Move logic from scrollIntoAnchor transformer to a useEffect.

Fixes #19686
Now, when navigating to a new TechDocs page, you are taken to the top of the page.

scroll-top-new-page.mov

Fixes #20159
Scrolling behavior is fixed when navigating using the forward and back browser buttons.

backward-forward-browser-nav.mov

Hey, I just made a Pull Request!

✔️ Checklist

  • A changeset describing the change and affected packages. (more info)
  • Added or updated documentation
  • Tests for new functionality and regression tests for bug fixes
  • Screenshots attached (for UI changes)
  • All your commits have a Signed-off-by line in the message. (more info)

…r to a useEffect .

Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
@github-actions github-actions bot added the area:techdocs Related to the TechDocs Project Area label Sep 29, 2023
@backstage-goalie
Copy link
Contributor

backstage-goalie bot commented Sep 29, 2023

Unexpected Changesets

The following changeset(s) reference packages that have not been changed in this PR:

  • .changeset/fresh-crews-promise.md: @backstage/plugin-techdocs-module-addons-contrib

Note that only changes that affect the published package require changesets, for example changes to tests and storybook stories do not require changesets.

Changed Packages

Package Name Package Path Changeset Bump Current Version
@backstage/plugin-techdocs plugins/techdocs patch v1.8.0

@github-actions
Copy link
Contributor

github-actions bot commented Sep 29, 2023

Uffizzi Preview deployment-38728 was deleted.

@github-actions
Copy link
Contributor

github-actions bot commented Oct 6, 2023

This PR has been automatically marked as stale because it has not had recent activity from the author. It will be closed if no further activity occurs. If the PR was closed and you want it re-opened, let us know and we'll re-open the PR so that you can continue the contribution!

@github-actions github-actions bot added the stale label Oct 6, 2023
@squid-ney
Copy link
Contributor Author

Not stale I was just on vacation :) Will pick up soon!

@github-actions github-actions bot removed the stale label Oct 10, 2023
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
@@ -167,7 +166,6 @@ export const useTechDocsReaderDom = (
const postRender = useCallback(
async (transformedElement: Element) =>
transformer(transformedElement, [
scrollIntoAnchor(),
Copy link
Contributor Author

@squid-ney squid-ney Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Transformers only run after adding TechDocs shadow root to the dom. This means that expected navigation behavior (backwards and forwards browser arrows) will not trigger this logic to run.

if (window.location.hash) {
const hash = window.location.hash.slice(1);
dom?.querySelector(`[id="${hash}"]`)?.scrollIntoView();
}
Copy link
Contributor Author

@squid-ney squid-ney Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic does not get called when navigating with browser back and forth arrows. Issue documented here: #20159. In order to fix this, the logic needs to run whenever the window.location.hash changes. I moved the logic to a useEffect.

if (hashElement && !isStyleLoading) {
hashElement.scrollIntoView();
} else {
document?.querySelector('header')?.scrollIntoView();
Copy link
Contributor Author

@squid-ney squid-ney Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scrolling the Backstage header element into view effectively scrolls user to the top of the page. If the url does not contain a hash, then scroll to the top of page by default.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because it always takes the first one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it always takes the first one. I was hesitant to do a more specific selection because the header component is different internally vs open source Backstage.

Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
@@ -14,3 +14,5 @@
* limitations under the License.
*/
import '@testing-library/jest-dom';

Element.prototype.scrollIntoView = jest.fn();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mock is needed because scrollIntoView is not implemented for jsdom. Jsdom is used by tests. More info on issue: jsdom/jsdom#1695. This seems to be the recommended approach as seen here.

Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
@squid-ney squid-ney marked this pull request as ready for review October 17, 2023 20:50
@squid-ney squid-ney requested a review from a team as a code owner October 17, 2023 20:50
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
Signed-off-by: Sydney Achinger <sydneynicoleachinger@spotify.com>
@squid-ney squid-ney merged commit f2dbf8c into master Oct 20, 2023
31 checks passed
@squid-ney squid-ney deleted the techdocs/navigation-bugfix branch October 20, 2023 19:42
@github-actions
Copy link
Contributor

Thank you for contributing to Backstage! The changes in this pull request will be part of the 1.20.0 release, scheduled for Tue, 14 Nov 2023.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:techdocs Related to the TechDocs Project Area
Projects
None yet
2 participants