-
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
Focal Point Picker #10925
Focal Point Picker #10925
Changes from all commits
684e09b
397ea7b
2d6d651
4958adc
b121c90
e019989
021888c
ded1017
97fcd04
7131d07
5567d16
7c7f605
491ede0
8bd268d
b67aaa4
2a1d14e
48d4942
87b63db
1163fe0
1bd8ea0
b44f1b9
d61d15a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ) => { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
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. |
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.
This example is going to be used in auto-generated docs so it should ideally provide a valid url that can be rendered.
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.
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?
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.
See how
ResponsiveWrapper
is documented:https://github.com/WordPress/gutenberg/blob/master/packages/components/src/responsive-wrapper/README.md
This is how it renders in Calypso DevDocs:
https://wpcalypso.wordpress.com/devdocs/gutenberg-components/responsive-wrapper