Skip to content

Commit 526cd27

Browse files
author
Dobromir Hristov
authored
Add extra offset when scrolling to a section in Documentati… (#434)
closes rdar://100028167
1 parent 0089110 commit 526cd27

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

src/utils/router-utils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { BreakpointAttributes } from 'docc-render/utils/breakpoints';
1717
import { waitFrames } from 'docc-render/utils/loading';
1818
import { cssEscapeTopicIdHash } from 'docc-render/utils/strings';
1919
import { areEquivalentLocations } from 'docc-render/utils/url-helper';
20+
import getExtraScrollOffset from 'theme/utils/scroll-offset.js';
2021

2122
/**
2223
* Returns the current absolute location, eg: '/tutorials/swiftui/something'
@@ -54,8 +55,8 @@ export async function scrollBehavior(to, from, savedPosition) {
5455
const baseNavOffset = getBaseNavOffset();
5556
// if on docs and have API changes enabled
5657
const apiChangesNavHeight = (isDocumentation && hasNavBarOpen) ? baseNavOffset : 0;
57-
// compensate for the nav sticky height.
58-
const offset = baseNavOffset + apiChangesNavHeight;
58+
// compensate for the nav sticky height and add any extra scroll offset we may need
59+
const offset = baseNavOffset + apiChangesNavHeight + getExtraScrollOffset(to);
5960

6061
const y = process.env.VUE_APP_TARGET === 'ide' ? 0 : offset;
6162
return { selector: cssEscapeTopicIdHash(hash), offset: { x: 0, y } };

src/utils/scroll-offset.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2022 Apple Inc. and the Swift project authors
5+
* Licensed under Apache License v2.0 with Runtime Library Exception
6+
*
7+
* See https://swift.org/LICENSE.txt for license information
8+
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import { documentationTopicName } from 'docc-render/constants/router';
12+
13+
/**
14+
* Pixel based value for the extra scroll offset for doc pages.
15+
* @type {number}
16+
*/
17+
export const EXTRA_DOCUMENTATION_OFFSET = 10;
18+
19+
/**
20+
* @param {import('vue-router').Route} to
21+
* @returns {Number}
22+
*/
23+
export default function getExtraScrollOffset(to) {
24+
const { name } = to;
25+
const isDocumentation = name.includes(documentationTopicName);
26+
return isDocumentation ? EXTRA_DOCUMENTATION_OFFSET : 0;
27+
}

tests/unit/utils/router-utils.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
restoreScrollOnReload,
2020
saveScrollOnReload,
2121
} from 'docc-render/utils/router-utils';
22+
import { EXTRA_DOCUMENTATION_OFFSET } from '@/utils/scroll-offset';
2223

2324
const appTarget = process.env.VUE_APP_TARGET;
2425

@@ -77,7 +78,7 @@ describe('router-utils', () => {
7778
const resolved = await scrollBehavior(routeDocsNoChanges, routeBar);
7879
expect(resolved).toEqual({
7980
selector: routeDocsNoChanges.hash,
80-
offset: { x: 0, y: baseNavHeight },
81+
offset: { x: 0, y: baseNavHeight + EXTRA_DOCUMENTATION_OFFSET },
8182
});
8283
});
8384

@@ -89,7 +90,7 @@ describe('router-utils', () => {
8990
const resolved = await scrollBehavior(routeDocsNoChanges, routeBar);
9091
expect(resolved).toEqual({
9192
selector: routeDocsNoChanges.hash,
92-
offset: { x: 0, y: baseNavHeightSmallBreakpoint },
93+
offset: { x: 0, y: baseNavHeightSmallBreakpoint + EXTRA_DOCUMENTATION_OFFSET },
9394
});
9495

9596
window.innerWidth = innerWidth;
@@ -101,7 +102,7 @@ describe('router-utils', () => {
101102
const resolved = await scrollBehavior(routeDocsNoChanges, routeBar);
102103
expect(resolved).toEqual({
103104
selector: routeDocsNoChanges.hash,
104-
offset: { x: 0, y: baseNavHeight * 2 },
105+
offset: { x: 0, y: baseNavHeight * 2 + EXTRA_DOCUMENTATION_OFFSET },
105106
});
106107
});
107108

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* This source file is part of the Swift.org open source project
3+
*
4+
* Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
* Licensed under Apache License v2.0 with Runtime Library Exception
6+
*
7+
* See https://swift.org/LICENSE.txt for license information
8+
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import getExtraScrollOffset, { EXTRA_DOCUMENTATION_OFFSET } from 'docc-render/utils/scroll-offset';
12+
import { documentationTopicName } from 'docc-render/constants/router';
13+
14+
describe('scroll-offset', () => {
15+
it('returns extra offset for documentation pages', () => {
16+
expect(getExtraScrollOffset({ name: documentationTopicName }))
17+
.toEqual(EXTRA_DOCUMENTATION_OFFSET);
18+
});
19+
20+
it('falls back to 0 for other pages', () => {
21+
expect(getExtraScrollOffset({ name: 'foo' }))
22+
.toEqual(0);
23+
});
24+
});

0 commit comments

Comments
 (0)