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

[DM] only update scroll position when page changes #840

Merged
merged 1 commit into from
Mar 15, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions packages/docs/src/components/styleguide.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ export class Styleguide extends React.Component<IStyleguideProps, IStyleguideSta
}

public componentDidMount() {
this.maybeScrollActiveMenuItemIntoView(true);
this.scrollToActiveSection();
this.maybeScrollToActivePageMenuItem();
this.props.onUpdate(this.state.activePageId);
// whoa handling future history...
window.addEventListener("hashchange", () => {
Expand All @@ -168,8 +169,13 @@ export class Styleguide extends React.Component<IStyleguideProps, IStyleguideSta

public componentDidUpdate(_prevProps: IStyleguideProps, prevState: IStyleguideState) {
const { activePageId, themeName } = this.state;

// only scroll to heading when switching pages, but always check if nav item needs scrolling.
this.maybeScrollActiveMenuItemIntoView(prevState.activePageId !== activePageId);
if (prevState.activePageId !== activePageId) {
this.scrollToActiveSection();
this.maybeScrollToActivePageMenuItem();
}

setHotkeysDialogProps({ className: themeName } as any as IHotkeysDialogProps);
this.props.onUpdate(activePageId);
}
Expand Down Expand Up @@ -203,22 +209,22 @@ export class Styleguide extends React.Component<IStyleguideProps, IStyleguideSta
setTheme(themeName);
}

private maybeScrollActiveMenuItemIntoView(alsoHeading: boolean) {
const { activeSectionId } = this.state;

if (alsoHeading) {
scrollToReference(activeSectionId, this.contentElement);
}

// only scroll nav menu if active item is not visible in viewport
const navMenuElement = queryHTMLElement(this.navElement, `a[href="#${activeSectionId}"]`);
private maybeScrollToActivePageMenuItem() {
const { activePageId } = this.state;
// only scroll nav menu if active item is not visible in viewport.
// using activePageId so you can see the page title in nav (may not be visible in document).
const navMenuElement = this.navElement.query(`a[href="#${activePageId}"]`);
const innerBounds = navMenuElement.getBoundingClientRect();
const outerBounds = this.navElement.getBoundingClientRect();
if (innerBounds.top < outerBounds.top || innerBounds.bottom > outerBounds.bottom) {
navMenuElement.scrollIntoView();
}
}

private scrollToActiveSection() {
scrollToReference(this.state.activeSectionId, this.contentElement);
}

private shiftSection(direction: 1 | -1) {
// use the current hash instead of `this.state.activeSectionId` to avoid cases where the
// active section cannot actually be selected in the nav (often a short one at the end).
Expand Down