-
Notifications
You must be signed in to change notification settings - Fork 185
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
fix(CustomScrollView): add resize container handler #7060
Merged
inomdzhon
merged 9 commits into
master
from
e.muhamethanov/7043/custom-scroll-view-resize
Jun 24, 2024
+81
−2
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1dd32c2
fix(CustomScrollView): add resize container handler
EldarMuhamethanov af0c7df
fix: improve code of useResizeObserver
EldarMuhamethanov ea5bb1d
test(useResizeObserver): add tests
EldarMuhamethanov 590620f
fix: run prettier
EldarMuhamethanov 3796377
feat: remake of using useResizeObserver
EldarMuhamethanov c6b95b5
fix: run prettier and fix test
EldarMuhamethanov 9416916
fix: remove getBoundingClientRect call
EldarMuhamethanov 4f21f25
fix: add call callback with element
EldarMuhamethanov 4a7abce
fix: add ignore coverage to useResizeObserver
EldarMuhamethanov 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { act, useRef } from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import { useResizeObserver } from './useResizeObserver'; | ||
|
||
describe('test useResizeObserver', () => { | ||
it('should call callback when add block', async () => { | ||
const callback = jest.fn(); | ||
const Fixture = (props: { mockedBlocksIds: string[] }) => { | ||
const ref = useRef(null); | ||
useResizeObserver(ref, callback); | ||
return ( | ||
<div ref={ref} style={{ position: 'static' }}> | ||
{props.mockedBlocksIds.map((id) => ( | ||
<div key={id} data-testid={id} style={{ height: 50 }}></div> | ||
))} | ||
</div> | ||
); | ||
}; | ||
|
||
const result = render(<Fixture mockedBlocksIds={['block-1']} />); | ||
|
||
await act(async () => { | ||
result.rerender(<Fixture mockedBlocksIds={['block-1', 'block-2']} />); | ||
}); | ||
|
||
expect(callback).toHaveBeenCalledTimes(1); | ||
expect(screen.getByTestId('block-2')).toBeInTheDocument(); | ||
}); | ||
}); |
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,30 @@ | ||
import * as React from 'react'; | ||
import { CustomResizeObserver } from '../lib/floating/customResizeObserver'; | ||
import { useIsomorphicLayoutEffect } from '../lib/useIsomorphicLayoutEffect'; | ||
import { useStableCallback } from './useStableCallback'; | ||
|
||
/** | ||
* Хук вызывает переданный коллбэк при изменении размеров элемента. | ||
*/ | ||
export function useResizeObserver( | ||
ref: React.MutableRefObject<HTMLElement | null>, | ||
callback: (element: HTMLElement) => void, | ||
) { | ||
const stableCallback = useStableCallback(callback); | ||
|
||
useIsomorphicLayoutEffect( | ||
function addResizeObserverHandler() { | ||
/* istanbul ignore if: невозможный кейс (в SSR вызова этой функции не будет) */ | ||
if (!ref.current) { | ||
EldarMuhamethanov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
const element = ref.current; | ||
const observer = new CustomResizeObserver(() => stableCallback(element)); | ||
inomdzhon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
observer.observe(element); | ||
observer.appendToTheDOM(); | ||
|
||
return () => observer.disconnect(); | ||
}, | ||
[ref], | ||
); | ||
} |
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
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.
@EldarMuhamethanov а добавьте
как у родителей, а то "растянутость" на контейнер теряется.
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.
@Semigradsky задумка была, такая что этот элемент, растягивается на реальные размеры контента, а не на размеры родителя. Мне кажется так сломается изначальный кейс. Можешь рассказать какой кейс сейчас ломается? если такой есть
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.
Например, растягиваем контейнер на всю страницу, у содержимого
min-height: 100%
. До этих исправлений отображался кастомный скролл, если содержимое занимает бОльшее пространство.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.
#7097 - PR с правкой