Use styles from settings controller in preview

[MAILPOET-5638]
This commit is contained in:
Jan Lysý
2024-02-29 16:09:26 +01:00
committed by Jan Lysý
parent db8dac03d2
commit 698c41b77b
3 changed files with 178 additions and 150 deletions

View File

@@ -1,8 +1,11 @@
import { import {
__unstableMotion as motion,
__experimentalHStack as HStack, __experimentalHStack as HStack,
__experimentalVStack as VStack, __experimentalVStack as VStack,
__unstableMotion as motion,
} from '@wordpress/components'; } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { storeName } from '../../store';
const firstFrame = { const firstFrame = {
start: { start: {
@@ -38,58 +41,67 @@ const secondFrame = {
const normalizedHeight = 152; const normalizedHeight = 152;
const normalizedColorSwatchSize = 32; const normalizedColorSwatchSize = 32;
type Props = {
label?: string;
isFocused?: boolean;
withHoverView?: boolean;
};
/** /**
* Component to render the styles preview based on the component from the site editor: * Component to render the styles preview based on the component from the site editor:
* https://github.com/WordPress/gutenberg/blob/trunk/packages/edit-site/src/components/global-styles/preview.js * https://github.com/WordPress/gutenberg/blob/trunk/packages/edit-site/src/components/global-styles/preview.js
*/ */
export function Preview(): JSX.Element { export function Preview({
const style = { label,
backgroundColor: '#f3f3f3', isFocused,
headingFontFamily: 'Arial', withHoverView,
headingColor: '#000000', }: Props): JSX.Element {
headingFontWeight: 'normal', const styles = useSelect((select) => select(storeName).getStyles());
paletteColors: [
{ const backgroundColor = styles.colors.background;
name: 'Sample Background', const headingFontFamily = styles.elements.h1.fontFamily;
slug: 'Sample-background', const headingColor = styles.elements.h1.color;
color: '#f9f8f3', const headingFontWeight = styles.elements.h1.fontWeight;
}, const paletteColors = [
],
highlightedColors: [
{ {
name: 'Sample primary', name: 'Sample primary',
slug: 'sample-primary', slug: 'sample-primary',
color: '#e5e2d3', color: '#a5a2a3',
}, },
{ {
name: 'Sample Secondary', name: 'Sample Secondary',
slug: 'sample-secondary', slug: 'sample-secondary',
color: '#111111', color: '#e5e2d3',
}, },
], {
}; name: 'Sample Background',
slug: 'Sample-background',
color: '#a8c7a9',
},
];
// https://github.com/WordPress/gutenberg/blob/7fa03fafeb421ab4c3604564211ce6007cc38e84/packages/edit-site/src/components/global-styles/hooks.js#L68-L73
const highlightedColors = paletteColors
.filter(({ color }) => color !== backgroundColor && color !== headingColor)
.slice(0, 2);
const isFocused = false;
const isHovered = false;
const disableMotion = false;
const label = 'Some Label';
const ratio = 1; const ratio = 1;
// When is set label, the preview animates the hover state and displays the label
const gradientValue = undefined; const [isHovered, setIsHovered] = useState(false);
const withHoverView = true;
return ( return (
<div
onMouseEnter={() => setIsHovered(true)}
onMouseLeave={() => setIsHovered(false)}
>
<motion.div <motion.div
style={{ style={{
height: normalizedHeight * ratio, height: normalizedHeight * ratio,
width: '100%', width: '100%',
background: gradientValue ?? style.backgroundColor, background: backgroundColor,
cursor: withHoverView ? 'pointer' : undefined, cursor: withHoverView ? 'pointer' : undefined,
}} }}
initial="start" initial="start"
animate={ animate={(isHovered || isFocused) && label ? 'hover' : 'start'}
(isHovered || isFocused) && !disableMotion && label ? 'hover' : 'start'
}
> >
<motion.div <motion.div
variants={firstFrame} variants={firstFrame}
@@ -108,10 +120,10 @@ export function Preview(): JSX.Element {
> >
<motion.div <motion.div
style={{ style={{
fontFamily: style.headingFontFamily, fontFamily: headingFontFamily,
fontSize: 65 * ratio, fontSize: 65 * ratio,
color: style.headingColor, color: headingColor,
fontWeight: style.headingFontWeight, fontWeight: headingFontWeight,
}} }}
animate={{ scale: 1, opacity: 1 }} animate={{ scale: 1, opacity: 1 }}
initial={{ scale: 0.1, opacity: 0 }} initial={{ scale: 0.1, opacity: 0 }}
@@ -120,7 +132,7 @@ export function Preview(): JSX.Element {
Aa Aa
</motion.div> </motion.div>
<VStack spacing={4 * ratio}> <VStack spacing={4 * ratio}>
{style.highlightedColors.map(({ slug, color }, index) => ( {highlightedColors.map(({ slug, color }, index) => (
<motion.div <motion.div
key={slug} key={slug}
style={{ style={{
@@ -165,7 +177,7 @@ export function Preview(): JSX.Element {
overflow: 'hidden', overflow: 'hidden',
}} }}
> >
{style.paletteColors.slice(0, 4).map(({ color }) => ( {paletteColors.slice(0, 4).map(({ color }) => (
<div <div
key={color} key={color}
style={{ style={{
@@ -201,9 +213,9 @@ export function Preview(): JSX.Element {
<div <div
style={{ style={{
fontSize: 40 * ratio, fontSize: 40 * ratio,
fontFamily: style.headingFontFamily, fontFamily: headingFontFamily,
color: style.headingColor, color: headingColor,
fontWeight: style.headingFontWeight, fontWeight: headingFontWeight,
lineHeight: '1em', lineHeight: '1em',
textAlign: 'center', textAlign: 'center',
}} }}
@@ -214,5 +226,6 @@ export function Preview(): JSX.Element {
</VStack> </VStack>
</motion.div> </motion.div>
</motion.div> </motion.div>
</div>
); );
} }

View File

@@ -39,6 +39,13 @@ export type EmailStyles = {
colors: { colors: {
background: string; background: string;
}; };
elements: {
h1: {
color: string;
fontFamily: string;
fontWeight: string;
};
};
}; };
export type EmailEditorLayout = { export type EmailEditorLayout = {

View File

@@ -113,10 +113,18 @@ class SettingsController {
], ],
], ],
'colors' => [ 'colors' => [
'background' => '#000000', 'background' => '#ffffff',
], ],
'typography' => [ 'typography' => [
], ],
// Value are only for purpose of displaying in the preview component in style sidebar
'elements' => [
'h1' => [
'color' => '#000000',
'fontWeight' => 'bold',
'fontFamily' => "Arial, 'Helvetica Neue', Helvetica, sans-serif",
],
],
]; ];
} }