Skip to content

Commit 044edaf

Browse files
Created hook to focus on components
1 parent 74ae62e commit 044edaf

File tree

4 files changed

+22
-1
lines changed

4 files changed

+22
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ type SubscribeStates =
215215
| typeof OUTSIDE_CANVAS
216216
| typeof COMPONENT_CREATED
217217
| "hover"
218-
| "hoverEnd";
218+
| "hoverEnd"
219+
| "focus"
220+
| "focusEnd";
219221

220222
export function createCanvasMachine(id: string) {
221223
const subscribers: { [key in SubscribeStates]: Callback[] } = {
@@ -227,6 +229,8 @@ export function createCanvasMachine(id: string) {
227229
[COMPONENT_CREATED]: [],
228230
hover: [],
229231
hoverEnd: [],
232+
focus: [],
233+
focusEnd: [],
230234
};
231235
function subscribeCanvasMachine(state: SubscribeStates, cb: Callback) {
232236
subscribers[state].push(cb);

‎packages/atri-app-core/src/editor-components/NormalComponentRenderer/NormalComponentRenderer.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { NormalComponentRendererProps } from "../../types";
22
import { componentStoreApi } from "../../api";
33
import { useAssignComponentId } from "../hooks/useAssignComponentId";
4+
import { useFocusComponent } from "../hooks/useFocusComponent";
45

56
export function NormalComponentRenderer(props: NormalComponentRendererProps) {
67
const {
@@ -10,5 +11,6 @@ export function NormalComponentRenderer(props: NormalComponentRendererProps) {
1011
callbacks,
1112
} = componentStoreApi.getComponent(props.id)!;
1213
useAssignComponentId({ id: props.id });
14+
useFocusComponent({ id: props.id });
1315
return <Comp {...compProps} ref={ref} {...callbacks} />;
1416
}

‎packages/atri-app-core/src/editor-components/ParentComponentRenderer/ParentComponentRenderer.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { NormalComponentRenderer } from "../NormalComponentRenderer/NormalCompon
44
import { useAssignParentMarker } from "../hooks/useAssignParentMaker";
55
import { useAssignComponentId } from "../hooks/useAssignComponentId";
66
import { useHandleNewChild } from "./hooks/useHandleNewChild";
7+
import { useFocusComponent } from "../hooks/useFocusComponent";
78

89
export function ParentComponentRenderer(props: ParentComponentRendererProps) {
910
const {
@@ -15,6 +16,7 @@ export function ParentComponentRenderer(props: ParentComponentRendererProps) {
1516
const { children } = useHandleNewChild(props);
1617
useAssignParentMarker({ id: props.id });
1718
useAssignComponentId({ id: props.id });
19+
useFocusComponent({ id: props.id });
1820
return (
1921
<Comp {...compProps} ref={ref} {...callbacks}>
2022
{children.map((childId) => {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { DecoratorData } from "../../types";
2+
import { componentStoreApi } from "../../api/componentStoreApi";
3+
import { useEffect } from "react";
4+
5+
export function useFocusComponent(props: DecoratorData) {
6+
useEffect(() => {
7+
const ref = componentStoreApi.getComponentRef(props.id);
8+
if (ref.current) {
9+
ref.current.tabIndex = 0;
10+
ref.current.focus();
11+
}
12+
}, [props.id]);
13+
}

0 commit comments

Comments
 (0)