Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow layers to utilise CoreEventHandler mouseleave options #705

Merged
merged 2 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading