| 1 |
/** |
| 2 |
* WordPress dependencies |
| 3 |
*/ |
| 4 |
import { Flex, FlexBlock, FlexItem, FormToggle } from '@wordpress/components'; |
| 5 |
import { useInstanceId } from '@wordpress/compose'; |
| 6 |
|
| 7 |
/** |
| 8 |
* BeyondWords toggle control: switch on the left, clickable label to its right. |
| 9 |
* |
| 10 |
* @param {Object} props |
| 11 |
* @param {string} [props.className] Extra class for the Flex wrapper. |
| 12 |
* @param {string} props.label Visible, clickable label. |
| 13 |
* @param {boolean} props.checked Whether the toggle is on. |
| 14 |
* @param {Function} props.onChange Change handler. |
| 15 |
* @param {boolean} [props.disabled] Whether the toggle is disabled. |
| 16 |
* |
| 17 |
* @return {Element} The toggle. |
| 18 |
*/ |
| 19 |
export function Toggle( { |
| 20 |
className, |
| 21 |
label, |
| 22 |
checked, |
| 23 |
onChange, |
| 24 |
disabled = false, |
| 25 |
} ) { |
| 26 |
const instanceId = useInstanceId( Toggle, 'beyondwords-toggle' ); |
| 27 |
|
| 28 |
const classes = [ 'beyondwords-toggle', className ] |
| 29 |
.filter( Boolean ) |
| 30 |
.join( ' ' ); |
| 31 |
|
| 32 |
return ( |
| 33 |
<Flex className={ classes } justify="flex-start" align="center"> |
| 34 |
<FlexItem> |
| 35 |
<FormToggle |
| 36 |
id={ instanceId } |
| 37 |
checked={ checked } |
| 38 |
onChange={ onChange } |
| 39 |
disabled={ disabled } |
| 40 |
/> |
| 41 |
</FlexItem> |
| 42 |
<FlexBlock> |
| 43 |
<label htmlFor={ instanceId }>{ label }</label> |
| 44 |
</FlexBlock> |
| 45 |
</Flex> |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
export default Toggle; |
| 50 |
|