Skip to content

Commit

Permalink
feat: Visibility for CMS Sections and Blocks (SWF-280)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored and patzick committed Mar 7, 2023
1 parent b036031 commit 5008dcb
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-ways-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/types": patch
---

Adds visibility object to section and block type
5 changes: 5 additions & 0 deletions .changeset/shaggy-actors-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopware-pwa/helpers-next": patch
---

Adds Visibility by device feature for section and block (CMS)
53 changes: 51 additions & 2 deletions packages/helpers/src/cms/getCmsLayoutConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { CmsBlock, CmsSection, CmsSlot } from "@shopware-pwa/types";
import {
CmsBlock,
CmsSection,
CmsSlot,
CmsVisibility,
} from "@shopware-pwa/types";

/**
* @beta
Expand Down Expand Up @@ -35,6 +40,45 @@ function isCmsSection(
): content is CmsSection {
return content.apiAlias === "cms_section";
}

const deviceMap: { [key in CmsVisibility]: "md" | "xl" | "lg" } = {
mobile: "md",
tablet: "lg",
desktop: "xl",
};


/**
* Get css object for visibility classes
*
* mobile -> "md"
* tablet -> "lg"
* desktop -> "xl"
*
* i.e. if tablet device is set to hidden, the output class will be "lg:hidden"
*/
function getVisibilityClasses(content: CmsBlock | CmsSection | CmsSlot) {
if (
isCmsSlot(content) ||
!content?.visibility ||
Object.keys(content?.visibility).length === 0
)
return {};

let visibilityCssClasses: {
"md:hidden"?: boolean;
"xl:hidden"?: boolean;
"lg:hidden"?: boolean;
} = {};

Object.entries(content?.visibility)?.forEach(([device, isVisible]) => {
if (!isVisible) {
visibilityCssClasses[`${deviceMap[device]}:hidden`] = true;
}
});
return visibilityCssClasses;
}

/**
* @category CMS (Shopping Experiences)
*/
Expand All @@ -48,8 +92,13 @@ export function getCmsLayoutConfiguration(
} as LayoutConfiguration;
}

const visibilityCssClasses = getVisibilityClasses(content);
const cssClasses = Object.keys(visibilityCssClasses).length
? Object.assign({}, content.cssClass, visibilityCssClasses)
: content.cssClass;

return {
cssClasses: content.cssClass,
cssClasses,
layoutStyles: {
backgroundColor: content.backgroundColor,
backgroundImage: content.backgroundMedia
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CmsSlot,
MobileBehavior,
SizingMode,
CmsVisibility,
} from "./CmsPage";
/**
* @public
Expand Down Expand Up @@ -38,4 +39,7 @@ export type CmsBlock = {
backgroundMedia: Media | null;
backgroundMediaMode: BackgroundMediaMode;
cssClass: string | null;
visibility: {
[key in CmsVisibility]: boolean;
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ export type MobileBehavior = "boxed" | "wrap" | "hidden";
*/
export type BackgroundMediaMode = "cover";

export type CmsVisibility = "mobile" | "tablet" | "desktop" | string;

/**
* @public
*/
Expand All @@ -240,5 +242,8 @@ export type CmsSection = {
extensions: [any];
id: string;
blocks: CmsBlock[];
visibility: {
[key in CmsVisibility]: boolean;
};
apiAlias: "cms_section";
};

0 comments on commit 5008dcb

Please sign in to comment.