Skip to content

Commit

Permalink
Merge pull request #95 from placetopay-org/feature/keep-scroll
Browse files Browse the repository at this point in the history
feat: keep scroll view
  • Loading branch information
meiyerDev committed Aug 23, 2024
2 parents e6f60bb + 0dbdc63 commit 5e63c51
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
6 changes: 5 additions & 1 deletion src/components/ApiReader.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useState } from 'react'
import { Disclosure, Transition } from '@headlessui/react'
import { Disclosure } from '@headlessui/react'
import ReactMarkdown from 'react-markdown'
import clsx from 'clsx'
import { PlusIcon } from '@heroicons/react/24/outline'
import { Properties, Property } from './mdx'
import { useLocale } from './LocaleProvider'
import { usePreventLayoutShift } from '@/hooks/usePreventLayoutShift'

const TITLES = {
request: {
Expand Down Expand Up @@ -33,6 +34,7 @@ const ParentProperty = ({
isRequired = false,
isChild = false,
}) => {
const {positionRef, preventLayoutShift} = usePreventLayoutShift();
const properties = property.properties ?? property.items?.properties ?? null
const requireds = property.required ?? property.items?.required ?? []
const title = property.title ?? property.items?.title ?? null
Expand Down Expand Up @@ -66,6 +68,8 @@ const ParentProperty = ({
{withChilds && (
<>
<Disclosure.Button
ref={positionRef}
onClick={() => preventLayoutShift(() => void 0)}
className={clsx(
'-mx-2 -my-1 flex cursor-pointer items-center gap-1 px-2 py-1 text-2xs font-medium text-gray-500 transition hover:text-gray-600 dark:text-gray-400 hover:dark:text-gray-300',
{ 'mt-2': property.description }
Expand Down
26 changes: 1 addition & 25 deletions src/components/Code.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import clsx from 'clsx'
import { create } from 'zustand'

import { Tag } from '@/components/Tag'
import { usePreventLayoutShift } from '@/hooks/usePreventLayoutShift'

const languageNames = {
js: 'JavaScript',
Expand Down Expand Up @@ -218,31 +219,6 @@ function CodeGroupPanels({ children, selectedIndex, ...props }) {
return <CodePanel {...props}>{content}</CodePanel>
}

function usePreventLayoutShift() {
let positionRef = useRef()
let rafRef = useRef()

useEffect(() => {
return () => {
window.cancelAnimationFrame(rafRef.current)
}
}, [])

return {
positionRef,
preventLayoutShift(callback) {
let initialTop = positionRef.current.getBoundingClientRect().top

callback()

rafRef.current = window.requestAnimationFrame(() => {
let newTop = positionRef.current.getBoundingClientRect().top
window.scrollBy(0, newTop - initialTop)
})
},
}
}

const usePreferredLanguageStore = create((set) => ({
preferredLanguages: [],
addPreferredLanguage: (language) =>
Expand Down
26 changes: 26 additions & 0 deletions src/hooks/usePreventLayoutShift.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useEffect, useRef } from "react"

export function usePreventLayoutShift() {
let positionRef = useRef()
let rafRef = useRef()

useEffect(() => {
return () => {
window.cancelAnimationFrame(rafRef.current)
}
}, [])

return {
positionRef,
preventLayoutShift(callback) {
let initialTop = positionRef.current.getBoundingClientRect().top

callback()

rafRef.current = window.requestAnimationFrame(() => {
let newTop = positionRef.current.getBoundingClientRect().top
window.scrollBy(0, newTop - initialTop)
})
},
}
}

0 comments on commit 5e63c51

Please sign in to comment.