Skip to content

Commit

Permalink
Focal Point Picker for the Cover Block (#10925)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffersonrabb authored and youknowriad committed Jan 29, 2019
1 parent b944ed7 commit 77f6176
Show file tree
Hide file tree
Showing 7 changed files with 423 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,12 @@
"markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/packages/components/src/external-link/README.md",
"parent": "components"
},
{
"title": "FocalPointPicker",
"slug": "focal-point-picker",
"markdown_source": "https://raw.githubusercontent.com/WordPress/gutenberg/master/packages/components/src/focal-point-picker/README.md",
"parent": "components"
},
{
"title": "FocusableIframe",
"slug": "focusable-iframe",
Expand Down
21 changes: 21 additions & 0 deletions packages/block-library/src/cover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import {
FocalPointPicker,
IconButton,
PanelBody,
RangeControl,
Expand Down Expand Up @@ -67,6 +68,9 @@ const blockAttributes = {
type: 'string',
default: 'image',
},
focalPoint: {
type: 'object',
},
};

export const name = 'core/cover';
Expand Down Expand Up @@ -175,6 +179,7 @@ export const settings = {
backgroundType,
contentAlign,
dimRatio,
focalPoint,
hasParallax,
id,
title,
Expand Down Expand Up @@ -224,6 +229,10 @@ export const settings = {
backgroundColor: overlayColor.color,
};

if ( focalPoint ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}

const controls = (
<Fragment>
<BlockControls>
Expand Down Expand Up @@ -265,6 +274,14 @@ export const settings = {
onChange={ toggleParallax }
/>
) }
{ IMAGE_BACKGROUND_TYPE === backgroundType && ! hasParallax && (
<FocalPointPicker
label={ __( 'Focal Point Picker' ) }
url={ url }
value={ focalPoint }
onChange={ ( value ) => setAttributes( { focalPoint: value } ) }
/>
) }
<PanelColorSettings
title={ __( 'Overlay' ) }
initialOpen={ true }
Expand Down Expand Up @@ -370,6 +387,7 @@ export const settings = {
contentAlign,
customOverlayColor,
dimRatio,
focalPoint,
hasParallax,
overlayColor,
title,
Expand All @@ -382,6 +400,9 @@ export const settings = {
if ( ! overlayColorClass ) {
style.backgroundColor = customOverlayColor;
}
if ( focalPoint && ! hasParallax ) {
style.backgroundPosition = `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`;
}

const classes = classnames(
dimRatioToClass( dimRatio ),
Expand Down
68 changes: 68 additions & 0 deletions packages/components/src/focal-point-picker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Focal Point Picker

Focal Point Picker is a component which creates a UI for identifying the most important visual point of an image. This component addresses a specific problem: with large background images it is common to see undesirable crops, especially when viewing on smaller viewports such as mobile phones. This component allows the selection of the point with the most important visual information and returns it as a pair of numbers between 0 and 1. This value can be easily converted into the CSS `background-position` attribute, and will ensure that the focal point is never cropped out, regardless of viewport.

Example focal point picker value: `{ x: 0.5, y: 0.1 }`
Corresponding CSS: `background-position: 50% 10%;`

## Usage

```jsx
import { FocalPointPicker } from '@wordpress/components';

const MyFocalPointPicker = withState( {
focalPoint: {
x: 0.5,
y: 0.5
},
} )( ( { focalPoint, setState } ) => {
const url = '/path/to/image';
const dimensions = {
width: 400,
height: 100
};
return (
<FocalPointPicker
url={ url }
dimensions={ dimensions }
value={ focalPoint }
onChange={ ( focalPoint ) => setState( { focalPoint } ) }
/>
)
} );

/* Example function to render the CSS styles based on Focal Point Picker value */
const renderImageContainerWithFocalPoint = ( url, focalPoint ) => {
const style = {
backgroundImage: `url(${ url })` ,
backgroundPosition: `${ focalPoint.x * 100 }% ${ focalPoint.y * 100 }%`
}
return <div style={ style } />;
};
```

## Props

### `url`

- Type: `Text`
- Required: Yes
- Description: URL of the image to be displayed

### `dimensions`

- Type: `Object`
- Required: Yes
- Description: An object describing the height and width of the image. Requires two paramaters: `height`, `width`.

### `value`

- Type: `Object`
- Required: Yes
- Description: The focal point. Should be an object containing `x` and `y` params.

### `onChange`

- Type: `Function`
- Required: Yes
- Description: Callback which is called when the focal point changes.
Loading

0 comments on commit 77f6176

Please sign in to comment.