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

File block: Add typography block support #55693

Open
wants to merge 15 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
File block: Add typography block supports
  • Loading branch information
carolinan committed Oct 27, 2023
commit 7ed7ea075e754f8cb2d58f0c9429f237155a20b8
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ Add a link to a downloadable file. ([Source](https://github.com/WordPress/gutenb

- **Name:** core/file
- **Category:** media
- **Supports:** align, anchor, color (background, gradients, link, ~~text~~), interactivity, spacing (margin, padding)
- **Supports:** align, anchor, color (background, gradients, link, ~~text~~), interactivity, spacing (margin, padding), typography (fontSize, lineHeight)
- **Attributes:** displayPreview, downloadButtonText, fileId, fileName, href, id, previewHeight, showDownloadButton, textLinkHref, textLinkTarget

## Footnotes
Expand Down
15 changes: 14 additions & 1 deletion packages/block-library/src/file/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,20 @@
"link": true
}
},
"interactivity": true
"interactivity": true,
"typography": {
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
}
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-file-editor",
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/file/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
useBlockProps,
store as blockEditorStore,
__experimentalGetElementClassName,
getTypographyClassesAndStyles as useTypographyProps,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useCopyToClipboard } from '@wordpress/compose';
Expand Down Expand Up @@ -82,7 +83,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
} ),
[ id ]
);

const typgraphyProps = useTypographyProps( attributes );
const { createErrorNotice } = useDispatch( noticesStore );
const { toggleSelection, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );
Expand Down Expand Up @@ -289,13 +290,15 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
>
{ /* Using RichText here instead of PlainText so that it can be styled like a button. */ }
<RichText
{ ...blockProps }
tagName="div" // Must be block-level or else cursor disappears.
aria-label={ __( 'Download button text' ) }
className={ classnames(
'wp-block-file__button',
__experimentalGetElementClassName(
'button'
)
),
typgraphyProps.className
) }
value={ downloadButtonText }
withoutInteractiveFormatting
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/file/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
RichText,
useBlockProps,
__experimentalGetElementClassName,
getTypographyClassesAndStyles as getTypographyClassesAndStyles,
} from '@wordpress/block-editor';

export default function save( { attributes } ) {
Expand All @@ -26,8 +27,8 @@ export default function save( { attributes } ) {
} = attributes;

const pdfEmbedLabel = RichText.isEmpty( fileName ) ? 'PDF embed' : fileName;

const hasFilename = ! RichText.isEmpty( fileName );
const typographyProps = getTypographyClassesAndStyles( attributes );
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any particular reason for adding these manually. Isn't this something that the hook adds automatically?

Copy link
Contributor Author

@carolinan carolinan Mar 25, 2024

Choose a reason for hiding this comment

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

It is because I needed the typography class names and inline styles to be on both the wrapper, and the download button.
If I only use the save props on the wrapper, or if I try to use an experimental selector for the typography in block.json, then the button styles override the user settings from the controls in the block editor.


// Only output an `aria-describedby` when the element it's referring to is
// actually rendered.
Expand Down Expand Up @@ -64,10 +65,12 @@ export default function save( { attributes } ) {
) }
{ showDownloadButton && (
<a
{ ...useBlockProps.save() }
href={ href }
className={ classnames(
'wp-block-file__button',
__experimentalGetElementClassName( 'button' )
__experimentalGetElementClassName( 'button' ),
typographyProps.className
) }
download={ true }
aria-describedby={ describedById }
Expand Down
Loading