feat: hide post side actions if footer actions showing (#605)

This commit is contained in:
Innei
2023-06-05 23:11:16 +01:00
committed by GitHub
parent 18e7644443
commit 97b9ea27e7
3 changed files with 33 additions and 81 deletions
+8 -1
View File
@@ -1,3 +1,5 @@
"use client"
import React, { useEffect, useRef, useState } from "react"
import { ReactionComment } from "~/components/common/ReactionComment"
@@ -6,6 +8,8 @@ import { ReactionMint } from "~/components/common/ReactionMint"
import { ReactionTip } from "~/components/common/ReactionTip"
import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
import { usePostFooterInView } from "./PostFooter"
export const PostActions: React.FC<{
page?: ExpandedNote
site?: ExpandedCharacter
@@ -23,12 +27,15 @@ export const PostActions: React.FC<{
)
}
}, [containerRef])
const isPostFooterInView = usePostFooterInView()
return (
<div
className="xlog-post-actions absolute right-full pr-14 h-full top-0"
className="xlog-post-actions absolute right-full pr-14 h-full top-0 transition-all duration-1000"
style={{
display: maxWidth > 60 ? "block" : "none",
opacity: isPostFooterInView ? 0 : 1,
visibility: isPostFooterInView ? "hidden" : "visible",
}}
ref={containerRef}
>
+25
View File
@@ -1,16 +1,41 @@
"use client"
import { useInViewport } from "ahooks"
import { useEffect, useRef } from "react"
import { useQuery, useQueryClient } from "@tanstack/react-query"
import { Comment } from "~/components/common/Comment"
import { ReactionLike } from "~/components/common/ReactionLike"
import { ReactionMint } from "~/components/common/ReactionMint"
import { ReactionTip } from "~/components/common/ReactionTip"
import { ExpandedCharacter, ExpandedNote } from "~/lib/types"
const key = ["PostFooterInView"]
export const usePostFooterInView = () => {
const { data } = useQuery<boolean>({
queryKey: key,
enabled: false,
})
return data
}
export const PostFooter: React.FC<{
page?: ExpandedNote
site?: ExpandedCharacter
}> = ({ page, site }) => {
const actionElRef = useRef<HTMLDivElement>(null)
const [isInView] = useInViewport(actionElRef)
const queryClient = useQueryClient()
useEffect(() => {
queryClient.setQueryData(key, () => isInView)
}, [isInView])
return (
<>
<div
ref={actionElRef}
className="xlog-reactions flex fill-gray-400 text-gray-500 sm:items-center space-x-6 sm:space-x-10 mt-14 mb-12"
data-hide-print
>
-80
View File
@@ -1,80 +0,0 @@
"use client"
import { FC, PropsWithChildren, useEffect, useRef, useState } from "react"
import { create } from "zustand"
import { useGetState } from "~/hooks/useGetState"
const targetQueue = [] as string[]
interface AnchorStoreState {
currentTarget: string
setTarget: (target: string) => void
pop: () => void
}
const useAnchorStore = create<AnchorStoreState>((setState) => ({
currentTarget: "",
setTarget(target: string) {
if (target === targetQueue.at(-1)) return
const payload = {
currentTarget: target,
}
targetQueue.push(target)
setState(payload)
},
pop() {
const prevTarget = targetQueue.pop()
if (prevTarget) setState({ currentTarget: prevTarget })
},
}))
export const HeadingAnchor: FC<PropsWithChildren<{ name: string }>> = (
props,
) => {
const getName = useGetState(props.name)
const ref = useRef<HTMLSpanElement>(null)
const [inViewport, setInViewport] = useState(false)
useEffect(() => {
const $el = ref.current
if (!$el) return
const options = {
rootMargin: "-33% 0% -33% 0%",
}
const observer = new IntersectionObserver(function (entries, observer) {
entries.forEach((entry) => {
setInViewport(entry.isIntersecting)
})
}, options)
observer.observe($el)
return () => {
observer.unobserve($el)
observer.disconnect()
}
}, [])
const isTriggered = useRef(false)
useEffect(() => {
const store = useAnchorStore.getState()
console.log(inViewport, "inViewport", getName())
if (inViewport) {
store.setTarget(getName())
isTriggered.current = true
} else {
if (isTriggered.current) {
store.pop()
}
isTriggered.current = false
}
}, [inViewport])
return <span ref={ref} data-anchor className="!inline-block" />
}