There's a Name for the Magic
The ability to be smart about other people's feelings is called Emotional Intelligence (EI). It is a crucial part of a socialization process and building healthy connections with the world around us. The concept of EI was introduced in 1950s by a psychologist Daniel Goleman. He suggested five basic components of social intelligence.
Self-Awareness: the ability to understand our own feelings and the outcomes of our particular emotional reactions.
Self-Regulation: the ability to manage our own emotions and tame impulsive reactions.
Motivation: the ability to act effectively and reach goals in spite of distracting feelings.
Empathy: the ability to understand the other people's feelings.
Social Skills: the ability to manage relationships and predict the response to certain kinds of behavior.
All these elements are closely interconnected and work best when combined. In general, the algorithm of EI-based behavior is similar to a chess play. A person should analyze a situation, understand preconditions, predict the outcomes, and make the best possible decision that will lead to the most favorable result.