Skip to content

Commit

Permalink
Atom style callbacks pass indexes rather than an extension of Atom
Browse files Browse the repository at this point in the history
The [TTFD](https://ttfd.vib.be) application uses this to display
selected atoms from a tracer metabolite. When the molecule itself is
used to store "selected" state the same instance must be used for
display. This prevents generalised React components where different
Molecule instances (of the same molecule) are used for display.
  • Loading branch information
MaybeJustJames committed Nov 5, 2024
1 parent d2649bb commit e446e1e
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ export type Atom = {
x: number;
y: number;
element: string;
selected?: boolean;
};

export type MoleculeData = {
Expand Down Expand Up @@ -392,11 +391,8 @@ type MoleculeProps = {
labelTranslateY?: number;
atomClicked?: (_index: number) => void;
atomLabel?: (_atom: Atom, _index: number) => string;
atomStyle?: (_element: string, _selected: boolean) => React.CSSProperties;
atomLabelStyle?: (
_element: string,
_selected: boolean,
) => React.CSSProperties;
atomStyle?: (_element: string, _index: number) => React.CSSProperties;
atomLabelStyle?: (_element: string, _index: number) => React.CSSProperties;
};

export const Molecule: React.FC<MoleculeProps> = (props: MoleculeProps) => {
Expand All @@ -416,20 +412,20 @@ export const Molecule: React.FC<MoleculeProps> = (props: MoleculeProps) => {
-(Math.min(min_y, 0) - ATOM_RADIUS) + (props.translateY || 0);

const defaultAtomLabel = (atom: Atom, _index: number): React.ReactElement => (
<>{atom.element}</>
<>{atom.element.toString()}</>
);

const defaultAtomStyle = (
element: string,
_selected: boolean,
_index: number,
): React.CSSProperties => ({
fill: element === "C" ? "rgba(1, 1, 1, 0)" : "white",
stroke: element === "C" ? "rgba(1, 1, 1, 0)" : "white",
});

const defaultAtomLabelStyle = (
element: string,
_selected: boolean,
_index: number,
): React.CSSProperties => ({
fill: element === "C" ? "rgba(0,0,0,0)" : "black",
});
Expand Down Expand Up @@ -473,14 +469,14 @@ export const Molecule: React.FC<MoleculeProps> = (props: MoleculeProps) => {
cy={atom.y}
r={ATOM_RADIUS}
strokeWidth="0.02"
style={atomStyle(atom.element, !!atom.selected)}
style={atomStyle(atom.element, i)}
/>
<text
key={`label-${i}`}
x={atom.x + (props.labelTranslateX || 0)}
y={atom.y + (props.labelTranslateY || 0.25)}
textAnchor="middle"
style={atomLabelStyle(atom.element, !!atom.selected)}
style={atomLabelStyle(atom.element, i)}
>
{atomLabel(atom, i)}
</text>
Expand Down

0 comments on commit e446e1e

Please sign in to comment.