-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #298 from humanmade/link-control
Add link control component
- Loading branch information
Showing
4 changed files
with
99 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# LinkControl | ||
|
||
The `LinkControl` component allows for adding a link. It is intended for use within the block settings UI that appears in the sidebar. | ||
|
||
The link control is just a wrapper for the core URL input component. The only difference is the visual appearance and API is more aligned with other sidebar controls such as the TextControl. | ||
|
||
## Usage | ||
|
||
```js | ||
const { InspectorControls } = wp.blocks; | ||
const { LinkControl } = hm.controls; | ||
const { attributes, setAttributes } = props; | ||
|
||
<InspectorControls> | ||
<LinkControl | ||
label="Link" | ||
help="Some description text" | ||
value={ attributes.link } | ||
onChange={ link => setAttributes( { link } ) } | ||
/> | ||
</InspectorControls> | ||
``` |
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,75 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, { useEffect } from 'react'; | ||
|
||
import { URLInput } from '@wordpress/block-editor'; | ||
import { BaseControl } from '@wordpress/components'; | ||
|
||
/** | ||
* InspectorControl for image upload. | ||
* | ||
* @param {object} props - Component properties. | ||
* @param {string} props.label - The label for the control. | ||
* @param {string} props.id - The id for the control. | ||
* @param {string} props.help - Help text for the control. | ||
* @param {Function} props.onChange - Function to call when the value changes. | ||
* @param {string} props.value - The current value of the control. | ||
* @returns {React.ReactNode} Component. | ||
*/ | ||
function LinkControl( { | ||
label, | ||
id, | ||
help, | ||
onChange, | ||
value, | ||
} ) { | ||
const className = 'hm-block-editor-components-link-control'; | ||
|
||
// Need to inject Style tag into the head in order to override the styles within URLInput component without a CSS file. | ||
useEffect( () => { | ||
const style = document.createElement( 'style' ); | ||
style.innerHTML = ` | ||
.${ className } { | ||
width: 100%; | ||
} | ||
.${ className } .block-editor-url-input { | ||
min-width: 0; | ||
max-width: none; | ||
position: relative; | ||
} | ||
.${ className } .components-base-control__field, | ||
.${ className } .components-input-control { | ||
margin-bottom: 0; | ||
} | ||
`; | ||
document.head.appendChild( style ); | ||
return () => { | ||
document.head.removeChild( style ); | ||
}; | ||
}, [ className ] ); | ||
|
||
return ( | ||
<BaseControl | ||
className={ className } | ||
help={ help } | ||
id={ id } | ||
label={ label } | ||
> | ||
<URLInput | ||
value={ value } | ||
onChange={ onChange } | ||
/> | ||
</BaseControl> | ||
); | ||
} | ||
|
||
LinkControl.propTypes = { | ||
label: PropTypes.string.isRequired, | ||
help: PropTypes.string, | ||
id: PropTypes.string, | ||
onChange: PropTypes.func.isRequired, | ||
value: PropTypes.string, | ||
}; | ||
|
||
export default LinkControl; |
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