Skip to content

Commit 21e630d

Browse files
author
Artem Tyurin
committed
Fix back button
1 parent d1d222b commit 21e630d

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ examplesNode!.addEventListener(
4040
const exampleKey = (<HTMLInputElement>(
4141
event.target
4242
)).value;
43-
render(findExample(exampleKey));
43+
location.hash = exampleKey;
4444
}
4545
);
4646

@@ -70,9 +70,19 @@ function render(example: Example) {
7070
location.hash = example.key;
7171
}
7272

73-
if (location.hash) {
73+
function renderFromHash() {
7474
const exampleKey = location.hash.slice(1);
75-
render(findExample(exampleKey));
75+
if (exampleKey) {
76+
render(findExample(exampleKey));
77+
}
78+
}
79+
80+
window.addEventListener("hashchange", () => {
81+
renderFromHash();
82+
});
83+
84+
if (location.hash) {
85+
renderFromHash();
7686
} else {
7787
render(examples[0]);
7888
}

0 commit comments

Comments
 (0)