When we heat a borax crystal, why does it first expand and then contract?
I've never seen this reaction, but it sounds quite entertaining. And I learned a couple of new words in looking at the literature to explain this phenomenon.


Intumescence: The property of a material to swell when heated. Borax is intumescent.
"Calcination is a thermal treatment process in presence of air applied to ores and other solid materials to bring about a thermal decomposition, phase transition, or removal of a volatile fraction. The calcination process normally takes place at temperatures below the melting point of the product materials."


The mechanism for the calcination of borax quite complex and is covered in some detail in this article. Borax decahydrate (BDH), $\ce{Na2B4O7.10H2O}$ is easily dehydrated to Borax pentahydrate (BPH), $\ce{Na2B4O7.5H2O}$ without much visible change. The actual form of the borate ion in both BDH and BPH is $\ce{B4O5(OH)4^2-}$ with 2 of the water molecules actually incorporated as hydroxyl groups in the ion. (The other 2 hydroxyl groups come for the water hydrogens and 2 oxygens from the $\ce{B4O7^2-}$.) That means that the last two water molecules can only be removed by molecular decomposition. So in BPH, you actually have only 3 "free" (i.e., easy to remove) water molecules. 

So here's what happens. The borax decahydrate dehydrates smoothly with minimal density changes (no swelling) until you get to borax pentahydrate. At this point, the BPH dissolves  in its own water of dehydration and the "blossoming" you see is from the solution erupting through the partially dehydrated crystal surface as it's heated. The density of the borax changes dramatically as it goes from the pentahydrate (about $0.85\ \mathrm{g/cm^3}$) to the dihydrate  (about $0.14\ \mathrm{g/cm^3}$) which is why it ends up looking "whipped". 

To do the final transformation to a tiny bead, the remaining 2 waters of hydration (those tied up as hydroxide ions) are driven off as the $\ce{B4O5(OH)4^2-}$ decomposes to give metaborate, $\ce{BO2^2-}$ and boric anhydride, $\ce{B2O3}$. $$\ce{B4O5(OH)4^2- -&gt; 2 BO2 + B2O3 + 2H2O}$$

Fascinating.