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

Fix Site title: Different markup in the editor and on the frontend #29021

Merged
merged 6 commits into from
Mar 17, 2021
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
1 change: 1 addition & 0 deletions packages/block-library/src/site-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalSelector": ".wp-block-site-title, .wp-block-site-title > a",
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we target the "inner a" here?

Copy link
Member Author

Choose a reason for hiding this comment

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

The goal is to change the colour of the site title, which is an anchor tag wrapped in a heading.

By default, an anchor tag does not inherit its parent's colour settings if an href attribute is present. So to change the appearance of the title we target the inner anchor.

Or well, that's what was done here. :)

Copy link
Contributor

Choose a reason for hiding this comment

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

By default, an anchor tag does not inherit its parent's colour settings if an href attribute is present. So to change the appearance of the title we target the inner anchor.

It does break font size and transform styles for the block though. I'm reverting that here #30558 to solve these. Maybe we can just add color: inherit for the link in the style of the block?

Copy link
Contributor

Choose a reason for hiding this comment

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

In my tests though, the color is applied properly even with the revert because it actually applies the link color (which is the right thing to do there)

Copy link
Contributor

Choose a reason for hiding this comment

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

I also tested the color in the revert and worked well.

"__experimentalTextTransform": true
}
}
25 changes: 12 additions & 13 deletions packages/block-library/src/site-title/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import LevelToolbar from './level-toolbar';
export default function SiteTitleEdit( { attributes, setAttributes } ) {
const { level, textAlign } = attributes;
const [ title, setTitle ] = useEntityProp( 'root', 'site', 'title' );
const tagName = level === 0 ? 'p' : `h${ level }`;
const TagName = level === 0 ? 'p' : `h${ level }`;
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );

return (
<>
<BlockControls>
Expand All @@ -47,17 +46,17 @@ export default function SiteTitleEdit( { attributes, setAttributes } ) {
}
/>
</BlockControls>

<RichText
tagName={ tagName }
aria-label={ __( 'Site title text' ) }
placeholder={ __( 'Write site title…' ) }
value={ title }
onChange={ setTitle }
allowedFormats={ [] }
disableLineBreaks
{ ...blockProps }
/>
<TagName { ...blockProps }>
<RichText
tagName="a"
aria-label={ __( 'Site title text' ) }
placeholder={ __( 'Write site title…' ) }
value={ title }
onChange={ setTitle }
allowedFormats={ [] }
disableLineBreaks
/>
</TagName>
</>
);
}