A formerly Hasidic mom lost custody of her kids after coming out as gay — and was barred from even telling her youngest children about her sexuality, according to a first-of-its-kind Brooklyn court ruling.

The shocking decision was finally struck down by an appeals court in August, but only after months of legal wrangling.

The case “really shines a light on the tensions that exist between the secular world and an insular religious community,’’ top divorce lawyer Michael Stutman, who was not involved in the proceedings, told The Post on Tuesday.

The saga began after Chavie Weisberger and Naftali Weisberger divorced in 2009, years after she’d told him she was attracted to women. Chavie was given primary residential custody of their kids, who were ages 2, 3 and 5 at the time.

But Naftali took her back to court in 2012 seeking sole custody on the grounds that she violated their agreement to raise them in a strict religious household.

He argued Chavie had “radically changed her lifestyle” since their divorce, coming out as gay — including to their eldest daughter — and living with a transgender man, court papers show.

Naftali also complained that Chavie had allowed the kids to watch “a movie about Christmas,” let them participate in an egg hunt during a Purim party, gave them a book about having two daddies, cut their son’s sidelocks and came out to their oldest daughter, according to the documents

Brooklyn Judge Eric Prus ruled in 2015 that Chavie had violated the “religious-upbringing clause” in the couple’s divorce agreement and awarded Naftali sole legal and residential custody of their kids.

Prus argued that the couple’s agreement had forced him “to consider the children’s religious upbringing as a paramount factor in any custody determination.”

He ruled that Chavie would have her visitation limited — to supervised face-to-faces with her kids — if she didn’t comply with the religious-upbringing clause.

He added one more restriction: She had to keep her sexuality hidden from the two youngest kids.

“During any period of visitation or during any appearance at the childrens’ schools, ‘the [mother] must practice full religious observance in accordance with the Hasidic practices of ultra Orthodoxy,’ ” court papers say.

Chavie appealed the ruling and on Aug. 16 was granted full custody of her kids again.

The appeals court of three judges unanimously determined that Prus’ 2015 ruling lacked a “sound substantial basis” — and that it violated Chavie’s rights.

“A religious-upbringing clause should not, and cannot, be enforced to the extend that it violates a parent’s legitimate due-process right to express oneself freely,” the judges wrote.

“The weight of the evidence does not support the conclusion that it is in the children’s best interests to have their mother categorically conceal the true nature of her feelings and beliefs from them at all times and in all respects,” they added.

The mother must continue to keep a Kosher home and the children will attend Hasidic schools and practice full religious observance while with their father, the order reads. Her ex is allowed weekend visitation and additional visitation on Jewish holidays.

She and her lawyers declined to comment. Her ex, reached through his building intercom, told The Post he was unavailable until “next year.”

Additional reporting by Reuven Fenton and Danika Fears