Repository: wagtail/wagtail
File: client/src/controllers/RevealController.ts (TypeScript)

Code before:
        peekZone.removeEventListener('mouseleave', onMouseLeave);
      };
    }

    // Set initial state based on stored value if available

    if (this.storageKeyValue) {
      const isStoredAsClosed = this.stored;
      if (
        typeof isStoredAsClosed === 'boolean' &&
        isStoredAsClosed !== this.closedValue
      ) {
        this.closedValue = isStoredAsClosed;
      } else {
        // No value stored yet, so store default(close) state
        this.stored = this.closedValue;
      }
    }

    // Dispatch initial event & class removal after timeout (allowing other JS content to load)

    new Promise((resolve) => {
      setTimeout(resolve);
    }).then(() => {
      this.dispatch('ready', {
        cancelable: false,
        detail: {
          closed: this.closedValue,
        },
      });
    });
  }

  /**
   * Handles changes to the closed state,updating element classes and `aria-expanded` attributes accordingly.
   *
   * Note: This may not trigger when clicking the toggle button if the element is already open in peeking mode.
   */

  closedValueChanged(shouldClose: boolean, previouslyClosed?: boolean) {
    if (previouslyClosed === shouldClose) return;

    const closedClasses = this.closedClasses;
    const openedClasses = this.openedClasses;
    const contentTargets = this.contentTargets;
    const isInitial = previouslyClosed === undefined;
    const isPeeking = this.peekingValue;
    const openedContentClasses = this.openedContentClasses;
    const toggles = this.toggles;

    if (!isPeeking) this.updateToggleIcon(shouldClose);

Review comment (suggestion):
This (else condition )addition is optional, as per https://github.com/wagtail/wagtail/pull/12677#issuecomment-2763302576, Due to this, It's always storing the default state even when the user doesn't interact with it (initially).

Code after:
      ? this.element.closest<HTMLElement>(this.peekTargetValue)
      : false;

    if (peekZone) {
      const onMouseLeave = () => {
        if (this.peekingValue) this.close();
        this.peekingValue = false;
      };
      peekZone.addEventListener('mouseleave', onMouseLeave, { passive: true });
      this.cleanUpPeekListener = () => {
        peekZone.removeEventListener('mouseleave', onMouseLeave);
      };
    }

    // Set initial state based on stored value if available

    if (this.storageKeyValue) {
      const isStoredAsClosed = this.stored;
      if (
        typeof isStoredAsClosed === 'boolean' &&
        isStoredAsClosed !== this.closedValue
      ) {
        this.closedValue = isStoredAsClosed;
      } else {
        // No value stored yet, so store default(close) state
        this.stored = this.closedValue;
      }
    }

    // Dispatch initial event & class removal after timeout (allowing other JS content to load)

    new Promise((resolve) => {
      setTimeout(resolve);
    }).then(() => {
      this.dispatch('ready', {
        cancelable: false,
        detail: {
          closed: this.closedValue,
        },
      });
    });
  }

  /**
   * Handles changes to the closed state,updating element classes and `aria-expanded` attributes accordingly.
   *
   * Note: This may not trigger when clicking the toggle button if the element is already open in peeking mode.
   */
  closedValueChanged(shouldClose: boolean, previouslyClosed?: boolean) {
    if (previouslyClosed === shouldClose) return;