Skip to content

Commit 0d92ab9

Browse files
committed
fix: ensure popovers display correctly when the plugin is invoked via an image input
1 parent 5874e60 commit 0d92ab9

File tree

13 files changed

+139
-105
lines changed

13 files changed

+139
-105
lines changed

package-lock.json

+45-45
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"@hookform/resolvers": "2.9.11",
5858
"@reduxjs/toolkit": "^1.9.0",
5959
"@sanity/incompatible-plugin": "^1.0.4",
60-
"@sanity/ui": "^1.0.0",
60+
"@sanity/ui": "^1.7.0",
6161
"@tanem/react-nprogress": "^5.0.0",
6262
"copy-to-clipboard": "^3.3.1",
6363
"date-fns": "^2.27.0",

src/components/Browser/index.tsx

+29-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type {MutationEvent} from '@sanity/client'
2-
import {Card, Flex, studioTheme, ThemeProvider, ToastProvider} from '@sanity/ui'
2+
import {Card, Flex, PortalProvider, studioTheme, ThemeProvider, ToastProvider} from '@sanity/ui'
33
import {Asset, Tag} from '@types'
44
import groq from 'groq'
5-
import React, {useEffect} from 'react'
5+
import React, {useEffect, useState} from 'react'
66
import {useDispatch} from 'react-redux'
77
import type {AssetSourceComponentProps, SanityDocument} from 'sanity'
88
import {TAG_DOCUMENT_NAME} from '../../constants'
@@ -33,6 +33,8 @@ type Props = {
3333
const BrowserContent = ({onClose}: {onClose?: AssetSourceComponentProps['onClose']}) => {
3434
const client = useVersionedClient()
3535

36+
const [portalElement, setPortalElement] = useState<HTMLDivElement | null>(null)
37+
3638
// Redux
3739
const dispatch = useDispatch()
3840

@@ -98,31 +100,33 @@ const BrowserContent = ({onClose}: {onClose?: AssetSourceComponentProps['onClose
98100
}, [])
99101

100102
return (
101-
<UploadDropzone>
102-
<Dialogs />
103-
<Notifications />
104-
105-
<Card display="flex" height="fill">
106-
<Flex direction="column" flex={1}>
107-
{/* Header */}
108-
<Header onClose={onClose} />
109-
110-
{/* Browser Controls */}
111-
<Controls />
112-
113-
<Flex flex={1}>
114-
<Flex align="flex-end" direction="column" flex={1} style={{position: 'relative'}}>
115-
<PickedBar />
116-
<Items />
103+
<PortalProvider element={portalElement}>
104+
<UploadDropzone>
105+
<Dialogs />
106+
<Notifications />
107+
108+
<Card display="flex" height="fill" ref={setPortalElement}>
109+
<Flex direction="column" flex={1}>
110+
{/* Header */}
111+
<Header onClose={onClose} />
112+
113+
{/* Browser Controls */}
114+
<Controls />
115+
116+
<Flex flex={1}>
117+
<Flex align="flex-end" direction="column" flex={1} style={{position: 'relative'}}>
118+
<PickedBar />
119+
<Items />
120+
</Flex>
121+
<TagsPanel />
117122
</Flex>
118-
<TagsPanel />
119-
</Flex>
120123

121-
{/* Debug */}
122-
<DebugControls />
123-
</Flex>
124-
</Card>
125-
</UploadDropzone>
124+
{/* Debug */}
125+
<DebugControls />
126+
</Flex>
127+
</Card>
128+
</UploadDropzone>
129+
</PortalProvider>
126130
)
127131
}
128132

src/components/ButtonAssetCopy/index.tsx

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,18 @@ import {ClipboardIcon} from '@sanity/icons'
22
import {Button, Popover, Text} from '@sanity/ui'
33
import copy from 'copy-to-clipboard'
44
import React, {useEffect, useRef, useState} from 'react'
5+
import {usePortalPopoverProps} from '../../hooks/usePortalPopoverProps'
56

67
type Props = {
78
disabled?: boolean
89
url: string
910
}
1011

11-
const ButtonAssetCopy = (props: Props) => {
12-
const {disabled, url} = props
13-
14-
// Refs
12+
const ButtonAssetCopy = ({disabled, url}: Props) => {
13+
const popoverProps = usePortalPopoverProps()
1514
const refPopoverTimeout = useRef<ReturnType<typeof window.setTimeout>>()
16-
17-
// State
1815
const [popoverVisible, setPopoverVisible] = useState(false)
1916

20-
// Callbacks
2117
const handleClick = () => {
2218
if (refPopoverTimeout.current) {
2319
clearTimeout(refPopoverTimeout.current)
@@ -51,6 +47,7 @@ const ButtonAssetCopy = (props: Props) => {
5147
padding={2}
5248
placement="top"
5349
radius={1}
50+
{...popoverProps}
5451
>
5552
<Button
5653
disabled={disabled}

src/components/FormBuilderTool/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ const useRootPortalElement = () => {
6262
return () => {
6363
document.body.removeChild(container)
6464
}
65-
}, [])
65+
}, [container])
6666

6767
return container
6868
}

src/components/OrderSelect/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import React from 'react'
44
import {useDispatch} from 'react-redux'
55
import {getOrderTitle} from '../../config/orders'
66
import {ORDER_OPTIONS} from '../../constants'
7+
import {usePortalPopoverProps} from '../../hooks/usePortalPopoverProps'
78
import useTypedSelector from '../../hooks/useTypedSelector'
89
import {assetsActions} from '../../modules/assets'
910

@@ -12,6 +13,8 @@ const OrderSelect = () => {
1213
const dispatch = useDispatch()
1314
const order = useTypedSelector(state => state.assets.order)
1415

16+
const popoverProps = usePortalPopoverProps()
17+
1518
return (
1619
<MenuButton
1720
button={
@@ -51,6 +54,7 @@ const OrderSelect = () => {
5154
})}
5255
</Menu>
5356
}
57+
popover={popoverProps}
5458
/>
5559
)
5660
}

src/components/SearchFacetNumber/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import React from 'react'
99
import {useDispatch} from 'react-redux'
1010
import {operators} from '../../config/searchFacets'
11+
import {usePortalPopoverProps} from '../../hooks/usePortalPopoverProps'
1112
import {searchActions} from '../../modules/search'
1213
import SearchFacet from '../SearchFacet'
1314
import TextInputNumber from '../TextInputNumber'
@@ -16,12 +17,12 @@ type Props = {
1617
facet: SearchFacetInputNumberProps
1718
}
1819

19-
const SearchFacetNumber = (props: Props) => {
20-
const {facet} = props
21-
20+
const SearchFacetNumber = ({facet}: Props) => {
2221
// Redux
2322
const dispatch = useDispatch()
2423

24+
const popoverProps = usePortalPopoverProps()
25+
2526
const modifiers = facet?.modifiers
2627
const selectedModifier = facet?.modifier
2728
? modifiers?.find(modifier => modifier.name === facet?.modifier)
@@ -75,6 +76,7 @@ const SearchFacetNumber = (props: Props) => {
7576
})}
7677
</Menu>
7778
}
79+
popover={popoverProps}
7880
/>
7981
)}
8082

@@ -116,6 +118,7 @@ const SearchFacetNumber = (props: Props) => {
116118
))}
117119
</Menu>
118120
}
121+
popover={popoverProps}
119122
/>
120123
)}
121124
</SearchFacet>

src/components/SearchFacetSelect/index.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ import {useDispatch} from 'react-redux'
1111
import {operators} from '../../config/searchFacets'
1212
import {searchActions} from '../../modules/search'
1313
import SearchFacet from '../SearchFacet'
14+
import {usePortalPopoverProps} from '../../hooks/usePortalPopoverProps'
1415

1516
type Props = {
1617
facet: SearchFacetInputSelectProps
1718
}
1819

19-
const SearchFacetSelect = (props: Props) => {
20-
const {facet} = props
21-
20+
const SearchFacetSelect = ({facet}: Props) => {
2221
// Redux
2322
const dispatch = useDispatch()
2423

24+
const popoverProps = usePortalPopoverProps()
25+
2526
const options = facet?.options
2627

2728
const selectedItem = options?.find(v => v.name === facet?.value)
@@ -73,6 +74,7 @@ const SearchFacetSelect = (props: Props) => {
7374
})}
7475
</Menu>
7576
}
77+
popover={popoverProps}
7678
/>
7779
)}
7880

@@ -101,6 +103,7 @@ const SearchFacetSelect = (props: Props) => {
101103
))}
102104
</Menu>
103105
}
106+
popover={popoverProps}
104107
/>
105108
</SearchFacet>
106109
)

0 commit comments

Comments
 (0)