Skip to content

Commit c0a4d57

Browse files
authored
Merge pull request #298 from humanmade/link-control
Add link control component
2 parents 5e661f1 + 62668d4 commit c0a4d57

4 files changed

Lines changed: 99 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ One way to ensure all dependencies are loaded is to use the [`@wordpress/depende
4747
- [`FileControls`](src/components/FileControls)
4848
- [`ImageControl`](src/components/ImageControl)
4949
- [`InnerBlockSlider`](src/components/InnerBlockSlider)
50+
- [`LinkControl`](src/components/LinkControl)
5051
- [`LinkToolbar`](src/components/LinkToolbar)
5152
- [`PlainTextWithLimit`](src/components/PlainTextWithLimit)
5253
- [`PostPicker`](src/components/PostPicker)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# LinkControl
2+
3+
The `LinkControl` component allows for adding a link. It is intended for use within the block settings UI that appears in the sidebar.
4+
5+
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.
6+
7+
## Usage
8+
9+
```js
10+
const { InspectorControls } = wp.blocks;
11+
const { LinkControl } = hm.controls;
12+
const { attributes, setAttributes } = props;
13+
14+
<InspectorControls>
15+
<LinkControl
16+
label="Link"
17+
help="Some description text"
18+
value={ attributes.link }
19+
onChange={ link => setAttributes( { link } ) }
20+
/>
21+
</InspectorControls>
22+
```
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import PropTypes from 'prop-types';
2+
import React, { useEffect } from 'react';
3+
4+
import { URLInput } from '@wordpress/block-editor';
5+
import { BaseControl } from '@wordpress/components';
6+
7+
/**
8+
* InspectorControl for image upload.
9+
*
10+
* @param {object} props - Component properties.
11+
* @param {string} props.label - The label for the control.
12+
* @param {string} props.id - The id for the control.
13+
* @param {string} props.help - Help text for the control.
14+
* @param {Function} props.onChange - Function to call when the value changes.
15+
* @param {string} props.value - The current value of the control.
16+
* @returns {React.ReactNode} Component.
17+
*/
18+
function LinkControl( {
19+
label,
20+
id,
21+
help,
22+
onChange,
23+
value,
24+
} ) {
25+
const className = 'hm-block-editor-components-link-control';
26+
27+
// Need to inject Style tag into the head in order to override the styles within URLInput component without a CSS file.
28+
useEffect( () => {
29+
const style = document.createElement( 'style' );
30+
style.innerHTML = `
31+
.${ className } {
32+
width: 100%;
33+
}
34+
35+
.${ className } .block-editor-url-input {
36+
min-width: 0;
37+
max-width: none;
38+
position: relative;
39+
}
40+
41+
.${ className } .components-base-control__field,
42+
.${ className } .components-input-control {
43+
margin-bottom: 0;
44+
}
45+
`;
46+
document.head.appendChild( style );
47+
return () => {
48+
document.head.removeChild( style );
49+
};
50+
}, [ className ] );
51+
52+
return (
53+
<BaseControl
54+
className={ className }
55+
help={ help }
56+
id={ id }
57+
label={ label }
58+
>
59+
<URLInput
60+
value={ value }
61+
onChange={ onChange }
62+
/>
63+
</BaseControl>
64+
);
65+
}
66+
67+
LinkControl.propTypes = {
68+
label: PropTypes.string.isRequired,
69+
help: PropTypes.string,
70+
id: PropTypes.string,
71+
onChange: PropTypes.func.isRequired,
72+
value: PropTypes.string,
73+
};
74+
75+
export default LinkControl;

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export { default as DateTimeControl } from './components/DateTimeControl';
66
export { default as ImageControl } from './components/ImageControl';
77
export { default as InnerBlockSlider } from './components/InnerBlockSlider';
88
export { default as InnerBlocksDisplaySingle } from './components/InnerBlockSlider/inner-block-single-display';
9+
export { default as LinkControl } from './components/LinkControl';
910
export { default as LinkToolbar } from './components/LinkToolbar';
1011
export { default as PlainTextWithLimit } from './components/PlainTextWithLimit';
1112
export { default as PostTitleControl } from './components/PostTitleControl';

0 commit comments

Comments
 (0)