Skip to content

Commit

Permalink
Block Editor: Add new TextAlignmentControl component (#60841)
Browse files Browse the repository at this point in the history
* Block Editor: Add new `TextAlignControl` component

* Rename `TextAlignControl` to `TextAlignmentControl`

* Replate `align` with `alignment`

* Use components to reduce CSS writing

* Refactor using new SegmentedTextControl component

* Make a component private

* SegmentedTextControl: Update JSDoc parameters

* TextAlignmentControl: Update JSDoc parameter

* TextAlignmentControl: Cache valid controls filtering

* WritingModeControl: fix control property

* TextAlignmentControl: Allow `justify` as an allowed value

* TextAlignmentControl: Update docs

* SegmentedTextControl: Update description

Co-authored-by: Aaron Robertshaw <[email protected]>

* TextAlignmentControl: Update readme

Co-authored-by: Aaron Robertshaw <[email protected]>

* TextAlignmentControl: Fix JSDoc

Co-authored-by: Aaron Robertshaw <[email protected]>

* TextAlignmentControl: Update readme

Co-authored-by: Aaron Robertshaw <[email protected]>

* TextAlignmentControl: Update readme

Co-authored-by: Aaron Robertshaw <[email protected]>

* SegmentedTextControl: Update JSDoc

* TextAlignmentcontrol: remove unecessary `unlock()`

* TextAlignmentControl: Remove unnecessary styles

* Restor default component classname

* Rename `control(s)` to `option(s)`

* TextAlignmentControl: Remove README

* TextAlignmentControl: Fix control name


---------

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: noisysocks <[email protected]>
Co-authored-by: jasmussen <[email protected]>
  • Loading branch information
5 people authored Apr 26, 2024
1 parent 08a8156 commit 2c4ef30
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { BaseControl, Button } from '@wordpress/components';

/**
* @typedef {Object} Option
* @property {string} label The label of the option.
* @property {string} value The value of the option.
* @property {string} icon The icon of the option.
*/

/**
* Control to facilitate selecting a text style from a set of options.
*
* @param {Object} props Component props.
* @param {string} props.label A label for the option.
* @param {string} props.value Currently selected value.
* @param {Function} props.onChange Callback to handle onChange.
* @param {Option[]} props.options Array of options to display.
* @param {string} props.className Additional class name to apply.
*
* @return {Element} Element to render.
*/
export default function SegmentedTextControl( {
label,
value,
options,
onChange,
className,
} ) {
return (
<fieldset
className={ classnames(
'block-editor-segmented-text-control',
className
) }
>
<BaseControl.VisualLabel as="legend">
{ label }
</BaseControl.VisualLabel>
<div className="block-editor-segmented-text-control__buttons">
{ options.map( ( option ) => {
return (
<Button
size="compact"
key={ option.value }
icon={ option.icon }
label={ option.label }
isPressed={ option.value === value }
onClick={ () => onChange( option.value ) }
/>
);
} ) }
</div>
</fieldset>
);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
.block-editor-writing-mode-control {
.block-editor-segmented-text-control {
border: 0;
margin: 0;
padding: 0;

.block-editor-writing-mode-control__buttons {
.block-editor-segmented-text-control__buttons {
// 4px of padding makes the row 40px high, same as an input.
padding: $grid-unit-05 0;
display: flex;
}

.components-button.has-icon {
height: $grid-unit-40;
margin-right: $grid-unit-05;
min-width: $grid-unit-40;
padding: 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
alignLeft,
alignCenter,
alignRight,
alignJustify,
} from '@wordpress/icons';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import SegmentedTextControl from '../segmented-text-control';

const TEXT_ALIGNMENT_OPTIONS = [
{
label: __( 'Align text left' ),
value: 'left',
icon: alignLeft,
},
{
label: __( 'Align text center' ),
value: 'center',
icon: alignCenter,
},
{
label: __( 'Align text right' ),
value: 'right',
icon: alignRight,
},
{
label: __( 'Justify text' ),
value: 'justify',
icon: alignJustify,
},
];

const DEFAULT_OPTIONS = [ 'left', 'center', 'right' ];

/**
* Control to facilitate text alignment selections.
*
* @param {Object} props Component props.
* @param {string} props.className Class name to add to the control.
* @param {string} props.value Currently selected text alignment.
* @param {Function} props.onChange Handles change in text alignment selection.
* @param {string[]} props.options Array of text alignment options to display.
*
* @return {Element} Text alignment control.
*/
export default function TextAlignmentControl( {
className,
value,
onChange,
options = DEFAULT_OPTIONS,
} ) {
const validOptions = useMemo(
() =>
TEXT_ALIGNMENT_OPTIONS.filter( ( option ) =>
options.includes( option.value )
),
[ options ]
);

if ( ! validOptions.length ) {
return null;
}

return (
<SegmentedTextControl
label={ __( 'Text alignment' ) }
options={ validOptions }
className={ classnames(
'block-editor-text-alignment-control',
className
) }
value={ value }
onChange={ ( newValue ) => {
onChange( newValue === value ? undefined : newValue );
} }
/>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import TextAlignmentControl from '../';

export default {
title: 'BlockEditor/TextAlignmentControl',
component: TextAlignmentControl,
argTypes: {
onChange: { action: 'onChange' },
className: { control: 'text' },
options: {
control: 'check',
options: [ 'left', 'center', 'right', 'justify' ],
},
value: { control: { type: null } },
},
};

const Template = ( { onChange, ...args } ) => {
const [ value, setValue ] = useState();
return (
<TextAlignmentControl
{ ...args }
onChange={ ( ...changeArgs ) => {
onChange( ...changeArgs );
setValue( ...changeArgs );
} }
value={ value }
/>
);
};

export const Default = Template.bind( {} );
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,27 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { BaseControl, Button } from '@wordpress/components';
import { reset, formatStrikethrough, formatUnderline } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import SegmentedTextControl from '../segmented-text-control';

const TEXT_DECORATIONS = [
{
name: __( 'None' ),
label: __( 'None' ),
value: 'none',
icon: reset,
},
{
name: __( 'Underline' ),
label: __( 'Underline' ),
value: 'underline',
icon: formatUnderline,
},
{
name: __( 'Strikethrough' ),
label: __( 'Strikethrough' ),
value: 'line-through',
icon: formatStrikethrough,
},
Expand All @@ -31,10 +35,10 @@ const TEXT_DECORATIONS = [
/**
* Control to facilitate text decoration selections.
*
* @param {Object} props Component props.
* @param {string} props.value Currently selected text decoration.
* @param {Function} props.onChange Handles change in text decoration selection.
* @param {string} [props.className] Additional class name to apply.
* @param {Object} props Component props.
* @param {string} props.value Currently selected text decoration.
* @param {Function} props.onChange Handles change in text decoration selection.
* @param {string} props.className Additional class name to apply.
*
* @return {Element} Text decoration control.
*/
Expand All @@ -44,34 +48,17 @@ export default function TextDecorationControl( {
className,
} ) {
return (
<fieldset
<SegmentedTextControl
label={ __( 'Decoration' ) }
options={ TEXT_DECORATIONS }
className={ classnames(
'block-editor-text-decoration-control',
className
) }
>
<BaseControl.VisualLabel as="legend">
{ __( 'Decoration' ) }
</BaseControl.VisualLabel>
<div className="block-editor-text-decoration-control__buttons">
{ TEXT_DECORATIONS.map( ( textDecoration ) => {
return (
<Button
key={ textDecoration.value }
icon={ textDecoration.icon }
label={ textDecoration.name }
isPressed={ textDecoration.value === value }
onClick={ () => {
onChange(
textDecoration.value === value
? undefined
: textDecoration.value
);
} }
/>
);
} ) }
</div>
</fieldset>
value={ value }
onChange={ ( newValue ) => {
onChange( newValue === value ? undefined : newValue );
} }
/>
);
}

This file was deleted.

Loading

0 comments on commit 2c4ef30

Please sign in to comment.