Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RN: Add width settings to button block #28472

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 49 additions & 2 deletions packages/block-library/src/button/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ToolbarGroup,
ToolbarButton,
LinkSettingsNavigation,
SelectControl,
} from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withSelect, withDispatch } from '@wordpress/data';
Expand All @@ -39,6 +40,41 @@ const MAX_BORDER_RADIUS_VALUE = 50;
const INITIAL_MAX_WIDTH = 108;
const MIN_WIDTH = 40;

function WidthPanel( { selectedWidth, setAttributes } ) {
function handleChange( newWidth ) {
// Check if we are toggling the width off
let width = selectedWidth === newWidth ? undefined : newWidth;
if ( newWidth === 'auto' ) {
width = undefined;
}
// Update attributes
setAttributes( { width } );
}

const options = [
{ value: 'auto', label: __( 'Auto' ) },
{ value: '25', label: '25%' },
{ value: '50', label: '50%' },
{ value: '75', label: '75%' },
{ value: '100', label: '100%' },
];

if ( ! selectedWidth ) {
selectedWidth = 'auto';
}

return (
<PanelBody title={ __( 'Width Settings' ) }>
<SelectControl
label={ __( 'Width' ) }
value={ selectedWidth }
onChange={ handleChange }
options={ options }
/>
</PanelBody>
);
}

class ButtonEdit extends Component {
constructor( props ) {
super( props );
Expand Down Expand Up @@ -293,8 +329,9 @@ class ButtonEdit extends Component {
onReplace,
mergeBlocks,
parentWidth,
setAttributes,
} = this.props;
const { placeholder, text, borderRadius, url } = attributes;
const { placeholder, text, borderRadius, url, width } = attributes;
const { maxWidth, isButtonFocused, placeholderTextWidth } = this.state;
const { paddingTop: spacing, borderWidth } = styles.defaultButton;

Expand All @@ -313,13 +350,19 @@ class ButtonEdit extends Component {
// To achieve proper expanding and shrinking `RichText` on iOS, there is a need to set a `minWidth`
// value at least on 1 when `RichText` is focused or when is not focused, but `RichText` value is
// different than empty string.
const minWidth =
let minWidth =
isButtonFocused || ( ! isButtonFocused && text && text !== '' )
? MIN_WIDTH
: placeholderTextWidth;
// 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,
// because `AztecView` is calculating a `minWidth` based on placeholder text.
// console.log( minWidth, maxWidth, width )

if ( width ) {
minWidth = Math.floor( maxWidth * ( width / 100 ) );
}

const placeholderText =
isButtonFocused || ( ! isButtonFocused && text && text !== '' )
? ''
Expand Down Expand Up @@ -408,6 +451,10 @@ class ButtonEdit extends Component {
onChange={ this.onChangeBorderRadius }
/>
</PanelBody>
<WidthPanel
selectedWidth={ width }
setAttributes={ setAttributes }
/>
<PanelBody title={ __( 'Link Settings' ) }>
{ this.getLinkSettings( true ) }
</PanelBody>
Expand Down
8 changes: 8 additions & 0 deletions packages/components/src/unit-control/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function UnitControl( {
unit,
getStylesFromColorScheme,
decimalNum,
disabledUnits,
...props
} ) {
const pickerRef = useRef();
Expand Down Expand Up @@ -90,6 +91,13 @@ function UnitControl( {
: undefined;

const renderUnitPicker = () => {
if ( disabledUnits ) {
return (
<View style={ styles.unitText }>
<Text>{ unit }</Text>
</View>
);
}
return (
<View style={ styles.unitMenu } ref={ anchorNodeRef }>
{ renderUnitButton() }
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/unit-control/style.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@
justify-content: center;
align-items: center;
}

.unitText {
justify-content: center;
align-items: center;
padding: 0 2px;
}