01 - Melt unsalted butter in a skillet over medium heat. Add sliced onions, thyme, salt, and pepper. Cook, stirring frequently, until onions are deeply caramelized and golden brown, about 18 to 20 minutes. Add balsamic vinegar and cook 1 minute more. Set aside.
02 - Lay out bread slices and spread mayonnaise or softened butter on one side of each slice.
03 - On the unbuttered side of two bread slices, spread Dijon mustard if using. Top each with half of the caramelized onions and half of the shredded Gruyère cheese. Close the sandwiches with remaining bread slices, ensuring the buttered side faces outward.
04 - Heat a nonstick skillet or griddle over medium-low heat. Place sandwiches in the pan and cook until the bread is golden and cheese is melted, about 4 to 5 minutes per side. Press gently with a spatula for even crust development.
05 - Slice the sandwiches and serve immediately while warm.