-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
129 lines (115 loc) · 3.36 KB
/
index.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* External dependencies
*/
import { colord, extend } from 'colord';
import namesPlugin from 'colord/plugins/names';
import a11yPlugin from 'colord/plugins/a11y';
/**
* WordPress dependencies
*/
import { SVG } from '@wordpress/components';
import { useCallback, useMemo, memo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import transformStyles from '../../utils/transform-styles';
import { store as blockEditorStore } from '../../store';
import { unlock } from '../../lock-unlock';
extend( [ namesPlugin, a11yPlugin ] );
function useDarkThemeBodyClassName( styles, scope ) {
return useCallback(
( node ) => {
if ( ! node ) {
return;
}
const { ownerDocument } = node;
const { defaultView, body } = ownerDocument;
const canvas = scope ? ownerDocument.querySelector( scope ) : body;
let backgroundColor;
if ( ! canvas ) {
// The real .editor-styles-wrapper element might not exist in the
// DOM, so calculate the background color by creating a fake
// wrapper.
const tempCanvas = ownerDocument.createElement( 'div' );
tempCanvas.classList.add( 'editor-styles-wrapper' );
body.appendChild( tempCanvas );
backgroundColor = defaultView
?.getComputedStyle( tempCanvas, null )
.getPropertyValue( 'background-color' );
body.removeChild( tempCanvas );
} else {
backgroundColor = defaultView
?.getComputedStyle( canvas, null )
.getPropertyValue( 'background-color' );
}
const colordBackgroundColor = colord( backgroundColor );
// If background is transparent, it should be treated as light color.
if (
colordBackgroundColor.luminance() > 0.5 ||
colordBackgroundColor.alpha() === 0
) {
body.classList.remove( 'is-dark-theme' );
} else {
body.classList.add( 'is-dark-theme' );
}
},
[ styles, scope ]
);
}
function EditorStyles( { styles, scope, transformOptions } ) {
const overrides = useSelect(
( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(),
[]
);
const [ transformedStyles, transformedSvgs ] = useMemo( () => {
const _styles = Object.values( styles ?? [] );
for ( const [ id, override ] of overrides ) {
const index = _styles.findIndex( ( { id: _id } ) => id === _id );
const overrideWithId = { ...override, id };
if ( index === -1 ) {
_styles.push( overrideWithId );
} else {
_styles[ index ] = overrideWithId;
}
}
return [
transformStyles(
_styles.filter( ( style ) => style?.css ),
scope,
transformOptions
),
_styles
.filter( ( style ) => style.__unstableType === 'svgs' )
.map( ( style ) => style.assets )
.join( '' ),
];
}, [ styles, overrides, scope, transformOptions ] );
return (
<>
{ /* Use an empty style element to have a document reference,
but this could be any element. */ }
<style
ref={ useDarkThemeBodyClassName( transformedStyles, scope ) }
/>
{ transformedStyles.map( ( css, index ) => (
<style key={ index }>{ css }</style>
) ) }
<SVG
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 0 0"
width="0"
height="0"
role="none"
style={ {
visibility: 'hidden',
position: 'absolute',
left: '-9999px',
overflow: 'hidden',
} }
dangerouslySetInnerHTML={ { __html: transformedSvgs } }
/>
</>
);
}
export default memo( EditorStyles );