Skip to content

Commit 5e59075

Browse files
committed
Updated column boxes
1 parent e756a75 commit 5e59075

File tree

6 files changed

+38
-25
lines changed

6 files changed

+38
-25
lines changed

packages/column-views/src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import { group } from "d3-array";
1111
import { RefObject, useContext, useMemo } from "react";
1212
import { AgeAxis } from "./age-axis";
1313
import styles from "./column.module.sass";
14-
import { CompositeUnitsColumn, TrackedLabeledUnit } from "./units";
14+
import {
15+
CompositeUnitsColumn,
16+
TrackedLabeledUnit,
17+
useUnitSelectionDispatch,
18+
} from "./units";
1519
import { IUnit } from "./units/types";
1620
export * from "./units";
1721
export * from "./age-axis";
@@ -256,9 +260,17 @@ function Column(
256260
"dark-mode": darkMode?.isEnabled ?? false,
257261
});
258262

263+
// Clear unit selection on click outside of units, if we have a dispatch function
264+
const dispatch = useUnitSelectionDispatch();
265+
259266
return h(
260267
"div.column-container",
261-
{ className },
268+
{
269+
className,
270+
onClick(evt) {
271+
dispatch?.(null, null, evt);
272+
},
273+
},
262274
h("div.column", { ref: columnRef }, [
263275
h("div.age-axis-label", "Age (Ma)"),
264276
h(

packages/column-views/src/selection-popover.module.sass

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
.popover-main
22
position: absolute
3-
outline: 2px solid #f22
3+
pointer-events: none
4+
5+
:global(.bp5-overlay)
6+
pointer-events: all
47

58
:global(.bp5-popover-target)
69
height: 100%
710
width: 100%
811
display: block
912
line-height: 0
13+
pointer-events: none
14+
outline: 2px solid #f22
1015

1116
.legend-panel-outer
1217
width: 25em
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.unit-label-container
22
pointer-events: none
3+

packages/column-views/src/units/boxes.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
useRef,
2020
} from "react";
2121
import { resolveID, scalePattern } from "./resolvers";
22-
import { useSelectedUnit, useUnitSelector } from "./selection";
22+
import { useSelectedUnit, useUnitSelectionDispatch } from "./selection";
2323
import { IUnit, transformAxisType } from "./types";
2424
import styles from "./boxes.module.sass";
2525

@@ -128,13 +128,15 @@ function useUnitSelectionManager(
128128
): [boolean, () => void] {
129129
const selectedUnit = useSelectedUnit();
130130
const selected = selectedUnit?.unit_id == unit.unit_id;
131-
const selectUnit = useUnitSelector(unit);
131+
132+
const dispatch = useUnitSelectionDispatch();
132133

133134
const onClick = useCallback(
134135
(evt: Event) => {
135-
selectUnit(ref.current, evt);
136+
dispatch(unit, ref.current, evt);
137+
evt.stopPropagation();
136138
},
137-
[unit, ref, selectUnit]
139+
[unit, ref, dispatch]
138140
);
139141

140142
useEffect(() => {

packages/column-views/src/units/selection.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ import {
1010
useMemo,
1111
useState,
1212
ReactNode,
13-
RefObject,
1413
useCallback,
1514
} from "react";
16-
import { IUnit } from "@macrostrat/column-views";
1715

1816
type UnitSelectDispatch = (
1917
unit: BaseUnit | null,
@@ -24,13 +22,8 @@ type UnitSelectDispatch = (
2422
const UnitSelectionContext = createContext<BaseUnit | null>(null);
2523
const DispatchContext = createContext<UnitSelectDispatch | null>(null);
2624

27-
export function useUnitSelector(u: BaseUnit | null) {
28-
const dispatch = useContext(DispatchContext);
29-
return (target: HTMLElement, evt: Event) => {
30-
console.log("Dispatch", u, target, evt);
31-
dispatch?.(u, target, evt);
32-
evt.stopPropagation();
33-
};
25+
export function useUnitSelectionDispatch() {
26+
return useContext(DispatchContext);
3427
}
3528

3629
export function useSelectedUnit() {
@@ -78,9 +71,15 @@ function BaseUnitSelectionProvider<T extends BaseUnit>({
7871
const value = useMemo(() => unit, [unit?.unit_id]);
7972

8073
const _onUnitSelected = useCallback(
81-
(unit: T, target: HTMLElement, event: Event) => {
82-
setUnit(unit);
83-
onUnitSelected?.(unit, target, event);
74+
(u: T, target: HTMLElement, event: Event) => {
75+
let newUnit = u;
76+
if (u == unit) {
77+
// If the same unit is selected, deselect it
78+
newUnit = null;
79+
}
80+
81+
setUnit(newUnit);
82+
onUnitSelected?.(newUnit, target, event);
8483
},
8584
[setUnit, onUnitSelected]
8685
);

packages/column-views/stories/column.stories.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,16 +150,10 @@ export function WithUnitSelectionPopover() {
150150
// Selected item position
151151
const [position, setPosition] = useState<RectBounds | null>(null);
152152

153-
useEffect(() => {
154-
if (position == null) return;
155-
console.log("Position", position);
156-
}, [position]);
157-
158153
return h(
159154
UnitSelectionProvider,
160155
{
161156
onUnitSelected: (unit, target: SVGElement | HTMLElement) => {
162-
console.log(target);
163157
if (unit == null) {
164158
setPosition(null);
165159
return;

0 commit comments

Comments
 (0)