CHESTERFIELD, Va. — The most powerful Republican in Virginia threw a cookout one sweltering Friday in July, in a mostly black community that a federal court recently made part of his legislative district.

Three enormous bouncy houses were part of the draw, though House Speaker Kirk Cox hardly needed to step inside to feel the ground shift beneath his feet.

“This is a new neighborhood for me,” the veteran legislator representing Colonial Heights said over and over, as he knocked on doors in the area a few days later.

Political reality has descended on Virginia Republicans the way some people go bankrupt: gradually and then suddenly.

While Virginia has grown more racially diverse over the past decade and less favorable to the GOP, the party has clung to control of the House under a map that a federal court said packed African Americans into certain legislative districts, diluting their political power elsewhere.

Judges declared that racial gerrymandering and imposed a new map in June — five months before a pivotal election with every seat on the ballot and Republicans defending razor-thin majorities in the House and Senate.

The revamped House districts are less white and less Republican — sometimes dramatically so. Cox’s swung from favoring Republicans by 26 points to tilting toward Democrats by six, from 76 percent white to 58 percent, and from 18 percent black to 34 percent, according to the nonpartisan Virginia Public Access Project.

Del. Chris Jones (R-Suffolk), chief mapmaker in 2011 and today chairman of the powerful Appropriations Committee, saw similarly seismic shifts. A district that had been rated “R+12” for its Republican bent now ranks “D+15.” Its white population shrank from 68 percent to 50 percent, while the proportion of black residents rose from 26 percent to 45 percent.

“For the first time this decade, Virginians are finally going to have the chance to elect candidates of their choice,” said former U.S. attorney general Eric H. Holder Jr., chairman of the National Democratic Redistricting Committee, which helped challenge the old maps. “The aim of everything that started in Jamestown 400 years ago was to allow the people to choose their representatives, and not to have politicians picking their voters, and that’s what gerrymandering has been all about.”

Cox’s Democratic challenger is an African American woman who in 2017 fell 819 votes shy of unseating veteran Del. Riley E. Ingram (R-Hopewell) in an adjacent district before the new maps placed her in Cox’s.

“I’m running to represent my community,” said Sheila Bynum-Coleman. “And I’m fiercely determined to be a voice for my community.”

Republicans struggling to find their footing in new territory insist there was nothing illegal about the old map, which was adopted in 2011 with broad bipartisan support. At the time, Republicans controlled the House and Democrats the Senate. Under a deal, Republicans drew the map for the House, and Democrats drew the one for the Senate without interference from the other chamber.

The Legislative Black Caucus was on board, as some incumbents welcomed the support of an overwhelmingly black district. So was President Barack Obama’s Justice Department, which at the time had to review new maps because of the state’s discriminatory history with voting.

Then came a lawsuit filed on behalf of voters who said they’d been disenfranchised by racial gerrymandering. Cox fought it all the way to the Supreme Court — twice — conceding that the map had been drawn to gain political advantage, but not to suppress the black vote.

The distinction mattered because racial gerrymandering was clearly unconstitutional, while the high court recently decided that it cannot stop partisan gerrymandering.

The courts ultimately found that House mapmakers had placed too much emphasis on race in 11 districts, all drawn to ensure that 55 percent of voters were black.

More than two dozen House districts were redrawn to remedy the 11 under court order. As a result, Republican incumbents in six of those districts will have a harder time defending their seats at a time when their party can afford to lose none.

The GOP controls the House 51 to 48 and the Senate 20 to 19, with one vacancy in each chamber.

The Senate’s map was not affected by the litigation, but control is also up for grabs in November in that chamber, where the majority has flipped back and forth since 2011.

Cox’s central Virginia district underwent the biggest partisan shift. A court-appointed special master took Cox’s largely rural-suburban territory and stretched it miles northward, sweeping in more diverse suburbs and apartment communities bordering majority-black Richmond.

For 29 years at the Capitol, Cox, 61, has represented the district where he has lived and worked his whole life. Now, the retired teacher is scrambling to introduce himself to 30,000 new constituents, out of a district of about 80,000.

But his hometown district was evolving even before the jolt of a court-ordered map.

“Chesterfield has changed a lot irrespective of the new lines,” said Stephen J. Farnsworth, a University of Mary Washington political scientist. “Generally speaking, the second wave of suburban residential settlement isn’t as conservative as the people who moved out of the city a generation or two ago. People are moving to Chesterfield to be closer to work. They’re moving into apartments in greater numbers rather than single-family homes. . . . So these are people who aren’t reflexively Republican.”

Still, the new map hardly makes Bynum-Coleman a shoo-in. Cox has far more money and name recognition from his years in local classrooms, as well as in the Capitol.

“My brother had him for social studies at Providence Middle School,” said Shawn Wood, 48, who stumbled upon Cox’s picnic on a walk through Harry G. Daniel Park and picked up a hot dog and campaign literature.

“I guess I’d vote for him,” said Wood, a customer service rep at Lowe’s who normally favors Democrats. “He gave the teachers a 5 percent raise. Sounds good.”

Bob Gibson, a founding member of the anti-gerrymandering group OneVirginia2021, said Cox “would be a tough person to knock off.”

Cox had $448,000 on hand in his personal campaign account heading into July, plus $761,000 in a political action committee set up to support his whole caucus. Bynum-Coleman had $124,000 on hand. An independent candidate, Linnard K. Harris Sr., had less than $9,000.

A competitive race will force Cox to spend time and money that otherwise could go to other Republicans. And big donations are expected to flow to Bynum-Coleman from national liberal groups, which have already provided endorsements and technical help. The Human Rights Campaign plans to target 11,000 district voters who support LGBTQ rights on her behalf, said HRC spokesman Lucas Acosta.

“That race is a huge priority for us,” said Ianthe Metzger, a spokesman for Emily’s List, one of the most influential women’s groups in national politics. It has dispatched financial and political advisers to meet with Bynum-Coleman and help guide her campaign.

Bynum-Coleman, 47, is a building contractor with a political science degree from Virginia Commonwealth University who has never held elective office. She grew up near the district in Midlothian and ran twice against Ingram. She got trounced in 2015 before nearly toppling him two years later amid a blue wave fueled by animosity toward President Trump.

She and Cox could not be more different in politics or style. The speaker is a reserved Christian conservative steeped in the intricacies of policy and parliamentary procedure. He supports gun rights and opposes abortion. But he has mostly tried to keep a lid on social legislation that has caused problems for his party, most notably in 2012, with an uproar over a bill that would have required most women seeking an abortion to first undergo a vaginal ultrasound. He has had certain hot-button bills — from conservatives and liberals alike — killed in committees.

His mantra: “Practical solutions for everyday issues.”

Bynum-Coleman speaks in passionate and personal terms about the liberal issues that have propelled her into politics. A son with a learning disability. A daughter who was shot. A cousin rejected by a landlord because she is married to another woman. Bynum-Coleman calls for more funding for schools, stricter gun control and a ban on LGBTQ discrimination in housing and public employment.

“ ‘We don’t rent to y’all,’ ” she says her cousin was told. “Right here in Richmond, Virginia. How is that possible in 2019?”

At an appearance at a pizza restaurant in July, she drew a diverse crowd of about 85 — members of the Liberal Women of Chesterfield County and the Sierra Club, Equal Rights Amendment (ERA) advocates and Commonwealth’s Attorney Scott Miles. She advocated for marijuana legalization and criticized Cox and the Republican leader in the Senate for shutting down a special session on gun control in July that was called by Gov. Ralph Northam (D) after a mass shooting in Virginia Beach.

“I’m a mom on a mission. I’m in this to make things better,” she said.

She framed Cox as someone who has undermined democracy — both through gerrymandering and the way as speaker he has been able to single-handedly kill bills, simply by assigning them to a committee he knows will reject them.

This year, Cox used that tactic on several measures that passed the Senate with bipartisan support and had the votes to pass the House on the floor. Those included bills to ratify the federal ERA and to ban anti-gay discrimination in housing and government employment.

“This is a democracy,” Bynum-Coleman said in an interview in her campaign office last month. “And if there’s a piece of legislation that Democrats and Republicans can work on and we have the votes to pass . . . who has the right to take away the fundamental process of our democracy by saying, ‘We’re not even going to allow it to come to the floor for a vote’?”

Cox stressed that he has used his power as speaker to push through popular legislation, such as a tuition freeze at state colleges and universities and a 5 percent raise for K-12 teachers.

As Cox campaigns, he plays up achievements on kitchen-table issues such as an income-tax cut and measures to clean up coal ash ponds, help veterans and create a special breast cancer awareness license plate. His literature bills him not as the powerhouse speaker, but as “Our Delegate.”

Kathryn Gilley, spokeswoman for the House Democratic Caucus, was skeptical of Cox’s community outreach.

“He literally spent a couple years and a lot of money to make sure these folks were not his constituents,” she said.

But Cox said he sincerely welcomes the chance to represent people in new parts of the district.

“It’s a challenge,” he said. “It’s also an opportunity. You have to look at it that way. . . . I always say, ‘I’d like to earn your vote.’ ”

Cox did win over some voters at his cookout, where Theresa Twitty relaxed at a shaded picnic table with her 18-year-old daughter, Lisa, and 9-month-old grandson, Malachi. They’d come to the “free community hot dog cookout” without realizing it was a political event.

“I got a postcard in the mail, and I counted down the days,” said Twitty, 56, as she fed Malachi spoonfuls of blue shaved ice. “I didn’t really know what it was, but I just thought it was nice that someone would do something like that.”

A pastor of a nondenominational church, Twitty was impressed that Cox had chatted with her and others at length as he passed out hot dogs. He won her vote.

“Oh, I’m decided,” she said. “Oh yeah, somebody has something like this for the community — oh yes.”