Youssef En-Nesyri gave the hosts an early lead with a superb effort as Barcelona struggled to create anything of any note.
But they improved after the break and second-half efforts from Luis Suarez and Vidal secured victory and moved them three points clear at the top, although Real Madrid will have the opportunity to move level when they play Real Sociedad later on Saturday.
Leganes took a 13th-minute lead when En-Nesyri hit his third goal in three games, the Morocco forward cutting in from the right and drilling a fine left-footed shot into the top corner.
Barcelona had plenty of possession but struggled to open up Leganes and clear-cut opportunities were few and far between.
It took a brilliant save from Ivan Cuellar to prevent Barcelona pulling level in the 31st minute.
Ousmane Dembele's delicate ball in from the left was met by Suarez and his flicked header was heading for the top corner until the fingers of Cuellar turned it behind for a corner.
Mesa Roque was given far too much space 25 yards out seven minutes before the break but his shot was easily gathered by Barcelona goalkeeper Marc-Andre Ter Stegen.
Leganes had another opportunity in first-half stoppage time when a long ball out of defence from Unai Bustinza picked out En-Nesyri and he raced down the left channel but his shot from 20 yards drifted wide of Ter Stegen's right-hand post.
Barcelona made a fast start after the break and Gerard Pique headed a Lionel Messi corner against a post before being penalised for handball from the rebound.
Barcelona were level in the 52nd minute when Messi's floated free-kick was met by a downward header from Suarez.
Moments later Leganes had a golden opportunity to retake the lead but En-Nesyri sent a free header from Roberto Rosales' cross straight at Ter Stegen.
A smart pass from Frenkie De Jong picked out Messi on the edge of the box but an uncharacteristically poor first touch from the Argentinian took the ball away from him and his snatched shot was easily gathered by Cuellar.
Barcelona went in front in the 80th minute with a fortunate strike from Vidal.
Dembele's corner from the right bounced off the inside of Ruben Perez's right boot and towards Vidal, who would have been well offside had the ball come off a Barcelona player, and he mishit a shot over Cuellar and into the net.
After a VAR check to determine if the ball rebounded off Perez or Ivan Rakitic the goal was awarded.