Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Add background gradient support #38

Merged
merged 2 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions themes/wds_headless/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,6 @@ function wds_theme_setup() {
]
);

// Disable background color gradient presets.
add_theme_support( 'editor-gradient-presets', [] );

// Reset available font size presets to only "normal" (16px).
add_theme_support(
'editor-font-sizes',
Expand Down
28 changes: 26 additions & 2 deletions themes/wds_headless/js/blocks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { validateThemeColors } = wp.blockEditor;
const { validateThemeColors, validateThemeGradients } = wp.blockEditor;
const { useEffect } = wp.element;
const { addFilter, applyFilters } = wp.hooks;

Expand Down Expand Up @@ -41,6 +41,14 @@ function wdsAddColorPaletteHexValues(settings) {
};
}

// Add gradient background hex attribute.
if (settings.attributes.hasOwnProperty("gradient")) {
settings.attributes.gradientHex = {
type: "string",
default: "",
};
}

// Add main color hex attribute.
if (settings.attributes.hasOwnProperty("mainColor")) {
settings.attributes.mainColorHex = {
Expand All @@ -61,14 +69,16 @@ function wdsAddColorPaletteHexValues(settings) {
...settings,
edit(props) {
const {
attributes: { backgroundColor, mainColor, textColor },
attributes: { backgroundColor, gradient, mainColor, textColor },
} = props;

useEffect(() => {
// Note: This may not work as expected if a custom theme palette has been set.
// In that case, this filter may need to be customized.
const defaultColors = validateThemeColors();

const defaultGradients = validateThemeGradients();

// Check for presence of background color attr.
if (backgroundColor) {
// Get color object by slug.
Expand All @@ -83,6 +93,20 @@ function wdsAddColorPaletteHexValues(settings) {
delete props.attributes.backgroundColorHex;
}

// Check for presence of gradient color attr.
if (gradient) {
// Get color object by slug.
const gradientObj = defaultGradients.filter(
(color) => color.slug === gradient
);

// Retrieve color hex value.
props.attributes.gradientHex =
gradientObj?.[0]?.gradient || null;
} else {
delete props.attributes.gradientHex;
}

// Check for presence of main color attr.
if (mainColor) {
// Get color object by slug.
Expand Down