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

Try generating random color palettes #40988

Merged
merged 4 commits into from
Nov 14, 2022
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
18 changes: 6 additions & 12 deletions lib/experimental/editor-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,19 @@ function gutenberg_initialize_editor( $editor_name, $editor_script_handle, $sett
}

/**
* Sets a global JS variable used to trigger the availability of zoomed out view.
* Sets a global JS variable used to trigger the availability of each Gutenberg Experiment.
*/
function gutenberg_enable_zoomed_out_view() {
function gutenberg_enable_experiments() {
$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-zoomed-out-view', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableZoomedOutView = true', 'before' );
}
}

add_action( 'admin_init', 'gutenberg_enable_zoomed_out_view' );

/**
* Sets a global JS variable used to trigger the availability of the Navigation List View experiment.
*/
function gutenberg_enable_off_canvas_navigation_editor() {
$gutenberg_experiments = get_option( 'gutenberg-experiments' );
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-color-randomizer', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableColorRandomizer = true', 'before' );
}
if ( $gutenberg_experiments && array_key_exists( 'gutenberg-off-canvas-navigation-editor', $gutenberg_experiments ) ) {
wp_add_inline_script( 'wp-block-editor', 'window.__experimentalEnableOffCanvasNavigationEditor = true', 'before' );
}
}

add_action( 'admin_init', 'gutenberg_enable_off_canvas_navigation_editor' );
add_action( 'admin_init', 'gutenberg_enable_experiments' );
12 changes: 12 additions & 0 deletions lib/experiments-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ function gutenberg_initialize_experiments_settings() {
'id' => 'gutenberg-off-canvas-navigation-editor',
)
);
add_settings_field(
'gutenberg-color-randomizer',
__( 'Color randomizer ', 'gutenberg' ),
'gutenberg_display_experiment_field',
'gutenberg-experiments',
'gutenberg_experiments_section',
array(
'label' => __( 'Test the Global Styles color randomizer; a utility that lets you mix the current color palette pseudo-randomly.', 'gutenberg' ),
'id' => 'gutenberg-color-randomizer',
)
);

register_setting(
'gutenberg-experiments',
'gutenberg-experiments'
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/edit-site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"@wordpress/url": "file:../url",
"@wordpress/viewport": "file:../viewport",
"classnames": "^2.3.1",
"colord": "^2.9.2",
"downloadjs": "^1.4.7",
"history": "^5.1.0",
"lodash": "^4.17.21",
Expand Down
36 changes: 36 additions & 0 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* External dependencies
*/
import { get, set, isEqual } from 'lodash';
import { colord, extend } from 'colord';
import a11yPlugin from 'colord/plugins/a11y';
vcanales marked this conversation as resolved.
Show resolved Hide resolved

/**
* WordPress dependencies
Expand All @@ -20,6 +22,9 @@ import {
import { getValueFromVariable, getPresetVariableFromValue } from './utils';
import { GlobalStylesContext } from './context';

// Enable colord's a11y plugin.
extend( [ a11yPlugin ] );

const EMPTY_CONFIG = { settings: {}, styles: {} };

export const useGlobalStylesReset = () => {
Expand Down Expand Up @@ -323,3 +328,34 @@ export function useGradientsPerOrigin( name ) {
return result;
}, [ customGradients, themeGradients, defaultGradients ] );
}

export function useColorRandomizer( name ) {
const [ themeColors, setThemeColors ] = useSetting(
'color.palette.theme',
name
);

function randomizeColors() {
/* eslint-disable no-restricted-syntax */
const randomRotationValue = Math.floor( Math.random() * 225 );
/* eslint-enable no-restricted-syntax */

const newColors = themeColors.map( ( colorObject ) => {
const { color } = colorObject;
const newColor = colord( color )
.rotate( randomRotationValue )
.toHex();

return {
...colorObject,
color: newColor,
};
} );

setThemeColors( newColors );
}

return window.__experimentalEnableColorRandomizer
? [ randomizeColors ]
: [];
}
16 changes: 15 additions & 1 deletion packages/edit-site/src/components/global-styles/palette.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ import {
__experimentalZStack as ZStack,
__experimentalVStack as VStack,
ColorIndicator,
Button,
} from '@wordpress/components';
import { __, _n, sprintf } from '@wordpress/i18n';
import { shuffle } from '@wordpress/icons';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import Subtitle from './subtitle';
import { NavigationButtonAsItem } from './navigation-button';
import { useSetting } from './hooks';
import { useColorRandomizer, useSetting } from './hooks';
import ColorIndicatorWrapper from './color-indicator-wrapper';

const EMPTY_COLORS = [];
Expand All @@ -31,6 +33,9 @@ function Palette( { name } ) {
'color.defaultPalette',
name
);

const [ randomizeThemeColors ] = useColorRandomizer();

const colors = useMemo(
() => [
...( customColors || EMPTY_COLORS ),
Expand Down Expand Up @@ -82,6 +87,15 @@ function Palette( { name } ) {
</HStack>
</NavigationButtonAsItem>
</ItemGroup>
{ randomizeThemeColors && (
<Button
variant="secondary"
icon={ shuffle }
onClick={ randomizeThemeColors }
>
{ __( 'Randomize colors' ) }
</Button>
) }
</VStack>
);
}
Expand Down