MCPcopy Create free account
hub / github.com/editablejs/editable / Resizer

Function Resizer

packages/ui/src/resizer.tsx:21–177  ·  view source on GitHub ↗
({
  onResize,
  onChange,
  maxHeight,
  maxWidth,
  minHeight = 10,
  minWidth = 10,
  previewImage,
  holders = ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
  className,
})

Source from the content-addressed store, hash-verified

19const StyledHolder = tw.div`absolute w-3 h-3 border-2 border-white rounded-full cursor-nwse-resize bg-primary pointer-events-auto z-[1]`
20
21export const Resizer: FC<ResizerProps> = ({
22 onResize,
23 onChange,
24 maxHeight,
25 maxWidth,
26 minHeight = 10,
27 minWidth = 10,
28 previewImage,
29 holders = ['top-left', 'top-right', 'bottom-left', 'bottom-right'],
30 className,
31}) => {
32 const pointRef = React.useRef({ x: 0, y: 0 })
33 const holdreRef = React.useRef<ResizerHolder>('top-left')
34 const [size, setSize] = React.useState({ width: 0, height: 0 })
35 const sizeRef = React.useRef(size)
36 const [resizing, setResizing] = React.useState(false)
37 const rateRef = React.useRef(1)
38 const ref = useRef<HTMLDivElement>(null)
39
40 const handleMouseDown = (event: React.MouseEvent<HTMLDivElement>, holder: ResizerHolder) => {
41 event.preventDefault()
42
43 pointRef.current = {
44 x: event.clientX,
45 y: event.clientY,
46 }
47 holdreRef.current = holder
48 const el = ref.current!
49 const { width, height } = el.getBoundingClientRect()
50 setSize({
51 width,
52 height,
53 })
54 sizeRef.current = {
55 width,
56 height,
57 }
58 setResizing(true)
59 rateRef.current = height / width
60
61 el.style.top = ~['top-left', 'top-right'].indexOf(holder) ? 'auto' : '0'
62 el.style.left = ~['top-left', 'bottom-left'].indexOf(holder) ? 'auto' : '0'
63 el.style.right = ~['top-right', 'bottom-right'].indexOf(holder) ? 'auto' : '0'
64 el.style.bottom = ~['bottom-left', 'bottom-right'].indexOf(holder) ? 'auto' : '0'
65
66 if ('ontouchstart' in document.documentElement)
67 window.addEventListener('touchmove', handleMouseMove)
68 else window.addEventListener('mousemove', handleMouseMove)
69 window.addEventListener('mouseup', handleMouseUp)
70 }
71
72 const handleMouseMove = (event: MouseEvent | TouchEvent) => {
73 let x = 0,
74 y = 0
75 if (event instanceof TouchEvent) {
76 const touch = event.touches[0]
77 x = touch.clientX
78 y = touch.clientY

Callers

nothing calls this directly

Calls 1

handleMouseDownFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…