-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from glweems/draggable
Draggable
- Loading branch information
Showing
24 changed files
with
542 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,102 +1,120 @@ | ||
import Select from '@components/Select'; | ||
import { colors } from '@lib/theme'; | ||
import theme, { colors } from '@lib/theme'; | ||
import { gridUnits } from '@lib/utils'; | ||
import { Grid } from '@primer/components'; | ||
import { GrabberIcon, XIcon } from '@primer/octicons-react'; | ||
import { useShiftKeyPressed } from '@ui/useShftKeyPressed'; | ||
import React, { FC, memo, useCallback } from 'react'; | ||
import { useRecoilState, useRecoilValue, useSetRecoilState } from 'recoil'; | ||
import { Entry } from 'css-grid-template-parser'; | ||
import React, { CSSProperties, memo, useCallback } from 'react'; | ||
import { SortableElement, SortableHandle } from 'react-sortable-hoc'; | ||
import { useRecoilState, useSetRecoilState } from 'recoil'; | ||
import { GridControlObjKey } from './GridControlId'; | ||
import { gridControlState, selectedControlState } from './gridState'; | ||
type Handler = ( | ||
e: React.ChangeEvent<HTMLInputElement & HTMLSelectElement> | ||
) => void; | ||
|
||
const GridControlProperties: FC<{ | ||
type GridControlPropertiesProps = Entry & { | ||
id: string | `${GridControlObjKey}.${number}`; | ||
}> = ({ id }) => { | ||
const { canDelete, ...control } = useRecoilValue(gridControlState(id)); | ||
const [selectedIds, setSelectedIds] = useRecoilState(selectedControlState); | ||
const setControl = useSetRecoilState(gridControlState(id)); | ||
const shiftKeyPressed = useShiftKeyPressed(); | ||
const handleChange: Handler = useCallback( | ||
(e) => { | ||
setControl((prev) => ({ | ||
...prev, | ||
[e.currentTarget.name]: e.currentTarget.value, | ||
})); | ||
}, | ||
[setControl] | ||
); | ||
const isSelected = selectedIds.includes(id); | ||
const style = isSelected | ||
? id.split('.').includes('rows') | ||
? { | ||
background: colors.yellow[4], | ||
boxShadow: `0 2px 0 0 ${colors.yellow[8]}`, | ||
} | ||
: { | ||
background: colors.blue[4], | ||
boxShadow: `0 2px 0 0 ${colors.blue[8]}`, | ||
} | ||
: { background: 'transparent' }; | ||
canDelete: boolean; | ||
}; | ||
|
||
const onEnter = useCallback(() => { | ||
setSelectedIds((ids) => { | ||
// Do nothing if the element is already selected | ||
if (isSelected) return ids; | ||
const GridControlProperties = SortableElement( | ||
({ id, amount, unit, canDelete }: GridControlPropertiesProps) => { | ||
const [selectedIds, setSelectedIds] = useRecoilState(selectedControlState); | ||
const setControl = useSetRecoilState(gridControlState(id)); | ||
const shiftKeyPressed = useShiftKeyPressed(); | ||
const handleChange: Handler = useCallback( | ||
(e) => { | ||
setControl((prev) => ({ | ||
...prev, | ||
[e.currentTarget.name]: e.currentTarget.value, | ||
})); | ||
}, | ||
[setControl] | ||
); | ||
const isSelected = selectedIds.includes(id); | ||
const style: CSSProperties = isSelected | ||
? id.split('.').includes('rows') | ||
? { | ||
background: colors.yellow[4], | ||
boxShadow: `0 2px 0 0 ${colors.yellow[8]}`, | ||
} | ||
: { | ||
background: colors.blue[4], | ||
boxShadow: `0 2px 0 0 ${colors.blue[8]}`, | ||
} | ||
: { background: 'transparent' }; | ||
|
||
// Add this element to the selection if shift is pressed | ||
if (shiftKeyPressed) return [...ids, id]; | ||
const onEnter = useCallback(() => { | ||
setSelectedIds((ids) => { | ||
if (isSelected) return ids; | ||
if (shiftKeyPressed) return [...ids, id]; | ||
return [id]; | ||
}); | ||
}, [id, isSelected, setSelectedIds, shiftKeyPressed]); | ||
|
||
// Otherwise, make this one the only selected element | ||
return [id]; | ||
}); | ||
}, [id, isSelected, setSelectedIds, shiftKeyPressed]); | ||
const onLeave = useCallback(() => { | ||
if (!shiftKeyPressed) setSelectedIds([]); | ||
}, [setSelectedIds, shiftKeyPressed]); | ||
return ( | ||
<Grid | ||
gridTemplateColumns="auto repeat(3, 1fr)" | ||
padding={2} | ||
style={style} | ||
justifyContent="start" | ||
onPointerEnter={onEnter} | ||
onPointerLeave={onLeave} | ||
gridGap="0 0.5rem" | ||
> | ||
<div> | ||
<GrabberIcon /> | ||
</div> | ||
const onLeave = useCallback(() => { | ||
if (!shiftKeyPressed) setSelectedIds([]); | ||
}, [setSelectedIds, shiftKeyPressed]); | ||
|
||
<input | ||
className="btn" | ||
autoComplete="off" | ||
name="amount" | ||
type="number" | ||
value={control.amount} | ||
onChange={handleChange} | ||
return ( | ||
<div | ||
style={{ ...gridPropertiesStyles, ...style }} | ||
css={` | ||
width: 100px; | ||
&:focus, | ||
:focus-within { | ||
outline: 4px dashed ${colors.focus}; | ||
outline-offset: 4px; | ||
} | ||
`} | ||
/> | ||
<Select | ||
className="btn" | ||
name="unit" | ||
value={control.unit} | ||
onChange={handleChange} | ||
options={gridUnits} | ||
/> | ||
<button | ||
className="btn" | ||
disabled={canDelete} | ||
onMouseDown={() => setControl(null)} | ||
onPointerEnter={onEnter} | ||
onPointerLeave={onLeave} | ||
> | ||
<XIcon /> | ||
</button> | ||
</Grid> | ||
); | ||
<DragHandle disabled={canDelete} /> | ||
<input | ||
autoComplete="off" | ||
name="amount" | ||
type="number" | ||
value={amount} | ||
onChange={handleChange} | ||
/> | ||
<Select | ||
name="unit" | ||
value={unit} | ||
onChange={handleChange} | ||
options={gridUnits} | ||
/> | ||
<button | ||
className="close-btn" | ||
disabled={canDelete} | ||
onMouseDown={() => setControl(null)} | ||
> | ||
<XIcon size={28} /> | ||
</button> | ||
</div> | ||
); | ||
} | ||
); | ||
|
||
const DragHandle = SortableHandle(({ disabled }) => ( | ||
<div | ||
style={{ | ||
cursor: disabled ? 'null' : 'grab', | ||
justifySelf: 'stretch', | ||
color: disabled ? colors.baseGlare : colors.white, | ||
}} | ||
> | ||
<GrabberIcon /> | ||
</div> | ||
)); | ||
|
||
export const gridPropertiesStyles: CSSProperties = { | ||
display: 'grid', | ||
gridTemplateColumns: 'auto repeat(2, 1fr) auto', | ||
padding: theme.space[2], | ||
alignItems: 'center', | ||
justifyContent: 'start', | ||
columnGap: theme.space[2], | ||
}; | ||
|
||
export default memo(GridControlProperties); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
2e8db4f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: