REACT 5, useRef() instance methods, React keyframe animations, useRef() scroll, onDrag() and onDrop() React Events
React events (onClick) and their Event handlers (onClick(()=>())) have access to the React Event Object. We use it to access the (event) type, DOM target, and clientX, pageX, and screenX positions.
event.key/event.code on React Event Object
event.crtlKey checks if the crtl button was being held during onClick(). Different React events will give access to different event object properties, like event.key/event.code on onKeyDown() input events.
function controlla(e){
console.log( e )
console.log( "Client X/Y: " + e.clientX + "/" + e.clientY)
console.log( "Page X/Y: " + e.pageX + "/" + e.pageY )
console.log( "Screen X/Y: " + e.screenX + "/" + e.screenY )
console.log( "Trigger event: " + e.type )
console.log( "Was the crtl key used? " + e.ctrlKey )
console.log( e.target )
console.log( e.target.className )
let pressed = e.target.className
if( pressed.includes("danger") ){
console.log("danger button")
}else if( pressed.includes("warning") ){
console.log("Warning button")
}else{
console.log("Success button")
}
}On DOM we:
<div className="text-end my-4">
<button onClick={controlla} className="btn btn-success">Uno</button>
<button onClick={controlla} className="btn btn-warning">Due</button>
<button onClick={controlla} className="btn btn-danger">Tre</button>
</div>
We e.target mouseEnter, mouseLeave, mouseOver, mouseOut, and mouseMove React events, and render with useState().

Instance methods on useRef() DOM elements.
We can useRef() to access DOM element attributes with hasAttribute() and getAttribute().
We edit a useRef() attribute with setAttribute(attribute, value):
We removeAttribute() (instead of setting it as null) and toggleAttribute() (to toggle in/out attributes on React Events)
We useRef() an input current.value to append() it in a JSX tag.
We need 2 useRef(), for the input ref and the append() DOM target.
On the DOM the ref will store and update the input.value (we append() to render it):
useState() can {render} the input.target value without a useRef(), but we need to value/onChange() the input.
On the DOM we:

We use scroll(), scrollBy(), and scrollIntoView() on an overflow:scroll useRef() DOM element. The scroll() method moves the element to a set of coordinates inside a container.
scrollBy() and scrollIntoView() guide
scrolllBy() adds up its X/Y coordinates to the current position.
scrollIntoView() scrolls the container so the element which calls it gets into the user browser viewpoint.
In the DOM we useRef() the scrollIntoView() on the element, not the container.

animate() applies a CSS keyframe() and a timing object to a useRef() DOM element.
The keyframe is an array of objects to iterate and the timing object has the animation properties.
animate() is the equivalent of adding an animation class to a DOM element.
The timing object contains the animation properties.
The animate() method on useRef() DOM elements won't trigger the onAnimationStart, onAnimationIteration, or onAnimationEnd events (we need CSS keyframes).
onAnimationIteration() will trigger only when iteration-count > 1.
The events go on the animated DOM element.
The events won't trigger even if the animation is replicated.
We store the onCopy() event value with document.getSelection() and useRef().

onDrag() and onDrop() ReactJS events
The HTML Drag and Drop API implements draggable elements in the browser. DOM elements with the draggable attribute trigger onDrag() React events.
The onDrop() target DOM element can trigger:
We use the dataTransfer object during the drag-and-drop events. We setData("type format", "value"), setDragImage(image, Xoffset, Yoffset) for the feedback image and effectsAllowed for the cursor dropEffect.
We set the dropEffect onDragOver()/onDragStart() and check it onDragEnd() to filter drag operations.

The onDragEnd() event returns a dropEffect= "none" if the onDrop() fails. We use dataTransfer.types.includes(data format) to filter the dataTransfer object. We can use e.clientX/Y to check the dropped position.
We can dataTransfer.clearData(data format) to delete the setData() (it seems to work only onDragStart() )
Image drag and drop DOM elements stacking and CSS responsive
We color the 50/50 bicolor dropzone CSS with:
We useRef() for the 2 columns and the image counter, then we set the dataTransfer data onDragstart().
In onDragEnd() if the onDrop() fails we return the columns to their starter colors.
In onDragEnter() we modify the CSS using classList depending on the "entered" column. In case of overlaying images we use their ID to check the column, onDragOver() is necessary for the onDrag() event.
We appendChild() an ID image onDrop() using the e.target.id for the columns, we also filter the dataTransfer data to avoid different onDrag() from triggering the onDrop().
The DOM drag and drop elements are:

Last updated