How to get the DOM element reference of a selected block? #70630
alexandrebuffet
started this conversation in
Developer Experience
Replies: 2 comments
-
Can you provide a code sample of how you're trying to find the selective block element now? |
Beta Was this translation helpful? Give feedback.
0 replies
-
This is likely because |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm developing a custom extension for the block editor that adds visual alignment guides (similar to those found in Figma or Photoshop) around the currently selected block.
While exploring the
@wordpress/block-editor
package, I noticed that some components (like the block grid visualizer) use theBlockPopover
component to position UI elements visually on top of blocks. However, there is no documentation on how to precisely position the popover so it matches the exact size and position of a block.Inside this package, there is an internal hook called
useBlockElement
that returns a ref to the active block element, which is exactly what I need. Unfortunately, this hook isn’t publicly exported.Currently, I can get the selected block’s
clientId
from thecore/block-editor
store and attempt to locate its DOM element using a[data-block]
selector. Butdocument.querySelector
returnsnull
, even after testing various selectors and using observers likeMutationObserver
.I don’t want to create a custom block just for this overlay feature, since it would unnecessarily complicate the block structure.
My question: is there a public and reliable way to get the DOM ref of a block from its
clientId
or from the current selection?Beta Was this translation helpful? Give feedback.
All reactions