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

Conditional Duotone Toolbar Visibilty #31519

Closed
Closed
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
5 changes: 5 additions & 0 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ ${ selector } {
function DuotonePanel( { attributes, setAttributes } ) {
const style = attributes?.style;
const duotone = style?.color?.duotone;
const isHidden = style?.ui?.duotoneHidden;

const duotonePalette = useSetting( 'color.duotone' ) || EMPTY_ARRAY;
const colorPalette = useSetting( 'color.palette' ) || EMPTY_ARRAY;
Expand All @@ -139,6 +140,10 @@ function DuotonePanel( { attributes, setAttributes } ) {
return null;
}

if ( isHidden ) {
return null;
}

return (
<BlockControls group="block">
<DuotoneControl
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/cover/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
},
"contentPosition": {
"type": "string"
},
"style": {
"type": "object",
"default": {
"ui": {
"duotoneHidden": true
}
}
}
},
"supports": {
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/cover/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
dimRatioToClass,
isContentPositionCenter,
getPositionClassName,
useDuotoneUI,
} from './shared';

/**
Expand Down Expand Up @@ -328,6 +329,8 @@ function CoverEdit( {
const onSelectMedia = attributesFromMedia( setAttributes );
const isUploadingMedia = isTemporaryMedia( id, url );

useDuotoneUI( attributes, setAttributes );

const [ prevMinHeightValue, setPrevMinHeightValue ] = useState( minHeight );
const [ prevMinHeightUnit, setPrevMinHeightUnit ] = useState(
minHeightUnit
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/cover/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
IMAGE_BACKGROUND_TYPE,
VIDEO_BACKGROUND_TYPE,
COVER_DEFAULT_HEIGHT,
useDuotoneUI,
} from './shared';
import Controls from './controls';

Expand Down Expand Up @@ -106,6 +107,8 @@ const Cover = ( {
false
);

useDuotoneUI( attributes, setAttributes );

useEffect( () => {
let isCurrent = true;

Expand Down
23 changes: 23 additions & 0 deletions packages/block-library/src/cover/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { getBlobTypeByURL, isBlobURL } from '@wordpress/blob';
import { useEffect } from '@wordpress/element';

const POSITION_CLASSNAMES = {
'top left': 'is-position-top-left',
Expand Down Expand Up @@ -104,3 +105,25 @@ export function getPositionClassName( contentPosition ) {

return POSITION_CLASSNAMES[ contentPosition ];
}

/**
* Hook that enables style.ui.duotone attribute based on block attributes.
*
* @param {Object} attributes Block attributes.
* @param {Function} setAttributes Set block attributes.
*/
export function useDuotoneUI( attributes, setAttributes ) {
const duotoneHidden =
! attributes.url || attributes.isRepeated || attributes.isParallax;
useEffect( () => {
setAttributes( {
style: {
...attributes.style,
ui: {
...attributes.style?.ui,
duotoneHidden,
},
},
} );
}, [ duotoneHidden, setAttributes ] );
}
8 changes: 8 additions & 0 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
"source": "attribute",
"selector": "figure > a",
"attribute": "target"
},
"style": {
"type": "object",
"default": {
"ui": {
"duotoneHidden": true
}
}
}
},
"supports": {
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ export function ImageEdit( {
captionRef.current = caption;
}, [ caption ] );

useEffect( () => {
setAttributes( {
style: {
...attributes.style,
ui: {
...attributes.style?.ui,
duotoneHidden: ! url,
},
},
} );
}, [ url, setAttributes ] );

const ref = useRef();
const { imageDefaultSize, mediaUpload } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
Expand Down
12 changes: 12 additions & 0 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,18 @@ export class ImageEdit extends Component {
image.source_url;
this.props.setAttributes( { url } );
}
if ( previousProps.attributes.url !== this.props.attributes.url ) {
const { style, url } = this.props.attributes;
this.props.setAttributes( {
style: {
...style,
ui: {
...style?.ui,
duotoneHidden: ! url,
},
},
} );
}
}

static getDerivedStateFromProps( props, state ) {
Expand Down
7 changes: 6 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__cover.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"hasParallax": false,
"isRepeated": false,
"dimRatio": 40,
"backgroundType": "image"
"backgroundType": "image",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@
"isRepeated": false,
"dimRatio": 50,
"backgroundType": "image",
"contentPosition": "bottom right"
"contentPosition": "bottom right",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"isRepeated": false,
"dimRatio": 30,
"backgroundType": "image",
"customGradient": "linear-gradient(135deg,rgba(0,208,132,1) 0%,rgba(6,147,227,1) 100%)"
"customGradient": "linear-gradient(135deg,rgba(0,208,132,1) 0%,rgba(6,147,227,1) 100%)",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"isRepeated": false,
"dimRatio": 70,
"backgroundType": "video",
"customGradient": "linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)"
"customGradient": "linear-gradient(135deg,rgba(6,147,227,1) 0%,rgb(155,81,224) 100%)",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"isRepeated": false,
"dimRatio": 50,
"backgroundType": "image",
"customGradient": "linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%)"
"customGradient": "linear-gradient(135deg,rgb(255,203,112) 0%,rgb(199,81,192) 50%,rgb(65,88,208) 100%)",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"isRepeated": false,
"dimRatio": 50,
"overlayColor": "primary",
"backgroundType": "image"
"backgroundType": "image",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"isRepeated": false,
"dimRatio": 10,
"customOverlayColor": "#3615d9",
"backgroundType": "video"
"backgroundType": "video",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"hasParallax": false,
"isRepeated": false,
"dimRatio": 40,
"backgroundType": "video"
"backgroundType": "video",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [
{
Expand Down
7 changes: 6 additions & 1 deletion packages/e2e-tests/fixtures/blocks/core__image.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
"attributes": {
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": ""
"caption": "",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /></figure>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"alt": "",
"caption": "",
"href": "http://localhost:8888/?attachment_id=7",
"linkDestination": "attachment"
"linkDestination": "attachment",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image\"><a href=\"http://localhost:8888/?attachment_id=7\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /></a></figure>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@
"align": "center",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": "Give it a try. Press the \"really wide\" button on the image toolbar."
"caption": "Give it a try. Press the \"really wide\" button on the image toolbar.",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /><figcaption>Give it a try. Press the &quot;really wide&quot; button on the image toolbar.</figcaption></figure></div>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"caption": "",
"href": "https://wordpress.org/",
"linkClass": "custom-link",
"linkDestination": "custom"
"linkDestination": "custom",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image\"><a class=\"custom-link\" href=\"https://wordpress.org/\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /></a></figure>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"caption": "",
"href": "https://wordpress.org/",
"rel": "external",
"linkDestination": "custom"
"linkDestination": "custom",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image\"><a href=\"https://wordpress.org/\" rel=\"external\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /></a></figure>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"alt": "",
"caption": "",
"href": "https://wordpress.org/",
"linkDestination": "custom"
"linkDestination": "custom",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image\"><a href=\"https://wordpress.org/\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /></a></figure>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@
"alt": "",
"caption": "",
"href": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"linkDestination": "media"
"linkDestination": "media",
"style": {
"ui": {
"duotoneHidden": true
}
}
},
"innerBlocks": [],
"originalContent": "<figure class=\"wp-block-image\"><a href=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\"><img src=\"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==\" alt=\"\" /></a></figure>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ exports[`Image allows zooming using the crop tools 1`] = `"data:image/jpeg;base6
exports[`Image allows zooming using the crop tools 2`] = `"data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4gIoSUNDX1BST0ZJTEUAAQEAAAIYAAAAAAIQAABtbnRyUkdCIFhZWiAAAAAAAAAAAAAAAABhY3NwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAA9tYAAQAAAADTLQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAlkZXNjAAAA8AAAAHRyWFlaAAABZAAAABRnWFlaAAABeAAAABRiWFlaAAABjAAAABRyVFJDAAABoAAAAChnVFJDAAABoAAAAChiVFJDAAABoAAAACh3dHB0AAAByAAAABRjcHJ0AAAB3AAAADxtbHVjAAAAAAAAAAEAAAAMZW5VUwAAAFgAAAAcAHMAUgBHAEIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFhZWiAAAAAAAABvogAAOPUAAAOQWFlaIAAAAAAAAGKZAAC3hQAAGNpYWVogAAAAAAAAJKAAAA+EAAC2z3BhcmEAAAAAAAQAAAACZmYAAPKnAAANWQAAE9AAAApbAAAAAAAAAABYWVogAAAAAAAA9tYAAQAAAADTLW1sdWMAAAAAAAAAAQAAAAxlblVTAAAAIAAAABwARwBvAG8AZwBsAGUAIABJAG4AYwAuACAAMgAwADEANv/bAEMAAwICAgICAwICAgMDAwMEBgQEBAQECAYGBQYJCAoKCQgJCQoMDwwKCw4LCQkNEQ0ODxAQERAKDBITEhATDxAQEP/bAEMBAwMDBAMECAQECBALCQsQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEP/AABEIAAUABQMBIgACEQEDEQH/xAAVAAEBAAAAAAAAAAAAAAAAAAAACP/EABoQAQABBQAAAAAAAAAAAAAAAAAEBwk4hbX/xAAUAQEAAAAAAAAAAAAAAAAAAAAA/8QAFBEBAAAAAAAAAAAAAAAAAAAAAP/aAAwDAQACEQMRAD8Aqm1xgnTLddiaAD//2Q=="`;

exports[`Image should drag and drop files into media placeholder 1`] = `
"<!-- wp:image -->
"<!-- wp:image {\\"style\\":{\\"ui\\":{\\"duotoneHidden\\":true}}} -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->"
`;

exports[`Image should undo without broken temporary state 1`] = `
"<!-- wp:image -->
"<!-- wp:image {"style\\":{\\"ui\\":{\\"duotoneHidden\\":true}}} -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
<!-- /wp:image -->"
`;
Loading