Description:
It appears that the ds:start:pre event is being triggered after the ds:select event. This is contrary to the expected behavior where ds:start:pre should be called before ds:select. This issue affects the logic for initializing and handling drag selection in the provided code snippet.
I am trying to prevent the selection of an element when clicking on it directly, and only enable the selection when the selection started outside the selectable elements.
Steps to Reproduce:
- Implement the provided code snippet in your project.
- Attempt to click directly on a selectable element to select it.
- Attempt to start a selection outside the selectable elements and drag over them.
- Observe the console logs or debugging tools to monitor the order of event triggers and selection behavior.
Expected Behavior:
The ds:start:pre event should be called before the ds:select event to ensure proper initialization and handling of drag selection. Clicking directly on a selectable element should not select it, while starting the selection outside should enable selection.
Actual Behavior:
The ds:select event is being triggered before the ds:start:pre event, causing the element to be selected even when clicked directly.
Code Snippet:
import React, { useEffect, useRef } from 'react';
const DragSelectComponent = () => {
const elementRef = useRef(null);
const ds = useDragSelect();
useEffect(() => {
const element = elementRef.current;
if (!element || !ds) return;
ds.addSelectables(element);
}, [ds, elementRef]);
useEffect(() => {
if (!ds) return;
const startID = ds.subscribe('DS:start:pre', ({ event, isDragging }) => {
log({ message: 'DS:start:pre triggered' });
if (isDragging) ds.break();
const currentTarget = event.currentTarget;
if (currentTarget?.classList?.contains('selectable-item')) {
ds.break();
}
});
const selectID = ds.subscribe('DS:select', ({ item, isDragging }) => {
log({ message: 'DS:select triggered' });
const selectedItemID = item.id;
if (isDragging) return;
// Custom logic for handling selected item
});
return () => {
ds.unsubscribe('DS:start:pre', startID);
ds.unsubscribe('DS:select', selectID);
};
}, [ds]);
return <div ref={elementRef} className="selectable-item">Selectable Element</div>;
};
export default DragSelectComponent;
import React, { createContext, useState, useEffect, useContext } from 'react'
import type { DSInputElement } from 'dragselect'
import DragSelect from 'dragselect'
// Add a new prop for the selection change callback
type ProviderProps = {
children: React.ReactNode
settings?: ConstructorParameters<typeof DragSelect<DSInputElement>>[0]
}
const Context = createContext<DragSelect<DSInputElement> | undefined>(undefined)
function DragSelectProvider({ children, settings }: ProviderProps) {
const [ds, setDS] = useState<DragSelect<DSInputElement>>()
useEffect(() => {
setDS((prevState) => {
if (prevState) return prevState
return new DragSelect({})
})
return () => {
if (ds) {
ds.stop()
setDS(undefined)
}
}
}, [ds])
useEffect(() => {
ds?.setSettings(settings || {})
}, [ds, settings])
return <Context.Provider value={ds}>{children}</Context.Provider>
}
function useDragSelect() {
return useContext(Context)
}
Additional Information:
This issue disrupts the logical flow and may cause unexpected behaviors in the drag selection functionality. Please investigate the event sequencing and ensure that ds:start:pre is called before ds:select.
Logs/Console Output:
DS:select triggered
DS:start:pre triggered
Thank you for your attention to this matter.
Description:
It appears that the
ds:start:preevent is being triggered after theds:selectevent. This is contrary to the expected behavior whereds:start:preshould be called beforeds:select. This issue affects the logic for initializing and handling drag selection in the provided code snippet.I am trying to prevent the selection of an element when clicking on it directly, and only enable the selection when the selection started outside the selectable elements.
Steps to Reproduce:
Expected Behavior:
The
ds:start:preevent should be called before theds:selectevent to ensure proper initialization and handling of drag selection. Clicking directly on a selectable element should not select it, while starting the selection outside should enable selection.Actual Behavior:
The
ds:selectevent is being triggered before theds:start:preevent, causing the element to be selected even when clicked directly.Code Snippet:
Additional Information:
This issue disrupts the logical flow and may cause unexpected behaviors in the drag selection functionality. Please investigate the event sequencing and ensure that
ds:start:preis called beforeds:select.Logs/Console Output:
Thank you for your attention to this matter.