|
| 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; |
0 commit comments