An Orange County Superior Court judge determined Thursday that California’s “sanctuary state” protections for undocumented immigrants infringe on Huntington Beach’s local control as a charter city, making Huntington the first city to successfully challenge the controversial law.

Senate Bill 54, authored by state Senate leader Kevin de León (D-Los Angeles), in many cases prohibits state and local police agencies from notifying federal officials about the impending release of immigrants in custody who may be deported.

But after an hours-long courtroom debate Thursday, Judge James Crandall sided with Huntington Beach City Attorney Michael Gates’ argument that the law is unconstitutional as it applies to charter cities, which are run by a charter adopted by local voters.

The ruling makes Huntington Beach and all of California’s 121 charter cities exempt from complying with SB54.

Gates said afterward that he was “ecstatic” about the ruling.

“The operation of a police department and its jail is a city affair,” Crandall said. “For the state to say one size fits all for policing isn’t going to fit everybody.”

Several other Orange County governments also have taken steps to oppose the state’s sanctuary policies.

Crandall said SB54 infringes on local governments’ authority to practice policies they know are appropriate for themselves. Cities have a “better view and better ability” to oversee their needs in certain areas, he added.

Though the state may have had good intentions, Crandall said, there are “constitutional protections” for cities from the “ever-extending tentacles” of state rule.

California Deputy Attorney General Jonathan Eisenberg contended that municipalities can exercise other forms of autonomy and criticized Huntington Beach’s “alleged sweeping effects of this law.”

Eisenberg also argued there is an “important need” for a “uniform” public safety law. He referred to comments included in his briefing in which a law professor suggested that if police officials lose the trust of immigrant communities, people in those areas would avoid police and stop reporting crime, possibly resulting in an increase in crime.

But Crandall contended that violent crime is already up in the state and said he would rather rely on the opinions of local police than a law professor who may not be familiar with a city like Huntington Beach.

Crandall commended both sides for submitting strong briefs and said that regardless of his decision, he expected it to be appealed and eventually taken up by the California Supreme Court.

After the hearing, Eisenberg referred questions to Jennifer Molina, press secretary for the state attorney general’s office. Molina could not immediately be reached for comment Thursday evening.

Last year, dozens of residents of Huntington Beach’s Oak View community, which has a high concentration of Latinos, voiced concerns at a town hall meeting following President Trump’s executive order calling for a more aggressive approach to finding and arresting people who are in the country illegally.

Fear has lingered in Oak View since April, when Gates sued over SB54 following a 6-1 vote of approval from the City Council.

Asked what he would say to those residents, Huntington Beach Police Chief Robert Handy, who was present during the hearing, said in an interview that he would ask them to rely on the trust already built among police officers and the community and ignore rhetoric in politics and the media.

Priscella.Vega@latimes.com

Twitter: @vegapriscella

UPDATES:

7:35 p.m.: This article was updated to say the ruling applies to all California charter cities.

This article was originally published at 6:35 p.m.