Skip to content

Commit

Permalink
feat: enable override of publish button (breaking change)
Browse files Browse the repository at this point in the history
`headerActions` overrides must now render `children` to render the publish button
  • Loading branch information
chrisvxd committed Apr 24, 2024
1 parent 5c8c0e1 commit 480467a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 26 deletions.
4 changes: 3 additions & 1 deletion apps/demo/app/[...puckPath]/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ export function Client({ path, isEdit }: { path: string; isEdit: boolean }) {
plugins={[headingAnalyzer]}
headerPath={path}
overrides={{
headerActions: () => (
headerActions: ({ children }) => (
<>
<div>
<Button href={path} newTab variant="secondary">
View page
</Button>
</div>

{children}
</>
),
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ const overrides = {

### `children`

The default node for the header actions.
The default node for the header actions, which includes the default publish button.
44 changes: 41 additions & 3 deletions apps/docs/pages/docs/extending-puck/custom-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,49 @@ export function Editor() {

There are many different overrides available. See the [`overrides` API reference](/docs/api-reference/overrides) for the full list.

### Field type override example
### Custom publish button example

Field type overrides deserve a special mention as they showcase the power of this API.
A common use case is to override the Puck header. You can either use the [`header` override](/docs/api-reference/overrides/header) to change the entire header, or use the [`headerActions` override](/docs/api-reference/overrides/header-actions) to inject new controls into the header and change the publish button.

Using [`overrides`](/docs/api-reference/components/puck#overrides), it's possible to provide a custom implementation of all fields of a certain type by specifying the [`fieldTypes`](/docs/api-reference/overrides/field-types).
Here's an example that replaces the default publish button with a custom one:

```tsx showLineNumbers copy {10-26}
import { Puck, usePuck } from "@measured/puck";

const save = () => {};

export function Editor() {
return (
<Puck
// ...
overrides={{
headerActions: ({ children }) => {
const { appState } = usePuck();

return (
<>
<button
onClick={() => {
save(appState.data);
}}
>
Save
</button>

{/* Render default header actions, such as the default Button */}
{/*{children}*/}
</>
);
},
}}
/>
);
}
```

### Custom field type example

An advanced use case is overriding all fields of a certain type by specifying the [`fieldTypes` override](/docs/api-reference/overrides/field-types).

```tsx showLineNumbers copy {8-18}
import { Puck } from "@measured/puck";
Expand Down
10 changes: 0 additions & 10 deletions packages/core/components/MenuBar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ export const MenuBar = ({
dispatch,
})}
</>
<div>
<Button
onClick={() => {
onPublish && onPublish(data);
}}
icon={<Globe size="14px" />}
>
Publish
</Button>
</div>
</div>
</div>
);
Expand Down
30 changes: 20 additions & 10 deletions packages/core/components/Puck/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -476,15 +476,16 @@ export function Puck<UserConfig extends Config = Config>({
<CustomHeader
actions={
<>
<CustomHeaderActions />
<Button
onClick={() => {
onPublish && onPublish(data);
}}
icon={<Globe size="14px" />}
>
Publish
</Button>
<CustomHeaderActions>
<Button
onClick={() => {
onPublish && onPublish(data);
}}
icon={<Globe size="14px" />}
>
Publish
</Button>
</CustomHeaderActions>
</>
}
>
Expand Down Expand Up @@ -547,7 +548,16 @@ export function Puck<UserConfig extends Config = Config>({
onPublish={onPublish}
menuOpen={menuOpen}
renderHeaderActions={() => (
<CustomHeaderActions />
<CustomHeaderActions>
<Button
onClick={() => {
onPublish && onPublish(data);
}}
icon={<Globe size="14px" />}
>
Publish
</Button>
</CustomHeaderActions>
)}
setMenuOpen={setMenuOpen}
/>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/types/Overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type OverridesGeneric<Shape extends { [key in OverrideKey]: any }> = Shape;
export type Overrides = OverridesGeneric<{
fieldTypes: Partial<FieldRenderFunctions>;
header: RenderFunc<{ actions: ReactNode; children: ReactNode }>;
headerActions: RenderFunc<{ children?: ReactNode }>;
headerActions: RenderFunc<{ children: ReactNode }>;
preview: RenderFunc;
fields: RenderFunc<{
children: ReactNode;
Expand Down

0 comments on commit 480467a

Please sign in to comment.