File size: 4,283 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import { colorToString } from '@react-page/editor';
import type { FC, PropsWithChildren } from 'react';
import React from 'react';
import { ModeEnum } from '../types/ModeEnum';
import type { BackgroundRendererProps } from '../types/renderer';
const getStyles = (props: BackgroundRendererProps) => {
const {
data: {
background = '',
modeFlag = props.defaultModeFlag,
isParallax = true,
backgroundColor = props.defaultBackgroundColor,
gradients = [],
} = {},
} = props;
let styles: React.CSSProperties = {};
if (modeFlag && modeFlag & ModeEnum.GRADIENT_MODE_FLAG) {
const usedGradients = gradients.filter((g) => g.colors && g.colors.length);
const usedGradientsString = usedGradients
.map((g, i) => {
const firstColor = g.colors?.[0].color;
const firstColorStr = colorToString(firstColor);
const deg =
i === props.gradientDegPreviewIndex &&
props.gradientDegPreview !== undefined
? props.gradientDegPreview
: g.deg;
const opacity =
i === props.gradientOpacityPreviewIndex &&
props.gradientOpacityPreview !== undefined
? props.gradientOpacityPreview
: g.opacity;
return (
'linear-gradient(' +
deg +
'deg, ' +
(g?.colors?.length !== 1
? g?.colors
?.map((c, cpIndex) => {
const color =
i === props.gradientColorPreviewIndex &&
cpIndex === props.gradientColorPreviewColorIndex &&
props.gradientColorPreview !== undefined
? props.gradientColorPreview
: c.color;
if (!color) {
return 'transparent';
}
const colorWithOpacity = {
...color,
a: color.a !== undefined ? color.a * opacity : opacity,
};
return colorToString(colorWithOpacity);
})
.join(', ')
: firstColorStr + ', ' + firstColorStr) +
')'
);
})
.join(', ');
if (usedGradientsString !== '') {
styles = { ...styles, background: usedGradientsString };
}
}
if (modeFlag && modeFlag & ModeEnum.COLOR_MODE_FLAG) {
const colorStr = colorToString(
props.backgroundColorPreview
? props.backgroundColorPreview
: backgroundColor
);
const modeStr = `linear-gradient(${colorStr}, ${colorStr})`;
styles = {
...styles,
background: styles.background
? styles.background + ', ' + modeStr
: modeStr,
};
}
if (modeFlag && modeFlag & ModeEnum.IMAGE_MODE_FLAG) {
const backgroundFinal = props.imagePreview
? props.imagePreview.dataUrl
: background;
const modeStr =
`url('${backgroundFinal}') center / cover no-repeat` +
(isParallax ? ' fixed' : '');
styles = {
...styles,
background: styles.background
? styles.background + ', ' + modeStr
: modeStr,
};
}
return styles;
};
const BackgroundHtmlRenderer: FC<PropsWithChildren<BackgroundRendererProps>> = (
props
) => {
const {
children,
data: {
darken = props.defaultDarken,
lighten = props.defaultLighten,
hasPadding = props.defaultHasPadding,
} = {},
} = props;
const darkenFinal =
props.darkenPreview !== undefined ? props.darkenPreview : darken;
const lightenFinal =
props.lightenPreview !== undefined ? props.lightenPreview : lighten;
const containerStyles = getStyles(props);
return (
<div
className="react-page-plugins-layout-background"
style={{ ...containerStyles, ...(hasPadding ? {} : { padding: 0 }) }}
>
<div
className="react-page-plugins-layout-background__backstretch"
style={{
// tslint:disable-next-line:max-line-length
backgroundImage: `linear-gradient(rgba(0, 0, 0, ${darkenFinal}), rgba(0, 0, 0, ${darkenFinal})),linear-gradient(rgba(255, 255, 255, ${lightenFinal}), rgba(255, 255, 255, ${lightenFinal}))`,
}}
/>
{children}
</div>
);
};
export default BackgroundHtmlRenderer;
|