It depends on if the player switches leagues (can’t play) or teams within the league (probably will play in new uniform.) The original team is not guaranteed a new representative.
This article considered the possibility of Manny Machado being traded from the Orioles (AL) after he was voted a starter but before the game in 2018 (Epilogue: He was traded the day after the game so played as an Oriole for the AL):
If Machado is traded to another American League team before the game, he’d wear the jersey of that team, not Baltimore’s.
If it was a trade to the other league:
the nearest example is pitcher Jeff Samardzija’s move from the Cubs to the Athletics in 2014. The right-hander was introduced as a National League all-star but wore a generic All-Star Game uniform and cap and was not eligible to appear in the game.
And if it was more than a few days before the game:
Carlos Beltran in 2004 was traded from the Royals to the Astros when Houston was still in the National League, but the transaction happened a week before the All-Star Game and Beltran played for the National League. Baseball officials then allowed Kansas City to send another player to the game to replace Beltran.
But it is not guaranteed now that the original team gets another representative. According to MLB in 2021:
It should be noted, however, that if a player is selected to the roster and can't participate, he does not necessarily have to be replaced by a teammate.
To the question of has a selectee been traded, the above two were players not voted in by fans (pitchers never are)- at the time they were selected by the league and manager. As for fan-voted starters, according to the WP article in 2018:
There is no precedent in MLB’s 88 previous All-Star Games of a player selected by fans switching leagues this soon before the Midsummer Classic.
I don’t think it happened in 2019 or 2021.