How to get reference to overlay anchor

Hello! I want to position an icon on the bottom-right of every contexteditable=true or textarea element that is currently focused. To do that, I was thinking on getting the element via getOverlayAnchor with a querySelector. However, to accomplish it I need to get a reference to said element in order to get its bounding rect and calculate the position accordingly. I tried the custom render function but I always get the "html" element inside the "anchor" prop. Is there a way to accomplish this? Is my approach correct or does anyone already know how to do this? Thank you!
20 Replies
Mael
Mael•17mo ago
The only way I found how to reference this is in the getRootContainer function. It has a "mountState" prop that gives you the list of elements that the anchor is related to. Seems like the anchor is passed down to the default exported component as a anchor prop. The docs are not really clear on this so might be good if they could be updated @louisgv !
lab
lab•17mo ago
yeah I haven't gotten around to update the docs on this lol - you are the first person to dug this deep into the CSUI feature :p docs updated
Mael
Mael•17mo ago
you're the best!
lab
lab•17mo ago
Plasmo Docs
Life Cycle of Plasmo CSUI – Plasmo
Plasmo's CSUI orchestrates a lifecycle dedicated to mounting and unmounting your React, Vue, or Svelte components in a content script.
lab
lab•17mo ago
and the example for using existing overlay also added a comment about passing an anchor to it
Mael
Mael•17mo ago
hEY, sorry to bother you again, but the docs do not specify how to customize the mutation osberer in the renderer fuction, any insights?
lab
lab•17mo ago
oh you just spawn your own mutation observer basically
Mael
Mael•17mo ago
Maybe I'm dumb but I can't get my head around this lol. I'm trying to set the current anchor to the currently focused element with some checks and the getOverlayAnchor function does not update as often as I like so I wanna create my own observer
lab
lab•17mo ago
You will need to remove the getOverlayAnchor to have your mutationObserver run independenty <- otherwise it would spawn twice
Mael
Mael•17mo ago
Yep. Commented it out
lab
lab•17mo ago
The mutation observer should also be inside the render function I think - it's basically just watching over something technically - you can implement these check within your react component via a mutationObserver inside an useEffect that watch over the anchor instead that would reduce the surface of observation (could be anchor or could be something you query) Then based on condition, hide/unhide your csui component
Mael
Mael•17mo ago
Yep. As far as I can see, the render function receives a default anchor. I'm trying to spawn a observer that modifies the anchor and sends it down the compoennt tree, but nothing gets rendered. Maybe I'm doing something wrong. I swear if I get this working I'll add it as an example so future generations know how to do it lol
No description
No description
Mael
Mael•17mo ago
Thanks for your help btw
lab
lab•17mo ago
Try something like this example instead: https://github.com/PlasmoHQ/examples/blob/main/with-content-scripts-ui/contents/plasmo-root-container.tsx right now, you are still limited by the default rootContainer (it spawns a shadowDOM, you can reimplement it or wrap it)
Mael
Mael•17mo ago
I still want a shadow DOM tho
Want results from more Discord servers?
Add your server