Skip to content

Convert EuiMutationObserver from class to function component#9508

Closed
ragini-pandey wants to merge 4 commits intoelastic:mainfrom
ragini-pandey:migrate/mutation-observer-to-function-component
Closed

Convert EuiMutationObserver from class to function component#9508
ragini-pandey wants to merge 4 commits intoelastic:mainfrom
ragini-pandey:migrate/mutation-observer-to-function-component

Conversation

@ragini-pandey
Copy link
Copy Markdown
Contributor

Summary

Converts EuiMutationObserver from a class component (extending EuiObserver) to a standalone function component, as requested in #9459.

Key implementation details:

  • Inlined the EuiObserver lifecycle logic (ref tracking via updateChildNode, observer connect/disconnect) directly into the function component using useRef and useCallback
  • Both onMutation and observerOptions are stored in refs so the updateChildNode callback identity stays stable across renders — this prevents React from cycling the ref callback (disconnect → reconnect), which would cause missed mutations
  • Cleanup on unmount is handled via a useEffect with an empty dependency array
  • The makeMutationObserver helper and useMutationObserver hook are unchanged
  • The EuiObserver base class is left intact since EuiResizeObserver still extends it
  • No changes to public API — EuiMutationObserver, EuiMutationObserverProps, and useMutationObserver exports remain identical

Why are we making this change?

Closes #9459 — Migrating class components to function components for consistency and to leverage React hooks.

Impact to users

No impact. This is a pure internal refactor — the public API, behavior, and rendered output are identical.

QA

General checklist

  • Browser QA
    • Checked in both light and dark modes (no visual changes)
    • Checked in both MacOS and Windows high contrast modes (no visual changes)
    • Checked in mobile (no visual changes)
    • Checked in Chrome, Safari, Edge, and Firefox (no visual changes)
    • Checked for accessibility (no visual changes)
  • Docs site QA
    • Added documentation (no API changes)
    • Props have proper autodocs (no API changes)
    • Checked Code Sandbox (no API changes)
  • Code quality checklist
    • Added or updated jest tests — existing tests pass with no modifications
    • Updated visual regression tests (no visual changes)
  • Release checklist
    • A changelog entry exists and is marked appropriately
    • Breaking change (not applicable)

@ragini-pandey ragini-pandey requested a review from a team as a code owner March 16, 2026 15:43
@github-actions
Copy link
Copy Markdown

👋 Since this is a community submitted pull request, a Buildkite build has not been started automatically. Would an Elastic organization member please verify the contents of this pull request and kick off a build manually?

@github-actions github-actions bot added the community contribution (Don't delete - used for automation) label Mar 16, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Refactors EuiMutationObserver from a class component (via EuiObserver) into a standalone function component implemented with React hooks, while aiming to preserve the existing API and behavior.

Changes:

  • Replaced the EuiObserver inheritance model with hook-based ref tracking and observer lifecycle management inside EuiMutationObserver.
  • Kept makeMutationObserver and useMutationObserver intact.
  • Added an upcoming changelog entry documenting the refactor.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
packages/eui/src/components/observer/mutation_observer/mutation_observer.tsx Converts EuiMutationObserver to a hook-based function component and inlines observer lifecycle logic.
packages/eui/changelogs/upcoming/9508.md Adds a changelog entry for the component refactor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ragini-pandey
Copy link
Copy Markdown
Contributor Author

Closing as per #9511 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community contribution (Don't delete - used for automation)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[EuiMutationObserver] Migrate from class to function component

3 participants