Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BBT-95] Adds Support for Gradients #101

Merged
merged 6 commits into from
Aug 30, 2024
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
6 changes: 4 additions & 2 deletions src/editor/components/StylesColor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { varToHex, hexToVar } from '../../utils/block-helpers';
import getThemeOption from '../../utils/get-theme-option';
import EditorContext from '../context/EditorContext';
import StylesContext from '../context/StylesContext';
import Gradient from './StylesGradient';

/**
* Reusable color control style component
Expand All @@ -33,7 +34,7 @@ const Color = ( { selector } ) => {
setUserConfig( config );
};

const allPalettes = [ 'background', 'text' ].map( ( key ) => (
const colorPalettes = [ 'background', 'text' ].map( ( key ) => (
<div key={ key } className="themer--styles__item__column">
<span className="themer--styles__item__label">{ key }</span>
<ColorPalette
Expand All @@ -51,7 +52,8 @@ const Color = ( { selector } ) => {
{ __( 'Color', 'themer' ) }
</span>
<div className="themer--styles__item__columns themer--styles__item__columns--2">
{ allPalettes }
{ colorPalettes }
<Gradient selector={ `${ selector }.gradient` } />
</div>
</>
);
Expand Down
84 changes: 84 additions & 0 deletions src/editor/components/StylesGradient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { set } from 'lodash';
import { __ } from '@wordpress/i18n';
import { useContext, useMemo } from '@wordpress/element';
import { GradientPicker } from '@wordpress/components';

import getThemeOption from '../../utils/get-theme-option';
import EditorContext from '../context/EditorContext';
import StylesContext from '../context/StylesContext';
/**
* Reusable GradientPicker component
*
* @param {Object} props Component props
* @param {string} props.selector Property target selector
*/
const Gradient = ( { selector } ) => {
const { userConfig, themeConfig } = useContext( EditorContext );
const { setUserConfig } = useContext( StylesContext );
const value = getThemeOption( selector, themeConfig );

const defaultPalette = getThemeOption(
'settings.color.gradients.default',
themeConfig
);
const themePalette = getThemeOption(
'settings.color.gradients.theme',
themeConfig
);
const defaultGradientOption = getThemeOption(
'settings.color.defaultGradients',
themeConfig
);
const customGradientOption = getThemeOption(
'settings.color.customGradient',
themeConfig
);

const onChange = ( newValue ) => {
let config = structuredClone( userConfig );
config = set( config, selector, newValue );
setUserConfig( config );
};

/*
* We only want to populate themePalette if themePalette is not empty to avoid unnecessary headings being created.
* and we only want to populate gradients object with defaultPalette if defaultGradientOption is true (which it is by default)
*/
const gradients = useMemo( () => {
const arr = [];

if ( themePalette ) {
arr.push( {
name: 'Theme',
gradients: themePalette,
} );
}

if ( defaultGradientOption && defaultPalette ) {
arr.push( {
name: 'Default',
gradients: defaultPalette,
} );
}

return arr;
}, [ themePalette, defaultGradientOption, defaultPalette ] );

if ( customGradientOption !== false || gradients ) {
return (
<div key="Gradient" className="themer--styles__item__column">
<span className="themer--styles__item__label">
{ __( 'Gradient', 'themer' ) }
</span>
<GradientPicker
value={ value }
onChange={ onChange }
gradients={ gradients }
disableCustomGradients={ ! customGradientOption }
/>
</div>
);
}
};

export default Gradient;