The justice secretary says he will quit if the next Tory leader pursues leaving the EU without a deal.