Skip to content

Is it possible to update options dynamically after lightbox has already been init? #2151

@katerlouis

Description

@katerlouis

After the lightbox has been init, but not opened yet, I want to change options like bgOpacity, i.e. when the user changes user settings.

I've written myself the following helper, which does work for updating hideAnimationDuration

  function updateOptions(options: Partial<PreparedPhotoSwipeOptions>) {
    const pswp = lightbox.value?.pswp;
    if (pswp) {
      pswp.options = {
        ...pswp.options,
        ...options,
      };
    }
  }

Unfortunately this does not work for bgOpacity. Opening an image shows the same bgOpacity as before the user has changed it in preferences.

However, since user preferences do persist, the bgOpacity is matching the users preference after refreshing the page and then opening an image, because I set bgOpacity on init as well:

// /src/composables/usePhotoSwipe.ts
export function usePhotoSwipe(_options: Partial<PreparedPhotoSwipeOptions>) {
  const preferencesStore = usePreferencesStore();
  const { fullscreenLightboxOpacity } = storeToRefs(preferencesStore);
  const options: PreparedPhotoSwipeOptions = {
    pswpModule: PhotoSwipe,
    bgOpacity: fullscreenLightboxOpacity.value,
    ..._options, // note: 
  };
  // ... initializing later on using these options
}

Ideally PhotoSwipe would provide a function to update options dynamically.

Until then, how do you suggest to solve this for now?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions