Skip to content

feat: controllable highlight state for embedding hosts #4401

Description

@vcnainala

Summary

NMRium's highlight state is currently private to HighlightProvider / useHighlight. Embedding hosts (for example via nmrium-react-wrapper) sometimes need to drive that state from outside NMRium — typically across a cross-origin iframe with postMessage.

Proposed API

export interface NMRiumHighlightState {
  highlighted: string[];
  highlightedPermanently: string[];
  sourceData: HighlightEventSource | null;
}

interface NMRiumProps {
  // ...existing
  /** When provided, highlight state is controlled by the host. */
  highlight?: NMRiumHighlightState;
  /** Called for every highlight change, including internal hover/click. */
  onHighlightChange?: (state: NMRiumHighlightState) => void;
}

Leaving highlight out keeps today's behaviour unchanged (uncontrolled).

Implementation sketch

  • HighlightProvider accepts optional value / onChange.
  • In controlled mode, render from value; dispatch runs the existing reducer against a latest-state ref and calls onChange(next) instead of updating internal state.
  • Keep that ref in sync with each reduced value (not only with props), so consecutive dispatches such as the useHighlight unmount cleanup (HIDE then UNSET_PERMANENT) do not drop updates.
  • The internal highlights record can stay derived from highlighted and remain private.
  • Wire highlight / onHighlightChange from NMRiumProps through InnerNMRium into HighlightProvider.
  • Export NMRiumHighlightState (and HighlightEventSource if not already public).

Out of scope (separate discussion)

NMRium regenerates peak ids on load, so imported ids from a host never match. Hosts currently fall back to matching by nucleus + ppm. Keeping imported peak ids stable would let hosts highlight by id; that can be a follow-up.

Questions for the team

  1. Is the controlled-prop shape preferred over an imperative NMRiumRefAPI.highlight(ids) / clearHighlight() method?
  2. Any concern about onHighlightChange firing on every hover (performance for large spectra / embedding hosts)?

Activity

  1. vcnainala commented on Oct 4, 2026

    @vcnainala
    CollaboratorAuthor

    Proposed implementation: #4402

  2. vcnainala commented on Oct 4, 2026

    @vcnainala
    CollaboratorAuthor

    Also pushed a 2.8-line backport for React 18 hosts (nmrium-react-wrapper): branch feat/controllable-highlight-v2.8 on https://github.com/vcnainala/nmrium/tree/feat/controllable-highlight-v2.8 — same API as #4402.

  3. hamed-musallam commented on Oct 5, 2026

    @hamed-musallam
    Member

    We would do it by adding a highlightedIds prop to highlight items in NMRium, and not follow the way you propose. I created a PR #4405 with the proper changes, and I would create a demo on nmrium.dev to show how to use it.

  4. hamed-musallam commented on Oct 6, 2026

    @hamed-musallam
    Member

    closed #4405

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions