-
Notifications
You must be signed in to change notification settings - Fork 42
feat: add scroll container prop #1166
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
DaffPunks
wants to merge
1
commit into
main
Choose a base branch
from
scroll-container-prop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,120 @@ | ||
| import {memo, useRef} from 'react'; | ||
|
|
||
| import {MarkdownEditorView, useMarkdownEditor} from '@gravity-ui/markdown-editor'; | ||
| import {Text} from '@gravity-ui/uikit'; | ||
|
|
||
| import {PlaygroundLayout} from '../../../components/PlaygroundLayout'; | ||
|
|
||
| const initialMarkup = `# Sticky toolbar in a scroll container | ||
|
|
||
| This example shows how the toolbar can stick to the top of a custom scroll container | ||
| instead of the browser window. | ||
|
|
||
| Try scrolling down inside the editor area — the toolbar will stay fixed at the top | ||
| of the scrollable box below, not at the top of the page. | ||
|
|
||
| ## Why this matters | ||
|
|
||
| By default \`useSticky\` listens to \`scroll\` events on \`window\`. When the editor lives | ||
| inside a fixed-height scrollable div, the window never scrolls, so the sticky logic | ||
| never triggers. Passing \`scrollContainerRef\` routes the \`scroll\` listener onto the | ||
| right element. | ||
|
|
||
| ## How to use it | ||
|
|
||
| \`\`\`tsx | ||
| const scrollRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| <div ref={scrollRef} style={{overflowY: 'auto', height: 400}}> | ||
| <MarkdownEditorView | ||
| editor={editor} | ||
| stickyToolbar | ||
| scrollContainerRef={scrollRef} | ||
| /> | ||
| </div> | ||
| \`\`\` | ||
|
|
||
| ## More content to make the area scrollable | ||
|
|
||
| Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor | ||
| incididunt ut labore et dolore magna aliqua. | ||
|
|
||
| - Item one | ||
| - Item two | ||
| - Item three | ||
|
|
||
| ### Heading level 3 | ||
|
|
||
| Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip | ||
| ex ea commodo consequat. | ||
|
|
||
| 1. First ordered item | ||
| 2. Second ordered item | ||
| 3. Third ordered item | ||
|
|
||
| ### Another section | ||
|
|
||
| Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu | ||
| fugiat nulla pariatur. | ||
|
|
||
| > Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia | ||
| > deserunt mollit anim id est laborum. | ||
|
|
||
| ### Yet another section | ||
|
|
||
| Curabitur pretium tincidunt lacus. Nulla gravida orci a odio. Nullam varius, | ||
| turpis molestie pretium placerat, arcu purus aliquam erat. | ||
|
|
||
| - Nested list item 1 | ||
| - Child item A | ||
| - Child item B | ||
| - Nested list item 2 | ||
|
|
||
| ### Final section | ||
|
|
||
| Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac | ||
| turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget. | ||
| `; | ||
|
|
||
| export type EditorWithStickyInContainerProps = {}; | ||
|
|
||
| export const EditorWithStickyInContainer = memo<EditorWithStickyInContainerProps>( | ||
| function EditorWithStickyInContainer() { | ||
| const scrollContainerRef = useRef<HTMLDivElement>(null); | ||
|
|
||
| const editor = useMarkdownEditor({ | ||
| initial: {markup: initialMarkup, mode: 'wysiwyg'}, | ||
| }); | ||
|
|
||
| return ( | ||
| <PlaygroundLayout | ||
| title="Sticky toolbar inside a scroll container" | ||
| editor={editor} | ||
| view={() => ( | ||
| <div | ||
| ref={scrollContainerRef} | ||
| style={{ | ||
| height: 600, | ||
| overflowY: 'auto', | ||
| border: '1px solid var(--g-color-line-generic)', | ||
| borderRadius: 4, | ||
| padding: 16, | ||
| }} | ||
| > | ||
| <Text variant="display-4" style={{paddingBottom: 16, display: 'block'}}> | ||
| Scroll container | ||
| </Text> | ||
| <MarkdownEditorView | ||
| className="g-md-editor-mode" | ||
| autofocus | ||
| stickyToolbar | ||
| settingsVisible | ||
| editor={editor} | ||
| scrollContainerRef={scrollContainerRef} | ||
| /> | ||
| </div> | ||
| )} | ||
| /> | ||
| ); | ||
| }, | ||
| ); |
13 changes: 13 additions & 0 deletions
13
demo/src/stories/experiments/sticky-toolbar/StickyToolbar.stories.tsx
This file contains hidden or 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,13 @@ | ||
| import type {StoryObj} from '@storybook/react'; | ||
|
|
||
| import {EditorWithStickyInContainer as component} from './Editor'; | ||
|
|
||
| export const Story: StoryObj<typeof component> = { | ||
| args: {}, | ||
| }; | ||
| Story.storyName = 'Sticky Toolbar in Scroll Container'; | ||
|
|
||
| export default { | ||
| title: 'Experiments / Sticky Toolbar in Scroll Container', | ||
| component, | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,10 @@ | |
| } | ||
| } | ||
|
|
||
| &_withScrollContainer { | ||
| height: auto; | ||
| } | ||
|
|
||
| &__editor { | ||
| flex-grow: 1; | ||
| gap: 2px; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -4,25 +4,46 @@ import {useEffectOnce, useLatest} from 'react-use'; | |
|
|
||
| import {REFLOW_EVENTS} from 'src/utils/dom'; | ||
|
|
||
| export function useSticky<T extends HTMLElement>(elemRef: React.RefObject<T>) { | ||
| const CONTAINER_EVENTS = new Set<keyof WindowEventMap>([ | ||
| 'scroll', | ||
| 'touchstart', | ||
| 'touchmove', | ||
| 'touchend', | ||
| ]); | ||
|
|
||
| export function useSticky<T extends HTMLElement>( | ||
| elemRef: React.RefObject<T>, | ||
| scrollContainerRef?: React.RefObject<HTMLElement>, | ||
| ) { | ||
| const [sticky, setSticky] = useState(false); | ||
| const stickyRef = useLatest(sticky); | ||
|
|
||
| useEffectOnce(() => { | ||
| let rafId: number | null = null; | ||
| const scrollContainer = scrollContainerRef?.current ?? null; | ||
|
|
||
| const naturalTopFromContainer = | ||
| scrollContainer && elemRef.current | ||
| ? elemRef.current.getBoundingClientRect().top - | ||
| scrollContainer.getBoundingClientRect().top - | ||
| scrollContainer.clientTop | ||
| : null; | ||
|
Comment on lines
+25
to
+30
Member
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. I think it's better to move this to the |
||
|
|
||
| const getTarget = (eventName: keyof WindowEventMap): EventTarget => | ||
| CONTAINER_EVENTS.has(eventName) && scrollContainer ? scrollContainer : window; | ||
|
|
||
| observe(); | ||
|
|
||
| for (const eventName of REFLOW_EVENTS) { | ||
| window.addEventListener(eventName, scheduleObserve, true); | ||
| getTarget(eventName).addEventListener(eventName, scheduleObserve, true); | ||
| } | ||
|
|
||
| return () => { | ||
| if (rafId !== null) { | ||
| cancelAnimationFrame(rafId); | ||
| } | ||
| for (const eventName of REFLOW_EVENTS) { | ||
| window.removeEventListener(eventName, scheduleObserve, true); | ||
| getTarget(eventName).removeEventListener(eventName, scheduleObserve, true); | ||
| } | ||
| }; | ||
|
|
||
|
|
@@ -36,9 +57,15 @@ export function useSticky<T extends HTMLElement>(elemRef: React.RefObject<T>) { | |
| function observe() { | ||
| rafId = null; | ||
| if (!elemRef.current) return; | ||
| const refPageOffset = elemRef.current.getBoundingClientRect().top; | ||
| const stickyOffset = parseInt(getComputedStyle(elemRef.current).top, 10); | ||
| const stickyActive = refPageOffset <= stickyOffset; | ||
|
|
||
| let stickyActive: boolean; | ||
| if (scrollContainer !== null && naturalTopFromContainer !== null) { | ||
| stickyActive = scrollContainer.scrollTop >= naturalTopFromContainer - stickyOffset; | ||
| } else { | ||
| const refPageOffset = elemRef.current.getBoundingClientRect().top; | ||
| stickyActive = refPageOffset <= stickyOffset; | ||
| } | ||
|
|
||
| if (stickyActive && !stickyRef.current) setSticky(true); | ||
| else if (!stickyActive && stickyRef.current) setSticky(false); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please, move this story to
demo/src/stories/examples