Repository navigation
Add: Fit text/stretch text block supports and variation. #71904
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3d55cfa
Add: Fit text/stretch text block supports and variation.
jorgefilipecosta b590767
Update fixtures
jorgefilipecosta 9a016a9
Improve calculation logic
jorgefilipecosta e83bab7
Make maximum stretching of font size of 600
jorgefilipecosta 959246e
feedback round 1
jorgefilipecosta 923a4b0
feedback round 2
jorgefilipecosta 5dc8fdb
simplification
jorgefilipecosta f334523
remove css duplication
jorgefilipecosta 13b211b
feedback application
jorgefilipecosta aff8a77
Added Stretchy Heading variation.
jorgefilipecosta 29844e1
Increase delay between typing and text fitting
jorgefilipecosta adb1fa5
fixtures update
jorgefilipecosta 11b487d
some e2e test fixes using exactmatch on a selector
jorgefilipecosta ae7361d
remove fixtures changes
jorgefilipecosta 4314503
additional e2e fixes
jorgefilipecosta 9445eec
e2e fixes
jorgefilipecosta cdfb3e8
e2e fixes
jorgefilipecosta c9c8abb
fix lint issue
jorgefilipecosta 94a6927
include backport information
jorgefilipecosta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| https://github.com/WordPress/wordpress-develop/pull/10190 | ||
|
|
||
| * https://github.com/WordPress/gutenberg/pull/71904 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,268 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { addFilter } from '@wordpress/hooks'; | ||
| import { hasBlockSupport } from '@wordpress/blocks'; | ||
| import { useEffect, useCallback } from '@wordpress/element'; | ||
| import { useSelect } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { | ||
| ToggleControl, | ||
| __experimentalToolsPanelItem as ToolsPanelItem, | ||
| } from '@wordpress/components'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { optimizeFitText } from '../utils/fit-text-utils'; | ||
| import { store as blockEditorStore } from '../store'; | ||
| import { useBlockElement } from '../components/block-list/use-block-props/use-block-refs'; | ||
| import InspectorControls from '../components/inspector-controls'; | ||
|
|
||
| export const FIT_TEXT_SUPPORT_KEY = 'typography.fitText'; | ||
|
|
||
| /** | ||
| * Filters registered block settings, extending attributes to include | ||
| * the `fitText` attribute. | ||
| * | ||
| * @param {Object} settings Original block settings. | ||
| * @return {Object} Filtered block settings. | ||
| */ | ||
| function addAttributes( settings ) { | ||
| if ( ! hasBlockSupport( settings, FIT_TEXT_SUPPORT_KEY ) ) { | ||
| return settings; | ||
| } | ||
|
|
||
| // Allow blocks to specify their own attribute definition. | ||
| if ( settings.attributes?.fitText ) { | ||
| return settings; | ||
| } | ||
|
|
||
| // Add fitText attribute. | ||
| return { | ||
| ...settings, | ||
| attributes: { | ||
| ...settings.attributes, | ||
| fitText: { | ||
| type: 'boolean', | ||
| }, | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook to handle fit text functionality in the editor. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {?boolean} props.fitText Fit text attribute. | ||
| * @param {string} props.name Block name. | ||
| * @param {string} props.clientId Block client ID. | ||
| */ | ||
| function useFitText( { fitText, name, clientId } ) { | ||
| const hasFitTextSupport = hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ); | ||
| const blockElement = useBlockElement( clientId ); | ||
|
|
||
| // Monitor block attribute changes | ||
| // Any attribute may change the available space. | ||
| const blockAttributes = useSelect( | ||
| ( select ) => { | ||
| if ( ! clientId ) { | ||
| return; | ||
| } | ||
| return select( blockEditorStore ).getBlockAttributes( clientId ); | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| const applyFitText = useCallback( () => { | ||
| if ( ! blockElement || ! hasFitTextSupport || ! fitText ) { | ||
| return; | ||
| } | ||
|
|
||
| // Get or create style element with unique ID | ||
| const styleId = `fit-text-${ clientId }`; | ||
| let styleElement = blockElement.ownerDocument.getElementById( styleId ); | ||
| if ( ! styleElement ) { | ||
| styleElement = blockElement.ownerDocument.createElement( 'style' ); | ||
| styleElement.id = styleId; | ||
| blockElement.ownerDocument.head.appendChild( styleElement ); | ||
| } | ||
|
|
||
| const blockSelector = `#block-${ clientId }`; | ||
|
|
||
| const applyStylesFn = ( css ) => { | ||
| styleElement.textContent = css; | ||
| }; | ||
|
|
||
| optimizeFitText( blockElement, blockSelector, applyStylesFn ); | ||
| }, [ blockElement, clientId, hasFitTextSupport, fitText ] ); | ||
|
|
||
| useEffect( () => { | ||
| if ( | ||
| ! fitText || | ||
| ! blockElement || | ||
| ! clientId || | ||
| ! hasFitTextSupport | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| // Apply initially | ||
| applyFitText(); | ||
|
|
||
| // Store current element value for cleanup | ||
| const currentElement = blockElement; | ||
|
|
||
| // Watch for size changes | ||
| let resizeObserver; | ||
| if ( window.ResizeObserver && currentElement.parentElement ) { | ||
| resizeObserver = new window.ResizeObserver( applyFitText ); | ||
| resizeObserver.observe( currentElement.parentElement ); | ||
| } | ||
|
|
||
| // Cleanup function | ||
| return () => { | ||
| if ( resizeObserver ) { | ||
| resizeObserver.disconnect(); | ||
| } | ||
|
|
||
| const styleId = `fit-text-${ clientId }`; | ||
| const styleElement = | ||
| currentElement.ownerDocument.getElementById( styleId ); | ||
| if ( styleElement ) { | ||
| styleElement.remove(); | ||
| } | ||
| }; | ||
| }, [ fitText, clientId, applyFitText, blockElement, hasFitTextSupport ] ); | ||
|
|
||
| // Trigger fit text recalculation when content changes | ||
| useEffect( () => { | ||
| if ( fitText && blockElement && hasFitTextSupport ) { | ||
| // Small delay to ensure DOM has updated after content changes | ||
| const timer = setTimeout( () => { | ||
| if ( blockElement ) { | ||
| applyFitText(); | ||
| } | ||
| }, 1000 ); | ||
|
|
||
| return () => clearTimeout( timer ); | ||
| } | ||
| }, [ | ||
| blockAttributes, | ||
| fitText, | ||
| applyFitText, | ||
| blockElement, | ||
| hasFitTextSupport, | ||
| ] ); | ||
| } | ||
|
|
||
| /** | ||
| * Fit text control component for the typography panel. | ||
| * | ||
| * @param {Object} props Component props. | ||
| * @param {string} props.clientId Block client ID. | ||
| * @param {Function} props.setAttributes Function to set block attributes. | ||
| * @param {string} props.name Block name. | ||
| * @param {boolean} props.fitText Whether fit text is enabled. | ||
| */ | ||
| export function FitTextControl( { | ||
| clientId, | ||
| fitText = false, | ||
| setAttributes, | ||
| name, | ||
| } ) { | ||
| if ( ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) { | ||
| return null; | ||
| } | ||
| return ( | ||
| <InspectorControls group="typography"> | ||
| <ToolsPanelItem | ||
| hasValue={ () => fitText } | ||
| label={ __( 'Fit text' ) } | ||
| onDeselect={ () => setAttributes( { fitText: undefined } ) } | ||
| resetAllFilter={ () => ( { fitText: undefined } ) } | ||
| panelId={ clientId } | ||
| > | ||
| <ToggleControl | ||
| __nextHasNoMarginBottom | ||
| label={ __( 'Fit text' ) } | ||
| checked={ fitText } | ||
| onChange={ () => | ||
| setAttributes( { fitText: ! fitText || undefined } ) | ||
| } | ||
| help={ | ||
| fitText | ||
| ? __( 'Text will resize to fit its container.' ) | ||
| : __( 'Resize text to fit its container.' ) | ||
| } | ||
| /> | ||
| </ToolsPanelItem> | ||
| </InspectorControls> | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Override props applied to the block element on save. | ||
| * | ||
| * @param {Object} props Additional props applied to the block element. | ||
| * @param {Object} blockType Block type. | ||
| * @param {Object} attributes Block attributes. | ||
| * @return {Object} Filtered props applied to the block element. | ||
| */ | ||
| function addSaveProps( props, blockType, attributes ) { | ||
| if ( ! hasBlockSupport( blockType, FIT_TEXT_SUPPORT_KEY ) ) { | ||
| return props; | ||
| } | ||
|
|
||
| const { fitText } = attributes; | ||
|
|
||
| if ( ! fitText ) { | ||
| return props; | ||
| } | ||
|
|
||
| // Add CSS class for frontend detection and styling | ||
| const className = props.className | ||
| ? `${ props.className } has-fit-text` | ||
| : 'has-fit-text'; | ||
|
|
||
| return { | ||
| ...props, | ||
| className, | ||
| }; | ||
| } | ||
| /** | ||
| * Override props applied to the block element in the editor. | ||
| * | ||
| * @param {Object} props Component props including block attributes. | ||
| * @param {string} props.name Block name. | ||
| * @param {boolean} props.fitText Whether fit text is enabled. | ||
| * @param {string} props.clientId Block client ID. | ||
| * @return {Object} Filtered props applied to the block element. | ||
| */ | ||
| function useBlockProps( { name, fitText, clientId } ) { | ||
| useFitText( { fitText, name, clientId } ); | ||
| if ( ! fitText || ! hasBlockSupport( name, FIT_TEXT_SUPPORT_KEY ) ) { | ||
| return {}; | ||
| } | ||
| return { | ||
| className: 'has-fit-text', | ||
| }; | ||
| } | ||
|
|
||
| addFilter( | ||
| 'blocks.registerBlockType', | ||
| 'core/fit-text/addAttribute', | ||
| addAttributes | ||
| ); | ||
|
|
||
| const hasFitTextSupport = ( blockNameOrType ) => { | ||
| return hasBlockSupport( blockNameOrType, FIT_TEXT_SUPPORT_KEY ); | ||
| }; | ||
|
|
||
| export default { | ||
| useBlockProps, | ||
| addSaveProps, | ||
| attributeKeys: [ 'fitText' ], | ||
| hasSupport: hasFitTextSupport, | ||
| edit: FitTextControl, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 can be undefined and causes the error @scruffian mentioned.
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.
Ah, I wrongly assumed there was a default value set up.