use-collapsible-panel.ts 665 B

12345678910111213141516171819202122
  1. import { RefObject, useLayoutEffect } from 'react'
  2. import { ImperativePanelHandle } from 'react-resizable-panels'
  3. export default function useCollapsiblePanel(
  4. panelIsOpen: boolean,
  5. panelRef: RefObject<ImperativePanelHandle>
  6. ) {
  7. // useLayoutEffect keeps the panel-size update in the same paint cycle as the
  8. // CSS class changes that show/hide the panel content, eliminating a visible
  9. // flash between the two changes.
  10. useLayoutEffect(() => {
  11. const panelHandle = panelRef.current
  12. if (panelHandle) {
  13. if (panelIsOpen) {
  14. panelHandle.expand()
  15. } else {
  16. panelHandle.collapse()
  17. }
  18. }
  19. }, [panelIsOpen, panelRef])
  20. }