Barca boss Ernesto Valverde fielded an attacking line-up that included Lionel Messi, Antoine Griezmann, Luis Suarez and Ousmane Dembele in a 4-2-3-1 formation, but it did not look like paying off for large parts.
Leganes has won just once all season but the underdogs took the lead through Youssef En-Nesyri's sublime early strike, before the champion hit back in the second half.
Suarez dragged his side back on level terms and Vidal scored in fortuitous fashion 11 minutes from time to move Barca three points clear at the summit, the winning goal allowed to stand after a VAR (video assistant referee) check showed the midfielder was not offside because the ball had reached him via a Leganes player.
Barcelona lost 2-1 at Leganes last season and was behind inside 12 minutes on this trip, En-Nesyri cutting inside on his left and curling the ball into the top-left corner.
Suarez wasted a couple of promising opportunities before forcing an excellent fingertip save from Ivan Cuellar with a header from Dembele's cross.
But the Uruguay international found a way through eight minutes into the second half after being left free on the edge of the six-yard box to head in Messi's free-kick.
Barcelona took control of the contest but was left frustrated until Vidal struck 22 minutes after being introduced from the bench.
Vidal was left in space to bundle the ball past Cuellar from close range after Dembele's corner deflected into his path, the touch off the leg of Ruben Perez ensuring the goalscorer was onside despite Leganes's protests.