WASHINGTON — Sue Gordon, a career CIA official who is serving as the principal deputy director of national intelligence, told the White House she would leave her job on Thursday after she learned she would be passed over as director of national intelligence.

Susan Gordon, the principal deputy director of national intelligence. Office of the Director of National Intelligence

In a handwritten note to President Donald Trump, Gordon wrote: "Mr. President — I offer this letter as an act of respect & patriotism, not preference. You should have your team. Godspeed, Sue."

Trump on Twitter called Gordon "a great professional with a long and distinguished career" for whom he had "developed great respect." In a second tweet, he said her departure coincided with the retirement of National Intelligence Director Dan Coats, whose planned exit on Aug. 15 was previously announced.

Trump named Joseph Maguire, director of the National Counterterrorism Center, as acting director of national intelligence.

A source with direct knowledge of the matter told NBC News that Coats had recommended that Gordon replace him but that White House Chief of Staff Mick Mulvaney told him the White House had someone else in mind.

When Coats told that to Gordon, she felt she had no choice but to hand in her resignation, the source said.

Coats praised Gordon, calling her a "visionary leader who has made an enormous impact" on the intelligence community. He also praised Maguire as having had "a long, distinguished career serving the nation."

Gordon's departure may raise the hackles of both Republican and Democratic senators, who supported her vigorously. The source said she had recently assured Sens. Richard Burr, R-N.C., chairman of the Intelligence Committee, and Mark Warner of Virginia, the ranking Democrat on the committee, that she would stay on and serve as acting director.

Warner praised Gordon in a statement, calling her departure "a great loss not only to the intelligence community, but to the country." He also sharply criticized Trump.

"President Trump has repeatedly demonstrated that he is seemingly incapable of hearing facts that contradict his own views," Warner said. "The mission of the intelligence community is to speak truth to power; Yet in pushing out two dedicated public servants in as many weeks, once again the President has shown that he has no problem prioritizing his political ego even if it comes at the expense of our national security."

Joseph Maguire testifies on Capitol Hill. Al Drago / Getty Images file

Burr said in a statement: "Sue Gordon's retirement is a significant loss for our Intelligence Community. In more than three decades of public service, Sue earned the respect and admiration of her colleagues with her patriotism and vision. She has been a stalwart partner to the Senate Intelligence Committee, and I will miss her candor and deep knowledge of the issues. I look forward to seeing what new challenges she will tackle next."

Trump had previously hoped to install Rep. John Ratcliffe, R-Texas, as intelligence director, but Ratcliffe's nomination was withdrawn after NBC News reported that he had overstated his experience prosecuting terrorism and amid objections from members of Congress from both parties.

Gordon started with the CIA in the 1980s. Earlier this decade, she was the CIA's director of support and then became the CIA's first cyber-czar in 2013. She was named to her current post in January 2015 by President Barack Obama.

Following Ratcliffe's withdrawal, a bipartisan push began to emerge in support of Gordon, who is widely respected both on Capitol Hill and in the president's orbit for her decades of experience in the intelligence community.

While one current and one former U.S. official familiar with the matter said the White House had been planning to prevent Gordon from stepping into Coats' role in an acting capacity, the president seemed to warm to the idea.

"Sue Gordon is there now, and I like her very much," he said on Friday. "Certainly she will be considered for the acting" job.

Coats and Gordon oversaw the country's intelligence community, including the CIA and the National Security Agency, and advised the president on national security and intelligence matters.