-
Notifications
You must be signed in to change notification settings - Fork 365
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
feat: add viewport switching
Showing
13 changed files
with
478 additions
and
49 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
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
144 changes: 144 additions & 0 deletions
144
packages/core/components/Puck/components/Canvas/index.tsx
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 |
---|---|---|
@@ -0,0 +1,144 @@ | ||
import { getBox } from "css-box-model"; | ||
import { | ||
ReactNode, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from "react"; | ||
import { useAppContext } from "../../context"; | ||
import { ViewportControls } from "../../../ViewportControls"; | ||
import styles from "../../styles.module.css"; | ||
import { getClassNameFactory } from "../../../../lib"; | ||
import { Preview } from "../Preview"; | ||
import { AppState } from "../../../../types/Config"; | ||
import { getZoomConfig } from "../../../../lib/get-zoom-config"; | ||
|
||
const getClassName = getClassNameFactory("Puck", styles); | ||
|
||
const ZOOM_ON_CHANGE = true; | ||
|
||
export const Canvas = () => { | ||
const { dispatch, state, overrides, setUi } = useAppContext(); | ||
const { ui } = state; | ||
const frameRef = useRef<HTMLDivElement>(null); | ||
|
||
const [rootHeight, setRootHeight] = useState(0); | ||
const [autoZoom, setAutoZoom] = useState(0); | ||
const [showTransition, setShowTransition] = useState(false); | ||
|
||
const defaultRender = useMemo< | ||
React.FunctionComponent<{ children?: ReactNode }> | ||
>(() => { | ||
const PuckDefault = ({ children }: { children?: ReactNode }) => ( | ||
<>{children}</> | ||
); | ||
|
||
return PuckDefault; | ||
}, []); | ||
|
||
const CustomPreview = useMemo( | ||
() => overrides.preview || defaultRender, | ||
[overrides] | ||
); | ||
|
||
const getFrameDimensions = useCallback(() => { | ||
if (frameRef.current) { | ||
const frame = frameRef.current; | ||
|
||
const box = getBox(frame); | ||
|
||
return { width: box.contentBox.width, height: box.contentBox.height }; | ||
} | ||
|
||
return { width: 0, height: 0 }; | ||
}, [frameRef]); | ||
|
||
// TODO deal with window resize | ||
const resetAutoZoom = useCallback( | ||
(uiViewport: AppState["ui"]["viewport"]) => { | ||
if (frameRef.current) { | ||
const zoomConfig = getZoomConfig(uiViewport, frameRef.current); | ||
|
||
setRootHeight(zoomConfig.rootHeight); | ||
setAutoZoom(zoomConfig.autoZoom); | ||
|
||
if (zoomConfig.zoom) { | ||
dispatch({ | ||
type: "setUi", | ||
ui: { | ||
...ui, | ||
viewport: { | ||
...uiViewport, | ||
zoom: zoomConfig.zoom, | ||
}, | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
[frameRef, ui.leftSideBarVisible, ui, autoZoom] | ||
); | ||
|
||
// Auto zoom | ||
useEffect(() => { | ||
resetAutoZoom(ui.viewport); | ||
}, [frameRef, ui.leftSideBarVisible, ui.rightSideBarVisible]); | ||
|
||
// Constrain height | ||
useEffect(() => { | ||
const { height: frameHeight } = getFrameDimensions(); | ||
|
||
if (ui.viewport.height === "auto") { | ||
setRootHeight(frameHeight / ui.viewport.zoom); | ||
} | ||
}, [ui.viewport.zoom]); | ||
|
||
return ( | ||
<div | ||
className={getClassName("canvas")} | ||
onClick={() => | ||
dispatch({ | ||
type: "setUi", | ||
ui: { itemSelector: null }, | ||
recordHistory: true, | ||
}) | ||
} | ||
> | ||
<div className={getClassName("canvasControls")}> | ||
<ViewportControls | ||
autoZoom={autoZoom} | ||
onViewportChange={(viewport) => { | ||
setShowTransition(true); | ||
|
||
const uiViewport = { ...viewport, zoom: ui.viewport.zoom }; | ||
|
||
setUi({ viewport: uiViewport }); | ||
|
||
if (ZOOM_ON_CHANGE) { | ||
resetAutoZoom(uiViewport); | ||
} | ||
}} | ||
/> | ||
</div> | ||
<div className={getClassName("frame")} ref={frameRef} id="puck-frame"> | ||
<div | ||
className={getClassName("root")} | ||
style={{ | ||
width: ui.viewport.width, | ||
height: rootHeight, | ||
transform: `scale(${ui.viewport.zoom})`, | ||
transition: showTransition | ||
? "width 150ms ease-out, transform 150ms ease-out" | ||
: "", | ||
}} | ||
> | ||
<CustomPreview> | ||
<Preview /> | ||
</CustomPreview> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
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.