Skip to content

Is there a way to expand/collapse folders without animation? #655

@unstoppablecarl

Description

@unstoppablecarl

Using the following to store/load state

  const localStorageKey = 'gui-state'

  const pane = new Pane()

  pane.on('change', () => {
    localStorage.setItem(localStorageKey, JSON.stringify(pane.exportState()))
    console.log(JSON.stringify(pane.exportState()))
  })

  const savedState = localStorage.getItem(localStorageKey)
  if (savedState) {
    pane.importState(JSON.parse(savedState))
  }

When the page first loads folders are in their default expanded/collapsed state then tween to the saved state. Is there a way to have them skip the tween so they appear to load initially in the same expanded/collapsed state?

I have a clumsy workaround but it only works with root level folders:

export class PaneFolderStore {
  private LOCAL_STORAGE_KEY: 'gui-folders'
  private folders = new Map<string, FolderApi>()
  private expandedByDefault = true
  private prevState: Record<string, boolean>

  constructor() {
    this.prevState = this.load()
  }

  add(pane: Pane, params: FolderParams) {
    const title = params.title as string
    params.expanded = this.prevState?.[title] ?? this.expandedByDefault

    const folder = pane.addFolder(params)
    this.folders.set(folder.title as string, folder)
    return folder
  }

  save() {
    const state: Record<string, boolean> = {}
    for (let [title, folder] of this.folders) {
      state[title] = folder.expanded
    }

    localStorage.setItem(this.LOCAL_STORAGE_KEY, JSON.stringify(state))
  }

  load() {
    const savedState = localStorage.getItem(this.LOCAL_STORAGE_KEY)
    if (savedState) {
      return JSON.parse(savedState)
    }
  }
}
const pane = new Pane()
const folders = new PaneFolderStore()
const myFolder = folders.add(pane, {title: 'Settings'})

pane.on('change', () => {
  folders.save()
})

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