143e2c802d
* Add common gutter styles as hook * Add computed scrollbar gutter CSS vars * Add new layout components * Replace layout components in settings screens * Remove old back button * Invert web border logic for easier migration * Clean up Slot API * Port over FF handling of scrollbar offset * Trade boilerplate for ease of use * Limit to one line * Allow two lines, fix wrapping * Fix alignment * sticky headers * set max with on header and center * [Layout] Notifications Header (#6910) * Replace notifications screen header * fix cropped indicator --------- Co-authored-by: Samuel Newman <mozzius@protonmail.com> * Replace Hashtag header (#6928) * [Layout] ChatList header (#6929) * Replace ChatList header * update chat settings as well --------- Co-authored-by: Samuel Newman <mozzius@protonmail.com> * Add web borders to Chat settings * Remove unused var * Move ChatList header outside center * Replace empty chat layout * fix breakpoints * [Layout] Scrollbar gutters (#6908) * Fix sidebar alignment * Make sure scrollbars don't hide * Gift left nav more space * Use stable one-edge, update logic in RightNav * Ope * Increase width * Reset * Add transform to sidebars * Remove bg in sidebars * Handle shifts in layout components * Replace scroll-removal handling * Make react-remove-scroll an explicit dep * Remove unused script * use correct scroll insets (#6950) * [Layout] Feeds headers (#6913) * Replace ViewHeader internals, duplicate old ViewHeader * Replace Feeds header * Replace SavedFeeds header * Visual alignment * Uglier but clear * Use old ViewHeader for SavedFeeds * use Layout.Center instead of Layout.Content * use left-aligned header for feed edit * delete unused old view header --------- Co-authored-by: Samuel Newman <mozzius@protonmail.com> * [Layout] Every other screen (#6953) * attempt to fix double borders on every other screen * delete ListHeaderDesktop * delete `SimpleViewHeader` and fix screens (#6956) * Make Layout.Center not full height * Refactor List to use Layout.Center, remove built-in borders * Fix Home screen * Refactor PagerWithHeader to use Layout components * Replace components in ProfileFeed and ProfileList * Borders on Profile * Search screen replacements * use new header for profile subpage header (#6958) * Search AutocompleteResults * use new header for starter pack wizard (#6957) * Fix post thread * Enable borders by default * Moderation muted and blocked accounts * Fix scrollbar offset on Labeler * Remove ScrollView from Moderation * Remove ScrollView from Deactivated * Remove ScrollView from onboarding * Remove ScrollView from SignupQueued * Mark deprecations * fix lint * Fix double borders on profile load * Remove unneeded CenteredView from noty Feed * Remove double Center layout on Notifications screen * Remove double Center layout on ChatList screen * Handle scrollbar offset in chat * Use new atom for other scrollbar offsets * Remove borders from old views * Better doc * Remove temp migration prop * Fix new atom usage on native * Clean up Hashtag screen * Layout docs * Clarify usage in Pager * Handle nested offset contexts * Clean up Layout * fix feeds page * asymmetric header on native (#6969) * Reusable header const * Fix up home header * Add back button to convo * Add hitslop to header buttons * Comment * Better handling on native for new atom * Format * Fix nested flatlist on mod screens * Use react-remove-scroll-bar directly * Fix notification count overflow on web * Clarify doc --------- Co-authored-by: Samuel Newman <mozzius@protonmail.com>
578 lines
15 KiB
TypeScript
578 lines
15 KiB
TypeScript
import React, {isValidElement, memo, startTransition, useRef} from 'react'
|
|
import {FlatListProps, StyleSheet, View, ViewProps} from 'react-native'
|
|
import {ReanimatedScrollEvent} from 'react-native-reanimated/lib/typescript/hook/commonTypes'
|
|
|
|
import {batchedUpdates} from '#/lib/batchedUpdates'
|
|
import {useNonReactiveCallback} from '#/lib/hooks/useNonReactiveCallback'
|
|
import {useScrollHandlers} from '#/lib/ScrollContext'
|
|
import {addStyle} from '#/lib/styles'
|
|
import * as Layout from '#/components/Layout'
|
|
|
|
export type ListMethods = any // TODO: Better types.
|
|
export type ListProps<ItemT> = Omit<
|
|
FlatListProps<ItemT>,
|
|
| 'onScroll' // Use ScrollContext instead.
|
|
| 'refreshControl' // Pass refreshing and/or onRefresh instead.
|
|
| 'contentOffset' // Pass headerOffset instead.
|
|
> & {
|
|
onScrolledDownChange?: (isScrolledDown: boolean) => void
|
|
headerOffset?: number
|
|
refreshing?: boolean
|
|
onRefresh?: () => void
|
|
onItemSeen?: (item: ItemT) => void
|
|
desktopFixedHeight?: number | boolean
|
|
// Web only prop to contain the scroll to the container rather than the window
|
|
disableFullWindowScroll?: boolean
|
|
/**
|
|
* @deprecated Should be using Layout components
|
|
*/
|
|
sideBorders?: boolean
|
|
}
|
|
export type ListRef = React.MutableRefObject<any | null> // TODO: Better types.
|
|
|
|
const ON_ITEM_SEEN_WAIT_DURATION = 0.5e3 // when we consider post to be "seen"
|
|
const ON_ITEM_SEEN_INTERSECTION_OPTS = {
|
|
rootMargin: '-200px 0px -200px 0px',
|
|
} // post must be 200px visible to be "seen"
|
|
|
|
function ListImpl<ItemT>(
|
|
{
|
|
ListHeaderComponent,
|
|
ListFooterComponent,
|
|
ListEmptyComponent,
|
|
disableFullWindowScroll,
|
|
contentContainerStyle,
|
|
data,
|
|
desktopFixedHeight,
|
|
headerOffset,
|
|
keyExtractor,
|
|
refreshing: _unsupportedRefreshing,
|
|
onStartReached,
|
|
onStartReachedThreshold = 2,
|
|
onEndReached,
|
|
onEndReachedThreshold = 2,
|
|
onRefresh: _unsupportedOnRefresh,
|
|
onScrolledDownChange,
|
|
onContentSizeChange,
|
|
onItemSeen,
|
|
renderItem,
|
|
extraData,
|
|
style,
|
|
...props
|
|
}: ListProps<ItemT>,
|
|
ref: React.Ref<ListMethods>,
|
|
) {
|
|
const contextScrollHandlers = useScrollHandlers()
|
|
|
|
const isEmpty = !data || data.length === 0
|
|
|
|
let headerComponent: JSX.Element | null = null
|
|
if (ListHeaderComponent != null) {
|
|
if (isValidElement(ListHeaderComponent)) {
|
|
headerComponent = ListHeaderComponent
|
|
} else {
|
|
// @ts-ignore Nah it's fine.
|
|
headerComponent = <ListHeaderComponent />
|
|
}
|
|
}
|
|
|
|
let footerComponent: JSX.Element | null = null
|
|
if (ListFooterComponent != null) {
|
|
if (isValidElement(ListFooterComponent)) {
|
|
footerComponent = ListFooterComponent
|
|
} else {
|
|
// @ts-ignore Nah it's fine.
|
|
footerComponent = <ListFooterComponent />
|
|
}
|
|
}
|
|
|
|
let emptyComponent: JSX.Element | null = null
|
|
if (ListEmptyComponent != null) {
|
|
if (isValidElement(ListEmptyComponent)) {
|
|
emptyComponent = ListEmptyComponent
|
|
} else {
|
|
// @ts-ignore Nah it's fine.
|
|
emptyComponent = <ListEmptyComponent />
|
|
}
|
|
}
|
|
|
|
if (headerOffset != null) {
|
|
style = addStyle(style, {
|
|
paddingTop: headerOffset,
|
|
})
|
|
}
|
|
|
|
const getScrollableNode = React.useCallback(() => {
|
|
if (disableFullWindowScroll) {
|
|
const element = nativeRef.current as HTMLDivElement | null
|
|
if (!element) return
|
|
|
|
return {
|
|
get scrollWidth() {
|
|
return element.scrollWidth
|
|
},
|
|
get scrollHeight() {
|
|
return element.scrollHeight
|
|
},
|
|
get clientWidth() {
|
|
return element.clientWidth
|
|
},
|
|
get clientHeight() {
|
|
return element.clientHeight
|
|
},
|
|
get scrollY() {
|
|
return element.scrollTop
|
|
},
|
|
get scrollX() {
|
|
return element.scrollLeft
|
|
},
|
|
scrollTo(options?: ScrollToOptions) {
|
|
element.scrollTo(options)
|
|
},
|
|
scrollBy(options: ScrollToOptions) {
|
|
element.scrollBy(options)
|
|
},
|
|
addEventListener(event: string, handler: any) {
|
|
element.addEventListener(event, handler)
|
|
},
|
|
removeEventListener(event: string, handler: any) {
|
|
element.removeEventListener(event, handler)
|
|
},
|
|
}
|
|
} else {
|
|
return {
|
|
get scrollWidth() {
|
|
return document.documentElement.scrollWidth
|
|
},
|
|
get scrollHeight() {
|
|
return document.documentElement.scrollHeight
|
|
},
|
|
get clientWidth() {
|
|
return window.innerWidth
|
|
},
|
|
get clientHeight() {
|
|
return window.innerHeight
|
|
},
|
|
get scrollY() {
|
|
return window.scrollY
|
|
},
|
|
get scrollX() {
|
|
return window.scrollX
|
|
},
|
|
scrollTo(options: ScrollToOptions) {
|
|
window.scrollTo(options)
|
|
},
|
|
scrollBy(options: ScrollToOptions) {
|
|
window.scrollBy(options)
|
|
},
|
|
addEventListener(event: string, handler: any) {
|
|
window.addEventListener(event, handler)
|
|
},
|
|
removeEventListener(event: string, handler: any) {
|
|
window.removeEventListener(event, handler)
|
|
},
|
|
}
|
|
}
|
|
}, [disableFullWindowScroll])
|
|
|
|
const nativeRef = React.useRef<HTMLDivElement>(null)
|
|
React.useImperativeHandle(
|
|
ref,
|
|
() =>
|
|
({
|
|
scrollToTop() {
|
|
getScrollableNode()?.scrollTo({top: 0})
|
|
},
|
|
|
|
scrollToOffset({
|
|
animated,
|
|
offset,
|
|
}: {
|
|
animated: boolean
|
|
offset: number
|
|
}) {
|
|
getScrollableNode()?.scrollTo({
|
|
left: 0,
|
|
top: offset,
|
|
behavior: animated ? 'smooth' : 'instant',
|
|
})
|
|
},
|
|
scrollToEnd({animated = true}: {animated?: boolean}) {
|
|
const element = getScrollableNode()
|
|
element?.scrollTo({
|
|
left: 0,
|
|
top: element.scrollHeight,
|
|
behavior: animated ? 'smooth' : 'instant',
|
|
})
|
|
},
|
|
} as any), // TODO: Better types.
|
|
[getScrollableNode],
|
|
)
|
|
|
|
// --- onContentSizeChange, maintainVisibleContentPosition ---
|
|
const containerRef = useRef(null)
|
|
useResizeObserver(containerRef, onContentSizeChange)
|
|
|
|
// --- onScroll ---
|
|
const [isInsideVisibleTree, setIsInsideVisibleTree] = React.useState(false)
|
|
const handleScroll = useNonReactiveCallback(() => {
|
|
if (!isInsideVisibleTree) return
|
|
|
|
const element = getScrollableNode()
|
|
contextScrollHandlers.onScroll?.(
|
|
{
|
|
contentOffset: {
|
|
x: Math.max(0, element?.scrollX ?? 0),
|
|
y: Math.max(0, element?.scrollY ?? 0),
|
|
},
|
|
layoutMeasurement: {
|
|
width: element?.clientWidth,
|
|
height: element?.clientHeight,
|
|
},
|
|
contentSize: {
|
|
width: element?.scrollWidth,
|
|
height: element?.scrollHeight,
|
|
},
|
|
} as Exclude<
|
|
ReanimatedScrollEvent,
|
|
| 'velocity'
|
|
| 'eventName'
|
|
| 'zoomScale'
|
|
| 'targetContentOffset'
|
|
| 'contentInset'
|
|
>,
|
|
null as any,
|
|
)
|
|
})
|
|
|
|
React.useEffect(() => {
|
|
if (!isInsideVisibleTree) {
|
|
// Prevents hidden tabs from firing scroll events.
|
|
// Only one list is expected to be firing these at a time.
|
|
return
|
|
}
|
|
|
|
const element = getScrollableNode()
|
|
|
|
element?.addEventListener('scroll', handleScroll)
|
|
return () => {
|
|
element?.removeEventListener('scroll', handleScroll)
|
|
}
|
|
}, [
|
|
isInsideVisibleTree,
|
|
handleScroll,
|
|
disableFullWindowScroll,
|
|
getScrollableNode,
|
|
])
|
|
|
|
// --- onScrolledDownChange ---
|
|
const isScrolledDown = useRef(false)
|
|
function handleAboveTheFoldVisibleChange(isAboveTheFold: boolean) {
|
|
const didScrollDown = !isAboveTheFold
|
|
if (isScrolledDown.current !== didScrollDown) {
|
|
isScrolledDown.current = didScrollDown
|
|
startTransition(() => {
|
|
onScrolledDownChange?.(didScrollDown)
|
|
})
|
|
}
|
|
}
|
|
|
|
// --- onStartReached ---
|
|
const onHeadVisibilityChange = useNonReactiveCallback(
|
|
(isHeadVisible: boolean) => {
|
|
if (isHeadVisible) {
|
|
onStartReached?.({
|
|
distanceFromStart: onStartReachedThreshold || 0,
|
|
})
|
|
}
|
|
},
|
|
)
|
|
|
|
// --- onEndReached ---
|
|
const onTailVisibilityChange = useNonReactiveCallback(
|
|
(isTailVisible: boolean) => {
|
|
if (isTailVisible) {
|
|
onEndReached?.({
|
|
distanceFromEnd: onEndReachedThreshold || 0,
|
|
})
|
|
}
|
|
},
|
|
)
|
|
|
|
return (
|
|
<View
|
|
{...props}
|
|
style={[
|
|
style,
|
|
disableFullWindowScroll && {
|
|
flex: 1,
|
|
// @ts-expect-error web only
|
|
'overflow-y': 'scroll',
|
|
},
|
|
]}
|
|
ref={nativeRef as any}>
|
|
<Visibility
|
|
onVisibleChange={setIsInsideVisibleTree}
|
|
style={
|
|
// This has position: fixed, so it should always report as visible
|
|
// unless we're within a display: none tree (like a hidden tab).
|
|
styles.parentTreeVisibilityDetector
|
|
}
|
|
/>
|
|
<Layout.Center>
|
|
<View
|
|
ref={containerRef}
|
|
style={[
|
|
contentContainerStyle,
|
|
desktopFixedHeight ? styles.minHeightViewport : null,
|
|
]}>
|
|
<Visibility
|
|
root={disableFullWindowScroll ? nativeRef : null}
|
|
onVisibleChange={handleAboveTheFoldVisibleChange}
|
|
style={[styles.aboveTheFoldDetector, {height: headerOffset}]}
|
|
/>
|
|
{onStartReached && !isEmpty && (
|
|
<EdgeVisibility
|
|
root={disableFullWindowScroll ? nativeRef : null}
|
|
onVisibleChange={onHeadVisibilityChange}
|
|
topMargin={(onStartReachedThreshold ?? 0) * 100 + '%'}
|
|
containerRef={containerRef}
|
|
/>
|
|
)}
|
|
{headerComponent}
|
|
{isEmpty
|
|
? emptyComponent
|
|
: (data as Array<ItemT>)?.map((item, index) => {
|
|
const key = keyExtractor!(item, index)
|
|
return (
|
|
<Row<ItemT>
|
|
key={key}
|
|
item={item}
|
|
index={index}
|
|
renderItem={renderItem}
|
|
extraData={extraData}
|
|
onItemSeen={onItemSeen}
|
|
/>
|
|
)
|
|
})}
|
|
{onEndReached && !isEmpty && (
|
|
<EdgeVisibility
|
|
root={disableFullWindowScroll ? nativeRef : null}
|
|
onVisibleChange={onTailVisibilityChange}
|
|
bottomMargin={(onEndReachedThreshold ?? 0) * 100 + '%'}
|
|
containerRef={containerRef}
|
|
/>
|
|
)}
|
|
{footerComponent}
|
|
</View>
|
|
</Layout.Center>
|
|
</View>
|
|
)
|
|
}
|
|
|
|
function EdgeVisibility({
|
|
root,
|
|
topMargin,
|
|
bottomMargin,
|
|
containerRef,
|
|
onVisibleChange,
|
|
}: {
|
|
root?: React.RefObject<HTMLDivElement> | null
|
|
topMargin?: string
|
|
bottomMargin?: string
|
|
containerRef: React.RefObject<Element>
|
|
onVisibleChange: (isVisible: boolean) => void
|
|
}) {
|
|
const [containerHeight, setContainerHeight] = React.useState(0)
|
|
useResizeObserver(containerRef, (w, h) => {
|
|
setContainerHeight(h)
|
|
})
|
|
return (
|
|
<Visibility
|
|
key={containerHeight}
|
|
root={root}
|
|
topMargin={topMargin}
|
|
bottomMargin={bottomMargin}
|
|
onVisibleChange={onVisibleChange}
|
|
/>
|
|
)
|
|
}
|
|
|
|
function useResizeObserver(
|
|
ref: React.RefObject<Element>,
|
|
onResize: undefined | ((w: number, h: number) => void),
|
|
) {
|
|
const handleResize = useNonReactiveCallback(onResize ?? (() => {}))
|
|
const isActive = !!onResize
|
|
React.useEffect(() => {
|
|
if (!isActive) {
|
|
return
|
|
}
|
|
const resizeObserver = new ResizeObserver(entries => {
|
|
batchedUpdates(() => {
|
|
for (let entry of entries) {
|
|
const rect = entry.contentRect
|
|
handleResize(rect.width, rect.height)
|
|
}
|
|
})
|
|
})
|
|
const node = ref.current!
|
|
resizeObserver.observe(node)
|
|
return () => {
|
|
resizeObserver.unobserve(node)
|
|
}
|
|
}, [handleResize, isActive, ref])
|
|
}
|
|
|
|
let Row = function RowImpl<ItemT>({
|
|
item,
|
|
index,
|
|
renderItem,
|
|
extraData: _unused,
|
|
onItemSeen,
|
|
}: {
|
|
item: ItemT
|
|
index: number
|
|
renderItem:
|
|
| null
|
|
| undefined
|
|
| ((data: {index: number; item: any; separators: any}) => React.ReactNode)
|
|
extraData: any
|
|
onItemSeen: ((item: any) => void) | undefined
|
|
}): React.ReactNode {
|
|
const rowRef = React.useRef(null)
|
|
const intersectionTimeout = React.useRef<
|
|
ReturnType<typeof setTimeout> | undefined
|
|
>(undefined)
|
|
|
|
const handleIntersection = useNonReactiveCallback(
|
|
(entries: IntersectionObserverEntry[]) => {
|
|
batchedUpdates(() => {
|
|
if (!onItemSeen) {
|
|
return
|
|
}
|
|
entries.forEach(entry => {
|
|
if (entry.isIntersecting) {
|
|
if (!intersectionTimeout.current) {
|
|
intersectionTimeout.current = setTimeout(() => {
|
|
intersectionTimeout.current = undefined
|
|
onItemSeen!(item)
|
|
}, ON_ITEM_SEEN_WAIT_DURATION)
|
|
}
|
|
} else {
|
|
if (intersectionTimeout.current) {
|
|
clearTimeout(intersectionTimeout.current as NodeJS.Timeout)
|
|
intersectionTimeout.current = undefined
|
|
}
|
|
}
|
|
})
|
|
})
|
|
},
|
|
)
|
|
|
|
React.useEffect(() => {
|
|
if (!onItemSeen) {
|
|
return
|
|
}
|
|
const observer = new IntersectionObserver(
|
|
handleIntersection,
|
|
ON_ITEM_SEEN_INTERSECTION_OPTS,
|
|
)
|
|
const row: Element | null = rowRef.current!
|
|
observer.observe(row)
|
|
return () => {
|
|
observer.unobserve(row)
|
|
}
|
|
}, [handleIntersection, onItemSeen])
|
|
|
|
if (!renderItem) {
|
|
return null
|
|
}
|
|
|
|
return (
|
|
<View ref={rowRef}>
|
|
{renderItem({item, index, separators: null as any})}
|
|
</View>
|
|
)
|
|
}
|
|
Row = React.memo(Row)
|
|
|
|
let Visibility = ({
|
|
root,
|
|
topMargin = '0px',
|
|
bottomMargin = '0px',
|
|
onVisibleChange,
|
|
style,
|
|
}: {
|
|
root?: React.RefObject<HTMLDivElement> | null
|
|
topMargin?: string
|
|
bottomMargin?: string
|
|
onVisibleChange: (isVisible: boolean) => void
|
|
style?: ViewProps['style']
|
|
}): React.ReactNode => {
|
|
const tailRef = React.useRef(null)
|
|
const isIntersecting = React.useRef(false)
|
|
|
|
const handleIntersection = useNonReactiveCallback(
|
|
(entries: IntersectionObserverEntry[]) => {
|
|
batchedUpdates(() => {
|
|
entries.forEach(entry => {
|
|
if (entry.isIntersecting !== isIntersecting.current) {
|
|
isIntersecting.current = entry.isIntersecting
|
|
onVisibleChange(entry.isIntersecting)
|
|
}
|
|
})
|
|
})
|
|
},
|
|
)
|
|
|
|
React.useEffect(() => {
|
|
const observer = new IntersectionObserver(handleIntersection, {
|
|
root: root?.current ?? null,
|
|
rootMargin: `${topMargin} 0px ${bottomMargin} 0px`,
|
|
})
|
|
const tail: Element | null = tailRef.current!
|
|
observer.observe(tail)
|
|
return () => {
|
|
observer.unobserve(tail)
|
|
}
|
|
}, [bottomMargin, handleIntersection, topMargin, root])
|
|
|
|
return (
|
|
<View ref={tailRef} style={addStyle(styles.visibilityDetector, style)} />
|
|
)
|
|
}
|
|
Visibility = React.memo(Visibility)
|
|
|
|
export const List = memo(React.forwardRef(ListImpl)) as <ItemT>(
|
|
props: ListProps<ItemT> & {ref?: React.Ref<ListMethods>},
|
|
) => React.ReactElement
|
|
|
|
// https://stackoverflow.com/questions/7944460/detect-safari-browser
|
|
|
|
const styles = StyleSheet.create({
|
|
minHeightViewport: {
|
|
// @ts-ignore web only
|
|
minHeight: '100vh',
|
|
},
|
|
parentTreeVisibilityDetector: {
|
|
// @ts-ignore web only
|
|
position: 'fixed',
|
|
top: 0,
|
|
left: 0,
|
|
right: 0,
|
|
bottom: 0,
|
|
},
|
|
aboveTheFoldDetector: {
|
|
position: 'absolute',
|
|
top: 0,
|
|
left: 0,
|
|
right: 0,
|
|
// Bottom is dynamic.
|
|
},
|
|
visibilityDetector: {
|
|
pointerEvents: 'none',
|
|
zIndex: -1,
|
|
},
|
|
})
|