drag a UI panel keeping it with the screen bounds

so i have created a player UI which the player can resize (to half the size) and also drag, however currently you’re able to drag it of the screen and if you let go you can’t get it back. I’m trying to stop the Player from being able to drag the UI Element off the screen but i’m readlly struggling with this as all the things i’ve tried result in strange behaviour.

this is what i have so far

     [SerializeField]
    private RectTransform rectTransform;
    [SerializeField]
    private Canvas canvas;
    private bool isMaxSize;

 public void OnDrag(PointerEventData eventData)
    {
        if (rectTransform == null)
            return;
               
        Vector2 anchoredPosition = eventData.delta / canvas.scaleFactor;

       //Bound Check to go here

        rectTransform.anchoredPosition += anchoredPosition;
    }

Safest way if to check each side individually.

bool leftSideOut = t(ransform.position.x - GetComponent<RectTransform>().sizeDelta.x) < 0;

bool rightSideOut = t(ransform.position.x + GetComponent<RectTransform>().sizeDelta.x) > Screen.width;

There may be some slight fiddling around with the UI position values, since sometimes 0,0 is the middle of the screen, not the bottom left

I ended up Changing my code and now use the rectTransform.AnchorMin and AnchorMax to figure this out, it seems to do the trick.