-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Global Styles: Add block-level Text Alignment UI #61717
Changes from 1 commit
0c1eb05
9c4be9e
d5d8bb3
cd19a01
f6f9f50
b1e0fa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ import FontFamilyControl from '../font-family'; | |
import FontAppearanceControl from '../font-appearance-control'; | ||
import LineHeightControl from '../line-height-control'; | ||
import LetterSpacingControl from '../letter-spacing-control'; | ||
import TextAlignmentControl from '../text-alignment-control'; | ||
import TextTransformControl from '../text-transform-control'; | ||
import TextDecorationControl from '../text-decoration-control'; | ||
import WritingModeControl from '../writing-mode-control'; | ||
|
@@ -32,6 +33,7 @@ export function useHasTypographyPanel( settings ) { | |
const hasLineHeight = useHasLineHeightControl( settings ); | ||
const hasFontAppearance = useHasAppearanceControl( settings ); | ||
const hasLetterSpacing = useHasLetterSpacingControl( settings ); | ||
const hasTextAlign = useHasTextAlignmentControl( settings ); | ||
const hasTextTransform = useHasTextTransformControl( settings ); | ||
const hasTextDecoration = useHasTextDecorationControl( settings ); | ||
const hasWritingMode = useHasWritingModeControl( settings ); | ||
|
@@ -43,6 +45,7 @@ export function useHasTypographyPanel( settings ) { | |
hasLineHeight || | ||
hasFontAppearance || | ||
hasLetterSpacing || | ||
hasTextAlign || | ||
hasTextTransform || | ||
hasFontSize || | ||
hasTextDecoration || | ||
|
@@ -91,6 +94,10 @@ function useHasTextTransformControl( settings ) { | |
return settings?.typography?.textTransform; | ||
} | ||
|
||
function useHasTextAlignmentControl( settings ) { | ||
return settings?.typography?.textAlign; | ||
} | ||
|
||
function useHasTextDecorationControl( settings ) { | ||
return settings?.typography?.textDecoration; | ||
} | ||
|
@@ -150,6 +157,7 @@ const DEFAULT_CONTROLS = { | |
fontAppearance: true, | ||
lineHeight: true, | ||
letterSpacing: true, | ||
textAlign: true, | ||
textTransform: true, | ||
textDecoration: true, | ||
writingMode: true, | ||
|
@@ -165,6 +173,13 @@ export default function TypographyPanel( { | |
panelId, | ||
defaultControls = DEFAULT_CONTROLS, | ||
} ) { | ||
// If `panelId` has a value, it means this panel will be rendered inside | ||
// the block sidebar. The text alignment UI for individual blocks is rendered | ||
// in the block toolbar, so disable support if it's not in the global style. | ||
if ( panelId ) { | ||
settings.typography.textAlign = false; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than relying on fudging the Perhaps the text alignment control could be rendered only where it is needed that way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried a different approach with 9c4be9e. In I can't think of any other good approach at the moment, do you have any ideas? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Only the suggestion in my last comment. Did you explore adding the global styles only control as a child to the global styles typography panel? It makes sense to me that if we have a component extending the typography panel for the site editor's global styles panel that something specific to that is added there instead of hacking the settings in an unexpected manner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The method I came up with is to pass the context to --- a/packages/edit-site/src/components/global-styles/screen-block.js
+++ b/packages/edit-site/src/components/global-styles/screen-block.js
@@ -260,6 +260,7 @@ function ScreenBlock( { name, variation } ) {
value={ style }
onChange={ setStyle }
settings={ settings }
+ context="global-styles"
/>
) }
{ hasDimensionsPanel && ( And depending on this context, the typography panel controls the UI. diff --git a/packages/block-editor/src/components/global-styles/typography-panel.js b/packages/block-editor/src/components/global-styles/typography-panel.js
index 3106723945..bbd10e97b2 100644
--- a/packages/block-editor/src/components/global-styles/typography-panel.js
+++ b/packages/block-editor/src/components/global-styles/typography-panel.js
@@ -173,7 +173,11 @@ export default function TypographyPanel( {
settings,
panelId,
defaultControls = DEFAULT_CONTROLS,
+ context,
} ) {
+ if ( context === 'global-styles' ) {
+ // Do something...
+ }
const decodeValue = ( rawValue ) =>
getValueFromVariable( { settings }, '', rawValue ); Is this your intended approach? Sorry if I misunderstood. |
||
|
||
const decodeValue = ( rawValue ) => | ||
getValueFromVariable( { settings }, '', rawValue ); | ||
|
||
|
@@ -334,6 +349,22 @@ export default function TypographyPanel( { | |
const hasWritingMode = () => !! value?.typography?.writingMode; | ||
const resetWritingMode = () => setWritingMode( undefined ); | ||
|
||
// Text Alignment | ||
const hasTextAlignmentControl = useHasTextAlignmentControl( settings ); | ||
|
||
const textAlign = decodeValue( inheritedValue?.typography?.textAlign ); | ||
const setTextAlign = ( newValue ) => { | ||
onChange( | ||
setImmutably( | ||
value, | ||
[ 'typography', 'textAlign' ], | ||
newValue || undefined | ||
) | ||
); | ||
}; | ||
const hasTextAlign = () => !! value?.typography?.textAlign; | ||
const resetTextAlign = () => setTextAlign( undefined ); | ||
|
||
const resetAllFilter = useCallback( ( previousValue ) => { | ||
return { | ||
...previousValue, | ||
|
@@ -514,6 +545,22 @@ export default function TypographyPanel( { | |
/> | ||
</ToolsPanelItem> | ||
) } | ||
{ hasTextAlignmentControl && ( | ||
<ToolsPanelItem | ||
label={ __( 'Text alignment' ) } | ||
hasValue={ hasTextAlign } | ||
onDeselect={ resetTextAlign } | ||
isShownByDefault={ defaultControls.textAlign } | ||
panelId={ panelId } | ||
> | ||
<TextAlignmentControl | ||
value={ textAlign } | ||
onChange={ setTextAlign } | ||
size="__unstable-large" | ||
__nextHasNoMarginBottom | ||
/> | ||
</ToolsPanelItem> | ||
) } | ||
</Wrapper> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This code is necessary to prevent the text alignment UI from being displayed in the Text, Links, Headings, Captions and Buttons typography panels.