Skip to content

Commit

Permalink
Improve dropcap behavior (#28685)
Browse files Browse the repository at this point in the history
* Try: Fix dropcap behavior.

* Remove comments.
  • Loading branch information
jasmussen authored Mar 1, 2021
1 parent 3fb70dc commit d3b7629
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 47 deletions.
49 changes: 2 additions & 47 deletions packages/block-library/src/paragraph/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,11 @@ import {
InspectorControls,
RichText,
useBlockProps,
getFontSize,
__experimentalUseEditorFeature as useEditorFeature,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { createBlock } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useEffect, useRef, useState } from '@wordpress/element';
import { formatLtr } from '@wordpress/icons';

function getComputedStyle( node, pseudo ) {
return node.ownerDocument.defaultView.getComputedStyle( node, pseudo );
}

const name = 'core/paragraph';

function ParagraphRTLToolbar( { direction, setDirection } ) {
Expand All @@ -50,23 +42,6 @@ function ParagraphRTLToolbar( { direction, setDirection } ) {
);
}

function useDropCapMinHeight( ref, isDisabled, dependencies ) {
const [ minHeight, setMinHeight ] = useState();

useEffect( () => {
if ( isDisabled ) {
setMinHeight();
return;
}

setMinHeight(
getComputedStyle( ref.current, 'first-letter' ).lineHeight
);
}, [ isDisabled, ...dependencies ] );

return minHeight;
}

function ParagraphBlock( {
attributes,
mergeBlocks,
Expand All @@ -75,34 +50,14 @@ function ParagraphBlock( {
setAttributes,
clientId,
} ) {
const {
align,
content,
direction,
dropCap,
placeholder,
fontSize,
style,
} = attributes;
const { align, content, direction, dropCap, placeholder } = attributes;
const isDropCapFeatureEnabled = useEditorFeature( 'typography.dropCap' );
const ref = useRef();
const inlineFontSize = style?.fontSize;
const size = useSelect(
( select ) => {
const { fontSizes } = select( blockEditorStore ).getSettings();
return getFontSize( fontSizes, fontSize, inlineFontSize ).size;
},
[ fontSize, inlineFontSize ]
);
const hasDropCap = isDropCapFeatureEnabled && dropCap;
const minHeight = useDropCapMinHeight( ref, ! hasDropCap, [ size ] );
const blockProps = useBlockProps( {
ref,
className: classnames( {
'has-drop-cap': dropCap,
[ `has-text-align-${ align }` ]: align,
} ),
style: { direction, minHeight },
style: { direction },
} );

return (
Expand Down
5 changes: 5 additions & 0 deletions packages/block-library/src/paragraph/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
font-style: normal;
}

// Prevent the dropcap from breaking out of the box when a background is applied.
p.has-drop-cap.has-background {
overflow: hidden;
}

p.has-background {
padding: $block-bg-padding--v $block-bg-padding--h;
}
Expand Down

0 comments on commit d3b7629

Please sign in to comment.