index.js
86 lines
| 1 | // Import CSS |
| 2 | import './editor.scss'; |
| 3 | |
| 4 | import { |
| 5 | Component, |
| 6 | } from '@wordpress/element'; |
| 7 | |
| 8 | import { |
| 9 | __, |
| 10 | sprintf, |
| 11 | _x, |
| 12 | } from '@wordpress/i18n'; |
| 13 | |
| 14 | import { |
| 15 | ButtonGroup, |
| 16 | Button, |
| 17 | Tooltip, |
| 18 | } from '@wordpress/components'; |
| 19 | |
| 20 | export default class UnitChooser extends Component { |
| 21 | render() { |
| 22 | const { |
| 23 | label, |
| 24 | value, |
| 25 | onClick, |
| 26 | units, |
| 27 | id, |
| 28 | } = this.props; |
| 29 | |
| 30 | return ( |
| 31 | <div className="components-gblocks-units-control-header__units"> |
| 32 | <div className="components-gblocks-units-control-label__units"> |
| 33 | { !! id ? ( |
| 34 | <label htmlFor={ id }>{ label }</label> |
| 35 | ) : ( |
| 36 | label |
| 37 | ) } |
| 38 | </div> |
| 39 | |
| 40 | <div className="components-gblocks-control__units"> |
| 41 | <ButtonGroup className="components-gblocks-control-buttons__units" aria-label={ __( 'Select Units', 'generateblocks' ) }> |
| 42 | { units.map( ( unit ) => { |
| 43 | let unitName = unit; |
| 44 | |
| 45 | if ( 'px' === unit ) { |
| 46 | unitName = _x( 'Pixel', 'A size unit for CSS markup', 'generateblocks' ); |
| 47 | } |
| 48 | |
| 49 | if ( 'em' === unit ) { |
| 50 | unitName = _x( 'Em', 'A size unit for CSS markup', 'generateblocks' ); |
| 51 | } |
| 52 | |
| 53 | if ( '%' === unit ) { |
| 54 | unitName = _x( 'Percentage', 'A size unit for CSS markup', 'generateblocks' ); |
| 55 | } |
| 56 | |
| 57 | if ( 'deg' === unit ) { |
| 58 | unitName = _x( 'Degree', 'A size unit for CSS markup', 'generateblocks' ); |
| 59 | } |
| 60 | |
| 61 | return <Tooltip |
| 62 | /* translators: Unit type (px, em, %) */ |
| 63 | text={ sprintf( __( '%s Units', 'generateblocks' ), unitName ) } |
| 64 | key={ unit } |
| 65 | > |
| 66 | <Button |
| 67 | key={ unit } |
| 68 | className={ 'components-gblocks-control-button__units--' + unit } |
| 69 | isSmall |
| 70 | isPrimary={ value === unit } |
| 71 | aria-pressed={ value === unit } |
| 72 | /* translators: %s: values associated with CSS syntax, 'Pixel', 'Em', 'Percentage' */ |
| 73 | aria-label={ sprintf( __( '%s Units', 'generateblocks' ), unitName ) } |
| 74 | onClick={ () => onClick( unit ) } |
| 75 | > |
| 76 | { unit } |
| 77 | </Button> |
| 78 | </Tooltip>; |
| 79 | } ) } |
| 80 | </ButtonGroup> |
| 81 | </div> |
| 82 | </div> |
| 83 | ); |
| 84 | } |
| 85 | } |
| 86 |