-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Paragraph: Add LineHeightControl + Attribute (#20775)
* Update emotion library to latest. Adds to block-library package. * Add LineHeightControl component to block-editor * Export LineHeightControl as experimental from block-editor * Hook up LineHeightControl with core Paragraph block * Improve undefined/reset interactions for line-height attribute * Refactor store hook usage with LineHeightControl * Refactor with withLineHeight HOC for Edit and Save components * Improve reset logic. Simplifies stlye value output. * Refactor function names and comments * Fix import/export of new experimental attribute hooks * Export LineHeightControls and withLineHeight for native * Adjust mechanism to get block attributes in useBlockAttributes hook * Add theme support for disable-custom-line-height * Clarify logic + add comments for lineHeight attribute -> value evaluation * Simplify CSS output for lineHeight in paragraph block * Add experimental prefix for disableCustomLineHeight setting * Add comment for :root based CSS rule * Removes @emotion from block-library. Refactor to use scss * Remove withLineHeight HOC abstraction. Reverted to use inline styles instead of CSS variable for line-height style rendering * Revert className implementation Also revert @emotion library versions to master * Revert @emotion/native from 10.0.23 -> 10.0.22 (to match master) * Updates package-lock.json * Fix e2e test to target correct text input * Reverting package-lock.json * Provide clientId in dependency array in useSetAttributes hook * Use CSS variables and a support flag for the line height customization * Improves handling of "zero" key press * Update packages/block-editor/src/components/line-height-control/utils.js Co-Authored-By: Zebulan Stanphill <[email protected]> * Remove unused import * Remove useless hooks file Co-authored-by: Riad Benguella <[email protected]> Co-authored-by: Zebulan Stanphill <[email protected]>
- Loading branch information
1 parent
65520ef
commit a9f23ba
Showing
22 changed files
with
271 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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
88 changes: 88 additions & 0 deletions
88
packages/block-editor/src/components/line-height-control/index.js
This file contains 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,88 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { TextControl } from '@wordpress/components'; | ||
import { ZERO } from '@wordpress/keycodes'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
BASE_DEFAULT_VALUE, | ||
RESET_VALUE, | ||
STEP, | ||
useIsLineHeightControlsDisabled, | ||
isLineHeightDefined, | ||
} from './utils'; | ||
|
||
export default function LineHeightControl( { value: lineHeight, onChange } ) { | ||
const isDisabled = useIsLineHeightControlsDisabled(); | ||
const isDefined = isLineHeightDefined( lineHeight ); | ||
|
||
// Don't render the controls if disabled by editor settings | ||
if ( isDisabled ) { | ||
return null; | ||
} | ||
|
||
const handleOnKeyDown = ( event ) => { | ||
const { keyCode } = event; | ||
|
||
if ( keyCode === ZERO && ! isDefined ) { | ||
/** | ||
* Prevents the onChange callback from firing, which prevents | ||
* the logic from assuming the change was triggered from | ||
* an input arrow CLICK. | ||
*/ | ||
event.preventDefault(); | ||
onChange( '0' ); | ||
} | ||
}; | ||
|
||
const handleOnChange = ( nextValue ) => { | ||
// Set the next value without modification if lineHeight has been defined | ||
if ( isDefined ) { | ||
onChange( nextValue ); | ||
return; | ||
} | ||
|
||
// Otherwise... | ||
/** | ||
* The following logic handles the initial up/down arrow CLICK of the | ||
* input element. This is so that the next values (from an undefined value state) | ||
* are more better suited for line-height rendering. | ||
*/ | ||
let adjustedNextValue = nextValue; | ||
|
||
switch ( nextValue ) { | ||
case `${ STEP }`: | ||
// Increment by step value | ||
adjustedNextValue = BASE_DEFAULT_VALUE + STEP; | ||
break; | ||
case '0': | ||
// Decrement by step value | ||
adjustedNextValue = BASE_DEFAULT_VALUE - STEP; | ||
break; | ||
} | ||
|
||
onChange( adjustedNextValue ); | ||
}; | ||
|
||
const value = isDefined ? lineHeight : RESET_VALUE; | ||
|
||
return ( | ||
<div className="block-editor-line-height-control"> | ||
<TextControl | ||
autoComplete="off" | ||
onKeyDown={ handleOnKeyDown } | ||
onChange={ handleOnChange } | ||
label={ __( 'Line height' ) } | ||
placeholder={ BASE_DEFAULT_VALUE } | ||
step={ STEP } | ||
type="number" | ||
value={ value } | ||
min={ 0 } | ||
/> | ||
</div> | ||
); | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/block-editor/src/components/line-height-control/style.scss
This file contains 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,8 @@ | ||
.block-editor-line-height-control { | ||
margin-bottom: 24px; | ||
|
||
input { | ||
display: block; | ||
max-width: 60px; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/block-editor/src/components/line-height-control/utils.js
This file contains 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,44 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
export const BASE_DEFAULT_VALUE = 1.5; | ||
export const STEP = 0.1; | ||
/** | ||
* There are varying value types within LineHeightControl: | ||
* | ||
* {undefined} Initial value. No changes from the user. | ||
* {string} Input value. Value consumed/outputted by the input. Empty would be ''. | ||
* {number} Block attribute type. Input value needs to be converted for attribute setting. | ||
* | ||
* Note: If the value is undefined, the input requires it to be an empty string ('') | ||
* in order to be considered "controlled" by props (rather than internal state). | ||
*/ | ||
export const RESET_VALUE = ''; | ||
|
||
/** | ||
* Retrieves whether custom lineHeight controls should be disabled from editor settings. | ||
* | ||
* @return {boolean} Whether lineHeight controls should be disabled. | ||
*/ | ||
export function useIsLineHeightControlsDisabled() { | ||
const isDisabled = useSelect( ( select ) => { | ||
const { getSettings } = select( 'core/block-editor' ); | ||
|
||
return !! getSettings().__experimentalDisableCustomLineHeight; | ||
}, [] ); | ||
|
||
return isDisabled; | ||
} | ||
|
||
/** | ||
* Determines if the lineHeight attribute has been properly defined. | ||
* | ||
* @param {any} lineHeight The value to check. | ||
* | ||
* @return {boolean} Whether the lineHeight attribute is valid. | ||
*/ | ||
export function isLineHeightDefined( lineHeight ) { | ||
return lineHeight !== undefined && lineHeight !== RESET_VALUE; | ||
} |
This file contains 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 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,64 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { hasBlockSupport } from '@wordpress/blocks'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
import { PanelBody } from '@wordpress/components'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import LineHeightControl from '../components/line-height-control'; | ||
import InspectorControls from '../components/inspector-controls'; | ||
import { cleanEmptyObject } from './utils'; | ||
|
||
export const LINE_HEIGHT_SUPPRT_KEY = '__experimentalLineHeight'; | ||
|
||
/** | ||
* Override the default edit UI to include new inspector controls for block | ||
* color, if block defines support. | ||
* | ||
* @param {Function} BlockEdit Original component | ||
* @return {Function} Wrapped component | ||
*/ | ||
export const withBlockControls = createHigherOrderComponent( | ||
( BlockEdit ) => ( props ) => { | ||
const { name: blockName } = props; | ||
if ( ! hasBlockSupport( blockName, LINE_HEIGHT_SUPPRT_KEY ) ) { | ||
return <BlockEdit key="edit" { ...props } />; | ||
} | ||
const { style } = props.attributes; | ||
const onChange = ( newLineHeightValue ) => { | ||
const newStyle = { | ||
...style, | ||
typography: { | ||
lineHeight: newLineHeightValue, | ||
}, | ||
}; | ||
props.setAttributes( { | ||
style: cleanEmptyObject( newStyle ), | ||
} ); | ||
}; | ||
|
||
return [ | ||
<InspectorControls key="control"> | ||
<PanelBody title={ __( 'Typography' ) }> | ||
<LineHeightControl | ||
value={ style?.typography?.lineHeight } | ||
onChange={ onChange } | ||
/> | ||
</PanelBody> | ||
</InspectorControls>, | ||
<BlockEdit key="edit" { ...props } />, | ||
]; | ||
}, | ||
'withToolbarControls' | ||
); | ||
|
||
addFilter( | ||
'editor.BlockEdit', | ||
'core/color/with-block-controls', | ||
withBlockControls | ||
); |
This file contains 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 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
File renamed without changes.
This file contains 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,23 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { pickBy, isEqual, isObject, identity, mapValues } from 'lodash'; | ||
|
||
/** | ||
* Removed undefined values from nested object. | ||
* | ||
* @param {*} object | ||
* @return {*} Object cleaned from undefined values | ||
*/ | ||
export const cleanEmptyObject = ( object ) => { | ||
if ( ! isObject( object ) ) { | ||
return object; | ||
} | ||
const cleanedNestedObjects = pickBy( | ||
mapValues( object, cleanEmptyObject ), | ||
identity | ||
); | ||
return isEqual( cleanedNestedObjects, {} ) | ||
? undefined | ||
: cleanedNestedObjects; | ||
}; |
This file contains 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 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 |
---|---|---|
|
@@ -26,7 +26,10 @@ | |
}, | ||
"direction": { | ||
"type": "string", | ||
"enum": [ "ltr", "rtl" ] | ||
"enum": [ | ||
"ltr", | ||
"rtl" | ||
] | ||
} | ||
} | ||
} |
This file contains 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 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 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 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.