-
Notifications
You must be signed in to change notification settings - Fork 311
Feat/scroll to top #266
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Feat/scroll to top #266
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { useState, forwardRef } from 'react'; | ||
import { IScrollToTopButton } from './types'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
const ScrollToTopButton = forwardRef<HTMLButtonElement, IScrollToTopButton>( | ||
({ className = '', ...rest }, ref) => { | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const toggleVisibility = () => { | ||
const scrolled = document.documentElement.scrollTop; | ||
if (scrolled > 300) { | ||
setIsVisible(true); | ||
} else if (scrolled <= 300) { | ||
setIsVisible(false); | ||
} | ||
}; | ||
|
||
const scrollToTop = () => { | ||
console.log('scroll top'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ficou esquecido aqui |
||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth', | ||
}); | ||
}; | ||
|
||
window.addEventListener('scroll', toggleVisibility); | ||
|
||
return ( | ||
<div className={cn('fixed bottom-24 right-8 hidden md:block', className)}> | ||
{isVisible && ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Não seria melhor não renderizar nada caso não fosse necessário? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. entao o botao so aparece en telas maiores do que 1024px, para celular ele nao aparece There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tem alguma motivação? Para celulares, exatamente por ter telas menores, essa função parece ser ainda mais importante, já que a rolagem lá fica bem maior que no desktop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. porem no celular voce consegue rolar dando duas jogadas de dedo para cima, ne minha visao no celular é mais facil de rolar a pagina e no computador nao |
||
<button | ||
ref={ref} | ||
onClick={scrollToTop} | ||
className=" w-16 p-4 text-lg font-bold text-black bg-white border-2 border-black rounded-full shadow-lg hover:bg-black hover:text-white" | ||
{...rest} | ||
> | ||
↑ | ||
</button> | ||
)} | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
export { ScrollToTopButton }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { ScrollToTopButton } from './ScrollToTopButton'; | ||
|
||
export { ScrollToTopButton }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// components/ScrollToTopButton/types.ts | ||
|
||
export interface IScrollToTopButton | ||
extends React.ComponentPropsWithoutRef<'button'> { | ||
className?: string; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -88,6 +88,5 @@ | |
|
||
#root { | ||
max-width: 100vw; | ||
overflow-x: hidden; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
De onde veio esse valor? Vai funcionar tanto para desktop como dispositivos móveis?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quantidade de pixels que a pagina tem que ser rolada para aparecer o botao
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eu entendi que era isso. Minha pergunta foi sobre como você chegou até o entendimento que 300 é o melhor valor a ser usado.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
foi apenas testando os valores e esse foi o que encaixou melhor