Skip to content

Commit

Permalink
Add placeholder to document editor block menu (#5618)
Browse files Browse the repository at this point in the history
* update to remove menu completely when no block items exist

* changeset
  • Loading branch information
gwyneplaine committed May 6, 2021
1 parent e69bcc2 commit 7e24b9c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/hip-peas-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/fields-document': patch
---

Added logic to ensure the block-menu in the toolbar is hidden when no component blocks or relationships are provided.
19 changes: 15 additions & 4 deletions packages-next/fields-document/src/DocumentEditor/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
/** @jsx jsx */

import { Fragment, ReactNode, forwardRef, useState, HTMLAttributes, useMemo } from 'react';
import {
Fragment,
ReactNode,
forwardRef,
useState,
HTMLAttributes,
useMemo,
useContext,
} from 'react';
import { Editor, Transforms } from 'slate';
import { applyRefs } from 'apply-ref';

Expand All @@ -25,12 +33,12 @@ import {
ToolbarSeparator,
} from './primitives';
import { linkButton } from './link';
import { BlockComponentsButtons } from './component-blocks';
import { BlockComponentsButtons, ComponentBlockContext } from './component-blocks';
import { clearFormatting, Mark, modifierKeyText } from './utils';
import { LayoutsButton } from './layouts';
import { ListButton } from './lists';
import { blockquoteButton } from './blockquote';
import { RelationshipButton } from './relationship';
import { DocumentFieldRelationshipsContext, RelationshipButton } from './relationship';
import { codeButton } from './code-block';
import { TextAlignMenu } from './alignment';
import { dividerButton } from './divider';
Expand All @@ -44,6 +52,9 @@ export function Toolbar({
viewState?: { expanded: boolean; toggle: () => void };
}) {
const ExpandIcon = viewState?.expanded ? Minimize2Icon : Maximize2Icon;
const relationship = useContext(DocumentFieldRelationshipsContext);
const blockComponent = useContext(ComponentBlockContext);
const hasBlockItems = Object.entries(relationship).length || Object.keys(blockComponent).length;

return (
<ToolbarContainer>
Expand Down Expand Up @@ -105,7 +116,7 @@ export function Toolbar({
{documentFeatures.formatting.blockTypes.blockquote && blockquoteButton}
{!!documentFeatures.layouts.length && <LayoutsButton layouts={documentFeatures.layouts} />}
{documentFeatures.formatting.blockTypes.code && codeButton}
<InsertBlockMenu />
{!!hasBlockItems && <InsertBlockMenu />}

<ToolbarSeparator />
{useMemo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type Relationships = Record<
)
>;

const DocumentFieldRelationshipsContext = createContext<Relationships>({});
export const DocumentFieldRelationshipsContext = createContext<Relationships>({});

export function useDocumentFieldRelationships() {
return useContext(DocumentFieldRelationshipsContext);
Expand Down

1 comment on commit 7e24b9c

@vercel
Copy link

@vercel vercel bot commented on 7e24b9c May 6, 2021

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.