Heavy quarkonium production in ultraperipheral nuclear collisions is described within the QCD dipole formalism. A realistic quarkonium wave functions in the rest frame are calculated solving the Schrödinger equation with a subsequent Lorentz boost to high energy. We rely on several selected Q Q potentials, which provide the best description of quarkonium spectra and decay widths, as well as data on diffractive electroproduction of quarkonia on protons. Nuclear effects are calculated with the phenomenological dipole cross sections fitted to DIS data. Higher twist effect related to the lowest Q Q Fock component of the photon, as well as the leading twist effects, related to higher components containing gluons, are included. The results for coherent and incoherent photoproduction of charmonia and bottomonia on nuclei are in a good accord with available data from the recent UPC measurements at the LHC. They can also be verified in future experiments at the planned electron-ion colliders.