Skip to content
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

Feat/scroll to top #266

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
44 changes: 44 additions & 0 deletions src/components/ScrollToTopButton/ScrollToTopButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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 = () => {
window.scrollTo({
top: 0,
behavior: 'smooth',
});
};

window.addEventListener('scroll', toggleVisibility);

return (
<div className={cn('fixed bottom-24 right-8 hidden lg:block', className)}>
{isVisible && (
<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;
}
}