-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add scroll to toolbar and useRouteChange
- Loading branch information
Showing
11 changed files
with
116 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ReactElement } from 'react'; | ||
import { AppRoutes } from '@/shared/const/router'; | ||
import { ScrollToolbar } from '@/widgets/ScrollToolbar'; | ||
import { useRouteChange } from '@/shared/lib/router/useRouteChange'; | ||
|
||
export function useAppToolbar() { | ||
const appRoute = useRouteChange(); | ||
|
||
const toolbarByAppRoute: OptionalRecord<AppRoutes, ReactElement> = { | ||
[AppRoutes.ARTICLES]: <ScrollToolbar />, | ||
[AppRoutes.ARTICLE_DETAILS]: <ScrollToolbar />, | ||
}; | ||
|
||
return toolbarByAppRoute[appRoute]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ScrollToTopButton } from './ui/ScrollToTopButton/ScrollToTopButton'; |
27 changes: 27 additions & 0 deletions
27
src/features/scrollToTopButton/ui/ScrollToTopButton/ScrollToTopButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { memo } from 'react'; | ||
import { classNames } from '@/shared/lib/classNames/classNames'; | ||
import { Icon } from '@/shared/ui/redesigned/Icon'; | ||
import CircleIcon from '@/shared/assets/icons/circle-up.svg'; | ||
|
||
interface ScrollToTopButtonProps { | ||
className?: string; | ||
} | ||
|
||
export const ScrollToTopButton = memo((props: ScrollToTopButtonProps) => { | ||
const { className } = props; | ||
|
||
const onCLick = () => { | ||
window.scrollTo({ top: 0, behavior: 'smooth' }); | ||
}; | ||
|
||
return ( | ||
<Icon | ||
Svg={CircleIcon} | ||
clickable | ||
onClick={onCLick} | ||
width={32} | ||
height={32} | ||
className={classNames('', {}, [className])} | ||
/> | ||
); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,5 @@ | |
|
||
.toolbar { | ||
height: 100%; | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { matchPath, useLocation } from 'react-router-dom'; | ||
import { useEffect, useState } from 'react'; | ||
import { AppRouteByPathPattern, AppRoutes } from '@/shared/const/router'; | ||
|
||
export function useRouteChange() { | ||
const location = useLocation(); | ||
const [appRoute, setAppRoute] = useState<AppRoutes>(AppRoutes.MAIN); | ||
|
||
useEffect(() => { | ||
Object.entries(AppRouteByPathPattern).forEach(([pattern, route]) => { | ||
if (matchPath(pattern, location.pathname)) { | ||
setAppRoute(route); | ||
} | ||
}); | ||
}, [location.pathname]); | ||
|
||
return appRoute; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ScrollToolbar } from './ui/ScrollToolbar/ScrollToolbar'; |
3 changes: 3 additions & 0 deletions
3
src/widgets/ScrollToolbar/ui/ScrollToolbar/ScrollToolbar.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.ScrollToolbar { | ||
height: 100%; | ||
} |
24 changes: 24 additions & 0 deletions
24
src/widgets/ScrollToolbar/ui/ScrollToolbar/ScrollToolbar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { memo } from 'react'; | ||
import { classNames } from '@/shared/lib/classNames/classNames'; | ||
import cls from './ScrollToolbar.module.scss'; | ||
import { VStack } from '@/shared/ui/redesigned/Stack'; | ||
import { ScrollToTopButton } from '@/features/scrollToTopButton'; | ||
|
||
interface ScrollToolbarProps { | ||
className?: string; | ||
} | ||
|
||
export const ScrollToolbar = memo((props: ScrollToolbarProps) => { | ||
const { className } = props; | ||
|
||
return ( | ||
<VStack | ||
justify="center" | ||
align="center" | ||
max | ||
className={classNames(cls.ScrollToolbar, {}, [className])} | ||
> | ||
<ScrollToTopButton /> | ||
</VStack> | ||
); | ||
}); |