-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Add option to use Post Featured Image with consistent height #27545
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
36 changes: 36 additions & 0 deletions
36
packages/block-library/src/post-featured-image/constants.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,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { Platform } from '@wordpress/element'; | ||
|
||
const isWeb = Platform.OS === 'web'; | ||
export const CSS_UNITS = [ | ||
{ | ||
value: 'px', | ||
label: isWeb ? 'px' : __( 'Pixels (px)' ), | ||
default: '300', | ||
}, | ||
{ | ||
value: 'em', | ||
label: isWeb ? 'em' : __( 'Relative to parent font size (em)' ), | ||
default: '20', | ||
}, | ||
{ | ||
value: 'rem', | ||
label: isWeb ? 'rem' : __( 'Relative to root font size (rem)' ), | ||
default: '20', | ||
}, | ||
{ | ||
value: 'vw', | ||
label: isWeb ? 'vw' : __( 'Viewport width (vw)' ), | ||
default: '20', | ||
}, | ||
{ | ||
value: 'vh', | ||
label: isWeb ? 'vh' : __( 'Viewport height (vh)' ), | ||
default: '50', | ||
}, | ||
]; | ||
export const ALLOWED_MEDIA_TYPES = [ 'image' ]; | ||
export const COVER_MIN_HEIGHT = 50; |
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
96 changes: 96 additions & 0 deletions
96
packages/block-library/src/post-featured-image/inspector-controls.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,96 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { BaseControl, PanelBody, ToggleControl } from '@wordpress/components'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { | ||
InspectorControls, | ||
__experimentalUnitControl as UnitControl, | ||
} from '@wordpress/block-editor'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { CSS_UNITS, COVER_MIN_HEIGHT } from './constants'; | ||
|
||
const CoverHeightInput = ( { | ||
onChange, | ||
onUnitChange, | ||
unit = 'px', | ||
value = '', | ||
} ) => { | ||
const inputId = useInstanceId( | ||
UnitControl, | ||
'block-post-featured-image-height-input' | ||
); | ||
const handleOnChange = ( unprocessedValue ) => { | ||
const inputValue = parseInt( unprocessedValue, 10 ); | ||
if ( Number.isNaN( inputValue ) ) return; | ||
onChange( inputValue ); | ||
}; | ||
const min = unit === 'px' ? COVER_MIN_HEIGHT : 0; | ||
return ( | ||
<BaseControl label={ __( 'Minimum height of cover' ) } id={ inputId }> | ||
<UnitControl | ||
id={ inputId } | ||
min={ min } | ||
onChange={ handleOnChange } | ||
onUnitChange={ onUnitChange } | ||
step="1" | ||
style={ { maxWidth: 80 } } | ||
unit={ unit } | ||
units={ CSS_UNITS } | ||
value={ value } | ||
isResetValueOnUnitChange | ||
/> | ||
</BaseControl> | ||
); | ||
}; | ||
|
||
const FeaturedImageInspectorControls = ( { | ||
attributes: { useAsCover, minHeight, minHeightUnit, isLink }, | ||
setAttributes, | ||
postType, | ||
} ) => { | ||
return ( | ||
<InspectorControls> | ||
<PanelBody title={ __( 'Image settings' ) }> | ||
<ToggleControl | ||
label={ __( 'Use as cover' ) } | ||
onChange={ () => | ||
setAttributes( { useAsCover: ! useAsCover } ) | ||
} | ||
checked={ !! useAsCover } | ||
/> | ||
{ useAsCover && ( | ||
<CoverHeightInput | ||
value={ minHeight } | ||
unit={ minHeightUnit } | ||
onChange={ ( newMinHeight ) => | ||
setAttributes( { minHeight: newMinHeight } ) | ||
} | ||
onUnitChange={ ( newUnit ) => | ||
setAttributes( { | ||
minHeightUnit: newUnit, | ||
} ) | ||
} | ||
/> | ||
) } | ||
</PanelBody> | ||
<PanelBody title={ __( 'Link settings' ) }> | ||
<ToggleControl | ||
label={ sprintf( | ||
// translators: %s: Name of the post type e.g: "post". | ||
__( 'Link to %s' ), | ||
postType | ||
) } | ||
onChange={ () => setAttributes( { isLink: ! isLink } ) } | ||
checked={ !! isLink } | ||
/> | ||
</PanelBody> | ||
</InspectorControls> | ||
); | ||
}; | ||
|
||
export default FeaturedImageInspectorControls; |
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
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.
@jasmussen is there a better way to achieve full height/width link with no content?
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.
would it make more sense to just wrap the image in an a tag?