If there is no way to fetch the bag/slot of the item when it's dropped, then this will require scanning through all the container slots to find the item. This can't be cached since the location of the item might change between being set and being traded.
This should only work for the (smaller) preview icons, not the main icon. The main icon can be dragged to clear, and if a roll is closed, it only makes sense to be trading from the preview icons.
If there is no way to fetch the bag/slot of the item when it's dropped, then this will require scanning through all the container slots to find the item. This can't be cached since the location of the item might change between being set and being traded.