Skip to content

Commit

Permalink
feat: allow layers to utilise CoreEventHandler mouseleave options (#705
Browse files Browse the repository at this point in the history
)
  • Loading branch information
itsbjoern authored Oct 24, 2024
1 parent 48cb5cb commit 2f133d9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/smooth-elephants-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@craftjs/core': patch
'@craftjs/layers': patch
---

Use removeHoverOnMouseleave option for layers package
2 changes: 1 addition & 1 deletion packages/core/src/events/CoreEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface CreateHandlerOptions {
}

export class CoreEventHandlers<O = {}> extends EventHandlers<
{ store: EditorStore } & O
{ store: EditorStore; removeHoverOnMouseleave: boolean } & O
> {
handlers() {
return {
Expand Down
12 changes: 8 additions & 4 deletions packages/core/src/events/DefaultEventHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,14 @@ export class DefaultEventHandlers<O = {}> extends CoreEventHandlers<
let unbindMouseleave: (() => void) | null = null;

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

return () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/layers/src/events/LayerHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ export class LayerHandlers extends DerivedCoreEventHandlers<{
}
);

let unbindMouseleave: (() => void) | null = null;

if (this.derived.options.removeHoverOnMouseleave) {
unbindMouseleave = this.addCraftEventListener(
el,
'mouseleave',
(e) => {
e.craft.stopPropagation();
layerStore.actions.setLayerEvent('hovered', null);
}
);
}

const unbindDragOver = this.addCraftEventListener(
el,
'dragover',
Expand Down Expand Up @@ -174,6 +187,12 @@ export class LayerHandlers extends DerivedCoreEventHandlers<{
unbindMouseOver();
unbindDragOver();
unbindDragEnter();

if (!unbindMouseleave) {
return;
}

unbindMouseleave();
};
},
layerHeader: (el: HTMLElement, layerId: NodeId) => {
Expand Down

0 comments on commit 2f133d9

Please sign in to comment.