Skip to content

Commit b341686

Browse files
committed
docs: handle route changes and scroll position better
1 parent 179d5d4 commit b341686

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

INSTALL.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ You can also [use SMUI in the Svelte REPL](REPL.md).
1414

1515
You will always import Svelte components from the individual packages.
1616

17-
This method will only include the JavaScript. Use this for the Default Theme or the [Easy Styling Method](SASS.md).
17+
This method will only include the JavaScript. Use this for the Default Theme or the [Easy Styling Method](SASS.md#easy-styling-method).
1818

1919
```svelte
2020
<script>
2121
import Button from '@smui/button';
2222
</script>
2323
```
2424

25-
This method will include Sass files as well. Use this for the [Advanced Styling Method](SASS.md).
25+
This method will include Sass files as well. Use this for the [Advanced Styling Method](SASS.md#advanced-styling-method).
2626

2727
```svelte
2828
<script>

SAPPER.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<sub>\* As of 2021-Apr-06, these instructions will now work without a flash of unstyled content!</sub>
44

5-
These instructions are for the Advanced Styling Method. If you don't need the advantages of this method, it's much easier to use the [Easy Styling Method](SASS.md).
5+
These instructions are for the Advanced Styling Method. If you don't need the advantages of this method, it's much easier to use the [Easy Styling Method](SASS.md#easy-styling-method).
66

77
Install the following packages as dev dependencies.
88

site/src/routes/_layout.svelte

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
</Section>
2424
<Section align="end" toolbar style="color: var(--mdc-on-surface, #000);">
2525
{#if activeSection}
26-
{#each repos as repo}
26+
{#each activeSection.repos || [] as repo}
2727
<Wrapper>
2828
<IconButton href={repo} target="_blank">
2929
<Icon component={Svg} viewBox="0 0 24 24">
@@ -111,8 +111,7 @@
111111
: 'shortcut' in section
112112
? section.shortcut
113113
: null}
114-
on:click={() => pickSection(section)}
115-
activated={'route' in section && section.route === $page.path}
114+
activated={section === activeSection}
116115
style={section.indent
117116
? 'margin-left: ' + section.indent * 25 + 'px;'
118117
: ''}
@@ -494,34 +493,26 @@
494493
},
495494
];
496495
497-
let activeSection = sections.find(
498-
(section) => 'route' in section && section.route === $page.path
496+
$: activeSection = sections.find(
497+
(section) => 'route' in section && routesEqual(section.route, $page.path)
499498
);
500-
$: repos =
501-
activeSection && 'repos' in activeSection ? activeSection.repos : [];
502-
503-
onMount(setMiniWindow);
504-
505-
function pickSection(section) {
506-
if (!('shortcut' in section) && !('route' in section)) {
507-
return;
508-
}
509-
499+
let previousPagePath = null;
500+
$: if (mainContent && previousPagePath !== $page.path) {
510501
drawerOpen = false;
511-
mainContent.scrollTop = 0;
502+
const top = window.location.hash
503+
? document.querySelector(window.location.hash)?.offsetTop || 0
504+
: 0;
505+
mainContent.scrollTop = top;
506+
previousPagePath = $page.path;
507+
}
512508
513-
// Svelte/Sapper is not updated the components correctly, so I need this.
514-
sections.forEach((section) => {
515-
if (!section.separator) {
516-
section.component.$set({ activated: false });
517-
}
518-
});
519-
section.component.$set({ activated: true });
509+
onMount(setMiniWindow);
520510
521-
activeSection =
522-
'shortcut' in section
523-
? sections.find((sec) => sec.route === section.shortcut)
524-
: section;
511+
function routesEqual(a, b) {
512+
return (
513+
(a.endsWith('/') ? a.slice(0, -1) : a) ===
514+
(b.endsWith('/') ? b.slice(0, -1) : b)
515+
);
525516
}
526517
527518
function setMiniWindow() {

0 commit comments

Comments
 (0)