Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Functionality for switching between editing and interactive mode #674

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
7 changes: 5 additions & 2 deletions apps/demo/app/custom-ui/[...puckPath]/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Drawer } from "@/core/components/Drawer";
import { ChevronUp, ChevronDown, Globe, Lock, Unlock } from "lucide-react";

const CustomHeader = ({ onPublish }: { onPublish: (data: Data) => void }) => {
const { appState, dispatch } = usePuck();
const { appState, dispatch, isInteractive, setIsInteractive } = usePuck();

return (
<header
Expand All @@ -28,7 +28,10 @@ const CustomHeader = ({ onPublish }: { onPublish: (data: Data) => void }) => {
>
<span style={{ fontWeight: 600 }}>Custom UI example </span>
<div style={{ marginLeft: "auto", display: "flex", gap: 8 }}>
<div>
<div style={{ gap: 8, display: "flex" }}>
<Button onClick={() => setIsInteractive(!isInteractive)}>
Switch to {isInteractive ? "editing" : "interactive"} mode
</Button>
<Button
onClick={() => onPublish(appState.data)}
icon={<Globe size="14" />}
Expand Down
20 changes: 15 additions & 5 deletions packages/core/components/DraggableComponent/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { useAppContext } from "../Puck/context";
import { DefaultDraggable } from "../Draggable";
import { Loader } from "../Loader";
import { ActionBar } from "../ActionBar";
import { DefaultOverride } from "../DefaultOverride";

const getClassName = getClassNameFactory("DraggableComponent", styles);

Expand Down Expand Up @@ -81,8 +80,15 @@ export const DraggableComponent = ({
indicativeHover?: boolean;
style?: CSSProperties;
}) => {
const { zoomConfig, status, overrides, selectedItem, getPermissions } =
useAppContext();
const {
zoomConfig,
status,
overrides,
selectedItem,
getPermissions,
isInteractive,
} = useAppContext();

const isModifierHeld = useModifierHeld("Alt");

const El = status !== "LOADING" ? Draggable : DefaultDraggable;
Expand Down Expand Up @@ -128,11 +134,15 @@ export const DraggableComponent = ({
isLocked,
forceHover,
indicativeHover,
isInteractive,
})}
style={{
...style,
...provided.draggableProps.style,
cursor: isModifierHeld || isDragDisabled ? "pointer" : "grab",
cursor:
isInteractive || isModifierHeld || isDragDisabled
? "default"
: "grab",
}}
onMouseOver={onMouseOver}
onMouseOut={onMouseOut}
Expand Down Expand Up @@ -178,7 +188,7 @@ export const DraggableComponent = ({
)}

<div className={getClassName("overlay")} />
<div className={getClassName("contents")}>{children}</div>
<div className={getClassName("contents")}> {children}</div>
</div>
)}
</El>
Expand Down
9 changes: 9 additions & 0 deletions packages/core/components/DraggableComponent/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
overflow: hidden;
}

.DraggableComponent--isInteractive > .DraggableComponent-contents {
pointer-events: auto !important;
}

.DraggableComponent--isInteractive > .DraggableComponent-overlay {
cursor: default !important;
background: transparent !important;
}

.DraggableComponent-contents {
position: relative; /* Reset stacking context */
pointer-events: none;
Expand Down
9 changes: 9 additions & 0 deletions packages/core/components/Puck/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export type AppContext<
plugins: Plugin[];
overrides: Partial<Overrides>;
history: Partial<PuckHistory>;
isInteractive: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any ideas for how you could add this to the custom-ui demo? I like to use features in demos for easier reviewing, and so we can be sure things build properly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any information about future release? How soon can I start using this changes?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

setIsInteractive: React.Dispatch<SetStateAction<boolean>>;
viewports: Viewports;
zoomConfig: ZoomConfig;
setZoomConfig: (zoomConfig: ZoomConfig) => void;
Expand All @@ -94,6 +96,8 @@ const defaultContext: AppContext = {
resolveData: () => {},
plugins: [],
overrides: {},
isInteractive: false,
setIsInteractive: () => {},
history: {},
viewports: defaultViewports,
zoomConfig: {
Expand Down Expand Up @@ -127,12 +131,15 @@ export const AppProvider = ({
| "componentState"
| "setComponentState"
| "resolveData"
| "setIsInteractive"
>;
}) => {
const [zoomConfig, setZoomConfig] = useState(defaultContext.zoomConfig);

const [status, setStatus] = useState<Status>("LOADING");

const [isInteractive, setIsInteractive] = useState(false);

// App is ready when client has loaded, after initial render
// This triggers DropZones to activate
useEffect(() => {
Expand Down Expand Up @@ -222,6 +229,8 @@ export const AppProvider = ({
componentState,
setComponentState,
resolveData,
isInteractive,
setIsInteractive,
}}
>
{children}
Expand Down
1 change: 1 addition & 0 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ export function Puck<
config,
plugins: plugins || [],
overrides: loadedOverrides,
isInteractive: false,
history,
viewports,
iframe,
Expand Down
4 changes: 4 additions & 0 deletions packages/core/lib/use-puck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const usePuck = <UserConfig extends Config = Config>() => {
state: appState,
config,
history,
isInteractive,
setIsInteractive,
dispatch,
selectedItem: currentItem,
getPermissions,
Expand All @@ -15,9 +17,11 @@ export const usePuck = <UserConfig extends Config = Config>() => {
return {
appState,
config,
isInteractive,
dispatch,
getPermissions,
refreshPermissions,
setIsInteractive,
history: {
back: history.back!,
forward: history.forward!,
Expand Down