Skip to content

Commit

Permalink
chore: improve responsive behaviour of Puck header
Browse files Browse the repository at this point in the history
  • Loading branch information
monospaced authored and chrisvxd committed Nov 22, 2023
1 parent 3d6c5d4 commit 349f44d
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 50 deletions.
3 changes: 2 additions & 1 deletion packages/core/components/Button/Button.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
border: none;
border-radius: 4px;
color: white;
display: flex;
display: inline-flex;
align-items: center;
gap: 8px;
letter-spacing: 0.05ch;
font-family: var(--puck-font-family-proportional);
font-size: 14px;
font-weight: 400;
line-height: 1;
text-align: center;
text-decoration: none;
cursor: pointer;
Expand Down
100 changes: 100 additions & 0 deletions packages/core/components/MenuBar/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Dispatch, ReactElement, SetStateAction } from "react";
import { Globe, ChevronLeft, ChevronRight } from "react-feather";

import { Button } from "../Button";
import { IconButton } from "../IconButton/IconButton";
import getClassNameFactory from "../../lib/get-class-name-factory";
import { usePuckHistory } from "../../lib/use-puck-history";
import { PuckAction } from "../../reducer";
import type { AppState, Data } from "../../types/Config";

import styles from "./styles.module.css";

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

export const MenuBar = ({
appState,
data = { content: [], root: { props: { title: "" } } },
dispatch,
menuOpen = false,
onPublish,
renderHeaderActions,
setMenuOpen,
}: {
appState: AppState;
data: Data;
dispatch: (action: PuckAction) => void;
onPublish: (data: Data) => void;
menuOpen: boolean;
renderHeaderActions?: (props: {
state: AppState;
dispatch: (action: PuckAction) => void;
}) => ReactElement;
setMenuOpen: Dispatch<SetStateAction<boolean>>;
}) => {
const { canForward, canRewind, rewind, forward } = usePuckHistory({
appState,
dispatch,
});

return (
<div
className={getClassName({ menuOpen })}
onClick={(event) => {
const element = event.target as HTMLElement;

if (window.matchMedia("(min-width: 638px)").matches) {
return;
}
if (
element.tagName === "A" &&
element.getAttribute("href")?.startsWith("#")
) {
setMenuOpen(false);
}
}}
>
<div className={getClassName("inner")}>
<div className={getClassName("history")}>
<IconButton title="undo" disabled={!canRewind} onClick={rewind}>
<ChevronLeft
size={21}
stroke={
canRewind
? "var(--puck-color-black)"
: "var(--puck-color-grey-7)"
}
/>
</IconButton>
<IconButton title="redo" disabled={!canForward} onClick={forward}>
<ChevronRight
size={21}
stroke={
canForward
? "var(--puck-color-black)"
: "var(--puck-color-grey-7)"
}
/>
</IconButton>
</div>
<>
{renderHeaderActions &&
renderHeaderActions({
state: appState,
dispatch,
})}
</>
<div>
<Button
onClick={() => {
onPublish(data);
}}
icon={<Globe size="14px" />}
>
Publish
</Button>
</div>
</div>
</div>
);
};
47 changes: 47 additions & 0 deletions packages/core/components/MenuBar/styles.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.MenuBar {
background-color: var(--puck-color-white);
border-bottom: 1px solid var(--puck-color-grey-8);
display: none;
left: 0;
margin-top: 1px;
padding: 8px 16px;
position: absolute;
right: 0;
top: 100%;
z-index: 2;
}

.MenuBar--menuOpen {
display: block;
}

@media (min-width: 638px) {
.MenuBar {
border: none;
display: block;
margin-top: 0;
overflow-y: visible;
padding: 0;
position: static;
}
}

.MenuBar-inner {
align-items: center;
display: flex;
flex-wrap: wrap;
gap: 8px 16px;
justify-content: flex-end;
}

@media (min-width: 638px) {
.MenuBar-inner {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
}
}

.MenuBar-history {
display: flex;
}
70 changes: 22 additions & 48 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { Plugin } from "../../types/Plugin";
import { usePlaceholderStyle } from "../../lib/use-placeholder-style";

import { SidebarSection } from "../SidebarSection";
import { Globe, Sidebar, ChevronLeft, ChevronRight } from "react-feather";
import { ChevronDown, ChevronUp, Globe, Sidebar } from "react-feather";
import { Heading } from "../Heading";
import { IconButton } from "../IconButton/IconButton";
import { DropZone, DropZoneProvider, dropZoneContext } from "../DropZone";
Expand All @@ -34,10 +34,10 @@ import { findZonesForArea } from "../../lib/find-zones-for-area";
import { areaContainsZones } from "../../lib/area-contains-zones";
import { flushZones } from "../../lib/flush-zones";
import getClassNameFactory from "../../lib/get-class-name-factory";
import { usePuckHistory } from "../../lib/use-puck-history";
import { AppProvider, defaultAppState } from "./context";
import { useComponentList } from "../../lib/use-component-list";
import { useResolvedData } from "../../lib/use-resolved-data";
import { MenuBar } from "../MenuBar";
import styles from "./styles.module.css";

const getClassName = getClassNameFactory("Puck", styles);
Expand Down Expand Up @@ -153,10 +153,7 @@ export function Puck({
dispatch
);

const { canForward, canRewind, rewind, forward } = usePuckHistory({
appState,
dispatch,
});
const [menuOpen, setMenuOpen] = useState(false);

const { itemSelector, leftSideBarVisible, rightSideBarVisible } = ui;

Expand Down Expand Up @@ -410,6 +407,7 @@ export function Puck({
<div
className={getClassName({
leftSideBarVisible,
menuOpen,
rightSideBarVisible,
})}
>
Expand Down Expand Up @@ -467,53 +465,29 @@ export function Puck({
</Heading>
</div>
<div className={getClassName("headerTools")}>
<div style={{ display: "flex" }}>
<div className={getClassName("menuButton")}>
<IconButton
title="undo"
disabled={!canRewind}
onClick={rewind}
>
<ChevronLeft
size={21}
stroke={
canRewind
? "var(--puck-color-black)"
: "var(--puck-color-grey-7)"
}
/>
</IconButton>
<IconButton
title="redo"
disabled={!canForward}
onClick={forward}
>
<ChevronRight
size={21}
stroke={
canForward
? "var(--puck-color-black)"
: "var(--puck-color-grey-7)"
}
/>
</IconButton>
</div>
<>
{renderHeaderActions &&
renderHeaderActions({
state: appState,
dispatch,
})}
</>
<div>
<Button
onClick={() => {
onPublish(data);
return setMenuOpen(!menuOpen);
}}
icon={<Globe size="14px" />}
title="Toggle menu bar"
>
Publish
</Button>
{menuOpen ? (
<ChevronUp focusable="false" />
) : (
<ChevronDown focusable="false" />
)}
</IconButton>
</div>
<MenuBar
appState={appState}
data={data}
dispatch={dispatch}
onPublish={onPublish}
menuOpen={menuOpen}
renderHeaderActions={renderHeaderActions}
setMenuOpen={setMenuOpen}
/>
</div>
</div>
)}
Expand Down
19 changes: 18 additions & 1 deletion packages/core/components/Puck/styles.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@
border-bottom: 1px solid var(--puck-color-grey-8);
color: var(--puck-color-black);
grid-area: header;
position: relative;
max-width: 100vw;
}

.Puck-headerInner {
align-items: start;
align-items: end;
display: grid;
gap: var(--puck-space-px);
grid-template-areas: "left middle right";
Expand Down Expand Up @@ -142,6 +144,21 @@
justify-content: flex-end;
}

.Puck-menuButton {
color: var(--puck-color-grey-4);
margin-left: -4px;
}

.Puck--menuOpen .Puck-menuButton {
color: var(--puck-color-black);
}

@media (min-width: 638px) {
.Puck-menuButton {
display: none;
}
}

.Puck-leftSideBar {
background: var(--puck-color-grey-11);
border-right: 1px solid var(--puck-color-grey-8);
Expand Down

1 comment on commit 349f44d

@vercel
Copy link

@vercel vercel bot commented on 349f44d Nov 22, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.