Skip to content

Instantly share code, notes, and snippets.

View crazypixel's full-sized avatar

Shay Keinan crazypixel

  • Salesforce
View GitHub Profile
@crazypixel
crazypixel / dnd-hooks-sort-end.jsx
Created September 8, 2019 18:01
hookd dnd - drag end
const handleDragEnd = useCallback(() => {
setState(state => ({
...state,
order: state.dragOrder,
draggedIndex: null
}));
}, []);
@crazypixel
crazypixel / dnd-hookd-sortable-ondrag.jsx
Created September 8, 2019 17:55
dnd hooks sortable
const handleDrag = useCallback(({translation, id}) => {
const delta = Math.round(translation.y / HEIGHT);
const index = state.order.indexOf(id);
const dragOrder = state.order.filter(index => index !== id);
if (!inRange(index + delta, 0, items.length)) {
return;
}
dragOrder.splice(index + delta, 0, id);
@crazypixel
crazypixel / dnd-translation.jsx
Created September 8, 2019 17:45
translation
const translation = {
x: clientX - state.origin.x,
y: clientY - state.origin.y
};
@crazypixel
crazypixel / dnd-hooks-styles.jsx
Created September 8, 2019 17:42
use memo for styles
const styles = useMemo(() => ({
cursor: state.isDragging ? '-webkit-grabbing' : '-webkit-grab',
transform: `translate(${state.translation.x}px, ${state.translation.y}px)`,
transition: state.isDragging ? 'none' : 'transform 500ms',
zIndex: state.isDragging ? 2 : 1,
position: state.isDragging ? 'absolute' : 'relative'
}), [state.isDragging, state.translation]);
@crazypixel
crazypixel / dnd-hooks-sortable.jsx
Last active November 12, 2019 17:36
Drag & Drop with hooks - sortable list example
import React, {useState, useCallback} from 'react';
import styled from 'styled-components';
import {range, inRange} from 'lodash';
import Draggable from './Draggable';
const MAX = 5;
const HEIGHT = 80;
const App = () => {
const items = range(MAX);
@crazypixel
crazypixel / dnd-hooks-draggable.jsx
Last active July 3, 2021 13:12
Drag and drop with hooks - Draggable component
import React, {useState, useCallback, useMemo, useEffect} from 'react';
const POSITION = {x: 0, y: 0};
const Draggable = ({children, id, onDrag, onDragEnd}) => {
const [state, setState] = useState({
isDragging: false,
origin: POSITION,
translation: POSITION
});
@crazypixel
crazypixel / dnd-hooks-usage.jsx
Last active September 8, 2019 15:53
Drag and Drop with hooks - basic usage example
import React from 'react';
import Draggable from './Draggble';
const App = () => {
return (
<div>
<Draggable onDrag={console.log} id="uniqueId">
<h2>Drag me</h2>
</Draggable>
</div>
import React from 'react';
import styled, { css } from 'styled-components';
export default class Draggable extends React.Component {
state = {
isDragging: false,
originalX: 0,
originalY: 0,
http://bit.ly/500course
ES2015 Playground - http://mikelambert.me/es6/
Course PPT - https://www.slideshare.net/secret/83Amj6pr7ExOrb
Cheatsheet - https://ihatetomatoes.net/wp-content/uploads/2017/01/react-cheat-sheet.pdf
First use git
git config --global user.name "Mona Lisa"
git config --global user.email "foo@bar.com"