For the complete documentation index, see llms.txt. This page is also available as Markdown.

Api

Accessing the API

You can control Chocolat using its API. Once you instanciated Chocolat you can access the api using :

const instance = Chocolat(document.querySelectorAll('.chocolat-image'), {})
const api = instance.api

// or 

const { api } = Chocolat(document.querySelectorAll('.chocolat-image'), {})

// or 

const api = Chocolat(document.querySelectorAll('.chocolat-image'), {}).api

Api methods

open([index])

Opens the lightbox on the image whose index is i. Opens on the first image if index undefined. Returns a Promise.

// Opens the first image
api.open() 

// Opens the 3rd image
api.open(2).then(() => {
    console.log('Third image is loaded and animations have finished.')
})

close()

Closes the lightbox. Returns a Promise.

prev()

Changes image backward. Returns a Promise.

next()

Changes image forward. Returns a Promise.

current()

Returns the index of the current image.

position()

Centers the image in its parent. Returns a Promise.

destroy()

Destroys the current instance. Removes elements, unbinds events, clears data.

getElem(elementName)

Returns a HTMLElement composing the lightbox.

get(optionName)

Classic getter

set(optionName, optionValue)

Classic setter

Last updated