Skip to content

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BrowserRouter } from 'react-router-dom';
import { Routes } from './routes/Routes';
import { SessionProvider } from './contexts';
import { Toaster } from './components/ui/toaster';
import { ScrollToTopButton } from './components/ScrollToTopButton';

const App = () => {
return (
Expand All @@ -12,6 +13,7 @@ const App = () => {
<BrowserRouter>
<SessionProvider>
<Routes />
<ScrollToTopButton />
</SessionProvider>
</BrowserRouter>
</Fragment>
Expand Down
45 changes: 45 additions & 0 deletions src/components/ScrollToTopButton/ScrollToTopButton.tsx
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) {
Copy link
Member

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?

Copy link
Author

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

Copy link
Member

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.

Copy link
Author

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

setIsVisible(true);
} else if (scrolled <= 300) {
setIsVisible(false);
}
};

const scrollToTop = () => {
console.log('scroll top');
Copy link
Member

Choose a reason for hiding this comment

The 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 && (
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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 };
3 changes: 3 additions & 0 deletions src/components/ScrollToTopButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { ScrollToTopButton } from './ScrollToTopButton';

export { ScrollToTopButton };
6 changes: 6 additions & 0 deletions src/components/ScrollToTopButton/types.ts
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;
}
1 change: 0 additions & 1 deletion src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,5 @@

#root {
max-width: 100vw;
overflow-x: hidden;
}
}