Repository navigation
feat(h5): support ScrollView anchor offsets #19706
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| /** Scroll only the supplied container, applying an offset to the aligned target position. */ | ||
| export function scrollIntoViewWithOffset ( | ||
| container: HTMLElement, | ||
| target: HTMLElement, | ||
| options: { | ||
| offset: number | ||
| scrollX: boolean | ||
| scrollY: boolean | ||
| alignment?: ScrollLogicalPosition | ||
| behavior: ScrollBehavior | ||
| } | ||
| ) { | ||
| const { offset, scrollX, scrollY, alignment, behavior } = options | ||
| const outer = container.getBoundingClientRect() | ||
| const inner = target.getBoundingClientRect() | ||
|
|
||
| function delta (start: number, size: number, extent: number, align: ScrollLogicalPosition) { | ||
| if (align === 'center') return start - (extent - size) / 2 | ||
| if (align === 'end') return start - extent + size | ||
| if (align === 'nearest') { | ||
| if (start >= 0 && start + size <= extent) return 0 | ||
| if (start < 0 && start + size > extent) return 0 | ||
| return (start < 0 && size <= extent) || (start >= 0 && size > extent) ? start : start + size - extent | ||
| } | ||
| return start | ||
| } | ||
|
|
||
| const position: ScrollToOptions = { behavior } | ||
| if (scrollY) { | ||
| position.top = container.scrollTop + delta( | ||
| inner.top - outer.top - container.clientTop, inner.height, container.clientHeight, alignment || 'center' | ||
| ) + offset | ||
|
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.
With Useful? React with 👍 / 👎. |
||
| } | ||
| if (scrollX) { | ||
| position.left = container.scrollLeft + delta( | ||
| inner.left - outer.left - container.clientLeft, inner.width, container.clientWidth, alignment || 'start' | ||
| ) + offset | ||
| } | ||
| container.scrollTo(position) | ||
|
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.
For a Stencil ScrollView retrieved through Useful? React with 👍 / 👎. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import './style/index.scss' | ||
|
|
||
| import { isFunction } from '@tarojs/shared' | ||
| import { isFunction, scrollIntoViewWithOffset } from '@tarojs/shared' | ||
| import classNames from 'classnames' | ||
|
|
||
| import { ScrollElementContext } from '../../contexts/ScrollElementContext' | ||
|
|
@@ -39,8 +39,23 @@ function scrollIntoView ( | |
| animated = true, | ||
| scrollX = false, | ||
| scrollY = false, | ||
| scrollIntoViewAlignment?: ScrollLogicalPosition | ||
| scrollIntoViewAlignment?: ScrollLogicalPosition, | ||
| container?: HTMLElement | null, | ||
| offset?: number | ||
| ) { | ||
| if (container && offset !== undefined) { | ||
| const target = Array.from(container.querySelectorAll<HTMLElement>('[id]')).find(node => node.id === id) | ||
| if (target) { | ||
| scrollIntoViewWithOffset(container, target, { | ||
| offset: Number.isFinite(offset) ? offset : 0, | ||
| scrollX, | ||
| scrollY, | ||
| alignment: scrollIntoViewAlignment, | ||
| behavior: animated ? 'smooth' : 'auto', | ||
| }) | ||
| } | ||
| return | ||
| } | ||
| document.querySelector(`#${id}`)?.scrollIntoView({ | ||
| behavior: animated ? 'smooth' : 'auto', | ||
| block: scrollY ? (scrollIntoViewAlignment || 'center') : 'nearest', | ||
|
|
@@ -79,6 +94,7 @@ interface IProps extends React.HTMLAttributes<HTMLDivElement> { | |
| scrollLeft: number | ||
| scrollIntoView?: string | ||
| scrollIntoViewAlignment?: ScrollLogicalPosition | ||
| scrollIntoViewOffset?: number | ||
| scrollWithAnimation: boolean | ||
| enableBackToTop?: boolean | ||
| forwardedRef?: React.MutableRefObject<HTMLDivElement> | ||
|
|
@@ -135,7 +151,7 @@ function ScrollView (props: IProps) { | |
| typeof props.scrollIntoView === 'string' && | ||
| document && | ||
| document.querySelector && | ||
| document.querySelector(`#${props.scrollIntoView}`) | ||
| (props.scrollIntoViewOffset !== undefined || document.querySelector(`#${props.scrollIntoView}`)) | ||
|
Contributor
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. 确认容器内目标存在后,再选择锚点分支。 如果 此前,目标不存在时会进入 Also applies to: 201-201
|
||
| ) { | ||
| if (isInit) { | ||
| setTimeout( | ||
|
|
@@ -145,7 +161,9 @@ function ScrollView (props: IProps) { | |
| props.scrollWithAnimation, | ||
| !!props.scrollX, | ||
| !!props.scrollY, | ||
| props.scrollIntoViewAlignment | ||
| props.scrollIntoViewAlignment, | ||
| container.current, | ||
| props.scrollIntoViewOffset | ||
|
Contributor
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. 延迟初始化定位不能覆盖挂载后更新的 offset。 挂载时的 effect 调用 如果 offset 在挂载后 500 ms 内从 请确保延迟初始化定位不会覆盖之后的定位参数更新。可选做法如下:
若挂载后没有发生更新,仍应保留 500 ms 的延迟定位。
|
||
| ), | ||
| 500 | ||
| ) | ||
|
|
@@ -155,7 +173,9 @@ function ScrollView (props: IProps) { | |
| props.scrollWithAnimation, | ||
| !!props.scrollX, | ||
| !!props.scrollY, | ||
| props.scrollIntoViewAlignment | ||
| props.scrollIntoViewAlignment, | ||
| container.current, | ||
| props.scrollIntoViewOffset | ||
| ) | ||
| } | ||
| } else { | ||
|
|
@@ -178,17 +198,19 @@ function ScrollView (props: IProps) { | |
| typeof sitv === 'string' && | ||
| document && | ||
| document.querySelector && | ||
| document.querySelector(`#${sitv}`) | ||
| (props.scrollIntoViewOffset !== undefined || document.querySelector(`#${sitv}`)) | ||
|
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.
If the requested anchor is temporarily absent, supplying an offset now makes Useful? React with 👍 / 👎. |
||
| ) | ||
|
|
||
| if (sitvOk) { | ||
| if (sitv !== lastAppliedScrollIntoViewIdRef.current) { | ||
| if (sitv !== lastAppliedScrollIntoViewIdRef.current || props.scrollIntoViewOffset !== undefined) { | ||
|
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.
When a parent keeps Useful? React with 👍 / 👎.
Contributor
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. 只在锚点定位参数变化时重新定位。 如果 例如,同一锚点位于 请记录已应用的锚点定位参数。只有这些参数变化时才重新定位;仅受控坐标变化时,应保留原有受控分支。
|
||
| scrollIntoView( | ||
| sitv, | ||
| props.scrollWithAnimation, | ||
| !!props.scrollX, | ||
| !!props.scrollY, | ||
| props.scrollIntoViewAlignment | ||
| props.scrollIntoViewAlignment, | ||
| container.current, | ||
| props.scrollIntoViewOffset | ||
| ) | ||
| lastAppliedScrollIntoViewIdRef.current = typeof sitv === 'string' ? sitv : null | ||
| } else { | ||
|
|
@@ -203,6 +225,7 @@ function ScrollView (props: IProps) { | |
| props.scrollLeft, | ||
| props.scrollIntoView, | ||
| props.scrollIntoViewAlignment, | ||
| props.scrollIntoViewOffset, | ||
| props.scrollWithAnimation, | ||
| props.scrollX, | ||
| props.scrollY | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { h } from '@stencil/core' | ||
| import { newSpecPage } from '@stencil/core/testing' | ||
|
|
||
| import { ScrollView } from '../src/components/scroll-view/scroll-view' | ||
|
|
||
| it('scrolls the stencil container with an offset', async () => { | ||
| const page = await newSpecPage({ | ||
| components: [ScrollView], | ||
| template: () => <taro-scroll-view-core scrollY mpScrollIntoViewOffset={-20} mpScrollIntoViewAlignment='start'> | ||
| <div id='anchor'>target</div> | ||
| </taro-scroll-view-core>, | ||
| }) | ||
| const root = page.root! | ||
| const target = root.querySelector<HTMLElement>('#anchor')! | ||
| root.getBoundingClientRect = () => ({ top: 10, left: 0 }) as DOMRect | ||
| target.getBoundingClientRect = () => ({ top: 160, left: 0, width: 20, height: 20 }) as DOMRect | ||
| Object.defineProperty(root, 'clientTop', { value: 0 }) | ||
| Object.defineProperty(root, 'clientHeight', { value: 100 }) | ||
| root.scrollTop = 0 | ||
| root.scrollTo = jest.fn() | ||
| await page.rootInstance.mpScrollIntoViewMethod('anchor') | ||
| expect(root.scrollTo).toHaveBeenCalledWith({ top: 130, behavior: 'auto' }) | ||
| }) |
Uh oh!
There was an error while loading. Please reload this page.