index.js
269 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import PanelArea from '../../../../components/panel-area'; |
| 3 | import getIcon from '../../../../utils/get-icon'; |
| 4 | import { useContext } from '@wordpress/element'; |
| 5 | import ControlsContext from '../../../../block-context'; |
| 6 | import LayoutControl from './components/LayoutControl'; |
| 7 | import Display from './components/Display'; |
| 8 | import isFlexItem from '../../../../utils/is-flex-item'; |
| 9 | import getAttribute from '../../../../utils/get-attribute'; |
| 10 | import getResponsivePlaceholder from '../../../../utils/get-responsive-placeholder'; |
| 11 | import FlexDirection from './components/FlexDirection'; |
| 12 | import LegacyLayoutControls from '../../../../blocks/container/components/LegacyLayoutControls'; |
| 13 | import ZIndex from './components/ZIndex'; |
| 14 | import FlexChild from '../flex-child-panel'; |
| 15 | import MigrateInnerContainer from '../../../../components/migrate-inner-container'; |
| 16 | import UnitControl from '../../../../components/unit-control'; |
| 17 | import { SelectControl } from '@wordpress/components'; |
| 18 | import { positionOptions, overflowOptions } from './options'; |
| 19 | import FlexControl from '../../../../components/flex-control'; |
| 20 | import getDeviceType from '../../../../utils/get-device-type'; |
| 21 | import ThemeWidth from './components/ThemeWidth'; |
| 22 | |
| 23 | export default function Layout( { attributes, setAttributes } ) { |
| 24 | const device = getDeviceType(); |
| 25 | const { id, supports: { layout, flexChildPanel } } = useContext( ControlsContext ); |
| 26 | |
| 27 | const componentProps = { |
| 28 | attributes, |
| 29 | deviceType: device, |
| 30 | }; |
| 31 | |
| 32 | const { |
| 33 | display, |
| 34 | displayTablet, |
| 35 | displayMobile, |
| 36 | useInnerContainer, |
| 37 | zindex, |
| 38 | innerZindex, |
| 39 | align, |
| 40 | } = attributes; |
| 41 | |
| 42 | const directionValue = getAttribute( 'flexDirection', componentProps ) || getResponsivePlaceholder( 'flexDirection', attributes, device, 'row' ); |
| 43 | |
| 44 | return ( |
| 45 | <PanelArea |
| 46 | title={ __( 'Layout', 'generateblocks' ) } |
| 47 | initialOpen={ false } |
| 48 | icon={ getIcon( 'layout' ) } |
| 49 | className="gblocks-panel-label" |
| 50 | id={ `${ id }Layout` } |
| 51 | > |
| 52 | { !! useInnerContainer && |
| 53 | <LegacyLayoutControls |
| 54 | attributes={ attributes } |
| 55 | setAttributes={ setAttributes } |
| 56 | deviceType={ device } |
| 57 | blockDefaults={ generateBlocksDefaults.container } |
| 58 | /> |
| 59 | } |
| 60 | |
| 61 | { layout.display && ! useInnerContainer && |
| 62 | <Display |
| 63 | value={ getAttribute( 'display', componentProps ) } |
| 64 | onChange={ ( nextDisplay ) => setAttributes( { |
| 65 | [ getAttribute( 'display', componentProps, true ) ]: nextDisplay, |
| 66 | } ) } |
| 67 | /> |
| 68 | } |
| 69 | |
| 70 | { isFlexItem( { device, display, displayTablet, displayMobile } ) && ! useInnerContainer && |
| 71 | <> |
| 72 | { layout.flexDirection && |
| 73 | <FlexDirection |
| 74 | value={ getAttribute( 'flexDirection', componentProps ) } |
| 75 | onChange={ ( value ) => { |
| 76 | const currentDirection = getAttribute( 'flexDirection', componentProps ); |
| 77 | value = currentDirection.includes( 'reverse' ) ? value + '-reverse' : value; |
| 78 | |
| 79 | setAttributes( { |
| 80 | [ getAttribute( 'flexDirection', componentProps, true ) ]: value !== getAttribute( 'flexDirection', componentProps ) ? value : '', |
| 81 | } ); |
| 82 | } } |
| 83 | onReverse={ ( value ) => { |
| 84 | if ( '' === value ) { |
| 85 | value = 'row'; |
| 86 | } |
| 87 | |
| 88 | value = value.includes( 'reverse' ) ? value.replace( '-reverse', '' ) : value + '-reverse'; |
| 89 | |
| 90 | setAttributes( { |
| 91 | [ getAttribute( 'flexDirection', componentProps, true ) ]: value, |
| 92 | } ); |
| 93 | } } |
| 94 | label={ __( 'Direction', 'generateblocks' ) } |
| 95 | directionValue={ directionValue } |
| 96 | fallback={ getResponsivePlaceholder( 'flexDirection', attributes, device, 'row' ) } |
| 97 | /> |
| 98 | } |
| 99 | |
| 100 | { layout.alignItems && |
| 101 | <LayoutControl |
| 102 | value={ getAttribute( 'alignItems', componentProps ) } |
| 103 | onChange={ ( value ) => setAttributes( { |
| 104 | [ getAttribute( 'alignItems', componentProps, true ) ]: value !== getAttribute( 'alignItems', componentProps ) ? value : '', |
| 105 | } ) } |
| 106 | label={ __( 'Align Items', 'generateblocks' ) } |
| 107 | attributeName="alignItems" |
| 108 | directionValue={ directionValue } |
| 109 | fallback={ getResponsivePlaceholder( 'alignItems', attributes, device, '' ) } |
| 110 | /> |
| 111 | } |
| 112 | |
| 113 | { layout.justifyContent && |
| 114 | <LayoutControl |
| 115 | value={ getAttribute( 'justifyContent', componentProps ) } |
| 116 | onChange={ ( value ) => setAttributes( { |
| 117 | [ getAttribute( 'justifyContent', componentProps, true ) ]: value !== getAttribute( 'justifyContent', componentProps ) ? value : '', |
| 118 | } ) } |
| 119 | label={ __( 'Justify Content', 'generateblocks' ) } |
| 120 | attributeName="justifyContent" |
| 121 | directionValue={ directionValue } |
| 122 | fallback={ getResponsivePlaceholder( 'justifyContent', attributes, device, '' ) } |
| 123 | /> |
| 124 | } |
| 125 | |
| 126 | { layout.flexWrap && |
| 127 | <LayoutControl |
| 128 | value={ getAttribute( 'flexWrap', componentProps ) } |
| 129 | onChange={ ( value ) => setAttributes( { |
| 130 | [ getAttribute( 'flexWrap', componentProps, true ) ]: value !== getAttribute( 'flexWrap', componentProps ) ? value : '', |
| 131 | } ) } |
| 132 | label={ __( 'Wrap', 'generateblocks' ) } |
| 133 | attributeName="flexWrap" |
| 134 | directionValue={ directionValue } |
| 135 | fallback={ getResponsivePlaceholder( 'flexWrap', attributes, device, '' ) } |
| 136 | /> |
| 137 | } |
| 138 | |
| 139 | { ( layout.columnGap || layout.rowGap ) && |
| 140 | <FlexControl> |
| 141 | { layout.columnGap && |
| 142 | <UnitControl |
| 143 | label={ __( 'Column Gap', 'generateblocks' ) } |
| 144 | id="gblocks-column-gap" |
| 145 | value={ getAttribute( 'columnGap', componentProps ) } |
| 146 | placeholder={ getResponsivePlaceholder( 'columnGap', attributes, device ) } |
| 147 | onChange={ ( value ) => setAttributes( { |
| 148 | [ getAttribute( 'columnGap', componentProps, true ) ]: value, |
| 149 | } ) } |
| 150 | /> |
| 151 | } |
| 152 | |
| 153 | { layout.rowGap && |
| 154 | <UnitControl |
| 155 | label={ __( 'Row Gap', 'generateblocks' ) } |
| 156 | id="gblocks-row-gap" |
| 157 | value={ getAttribute( 'rowGap', componentProps ) } |
| 158 | placeholder={ getResponsivePlaceholder( 'rowGap', attributes, device ) } |
| 159 | onChange={ ( value ) => setAttributes( { |
| 160 | [ getAttribute( 'rowGap', componentProps, true ) ]: value, |
| 161 | } ) } |
| 162 | /> |
| 163 | } |
| 164 | </FlexControl> |
| 165 | } |
| 166 | </> |
| 167 | } |
| 168 | |
| 169 | { ! useInnerContainer && |
| 170 | <> |
| 171 | { layout.position && |
| 172 | <SelectControl |
| 173 | label={ __( 'Position', 'generateblocks' ) } |
| 174 | value={ getAttribute( 'position', componentProps ) } |
| 175 | options={ positionOptions } |
| 176 | onChange={ ( value ) => setAttributes( { |
| 177 | [ getAttribute( 'position', componentProps, true ) ]: value, |
| 178 | } ) } |
| 179 | /> |
| 180 | } |
| 181 | |
| 182 | { layout.overflow && |
| 183 | <FlexControl> |
| 184 | <SelectControl |
| 185 | label={ __( 'Overflow-x', 'generateblocks' ) } |
| 186 | value={ getAttribute( 'overflowX', componentProps ) } |
| 187 | options={ overflowOptions } |
| 188 | onChange={ ( value ) => setAttributes( { |
| 189 | [ getAttribute( 'overflowX', componentProps, true ) ]: value, |
| 190 | } ) } |
| 191 | /> |
| 192 | |
| 193 | <SelectControl |
| 194 | label={ __( 'Overflow-y', 'generateblocks' ) } |
| 195 | value={ getAttribute( 'overflowY', componentProps ) } |
| 196 | options={ overflowOptions } |
| 197 | onChange={ ( value ) => setAttributes( { |
| 198 | [ getAttribute( 'overflowY', componentProps, true ) ]: value, |
| 199 | } ) } |
| 200 | /> |
| 201 | </FlexControl> |
| 202 | } |
| 203 | </> |
| 204 | } |
| 205 | |
| 206 | { layout.zIndex && |
| 207 | <> |
| 208 | { !! useInnerContainer && 'Desktop' === device && |
| 209 | <> |
| 210 | <ZIndex |
| 211 | label={ __( 'Outer z-index', 'generateblocks' ) } |
| 212 | value={ zindex } |
| 213 | onChange={ ( value ) => setAttributes( { zindex: value } ) } |
| 214 | /> |
| 215 | |
| 216 | <ZIndex |
| 217 | label={ __( 'Inner z-index', 'generateblocks' ) } |
| 218 | value={ innerZindex } |
| 219 | onChange={ ( value ) => setAttributes( { innerZindex: value } ) } |
| 220 | /> |
| 221 | </> |
| 222 | } |
| 223 | |
| 224 | { ! useInnerContainer && |
| 225 | <ZIndex |
| 226 | label={ __( 'z-index', 'generateblocks' ) } |
| 227 | value={ getAttribute( 'zindex', componentProps ) } |
| 228 | placeholder={ getResponsivePlaceholder( 'zindex', attributes, device ) } |
| 229 | onChange={ ( value ) => setAttributes( { |
| 230 | [ getAttribute( 'zindex', componentProps, true ) ]: value, |
| 231 | [ getAttribute( 'position', componentProps, true ) ]: ! getAttribute( 'position', componentProps ) |
| 232 | ? 'relative' |
| 233 | : getAttribute( 'position', componentProps ), |
| 234 | } ) } |
| 235 | /> |
| 236 | } |
| 237 | </> |
| 238 | } |
| 239 | |
| 240 | { layout.themeWidth && |
| 241 | <> |
| 242 | <ThemeWidth |
| 243 | value={ align } |
| 244 | onChange={ ( value ) => { |
| 245 | setAttributes( { |
| 246 | align: value, |
| 247 | } ); |
| 248 | } } |
| 249 | /> |
| 250 | </> |
| 251 | } |
| 252 | |
| 253 | { !! useInnerContainer && |
| 254 | <MigrateInnerContainer |
| 255 | attributes={ attributes } |
| 256 | setAttributes={ setAttributes } |
| 257 | /> |
| 258 | } |
| 259 | |
| 260 | { flexChildPanel.enabled && |
| 261 | <FlexChild |
| 262 | attributes={ attributes } |
| 263 | setAttributes={ setAttributes } |
| 264 | /> |
| 265 | } |
| 266 | </PanelArea> |
| 267 | ); |
| 268 | } |
| 269 |