From 66c211594f289d7d18bd665de626a96a25fa4f3d Mon Sep 17 00:00:00 2001 From: Gerardo Pacheco Date: Wed, 24 Feb 2021 12:35:15 +0100 Subject: [PATCH] Mobile - Buttons block - Fix issues with full width and safe area --- .../block-library/src/button/edit.native.js | 20 +++++++++---------- .../src/button/editor.native.scss | 12 +++++++++++ .../block-library/src/buttons/edit.native.js | 10 ++++++---- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/packages/block-library/src/button/edit.native.js b/packages/block-library/src/button/edit.native.js index c5be2be1d9608..a1272620734bd 100644 --- a/packages/block-library/src/button/edit.native.js +++ b/packages/block-library/src/button/edit.native.js @@ -40,6 +40,13 @@ const MIN_BORDER_RADIUS_VALUE = 0; const MAX_BORDER_RADIUS_VALUE = 50; const INITIAL_MAX_WIDTH = 108; const MIN_WIDTH = 40; +// Map of the percentage width to pixel subtraction that make the buttons fit nicely into columns. +const MIN_WIDTH_MARGINS = { + 100: 0, + 75: styles.button75.marginLeft, + 50: styles.button50.marginLeft, + 25: styles.button25.marginLeft, +}; function WidthPanel( { selectedWidth, setAttributes } ) { function handleChange( newWidth ) { @@ -376,17 +383,10 @@ class ButtonEdit extends Component { ? MIN_WIDTH : placeholderTextWidth; if ( width ) { - // Map of the percentage width to pixel subtraction that make the buttons fit nicely into columns. - const minWidthSubtractionMap = { - 100: 0, - 75: 11, - 50: 22, - 25: 33, - }; // Set the width of the button. - minWidth = - Math.floor( maxWidth * ( width / 100 ) ) - - minWidthSubtractionMap[ width ]; + minWidth = Math.floor( + maxWidth * ( width / 100 ) - MIN_WIDTH_MARGINS[ width ] + ); } // To achieve proper expanding and shrinking `RichText` on Android, there is a need to set // a `placeholder` as an empty string when `RichText` is focused, diff --git a/packages/block-library/src/button/editor.native.scss b/packages/block-library/src/button/editor.native.scss index 31ce81004e09a..64d3a129e194f 100644 --- a/packages/block-library/src/button/editor.native.scss +++ b/packages/block-library/src/button/editor.native.scss @@ -56,3 +56,15 @@ $block-spacing: 4px; padding-left: 0; padding-right: 0; } + +.button75 { + margin: $solid-border-space - $block-selected-border-width; +} + +.button50 { + margin: $solid-border-space * 2 - $block-selected-border-width * 2; +} + +.button25 { + margin: $block-edge-to-content * 2 + $block-selected-border-width; +} diff --git a/packages/block-library/src/buttons/edit.native.js b/packages/block-library/src/buttons/edit.native.js index 9d887c00b6c95..788aa3c127cbd 100644 --- a/packages/block-library/src/buttons/edit.native.js +++ b/packages/block-library/src/buttons/edit.native.js @@ -2,7 +2,7 @@ * External dependencies */ import { debounce } from 'lodash'; -import { View, Dimensions } from 'react-native'; +import { View } from 'react-native'; /** * WordPress dependencies @@ -75,9 +75,11 @@ export default function ButtonsEdit( { useEffect( () => { const { width } = sizes || {}; const { isFullWidth } = alignmentHelpers; - const screenWidth = Math.floor( Dimensions.get( 'window' ).width ); - const isFullWidthBlock = isFullWidth( align ); - setMaxWidth( isFullWidthBlock ? screenWidth : width ); + + if ( width ) { + const isFullWidthBlock = isFullWidth( align ); + setMaxWidth( isFullWidthBlock ? blockWidth : width ); + } }, [ sizes, align ] ); const onAddNextButton = useCallback(