Skip to content

Commit 52e4975

Browse files
committed
add smooth scroll to top and separate id and selectors
1 parent bc68ba7 commit 52e4975

File tree

7 files changed

+38
-7
lines changed

7 files changed

+38
-7
lines changed

.github/workflows/bash__deploy-docker.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
workflows:
66
- 'Build and push Docker'
77
types:
8-
- completed
8+
- success
99

1010
workflow_dispatch:
1111

docs/working-notes/todo3.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,5 +499,8 @@ custom my-prose-links
499499
// dark theme for github markdown
500500
https://github.com/sindresorhus/github-markdown-css/issues/104
501501
https://github.com/sindresorhus/github-markdown-css
502+
503+
table of contents id links
504+
a href open in new tab
502505
------------
503506
```

src/components/Giscus.astro

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
---
2+
import { SELECTORS } from '@/constants/dom';
23
import { cn } from '@/utils/styles';
34
45
import type { HTMLAttributes } from 'astro/types';
56
67
export interface Props extends HTMLAttributes<'div'> {}
78
9+
const { GISCUS_WIDGET_ID } = SELECTORS;
10+
811
const { class: className } = Astro.props;
912
---
1013

1114
<div class={cn('giscus', className)}>
1215
<giscus-widget
13-
id="giscus-comments"
16+
id={GISCUS_WIDGET_ID}
1417
repo="nemanjam/nemanjam.github.io"
1518
repoid="R_kgDOKc-LeA"
1619
category="General"

src/components/Header.astro

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import clsx from 'clsx';
66
77
import HeaderLink from '@/components/HeaderLink.astro';
88
import ThemeToggle from '@/components/ThemeToggle.astro';
9+
import { SELECTORS } from '@/constants/dom';
910
import { IMAGE_SIZES } from '@/constants/image';
1011
import { NAVIGATION_ITEMS } from '@/constants/navigation';
1112
import { ROUTES } from '@/constants/routes';
@@ -14,14 +15,16 @@ import { getActiveNavItemPath } from '@/utils/navigation';
1415
1516
import Avatar from '@/assets/images/avatar.jpg';
1617
18+
const { SCROLL_TO_TOP_ID } = SELECTORS;
19+
1720
const { pathname: routePathname } = Astro.url;
1821
const activeNavItemPath = getActiveNavItemPath(routePathname);
1922
2023
// group-[.menu-open]: increases specificity everywhere
2124
// group-[.menu-open]:flex-col md:!flex-row, both on flex-direction
2225
---
2326

24-
<header id="main-header" class="group border-b border-base-300">
27+
<header id={SCROLL_TO_TOP_ID} class="group border-b border-base-300">
2528
<div
2629
class={clsx(
2730
'flex gap-2',

src/components/react/ScrollToTop.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import React, { useEffect, useRef, useState } from 'react';
22

3+
import { SELECTORS } from '@/constants/dom';
34
import { cn } from '@/utils/styles';
45

5-
import type { ReactNode } from 'react';
6+
import type { MouseEvent, ReactNode } from 'react';
67

78
interface Props {
89
children: ReactNode;
910
}
1011

12+
const { SCROLL_TO_TOP_SELECTOR } = SELECTORS;
13+
1114
const fixedClasses = ['opacity-1', 'translate-y-0'];
1215
const hiddenClasses = ['opacity-0', 'translate-y-20'];
1316

@@ -77,6 +80,15 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
7780
};
7881
}, []);
7982

83+
const handleScrollToTop = (event: MouseEvent<HTMLAnchorElement>) => {
84+
event.preventDefault();
85+
86+
const anchorElement = document.querySelector(SCROLL_TO_TOP_SELECTOR);
87+
if (!anchorElement) return;
88+
89+
anchorElement.scrollIntoView({ block: 'start', behavior: 'smooth' });
90+
};
91+
8092
return (
8193
<>
8294
<div
@@ -93,7 +105,8 @@ const ScrollToTop: React.FC<Props> = ({ children }) => {
93105
<a
94106
ref={linkRef}
95107
id="to-top"
96-
href="#top"
108+
href={SCROLL_TO_TOP_SELECTOR}
109+
onClick={handleScrollToTop}
97110
className={cn(
98111
// default styles
99112
'z-10 fixed bottom-6 right-6 rounded bg-base-200 border border-base-300',

src/constants/dom.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export const SELECTORS = {
2+
GISCUS_WIDGET_ID: 'giscus-comments',
23
GISCUS_WIDGET_SELECTOR: '#giscus-comments',
34
GISCUS_IFRAME_SELECTOR: 'iframe[title="Comments"]',
5+
GITHUB_MARKDOWN_BODY_ID: 'markdown-body-id',
46
GITHUB_MARKDOWN_BODY_SELECTOR: '#markdown-body-id',
7+
SCROLL_TO_TOP_ID: 'main-header',
8+
SCROLL_TO_TOP_SELECTOR: '#main-header',
59
} as const;

src/pages/links.astro

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,21 @@ import { getPageMetadata } from '@/utils/metadata';
77
88
import '@/assets/styles/github-markdown.css';
99
10+
import { SELECTORS } from '@/constants/dom';
11+
1012
const { README_RAW_URL, REPO } = BOOKMARKS;
1113
1214
const readmeContent = await fetch(README_RAW_URL).then((response) => response.text());
1315
1416
const metadata = getPageMetadata('lists/links');
1517
18+
const { GITHUB_MARKDOWN_BODY_ID } = SELECTORS;
19+
1620
// not generating id="..." links href="#..."
1721
---
1822

1923
<List {metadata}>
20-
<div id="markdown-body-id" class="markdown-body">
24+
<div id={GITHUB_MARKDOWN_BODY_ID} class="markdown-body">
2125
<p>
2226
My personal bookmarks collection which is hosted in this repo: <a href={REPO} target="_blank"
2327
>{REPO}</a
@@ -49,8 +53,9 @@ const metadata = getPageMetadata('lists/links');
4953
import type { ChangeThemeCustomEvent, Mode } from '@/types/constants';
5054

5155
const { CHANGE_EVENT } = THEME_CONFIG;
56+
const { GITHUB_MARKDOWN_BODY_SELECTOR } = SELECTORS;
5257

53-
const markdownBodyElement = document.querySelector(SELECTORS.GITHUB_MARKDOWN_BODY_SELECTOR);
58+
const markdownBodyElement = document.querySelector(GITHUB_MARKDOWN_BODY_SELECTOR);
5459

5560
const loadGithubTheme = (mode: Mode) => {
5661
if (!markdownBodyElement) return;

0 commit comments

Comments
 (0)