Skip to content

Commit 42f74f0

Browse files
authored
Merge pull request #32 from increments/scroll-to-top-with-page-transition
Scroll to top with page transition
2 parents 5feb3a2 + ea854e1 commit 42f74f0

File tree

7 files changed

+27
-22
lines changed

7 files changed

+27
-22
lines changed

src/client/components/Article.tsx

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,8 @@ interface Props {
2121

2222
export const Article = ({ renderedBody, tags, title }: Props) => {
2323
const bodyElement = useRef<HTMLDivElement>(null);
24-
const [isOpen, setIsOpen] = useState(
25-
localStorage.getItem("openArticleState") === "true" ? true : false
26-
);
27-
2824
const [isRendered, setIsRendered] = useState(false);
2925

30-
const toggleAccordion = (event: React.MouseEvent<HTMLInputElement>) => {
31-
event.preventDefault();
32-
setIsOpen((prev) => !prev);
33-
};
34-
35-
useEffect(() => {
36-
localStorage.setItem("openArticleState", JSON.stringify(isOpen));
37-
}, [isOpen]);
38-
3926
useEffect(() => {
4027
if (isRendered) {
4128
bodyElement.current && executeScriptTagsInElement(bodyElement.current);

src/client/components/Layout.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { Outlet, ScrollRestoration } from "react-router-dom";
2+
3+
export const Layout = () => {
4+
return (
5+
<div>
6+
<Outlet />
7+
<ScrollRestoration />
8+
</div>
9+
);
10+
};

src/client/components/Router.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
import { createBrowserRouter, RouterProvider } from "react-router-dom";
22
import { ItemsIndex } from "../pages/items";
33
import { ItemsShow } from "../pages/items/show";
4+
import { Layout } from "./Layout";
45

56
const router = createBrowserRouter([
67
{
78
path: "/",
8-
element: <ItemsIndex />,
9-
},
10-
{
11-
path: "/items/:id",
12-
element: <ItemsShow />,
9+
element: <Layout />,
10+
children: [
11+
{
12+
index: true,
13+
element: <ItemsIndex />,
14+
},
15+
{
16+
path: "/items/:id",
17+
element: <ItemsShow />,
18+
},
19+
],
1320
},
1421
]);
1522

src/client/components/SidebarContents.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,8 @@ const closedSidebarStyle = css({
694694
justifyContent: "space-between",
695695
maxWidth: 48,
696696
padding: `${getSpace(1)}px ${getSpace(1 / 2)}px ${getSpace(2)}px`,
697+
position: "sticky",
698+
top: 0,
697699

698700
...viewport.S({
699701
display: "none",

src/client/templates/Contents.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,5 @@ export const Contents = ({ children }: Props) => {
1111

1212
const contentsStyle = css({
1313
gridArea: "contents",
14-
height: "100vh",
15-
overflowX: "auto",
1614
width: "100%",
1715
});

src/client/templates/Main.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ const mainStyle = css({
1818
"sidebar contents"
1919
`,
2020
gridTemplateColumns: "auto minmax(450px, 1fr)",
21-
height: "100vh",
2221

2322
...viewport.S({
24-
gridTemplateColumns: "auto 1fr",
23+
gridTemplateColumns: "auto minmax(0, 1fr)", // Setting min width to prevent widening
2524
}),
2625
});

src/client/templates/Sidebar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ export const Sidebar = ({ children }: Props) => {
1414
const sidebarStyle = css({
1515
backgroundColor: Colors.gray0,
1616
gridArea: "sidebar",
17+
position: "sticky",
18+
zIndex: 1,
1719
});

0 commit comments

Comments
 (0)