Skip to content

Commit

Permalink
feat: introduce rendering cfVisibility in SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaoste committed Sep 10, 2024
1 parent 6b2cf49 commit d53a8d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export const ASSEMBLY_BLOCK_NODE_TYPE = 'assemblyBlock';
export const ASSEMBLY_NODE_TYPES = [ASSEMBLY_NODE_TYPE, ASSEMBLY_BLOCK_NODE_TYPE];
export const LATEST_SCHEMA_VERSION = '2023-09-28';
export const CF_STYLE_ATTRIBUTES = [
'cfVisibility',
'cfHorizontalAlignment',
'cfVerticalAlignment',
'cfMargin',
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/styleUtils/ssrStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ export const resolveBackgroundImageBinding = ({
const variableDefinitionKey = variableData.key;
const variableDefinition = componentSettings.variableDefinitions[variableDefinitionKey];

// @ts-expect-error TODO: fix the types as it thinks taht `defaultValue` is of type string
// @ts-expect-error TODO: Types coming from validations erroneously assume that `defaultValue` can be a primitive value (e.g. string or number)
const defaultValueKey = variableDefinition.defaultValue?.key;
const defaultValue = unboundValues[defaultValueKey].value;

Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/utils/styleUtils/styleTransformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import {
BackgroundImageAlignmentOption,
} from '@/types';

export const transformVisibility = (value?: boolean): CSSProperties => {
if (value === false) {
return {
display: 'none',
};
}
// Don't explicitly set anything when visible to not overwrite values like `grid` or `flex`.
return {};
};

// Keep this for backwards compatilibity - deleting this would be a breaking change
// because existing components on a users experience will have the width value as fill
// rather than 100%
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/styleUtils/stylesUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
transformBorderStyle,
transformFill,
transformGridColumn,
transformVisibility,
} from './styleTransformers';
import { isContentfulStructureComponent } from '../components';
import { EMPTY_CONTAINER_HEIGHT } from '../../constants';
Expand Down Expand Up @@ -64,8 +65,10 @@ export const buildCfStyles = ({
cfTextItalic,
cfTextUnderline,
cfColumnSpan,
cfVisibility,
}: Partial<StyleProps>): CSSProperties => {
return {
...transformVisibility(cfVisibility),
margin: cfMargin,
padding: cfPadding,
backgroundColor: cfBackgroundColor,
Expand Down
2 changes: 1 addition & 1 deletion packages/visual-editor/src/hooks/useComponentProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const useComponentProps = ({
renderDropzone,
]);

const cfStyles = buildCfStyles(props as StyleProps);
const cfStyles = useMemo(() => buildCfStyles(props as StyleProps), [props]);

const sizeStyles: CSSProperties = {
width: cfStyles.width,
Expand Down

0 comments on commit d53a8d2

Please sign in to comment.