Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Revert "[PAY-1534] Allow Popup to be mounted inside a container (#3669)" #3715

Merged
merged 1 commit into from
Jul 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 14 additions & 31 deletions packages/stems/src/components/Popup/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
forwardRef,
RefObject,
MutableRefObject,
useCallback,
useEffect,
useRef,
Expand Down Expand Up @@ -106,7 +106,7 @@ const getComputedOrigins = (
transformOrigin: Origin,
anchorRect: DOMRect,
wrapperRect: DOMRect,
containerRef?: RefObject<HTMLElement>
containerRef?: MutableRefObject<HTMLDivElement | undefined>
) => {
if (!anchorRect || !wrapperRect) return { anchorOrigin, transformOrigin }

Expand Down Expand Up @@ -152,7 +152,6 @@ const getComputedOrigins = (
anchorOrigin.vertical = 'top'
transformOrigin.vertical = 'bottom'
}

return { anchorOrigin, transformOrigin }
}

Expand All @@ -163,13 +162,12 @@ const getComputedOrigins = (
const getAdjustedPosition = (
top: number,
left: number,
wrapperRect: DOMRect,
containerRect: Pick<DOMRect, 'width' | 'height'>
wrapperRect: DOMRect
): { adjustedTop: number; adjustedLeft: number } => {
if (!wrapperRect) return { adjustedTop: 0, adjustedLeft: 0 }

const containerWidth = containerRect.width - CONTAINER_INSET_PADDING
const containerHeight = containerRect.height - CONTAINER_INSET_PADDING
const containerWidth = window.innerWidth - CONTAINER_INSET_PADDING
const containerHeight = window.innerHeight - CONTAINER_INSET_PADDING

const overflowRight = left + wrapperRect.width > containerWidth
const overflowLeft = left < 0
Expand Down Expand Up @@ -258,10 +256,8 @@ export const Popup = forwardRef<HTMLDivElement, PopupProps>(function Popup(
className,
wrapperClassName,
zIndex,
containerRef,
mountRef
containerRef
} = props

const handleClose = useCallback(() => {
onClose()
setTimeout(() => {
Expand Down Expand Up @@ -293,19 +289,11 @@ export const Popup = forwardRef<HTMLDivElement, PopupProps>(function Popup(
// On visible, set the position
useEffect(() => {
if (isVisible) {
const [anchorRect, wrapperRect, mountRect] = [
anchorRef,
wrapperRef,
mountRef
].map((r) => r?.current?.getBoundingClientRect())
const [anchorRect, wrapperRect] = [anchorRef, wrapperRef].map((r) =>
r?.current?.getBoundingClientRect()
)
if (!anchorRect || !wrapperRect) return

// If using a mount source, subtract off the position of the containing mount
if (mountRect) {
anchorRect.x -= mountRect.x
anchorRect.y -= mountRect.y
}

const {
anchorOrigin: anchorOriginComputed,
transformOrigin: transformOriginComputed
Expand Down Expand Up @@ -333,11 +321,7 @@ export const Popup = forwardRef<HTMLDivElement, PopupProps>(function Popup(
const { adjustedTop, adjustedLeft } = getAdjustedPosition(
top,
left,
wrapperRect,
{
width: containerRef?.current?.clientWidth ?? window.innerWidth,
height: containerRef?.current?.clientHeight ?? window.innerHeight
}
wrapperRect
)

if (wrapperRef.current) {
Expand All @@ -355,8 +339,7 @@ export const Popup = forwardRef<HTMLDivElement, PopupProps>(function Popup(
transformOrigin,
setComputedTransformOrigin,
originalTopPosition,
containerRef,
mountRef
containerRef
])

// Callback invoked on each scroll. Uses original top position to scroll with content.
Expand All @@ -365,13 +348,13 @@ export const Popup = forwardRef<HTMLDivElement, PopupProps>(function Popup(
const watchScroll = useCallback(
(scrollParent: Element, initialScrollPosition: number) => {
const scrollTop = scrollParent.scrollTop
if (wrapperRef.current && !mountRef?.current) {
if (wrapperRef.current) {
wrapperRef.current.style.top = `${
originalTopPosition.current - scrollTop + initialScrollPosition
}px`
}
},
[wrapperRef, originalTopPosition, mountRef]
[wrapperRef, originalTopPosition]
)

// Set up scroll listeners
Expand Down Expand Up @@ -469,7 +452,7 @@ export const Popup = forwardRef<HTMLDivElement, PopupProps>(function Popup(
) : null
)}
</div>,
mountRef?.current ?? document.body
document.body
)}
</>
)
Expand Down
13 changes: 4 additions & 9 deletions packages/stems/src/components/Popup/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ReactNode, RefObject } from 'react'
import { MutableRefObject, ReactChild } from 'react'

export enum Position {
TOP_LEFT = 'topLeft',
Expand All @@ -18,7 +18,7 @@ export type PopupProps = {
/**
* A ref to the element whose position will be used to anchor the Popup
*/
anchorRef: RefObject<HTMLElement>
anchorRef: MutableRefObject<HTMLElement | null>

/**
* Duration of the animations in ms
Expand All @@ -35,7 +35,7 @@ export type PopupProps = {
/**
* Children to render inside the Popup
*/
children: ReactNode
children: ReactChild

/**
* Class name to apply to the popup itself
Expand All @@ -52,7 +52,7 @@ export type PopupProps = {
* to be the size of the container it belongs to. If the popup expands outside
* the bounds of the container, it repositions itself.
*/
containerRef?: RefObject<HTMLElement>
containerRef?: MutableRefObject<HTMLDivElement | undefined>

/**
* Boolean representing whether the Popup is visible
Expand Down Expand Up @@ -110,11 +110,6 @@ export type PopupProps = {
* An optional z-index to override the default of 10000
*/
zIndex?: number

/**
* The element to portal to
*/
mountRef?: RefObject<HTMLElement>
}

export const popupDefaultProps: Partial<PopupProps> = {
Expand Down
4 changes: 1 addition & 3 deletions packages/stems/src/components/PopupMenu/PopupMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ export const PopupMenu = forwardRef<HTMLDivElement, PopupMenuProps>(
containerRef,
anchorOrigin,
transformOrigin,
id,
mountRef
id
} = props
const clickInsideRef = useRef<any>()
const anchorRef = useRef<HTMLElement>(null)
Expand Down Expand Up @@ -84,7 +83,6 @@ export const PopupMenu = forwardRef<HTMLDivElement, PopupMenuProps>(
transformOrigin={transformOrigin}
anchorOrigin={anchorOrigin}
wrapperClassName={styles.popup}
mountRef={mountRef}
>
<ul
className={styles.menu}
Expand Down
1 change: 0 additions & 1 deletion packages/stems/src/components/PopupMenu/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ type ApplicablePopupProps = Pick<
| 'containerRef'
| 'transformOrigin'
| 'anchorOrigin'
| 'mountRef'
>

export type PopupMenuProps = {
Expand Down
4 changes: 1 addition & 3 deletions packages/stems/src/utils/scrollParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ export const getScrollParent = (node: HTMLElement): Element | null => {
style(_node, 'overflow') +
style(_node, 'overflow-y') +
style(_node, 'overflow-x')
const scroll = (_node: HTMLElement) =>
regex.test(overflow(_node)) ||
_node.classList.contains('scrollbar-container') // Perfect scrollbar does overflow: hidden
const scroll = (_node: HTMLElement) => regex.test(overflow(_node))

const scrollParent = (_node: HTMLElement) => {
if (!_node.parentNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type AudioTransactionsTableProps = {
tableClassName?: string
wrapperClassName?: string
totalRowCount: number
scrollRef?: React.RefObject<HTMLDivElement>
scrollRef?: React.MutableRefObject<HTMLDivElement | undefined>
fetchBatchSize: number
}

Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/components/table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type TableProps = {
onSort?: (...props: any[]) => void
isEmptyRow?: (row: any) => boolean
pageSize?: number
scrollRef?: React.RefObject<HTMLDivElement>
scrollRef?: React.MutableRefObject<HTMLDivElement | undefined>
showMoreLimit?: number
tableClassName?: string
totalRowCount?: number
Expand Down Expand Up @@ -663,7 +663,7 @@ export const Table = ({
minimumBatchSize={fetchBatchSize}
>
{({ onRowsRendered, registerChild: registerListChild }) => (
<WindowScroller scrollElement={scrollRef?.current ?? undefined}>
<WindowScroller scrollElement={scrollRef?.current}>
{({
height,
registerChild,
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/components/tracks-table/TracksTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { MouseEvent, useCallback, useMemo, useRef } from 'react'
import { MouseEvent, useCallback, useMemo, useRef } from 'react'

import {
formatCount,
Expand Down Expand Up @@ -96,7 +96,7 @@ type TracksTableProps = {
playing?: boolean
playingIndex?: number
removeText?: string
scrollRef?: React.RefObject<HTMLDivElement>
scrollRef?: React.MutableRefObject<HTMLDivElement | undefined>
showMoreLimit?: number
tableClassName?: string
totalRowCount?: number
Expand Down
4 changes: 2 additions & 2 deletions packages/web/src/pages/App.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { RefObject } from 'react'
import { MutableRefObject } from 'react'

type AppProps = {
mainContentRef: RefObject<HTMLDivElement>
mainContentRef: MutableRefObject<HTMLDivElement | undefined>
}

const App: (props: AppProps) => JSX.Element
Expand Down
7 changes: 5 additions & 2 deletions packages/web/src/pages/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createRef } from 'react'
import { createRef, MutableRefObject } from 'react'

import { ConnectedRouter } from 'connected-react-router'
import ReactDOM from 'react-dom'
Expand Down Expand Up @@ -30,7 +30,10 @@ jest.mock('services/solana-client/SolanaClient', () => ({
describe('smoke test', () => {
it('renders without crashing', () => {
const rootNode = document.createElement('div')
const mainContentRef = createRef<HTMLDivElement>()
const mainContentRef =
createRef<HTMLDivElement | null>() as MutableRefObject<
HTMLDivElement | undefined
>
ReactDOM.render(
<Provider store={store}>
<ConnectedRouter history={history}>
Expand Down
10 changes: 4 additions & 6 deletions packages/web/src/pages/MainContentContext.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { createContext, memo, useRef, RefObject } from 'react'
import { createContext, memo, useRef, MutableRefObject } from 'react'

export const MainContentContext = createContext<{
mainContentRef: RefObject<HTMLDivElement>
}>({
mainContentRef: { current: null }
export const MainContentContext = createContext({
mainContentRef: {} as MutableRefObject<HTMLDivElement | undefined>
})

export const MainContentContextProvider = memo(
(props: { children: JSX.Element }) => {
const mainContentRef = useRef<HTMLDivElement>(null)
const mainContentRef = useRef<HTMLDivElement>()
return (
<MainContentContext.Provider
value={{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useRef } from 'react'
import { useCallback, useEffect } from 'react'

import {
accountSelectors,
Expand Down Expand Up @@ -104,8 +104,6 @@ export const MessageUserSearchResult = (props: UserResultComposeProps) => {
getCanCreateChat(state, { userId: user.user_id })
)

const ref = useRef<HTMLDivElement | null>(null)

const handleComposeClicked = useCallback(() => {
if (canCreateChat) {
closeParentModal()
Expand Down Expand Up @@ -175,12 +173,7 @@ export const MessageUserSearchResult = (props: UserResultComposeProps) => {
}

return (
<div
className={styles.root}
ref={(rootRef) =>
(ref.current = (rootRef?.parentElement as HTMLDivElement) ?? null)
}
>
<div className={styles.root}>
<ArtistChip
className={styles.artistChip}
user={user}
Expand All @@ -195,8 +188,6 @@ export const MessageUserSearchResult = (props: UserResultComposeProps) => {
zIndex={zIndex.MODAL_OVERFLOW_MENU_POPUP}
anchorOrigin={{ horizontal: 'right', vertical: 'bottom' }}
transformOrigin={{ horizontal: 'right', vertical: 'top' }}
mountRef={ref}
containerRef={ref}
/>
</div>
)
Expand Down