Skip to content

Commit 1567bad

Browse files
committed
Backward compatibility support for textAlign attribute
1 parent c29c79c commit 1567bad

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

packages/block-library/src/button/edit.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { getUpdatedLinkAttributes } from './get-updated-link-attributes';
1111
import removeAnchorTag from '../utils/remove-anchor-tag';
1212
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
1313
import { unlock } from '../lock-unlock';
14+
import useDeprecatedTextAlign from '../utils/deprecated-text-align-attributes';
1415

1516
/**
1617
* WordPress dependencies
@@ -194,6 +195,7 @@ function ButtonEdit( props ) {
194195
width,
195196
metadata,
196197
} = attributes;
198+
useDeprecatedTextAlign( props );
197199

198200
const TagName = tagName || 'a';
199201

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { useEvent } from '@wordpress/compose';
5+
import { useEffect, useRef } from '@wordpress/element';
6+
import deprecated from '@wordpress/deprecated';
7+
import { useDispatch } from '@wordpress/data';
8+
import { store as blockEditorStore } from '@wordpress/block-editor';
9+
10+
/**
11+
* If a plugin is still using the old textAlign attribute, we need to migrate its value
12+
* to the new style.typography.textAlign attribute.
13+
*
14+
* @param {Object} props Block props.
15+
*/
16+
export default function useDeprecatedTextAlign( props ) {
17+
const { name, attributes, setAttributes } = props;
18+
const { textAlign, style } = attributes;
19+
const { __unstableMarkNextChangeAsNotPersistent } =
20+
useDispatch( blockEditorStore );
21+
const updateStyleWithAlign = useEvent( () => {
22+
deprecated( `textAlign attribute in ${ name }`, {
23+
alternative: 'style.typography.textAlign',
24+
since: '7.0',
25+
} );
26+
__unstableMarkNextChangeAsNotPersistent();
27+
setAttributes( {
28+
style: {
29+
...style,
30+
typography: {
31+
...style?.typography,
32+
textAlign,
33+
},
34+
},
35+
} );
36+
} );
37+
const lastUpdatedAlignRef = useRef();
38+
useEffect( () => {
39+
if ( textAlign === lastUpdatedAlignRef.current ) {
40+
return;
41+
}
42+
lastUpdatedAlignRef.current = textAlign;
43+
updateStyleWithAlign();
44+
}, [ textAlign, updateStyleWithAlign ] );
45+
}

0 commit comments

Comments
 (0)