-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme-default): Scroll to top button on docs | enableScrollToTop (…
- Loading branch information
Showing
7 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
--- | ||
"@rspress/theme-default": minor | ||
"@rspress/shared": minor | ||
"@rspress/docs": patch | ||
--- | ||
|
||
feat(theme-default): Scroll to top button on docs | enableScrollToTop | ||
chore(docs): weixin -> wechat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/theme-default/src/components/ScrollToTop/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.scroll-to-top { | ||
z-index: 1; | ||
position: fixed; | ||
right: 1rem; | ||
bottom: 1rem; | ||
padding: 0.5rem; | ||
border-radius: 9999px; | ||
color: var(--rp-c-text-1); | ||
border-color: var(--rp-c-text-3); | ||
border-width: 0.1px; | ||
background-color: var(--rp-c-bg-soft); | ||
box-shadow: | ||
0 1px 3px 0 var(--rp-shadow-1), | ||
0 1px 2px 0 var(--rp-shadow-2); | ||
transform: scale(0); | ||
transition-property: transform; | ||
transition-timing-function: cubic-bezier(0.4, 0, 0.2, 1); | ||
transition-duration: 75ms; | ||
} | ||
|
||
.scroll-to-top.entered { | ||
transform: scale(1) !important; | ||
} |
42 changes: 42 additions & 0 deletions
42
packages/theme-default/src/components/ScrollToTop/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useState, useEffect } from 'react'; | ||
import styles from './index.module.scss'; | ||
|
||
export default function ScrollToTop() { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const handleScroll = () => { | ||
const scrollTop = window.scrollY || document.documentElement.scrollTop; | ||
setIsVisible(scrollTop > 0); | ||
}; | ||
|
||
const scrollToTop = () => { | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth', | ||
}); | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
}, []); | ||
|
||
return ( | ||
<button | ||
className={`${styles.scrollToTop} ${isVisible ? styles.entered : ''}`} | ||
onClick={scrollToTop} | ||
> | ||
<svg | ||
xmlns="http://www.w3.org/2000/svg" | ||
viewBox="0 0 24 24" | ||
fill="currentColor" | ||
className="w-6 h-6" | ||
> | ||
<path | ||
fillRule="evenodd" | ||
d="M11.47 2.47a.75.75 0 0 1 1.06 0l7.5 7.5a.75.75 0 1 1-1.06 1.06l-6.22-6.22V21a.75.75 0 0 1-1.5 0V4.81l-6.22 6.22a.75.75 0 1 1-1.06-1.06l7.5-7.5Z" | ||
clipRule="evenodd" | ||
/> | ||
</svg> | ||
</button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters