Skip to content
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

Focal Point Picker #10925

Merged
merged 22 commits into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
684e09b
Focal Point Picker, initial commit. Cover block implementation of Foc…
jeffersonrabb Oct 22, 2018
397ea7b
Focal Point Picker README, initial commit.
jeffersonrabb Oct 22, 2018
2d6d651
Fixes for Cover blocks that pre-date the Focal Point Picker: 1) suppr…
jeffersonrabb Oct 22, 2018
4958adc
Small mistake in sample code.
jeffersonrabb Oct 22, 2018
b121c90
README edits.
jeffersonrabb Oct 22, 2018
e019989
Missing file.
jeffersonrabb Nov 12, 2018
021888c
Include Focal Point Picker stylesheet in packages/components/src/styl…
jeffersonrabb Jan 16, 2019
ded1017
Some conflict resolution in cover block's index.js.
jeffersonrabb Jan 16, 2019
97fcd04
Regrouping of component dependencies.
jeffersonrabb Jan 18, 2019
7131d07
Display horizontal and vertical positions as percentages in text boxe…
jeffersonrabb Jan 18, 2019
5567d16
Alternate approach to getting image dimensions, without requiring tha…
jeffersonrabb Jan 18, 2019
7c7f605
Removing dimensions attribute from Cover block.
jeffersonrabb Jan 18, 2019
491ede0
Fix to bug that displayed NaN/NaN in position fields before target is…
jeffersonrabb Jan 18, 2019
8bd268d
Make text input fields for horizontal and vertical position editable.
jeffersonrabb Jan 18, 2019
b67aaa4
Completely replace background image with IMG tag approach.
jeffersonrabb Jan 19, 2019
2a1d14e
Calculate bounds only when image onLoad fires.
jeffersonrabb Jan 19, 2019
48d4942
Replace TextControl elements with plain inputs. Percentage sign after…
jeffersonrabb Jan 19, 2019
87b63db
Adding a bit of padding around the image, so the white space that occ…
jeffersonrabb Jan 19, 2019
1163fe0
Small a11y and visual fixes
Jan 21, 2019
1bd8ea0
Use an SVG, and theme the colors
Jan 21, 2019
b44f1b9
CSS tweak to avoid visual artifacts in Safari during dragging.
jeffersonrabb Jan 21, 2019
d61d15a
Changed "component" prefix for all classes to "components" to match c…
jeffersonrabb Jan 21, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is going to be used in auto-generated docs so it should ideally provide a valid url that can be rendered.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there examples of valid url parameters in any other Gutenberg components? I just did a quick scan looking for one and came up blank, but maybe you can point me to a relevant one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would this function be used? It’s not clear from this document.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is meant to illustrate how the percentage values from the component can be translated into a "real-world" CSS background-position definition. The relevant line in Cover block is:

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

It would probably make sense for me to use the actual example instead of the hypothetical, but I'd prefer to hold off on updating the README until the PR is ready to merge, in case there are changes still to come. Would this be a reasonable approach?

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