Skip to content

Commit b7398d2

Browse files
Focus hook updated to work when a component is selected
1 parent 9d38513 commit b7398d2

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

‎packages/atri-app-core/src/canvasMachine.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@ export function createCanvasMachine(id: string) {
401401
callSubscribers("focusEnd", context, event);
402402
console.log("Exited Selected focused State", id);
403403
},
404+
404405
on: {
405406
[BLUR]: {
406407
target: unfocused,
Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
import { DecoratorData } from "../../types";
22
import { componentStoreApi } from "../../api/componentStoreApi";
3-
import { useEffect } from "react";
3+
import { useEffect, useState } from "react";
4+
import { subscribeCanvasMachine } from "../../api";
45

56
export function useFocusComponent(props: DecoratorData) {
7+
const [id, setId] = useState("");
8+
9+
useEffect(() => {
10+
console.log("State useFocusComponent");
11+
const setFocus = subscribeCanvasMachine("focus", (context) => {
12+
setId(context.selected || "");
13+
});
14+
return () => {
15+
setFocus();
16+
};
17+
}, [id]);
18+
619
useEffect(() => {
7-
const ref = componentStoreApi.getComponentRef(props.id);
8-
if (ref.current) {
9-
ref.current.tabIndex = 0;
10-
ref.current.focus();
20+
if (props.id === id) {
21+
const ref = componentStoreApi.getComponentRef(props.id);
22+
if (ref.current) {
23+
ref.current.tabIndex = 0;
24+
ref.current.focus();
25+
console.log("Running ENter");
26+
}
1127
}
12-
}, [props.id]);
28+
}, [props.id, id]);
1329
}

0 commit comments

Comments
 (0)