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

Block editor: remove __unstableElementContext and filter EditorStyles instead #52888

Merged
merged 2 commits into from
Sep 14, 2023
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
133 changes: 0 additions & 133 deletions packages/block-editor/src/components/duotone/components.js

This file was deleted.

7 changes: 0 additions & 7 deletions packages/block-editor/src/components/duotone/index.js

This file was deleted.

65 changes: 65 additions & 0 deletions packages/block-editor/src/components/duotone/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,68 @@ export function getValuesFromColors( colors = [] ) {

return values;
}

/**
* Stylesheet for disabling a global styles duotone filter.
*
* @param {string} selector Selector to disable the filter for.
*
* @return {string} Filter none style.
*/
export function getDuotoneUnsetStylesheet( selector ) {
return `${ selector }{filter:none}`;
}

/**
* SVG and stylesheet needed for rendering the duotone filter.
*
* @param {string} selector Selector to apply the filter to.
* @param {string} id Unique id for this duotone filter.
*
* @return {string} Duotone filter style.
*/
export function getDuotoneStylesheet( selector, id ) {
return `${ selector }{filter:url(#${ id })}`;
}

/**
* The SVG part of the duotone filter.
*
* @param {string} id Unique id for this duotone filter.
* @param {string[]} colors Color strings from dark to light.
*
* @return {string} Duotone SVG.
*/
export function getDuotoneFilter( id, colors ) {
const values = getValuesFromColors( colors );
return `
<svg
xmlns:xlink="http://www.w3.org/1999/xlink"
viewBox="0 0 0 0"
width="0"
height="0"
focusable="false"
role="none"
aria-hidden="true"
style="visibility: hidden; position: absolute; left: -9999px; overflow: hidden;"
>
<defs>
<filter id="${ id }">
<!--
Use sRGB instead of linearRGB so transparency looks correct.
Use perceptual brightness to convert to grayscale.
-->
<feColorMatrix color-interpolation-filters="sRGB" type="matrix" values=" .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 .299 .587 .114 0 0 "></feColorMatrix>
<!-- Use sRGB instead of linearRGB to be consistent with how CSS gradients work. -->
<feComponentTransfer color-interpolation-filters="sRGB">
<feFuncR type="table" tableValues="${ values.r.join( ' ' ) }"></feFuncR>
<feFuncG type="table" tableValues="${ values.g.join( ' ' ) }"></feFuncG>
<feFuncB type="table" tableValues="${ values.b.join( ' ' ) }"></feFuncB>
<feFuncA type="table" tableValues="${ values.a.join( ' ' ) }"></feFuncA>
</feComponentTransfer>
<!-- Re-mask the image with the original transparency since the feColorMatrix above loses that information. -->
<feComposite in2="SourceGraphic" operator="in"></feComposite>
</filter>
</defs>
</svg>`;
}
42 changes: 27 additions & 15 deletions packages/block-editor/src/components/editor-styles/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ import a11yPlugin from 'colord/plugins/a11y';
*/
import { SVG } from '@wordpress/components';
import { useCallback, useMemo } 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 ] );

Expand Down Expand Up @@ -65,33 +68,42 @@ function useDarkThemeBodyClassName( styles, scope ) {
}

export default function EditorStyles( { styles, scope } ) {
const stylesArray = useMemo(
() => Object.values( styles ?? [] ),
[ styles ]
const overrides = useSelect(
( select ) => unlock( select( blockEditorStore ) ).getStyleOverrides(),
[]
);
const transformedStyles = useMemo(
() =>
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(
stylesArray.filter( ( style ) => style?.css ),
_styles.filter( ( style ) => style?.css ),
scope
),
[ stylesArray, scope ]
);

const transformedSvgs = useMemo(
() =>
stylesArray
_styles
.filter( ( style ) => style.__unstableType === 'svgs' )
.map( ( style ) => style.assets )
.join( '' ),
[ stylesArray ]
);
];
}, [ styles, overrides, scope ] );

return (
<>
{ /* Use an empty style element to have a document reference,
but this could be any element. */ }
<style ref={ useDarkThemeBodyClassName( stylesArray, scope ) } />
<style
ref={ useDarkThemeBodyClassName( transformedStyles, scope ) }
/>
{ transformedStyles.map( ( css, index ) => (
<style key={ index }>{ css }</style>
) ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
store as blocksStore,
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { renderToString, useContext, useMemo } from '@wordpress/element';
import { useContext, useMemo } from '@wordpress/element';
import { getCSSRules } from '@wordpress/style-engine';

/**
Expand All @@ -23,7 +23,7 @@ import {
} from './typography-utils';
import { GlobalStylesContext } from './context';
import { useGlobalSetting } from './hooks';
import { PresetDuotoneFilter } from '../duotone/components';
import { getDuotoneFilter } from '../duotone/utils';
import { getGapCSSValue } from '../../hooks/gap';
import { store as blockEditorStore } from '../../store';
import { LAYOUT_DEFINITIONS } from '../../layouts/definitions';
Expand Down Expand Up @@ -164,11 +164,9 @@ function getPresetsSvgFilters( blockPresets = {} ) {
.filter( ( origin ) => presetByOrigin[ origin ] )
.flatMap( ( origin ) =>
presetByOrigin[ origin ].map( ( preset ) =>
renderToString(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice how we also get rid of this renderToString call.

<PresetDuotoneFilter
preset={ preset }
key={ preset.slug }
/>
getDuotoneFilter(
`wp-duotone-${ preset.slug }`,
preset.colors
)
)
)
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
export * from './colors';
export * from './gradients';
export * from './font-sizes';
export * from './duotone';
export { AlignmentControl, AlignmentToolbar } from './alignment-control';
export { default as Autocomplete } from './autocomplete';
export {
Expand Down
Loading
Loading