-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BlockPopover: Remove __unstableCoverTarget in favour of BlockPopoverC…
…over
- Loading branch information
1 parent
5041f61
commit 943c53f
Showing
7 changed files
with
79 additions
and
61 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
packages/block-editor/src/components/block-popover/cover.js
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,63 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect, useState, useMemo, forwardRef } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; | ||
import BlockPopover from '.'; | ||
|
||
function BlockPopoverCover( | ||
{ clientId, bottomClientId, children, shift = false, ...props }, | ||
ref | ||
) { | ||
bottomClientId ??= clientId; | ||
|
||
const selectedElement = useBlockElement( clientId ); | ||
|
||
return ( | ||
<BlockPopover | ||
ref={ ref } | ||
clientId={ clientId } | ||
bottomClientId={ bottomClientId } | ||
shift={ shift } | ||
{ ...props } | ||
> | ||
{ selectedElement && clientId === bottomClientId ? ( | ||
<CoverContainer selectedElement={ selectedElement }> | ||
{ children } | ||
</CoverContainer> | ||
) : ( | ||
children | ||
) } | ||
</BlockPopover> | ||
); | ||
} | ||
|
||
function CoverContainer( { selectedElement, children } ) { | ||
const [ width, setWidth ] = useState( selectedElement.offsetWidth ); | ||
const [ height, setHeight ] = useState( selectedElement.offsetHeight ); | ||
|
||
useEffect( () => { | ||
const observer = new window.ResizeObserver( () => { | ||
setWidth( selectedElement.offsetWidth ); | ||
setHeight( selectedElement.offsetHeight ); | ||
} ); | ||
observer.observe( selectedElement, { box: 'border-box' } ); | ||
return () => observer.disconnect(); | ||
}, [ selectedElement ] ); | ||
|
||
const style = useMemo( () => { | ||
return { | ||
position: 'absolute', | ||
width, | ||
height, | ||
}; | ||
}, [ width, height ] ); | ||
|
||
return <div style={ style }>{ children }</div>; | ||
} | ||
|
||
export default forwardRef( BlockPopoverCover ); |
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
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