If one sign that a city has transcended racism is its willingness to criticize its citizens regardless of their race or ethnicity, San Francisco’s treatment of its late mayor, Ed Lee, ought to place it in the colorblind hall of fame.

During his two terms in office — the second cut tragically short by his sudden death early Tuesday morning — Lee was whacked as thoroughly and regularly as a dusty old carpet, for reasons that had nothing to do with the fact that he was Chinese American. Whether you were a critic of Lee or a supporter, his racial background was completely irrelevant. No one even thought about it.

San Francisco’s mostly post-racial perspective on its Chinese inhabitants is so familiar, and so accepted, that it’s hard to imagine it hasn’t always been like this. Yet for much of its history, the city took a far less enlightened view of the Chinese. An examination of that often-ugly history gives a greater appreciation of Lee’s achievement in becoming the city’s first Chinese-American mayor.

The first Chinese arrived in San Francisco at the beginning of the Gold Rush, in 1848. Mostly Cantonese from the Pearl River Delta in Guangdong Province, they had no desire to stay or to become citizens of a country they regarded as barbaric. They had left their families behind and planned to return to China after striking it rich; many arranged for their bones to be shipped home after they died.

By 1853, 4,000 Chinese had arrived and settled in the area around Sacramento Street and Dupont (later Grant Avenue), which became a separate district known as Chinatown.

White San Franciscans initially tolerated the Chinese, whom they found useful because they did work, like laundry and cooking, that most whites would not do, and because they worked for much lower wages. But majority attitudes were far from enlightened. In 1858, the San Francisco Evening Bulletin spoke for many when it editorialized against allowing Chinese to attend schools with whites, opining, “We want no mongrel race of moral and mental hybrids to people the mountains and valleys of California.”

In the troubled decade of the 1870s, the limited tolerance extended to the Chinese turned to resentment and outright hatred. A severe economic downturn was exacerbated when thousands of Chinese who had labored on the transcontinental railroad returned to San Francisco. As cheap Chinese labor undercut white wages, the white working class turned against the “coolies” with a vengeance. So-called “sandlot orators” began haranguing large crowds of working men who gathered in vacant lots near City Hall, blasting “bloodsucking capitalists and their Chinese slaves.”

In the summer of 1877, sandlot agitation against the “moon-faced lepers” reached its peak. Hundreds of angry men stormed Chinatown, attacking Chinese and burning laundries. Police had to erect barricades on Pine and Broadway, at both ends of the quarter, and beat back the mob. Another horde attacked the Pacific Mail docks at the foot of Brannan St., where Chinese immigrants arrived. The riots were the worst in the city’s history to that point.

Anti-Chinese sentiments were not confined to California. In 1882, Congress passed the Chinese Exclusion Act, which essentially froze Chinese immigration into the country. It was the first law that specifically prohibited an ethnic group from entering the United States. It was not repealed until 1943.

Meanwhile, San Franciscan officials inveighed against Chinatown, denouncing its filthy, overcrowded alleys, brothels and opium dens as a menace to public health and morality (but making no effort to ameliorate its slum-like conditions). When the 1906 earthquake and fire destroyed Chinatown, city fathers — including former Mayor James Phelan, who despite being an ardent progressive was virulently anti-Chinese — saw a golden opportunity to get rid of the quarter. A city committee laid plans to move Chinatown to San Francisco’s unofficial dumping ground, the slaughterhouse district of Hunters Point. Only the concerted opposition of Chinese notables and merchants, who flexed their political muscles for the first time, prevented the quarter’s relocation.

In the 20th century, bigotry against the Chinese gradually eased, for a number of reasons. Economic rivalry became less of a factor. Anti-Chinese sentiments did not comport well with San Franciscans’ image of themselves as cosmopolitan and tolerant. The new, cleaned-up, Orientalized Chinatown was a less-sordid tourist attraction. And the Chinese themselves became more Western, cutting their queues and integrating themselves more into society.

By the 1920s, most Chinese Americans were attending San Francisco public schools. World War II further eroded prejudice, as the good, loyal Chinese were contrasted favorably with the Japanese, seen as evil and disloyal. In 1948, racially restrictive covenants that had prevented Chinese Americans from buying property west of Chinatown’s unofficial border on Powell Street were struck down. After the second great wave of immigration that began in 1965, Chinese Americans settled in every part of the city.

So when Ed Lee became San Francisco’s first Chinese American mayor, it represented the triumphant culmination of a long, hard struggle for acceptance, civil rights and justice. For the Chinese Americans whose predecessors arrived here at the city’s very beginning, and for all San Franciscans, regardless of their race, ethnicity or political persuasion, that achievement was, and remains, a moment to cherish.

Gary Kamiya writes the “Portals of the Past” column that appears every other Saturday in Datebook. To comment, submit your letter to the editor at SFChronicle.com/letters.