Carroll's book was published in 1865, but the Oxford English Dictionary puts the earliest known use of "mad as a hatter" in 1829. That's three and a half decades before any March hares or dormice sipped tea, or the Cheshire cat made his famous claim of general madness. The actual origin of the phrase is unknown, but it's believed to be connected to mercury poisoning in hatmakers.
Several years after the Alice first appeared, in 1883, the phrase "hatter's shakes" was used to describe the condition caused by mercury poisoning. The symptoms included muscle tremors, plus mental and behavioral changes. The Hatter behaves strangely in the novel (as do many other characters), but his friends accept his oddities as being the usual.
Today, mercury poisoning is know to the medical and scientific communities as erethism. The modern list of symptoms including irritability and mania, both of which the Hatter has. But there's also sleep disturbance, depression, visual disturbance, hearing loss and those telltale tremors, which the Hatter doesn't seem to have.
You'll be glad to learn that short-term exposure to mercury can cause erethism, but it usually goes away if you can stay away from touching or inhaling mercury. Long-term exposure, such as dental professionals and chemical workers experience, can mean the symptoms persist. In any case, erethism is a rare disease.