DES MOINES, Iowa — A judge agreed Friday to temporarily block the most restrictive abortion law in the country from taking effect in Iowa next month under an agreement between lawyers for the state and abortion rights groups.

Attorneys for the state and Republican Gov. Kim Reynolds said during a court hearing that they agreed to prevent the law from taking effect on July 1 after discussions with the American Civil Liberties Union of Iowa, Planned Parenthood Federation of American and the Emma Goldman Clinic.

The advocacy groups argue the law is unconstitutional and have filed a lawsuit to block the law, which bans most abortions if a fetal heartbeat can be detected. An attorney representing the state said the goal now is to quickly get the case before a judge so the state can argue the law is constitutional.

Judge Michael Huppert said he would formally issue a temporary injunction later in the day.

Reynolds signed the law May 2, two days after lawmakers approved it. If the law is eventually allowed to take effect, the fetal heartbeat requirement would ban abortions around the sixth week of pregnancy — a time when, abortion-rights groups say, many women don't even know they're pregnant.

Iowa Attorney General Tom Miller has refused to defend the law, so the Thomas More Society, a conservative Chicago-based law firm, has agreed to handle the defense for free.

The law's approval marked a dramatic change in abortion policy in Iowa. Until the 2016 election, the state had little to no role in the broad Republican effort to overturn Roe v. Wade, the landmark 1973 U.S. Supreme Court ruling that established a woman's right to terminate a pregnancy until a fetus is viable.

But the election flipped control of the Iowa Senate, putting Republicans in charge of the Legislature and the governor's office for the first time in two decades. Up to that point, Democrats had maintained enough political power to curtail most Republican anti-abortion attempts.

Lawmakers adopted several abortion restrictions in 2017, including a 20-week abortion ban and a requirement that women wait three days before ending a pregnancy. The waiting provision, one of the longest in the country, is on hold because of a different lawsuit.

Separately, a new Iowa-based coalition of anti-abortion organizations was formed last year to renew efforts toward an abortion ban. The Coalition of Pro-Life Leaders, which includes Family Leader, put aside years of disagreement among the groups to help win passage of the 20-week ban and the six-week ban.

Iowa Republicans last year also gave up millions in federal dollars to create a state-funded family planning program that prohibits participation from abortion providers such as Planned Parenthood.