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

Add support for custom labels in createOneOfBlock #2413

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
30 changes: 30 additions & 0 deletions .changeset/fluffy-points-think.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
"@comet/blocks-admin": minor
---

Add support for custom labels in OneOfBlock tabs

Usage:

- Add labels to `createOneOfBlock` props
- ````ts
labels: {
image: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.image" defaultMessage="Image" />}>
<Image />
</Tooltip>
),
damVideo: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.video.dam" defaultMessage="Video (DAM)" />}>
<Video />
</Tooltip>
),
youTubeVideo: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.video.youtube" defaultMessage="Video (YouTube)" />}>
<YouTube />
</Tooltip>
),
}
```
````
- the keys in the labels need to be the same as the `supportedBlocks`
24 changes: 24 additions & 0 deletions demo/admin/src/common/blocks/MediaBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Tooltip } from "@comet/admin";
import { Image, Video, Vimeo, Youtube } from "@comet/admin-icons";
import { BlockCategory, BlockInterface, createOneOfBlock } from "@comet/blocks-admin";
import { DamImageBlock, DamVideoBlock, VimeoVideoBlock, YouTubeVideoBlock } from "@comet/cms-admin";
import { FormattedMessage } from "react-intl";
Expand All @@ -8,5 +10,27 @@ export const MediaBlock: BlockInterface = createOneOfBlock({
displayName: <FormattedMessage id="mediaBlock.displayName" defaultMessage="Media" />,
allowEmpty: false,
variant: "toggle",
labels: {
image: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.image" defaultMessage="Image" />}>
johnnyomair marked this conversation as resolved.
Show resolved Hide resolved
<Image />
</Tooltip>
),
damVideo: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.video.dam" defaultMessage="Video (DAM)" />}>
<Video />
</Tooltip>
),
youTubeVideo: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.video.youtube" defaultMessage="Video (YouTube)" />}>
<Youtube />
</Tooltip>
),
vimeoVideo: (
<Tooltip trigger="hover" title={<FormattedMessage id="pages.blocks.media.video.vimeo" defaultMessage="Video (Vimeo)" />}>
<Vimeo />
</Tooltip>
),
},
category: BlockCategory.Media,
});
12 changes: 12 additions & 0 deletions packages/admin/admin-icons/icons/vimeo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions packages/admin/admin-icons/icons/youtube.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 0 additions & 16 deletions packages/admin/admin-icons/src/Vimeo.tsx

This file was deleted.

16 changes: 0 additions & 16 deletions packages/admin/admin-icons/src/YouTube.tsx

This file was deleted.

6 changes: 1 addition & 5 deletions packages/admin/admin-icons/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ export { default as MovePage } from "./MovePage";
export { default as RadioChecked } from "./RadioChecked";
export { default as RadioUnchecked } from "./RadioUnchecked";
export { default as ThreeDotSaving } from "./ThreeDotSaving";
export { default as Vimeo } from "./Vimeo";
export { default as YouTube } from "./YouTube";

export type IconName =
| GeneratedIconName
Expand All @@ -25,6 +23,4 @@ export type IconName =
| "MovePage"
| "RadioChecked"
| "RadioUnchecked"
| "ThreeDotSaving"
| "Vimeo"
| "YouTube";
| "ThreeDotSaving";
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ export interface CreateOneOfBlockOptions<T extends boolean> {
name: string;
displayName?: ReactNode;
supportedBlocks: Record<BlockType, BlockInterface>;
labels?: Record<BlockType, React.ReactNode>;
category?: BlockCategory | CustomBlockCategory;
variant?: "select" | "radio" | "toggle";
allowEmpty?: T;
}

export const createOneOfBlock = <T extends boolean = boolean>({
supportedBlocks,
labels,
name,
displayName = "Switch",
category = BlockCategory.Other,
Expand Down Expand Up @@ -116,7 +118,7 @@ export const createOneOfBlock = <T extends boolean = boolean>({
Object.entries(supportedBlocks).forEach(([blockType, block]) => {
options.push({
value: blockType,
label: block.displayName,
label: labels && labels[blockType] ? labels[blockType] : block.displayName,
});
});

Expand Down
Loading