Skip to content

Commit

Permalink
feat: add option remove hover state on mouseleave (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored Nov 27, 2023
1 parent 528035d commit 12b4af9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/two-windows-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@craftjs/core': patch
---

Add option to remove hover state on mouseleave
1 change: 1 addition & 0 deletions packages/core/src/editor/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const editorInitialState: EditorState = {
handlers: (store) =>
new DefaultEventHandlers({
store,
removeHoverOnMouseleave: false,
isMultiSelectEnabled: (e: MouseEvent) => !!e.metaKey,
}),
normalizeNodes: () => {},
Expand Down
18 changes: 12 additions & 6 deletions packages/core/src/events/DefaultEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Indicator, NodeId, DragTarget, NodeTree } from '../interfaces';

export type DefaultEventHandlersOptions = {
isMultiSelectEnabled: (e: MouseEvent) => boolean;
removeHoverOnMouseleave: boolean;
};

/**
Expand Down Expand Up @@ -134,17 +135,22 @@ export class DefaultEventHandlers<O = {}> extends CoreEventHandlers<
}
);

const unbindMouseleave = this.addCraftEventListener(
el,
'mouseleave',
(e) => {
let unbindMouseleave: (() => void) | null = null;

if (this.options.removeHoverOnMouseleave) {
this.addCraftEventListener(el, 'mouseleave', (e) => {
e.craft.stopPropagation();
store.actions.setNodeEvent('hovered', null);
}
);
});
}

return () => {
unbindMouseover();

if (!unbindMouseleave) {
return;
}

unbindMouseleave();
};
},
Expand Down

0 comments on commit 12b4af9

Please sign in to comment.