-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathhooks.js
94 lines (79 loc) · 2.34 KB
/
hooks.js
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
/**
* External dependencies
*/
import { colord, extend } from 'colord';
import a11yPlugin from 'colord/plugins/a11y';
/**
* WordPress dependencies
*/
import { store as blocksStore } from '@wordpress/blocks';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { useSelect } from '@wordpress/data';
const { useGlobalSetting, useGlobalStyle } = unlock( blockEditorPrivateApis );
// Enable colord's a11y plugin.
extend( [ a11yPlugin ] );
export function useColorRandomizer( name ) {
const [ themeColors, setThemeColors ] = useGlobalSetting(
'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 ]
: [];
}
export function useStylesPreviewColors() {
const [ textColor = 'black' ] = useGlobalStyle( 'color.text' );
const [ backgroundColor = 'white' ] = useGlobalStyle( 'color.background' );
const [ headingColor = textColor ] = useGlobalStyle(
'elements.h1.color.text'
);
const [ coreColors ] = useGlobalSetting( 'color.palette.core' );
const [ themeColors ] = useGlobalSetting( 'color.palette.theme' );
const [ customColors ] = useGlobalSetting( 'color.palette.custom' );
const paletteColors = ( themeColors ?? [] )
.concat( customColors ?? [] )
.concat( coreColors ?? [] );
const highlightedColors = paletteColors
.filter(
// we exclude these two colors because they are already visible in the preview.
( { color } ) => color !== backgroundColor && color !== headingColor
)
.slice( 0, 2 );
return {
paletteColors,
highlightedColors,
};
}
export function useSupportedStyles( name, element ) {
const { supportedPanels } = useSelect(
( select ) => {
return {
supportedPanels: unlock(
select( blocksStore )
).getSupportedStyles( name, element ),
};
},
[ name, element ]
);
return supportedPanels;
}