generated from xhofe/solid-lib
-
Notifications
You must be signed in to change notification settings - Fork 510
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sticky header & markdown toc (#141)
* feat: keep the position of breadcrumb sticky to top while scrolling * feat: add a button with function "back to top" * feat: add toc for markdown previewer * fix: reassignment error * fix: toc visible issue while changed page * feat: add local setting for sticky position * refactor: move toc component into markdown component
- Loading branch information
Showing
11 changed files
with
286 additions
and
16 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
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
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
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,58 @@ | ||
import { Show, createSignal, onCleanup } from "solid-js" | ||
import { Box, Icon } from "@hope-ui/solid" | ||
import { FiArrowUp } from "solid-icons/fi" | ||
import { Motion } from "@motionone/solid" | ||
import { isMobile } from "~/utils/compatibility" | ||
import { getMainColor } from "~/store" | ||
|
||
export const useScrollListener = ( | ||
callback: (e?: Event) => void, | ||
options?: { immediate?: boolean }, | ||
) => { | ||
if (options?.immediate) callback() | ||
window.addEventListener("scroll", callback, { passive: true }) | ||
onCleanup(() => window.removeEventListener("scroll", callback)) | ||
} | ||
|
||
export const BackTop = () => { | ||
if (isMobile) return null | ||
|
||
const [visible, setVisible] = createSignal(false) | ||
|
||
useScrollListener(() => setVisible(window.scrollY > 100)) | ||
|
||
return ( | ||
<Show when={visible()}> | ||
<Box | ||
as={Motion.div} | ||
initial={{ y: -999 }} | ||
animate={{ y: 0 }} | ||
zIndex="$overlay" | ||
pos="fixed" | ||
right="$5" | ||
top="0" | ||
borderBottomRadius="50%" | ||
bgColor="$whiteAlpha12" | ||
color={getMainColor()} | ||
overflow="hidden" | ||
shadow="$lg" | ||
_dark={{ bgColor: getMainColor(), color: "white" }} | ||
_hover={{ bgColor: getMainColor(), color: "white" }} | ||
> | ||
<Icon | ||
_focus={{ | ||
outline: "none", | ||
}} | ||
cursor="pointer" | ||
boxSize="$7" | ||
p="$1" | ||
rounded="$lg" | ||
as={FiArrowUp} | ||
onClick={() => { | ||
window.scrollTo({ top: 0, behavior: "smooth" }) | ||
}} | ||
/> | ||
</Box> | ||
</Show> | ||
) | ||
} |
Oops, something went wrong.