-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(System): headers of sections should be sticky [YTFRONT-4420]
- Loading branch information
1 parent
cdee29c
commit 7e04dae
Showing
12 changed files
with
225 additions
and
140 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
18 changes: 18 additions & 0 deletions
18
packages/ui/src/ui/components/StickyContainer/StickyContainer.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,18 @@ | ||
.yt-sticky-container { | ||
&__sticky { | ||
position: sticky; | ||
z-index: 1; | ||
|
||
background-color: var(--main-background); | ||
box-shadow: 0 1px 10px var(--opacity-background); | ||
|
||
width: calc(100vw - var(--gn-aside-header-size)); | ||
margin-left: -20px; | ||
padding-left: 20px; | ||
padding-right: 20px; | ||
|
||
&_top { | ||
top: var(--app-header-height); | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
packages/ui/src/ui/components/StickyContainer/StickyContainer.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,56 @@ | ||
import React from 'react'; | ||
import cn from 'bem-cn-lite'; | ||
import {HEADER_HEIGHT} from '../../constants'; | ||
|
||
import './StickyContainer.scss'; | ||
|
||
const block = cn('yt-sticky-container'); | ||
|
||
export function StickyContainer({ | ||
topOffset = HEADER_HEIGHT, | ||
children, | ||
}: { | ||
topOffset?: number; | ||
children: (params: {sticky: boolean; topStickyClassName?: string}) => React.ReactNode; | ||
}) { | ||
const [sticky, setSticky] = React.useState(false); | ||
const [element, setElement] = React.useState<HTMLDivElement | null>(null); | ||
|
||
const observer = React.useMemo(() => { | ||
return new IntersectionObserver( | ||
(entries) => { | ||
if (entries[0].intersectionRatio === 0) { | ||
setSticky(true); | ||
} else if (entries[0].intersectionRatio === 1) { | ||
setSticky(false); | ||
} | ||
}, | ||
{threshold: [0, 1], rootMargin: `${-topOffset ?? 0}px 0px 0px 0px`}, | ||
); | ||
}, [topOffset]); | ||
|
||
React.useEffect(() => { | ||
if (element) { | ||
observer.observe(element); | ||
} | ||
return () => { | ||
if (element) { | ||
observer.unobserve(element); | ||
} | ||
}; | ||
}, [element]); | ||
|
||
const onRef = React.useCallback((div: HTMLDivElement | null) => { | ||
setElement(div); | ||
}, []); | ||
|
||
return ( | ||
<div className={block()}> | ||
<div className={block('top')} ref={onRef} /> | ||
{children({ | ||
sticky, | ||
topStickyClassName: sticky ? block('sticky', {top: true}) : undefined, | ||
})} | ||
</div> | ||
); | ||
} |
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
.system { | ||
&__chunks { | ||
margin-bottom: 20px; | ||
z-index: -1; | ||
position: relative; | ||
} | ||
|
||
.chunk-cells { | ||
|
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
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
Oops, something went wrong.