When parked and locked, and the sunroof is open, use the rain sensor to automatically close the sunroof if rain is detected.
I was unable to find a car that has this feature, although some VW’s have an undocumented option that can be turned on with special tools.
One legal concern may be in the exceptionally rare instance when someone’s hand is in the opening and the sunroof closes by itself. There may need to be some kind of additional safety system in place to avoid this situation.
Created from a suggestion by Robert in the Tesla forums.