California Gov. Jerry Brown (D). (Rich Pedroncelli/AP)

PRESIDENT TRUMP’S campaign against immigrants who are in the country illegally has triggered a backlash in some Democratic-leaning states and localities. Perhaps the most sweeping example just emerged from the state legislature in California, which extended so-called sanctuary protections to people who lack legal authorization to live in the United States. Gov. Jerry Brown (D) agreed to sign the legislation, known as the California Values Act, after insisting on changes that injected a much-needed dollop of restraint to the original bill, which disregarded public safety in its determination to shield illegal immigrants.

The bill’s supporters boast that it has made California, where at least a fifth of the nation’s roughly 11 million undocumented immigrants live, the first bona fide “sanctuary state.” Local police and sheriffs may no longer ask about people’s immigration status in many cases, nor hold most detainees behind bars at the request of federal immigration agents.

Similar if less sweeping laws in scores of cities and counties nationwide have infuriated the Trump administration, prompting the Justice Department’s counterproductive threat to withhold federal law enforcement funds from so-called sanctuary localities. In a challenge to that threat brought by Chicago, a federal judge ruled last week that the funds could not be withheld without Congress’s say-so.

The California bill, like the court ruling, limits the administration’s enforcement discretion. It does so in keeping with common sense.

In its modified form, the bill, passed by lawmakers on a straight party-line vote, allows — but does not require — localities to cooperate in detaining and handing over undocumented immigrants convicted of one or more on a list of some 800 violent and serious crimes. They include sex offenses, arson, domestic violence and even some lesser crimes chargeable either as misdemeanors or felonies.

It’s critical that even the state’s most liberal precincts — we’re talking to you, San Francisco — receive that message. It’s one thing to stand on the principle that illegal immigrants, most of whom have been in the country for 15 years or more, are a productive and vital part of America’s social fabric. It’s another to turn a blind eye to undocumented residents who have committed major crimes, imperil public safety and should be removed. As Mr. Brown put it on NBC’s “Meet the Press,” those who have committed serious crimes “have no business being in the country.”

The final bill allows more cooperation between federal and local law enforcement agencies than many advocates for illegal immigrants would like. Immigration agents will be allowed to interview people in jails, though they’ll be barred from setting up offices in them, and they’ll have access to some California enforcement databases under rules set by the state attorney general.

The attempt at striking a legislative balance prompted the state police chiefs’ association, but not the sheriffs’ association, to drop its initial opposition to the bill. The generally more lenient stance by police reflects the challenge they face in cultivating strong relations with immigrant communities, without which neither victims nor witnesses will cooperate with them. Such on-the-ground facts have carried the day in California. The administration should take note.