Skip to content

Commit

Permalink
Improve layout selection UX in createColumnsBlock (vivid-planet#1384)
Browse files Browse the repository at this point in the history
Hide select when there's only one layout for a specific number of
columns.

---------

Co-authored-by: Ricky James Smith <jamesricky@me.com>
Co-authored-by: Johannes Obermair <48853629+johnnyomair@users.noreply.github.com>
  • Loading branch information
3 people authored and VP-DS committed Jan 12, 2024
1 parent 6e44833 commit 4bfaa9b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
7 changes: 7 additions & 0 deletions .changeset/yellow-dots-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/blocks-admin": minor
---

Improve layout selection UX in `createColumnsBlock`

Hide select when there's only one layout for a specific number of columns
14 changes: 14 additions & 0 deletions demo/admin/src/pages/blocks/ColumnsBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ const ColumnsBlock = createColumnsBlock({
</ColumnsLayoutPreview>
),
},
{
name: "two-columns-12-6",
label: "Two columns 12-6",
columns: 2,
preview: (
<ColumnsLayoutPreview>
<ColumnsLayoutPreviewSpacing width={2} />
<ColumnsLayoutPreviewContent width={12} />
<ColumnsLayoutPreviewSpacing width={2} />
<ColumnsLayoutPreviewContent width={6} />
<ColumnsLayoutPreviewSpacing width={2} />
</ColumnsLayoutPreview>
),
},
],
contentBlock: ColumnsContentBlock,
});
Expand Down
2 changes: 1 addition & 1 deletion demo/api/src/pages/blocks/columns.block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ColumnsContentBlock = createBlocksBlock(
const ColumnsBlock = ColumnsBlockFactory.create(
{
contentBlock: ColumnsContentBlock,
layouts: [{ name: "one-column" }, { name: "two-columns" }],
layouts: [{ name: "one-column" }, { name: "two-columns" }, { name: "two-columns-12-6" }],
},
"Columns",
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface Props extends FieldRenderProps<ColumnsBlockLayout> {
layouts: ColumnsBlockLayout[];
}

export function FinalFormLayoutSelect({ input: { value, onChange }, layouts }: Props): React.ReactElement {
export function FinalFormLayoutSelect({ input: { value, onChange }, layouts }: Props) {
const sections = React.useMemo(() => {
const sections: Section[] = [];

Expand Down Expand Up @@ -57,6 +57,18 @@ export function FinalFormLayoutSelect({ input: { value, onChange }, layouts }: P
onChange(layouts.find((layout) => layout.name === event.target.value));
};

if (layouts.length === 1) {
const layout = layouts[0];
return (
<SingleLayoutPreview>
<MenuItemContent>
{layout.preview}
<ListItemText primary={layout.label} secondary={layout.name} />
</MenuItemContent>
</SingleLayoutPreview>
);
}

return (
<Select value={value.name} onChange={handleChange} fullWidth>
{sections.map((section, i) => [
Expand Down Expand Up @@ -112,3 +124,9 @@ const ListItemText = styled(MuiListItemText)`
margin-top: 0;
margin-bottom: 0;
`;

const SingleLayoutPreview = styled("div")`
background-color: ${({ theme }) => theme.palette.background.paper};
border: 1px solid ${({ theme }) => theme.palette.divider};
padding: 9px;
`;
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export function createColumnsBlock<T extends BlockInterface>({
fullWidth
/>
</BlocksFinalForm>
<AdminComponentSection title={<FormattedMessage id="comet.blocks.columns.columns" defaultMessage="Columns" />}>
<AdminComponentSection title={<FormattedMessage id="comet.blocks.columns.items" defaultMessage="Items" />}>
<AdminComponentPaper disablePadding>
<StackSwitchApiContext.Consumer>
{(stackApi) => {
Expand Down

0 comments on commit 4bfaa9b

Please sign in to comment.