UNPKG

@wordpress/editor

Version:
135 lines (134 loc) 3.65 kB
// packages/editor/src/components/resizable-editor/index.js import clsx from "clsx"; import { useDispatch } from "@wordpress/data"; import { useRef, useCallback, useState } from "@wordpress/element"; import { ResizableBox, __unstableMotion as motion } from "@wordpress/components"; import { useReducedMotion } from "@wordpress/compose"; import ResizeHandle from "./resize-handle.mjs"; import { store as editorStore } from "../../store/index.mjs"; import { unlock } from "../../lock-unlock.mjs"; import { jsx } from "react/jsx-runtime"; var HANDLE_STYLES_OVERRIDE = { position: void 0, userSelect: void 0, cursor: void 0, width: void 0, height: void 0, top: void 0, right: void 0, bottom: void 0, left: void 0 }; function isAtMaxWidth(currentWidth, containerWidth, tolerance = 0) { return containerWidth > 0 && currentWidth >= containerWidth - tolerance; } function ResizableEditor({ className, enableResizing, width = "100%", height = "100%", children }) { const [isResizing, setIsResizing] = useState(false); const disableMotion = useReducedMotion(); const { setCanvasWidth } = unlock(useDispatch(editorStore)); const resizableRef = useRef(); const resizeWidthBy = useCallback( (deltaPixels) => { if (resizableRef.current) { const _isAtMaxWidth = isAtMaxWidth( resizableRef.current.offsetWidth + deltaPixels, resizableRef.current.parentElement?.offsetWidth ?? 0, 80 ); setCanvasWidth( _isAtMaxWidth ? void 0 : resizableRef.current.offsetWidth + deltaPixels ); } }, [setCanvasWidth] ); const updateCanvasWidth = useCallback( (element, isResizeEnd) => { const currentWidth = element.offsetWidth; const containerWidth = element.parentElement?.offsetWidth ?? 0; setCanvasWidth( isResizeEnd && isAtMaxWidth(currentWidth, containerWidth, 80) ? void 0 : currentWidth ); }, [setCanvasWidth] ); return /* @__PURE__ */ jsx( ResizableBox, { as: motion.div, initial: false, animate: { height }, transition: { type: "tween", duration: isResizing || disableMotion ? 0 : 0.2, ease: "easeOut" }, className: clsx("editor-resizable-editor", className, { "is-resizable": enableResizing, "is-resizing": isResizing }), ref: (api) => { resizableRef.current = api?.resizable; }, size: { width, height }, onResizeStart: () => { setIsResizing(true); }, onResize: (event, direction, element) => { updateCanvasWidth(element); }, onResizeStop: (event, direction, element) => { updateCanvasWidth(element, true); setIsResizing(false); }, minWidth: 300, maxWidth: "100%", maxHeight: "100%", enable: { left: enableResizing, right: enableResizing }, showHandle: enableResizing, resizeRatio: 2, handleComponent: { left: /* @__PURE__ */ jsx( ResizeHandle, { direction: "left", resizeWidthBy } ), right: /* @__PURE__ */ jsx( ResizeHandle, { direction: "right", resizeWidthBy } ) }, handleClasses: void 0, handleStyles: { left: HANDLE_STYLES_OVERRIDE, right: HANDLE_STYLES_OVERRIDE }, children } ); } var resizable_editor_default = ResizableEditor; export { resizable_editor_default as default }; //# sourceMappingURL=index.mjs.map