Skip to content

Commit

Permalink
Add margin-bottom lint rules for BaseControl (#64355)
Browse files Browse the repository at this point in the history
* Fix in Gallery block

* Fix in Latest Posts block

* Fix in Search block

* Fix in Tag Cloud block

* Fix in Video block

* Fix in Global Styles Font Size

* Fix in Global Styles PushChangesToGlobalStylesControl

* Fix in new template part modal

* Fix in pattern overrides block inspector

* Add lint rule

* Update docs

Co-authored-by: mirka <[email protected]>
Co-authored-by: tyxla <[email protected]>
  • Loading branch information
3 people authored and getdave committed Aug 14, 2024
1 parent 519b2c2 commit 0bc3042
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 92 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ module.exports = {
...restrictedSyntax,
...restrictedSyntaxComponents,
...[
'BaseControl',
'CheckboxControl',
'ComboboxControl',
'DimensionControl',
Expand Down
5 changes: 4 additions & 1 deletion packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,10 @@ export default function GalleryEdit( props ) {
/>
) }
{ Platform.isWeb && ! imageSizeOptions && hasImageIds && (
<BaseControl className="gallery-image-sizes">
<BaseControl
className="gallery-image-sizes"
__nextHasNoMarginBottom
>
<BaseControl.VisualLabel>
{ __( 'Resolution' ) }
</BaseControl.VisualLabel>
Expand Down
18 changes: 9 additions & 9 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
ToolbarButton,
ResizableBox,
PanelBody,
BaseControl,
__experimentalVStack as VStack,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
} from '@wordpress/components';
Expand Down Expand Up @@ -408,12 +408,14 @@ export default function SearchEdit( {

<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<BaseControl
label={ __( 'Width' ) }
id={ unitControlInputId }
<VStack
className="wp-block-search__inspector-controls"
spacing={ 4 }
>
<UnitControl
id={ unitControlInputId }
__next40pxDefaultSize
label={ __( 'Width' ) }
id={ unitControlInputId } // unused, kept for backwards compatibility
min={
isPercentageUnit( widthUnit ) ? 0 : MIN_WIDTH
}
Expand All @@ -427,7 +429,6 @@ export default function SearchEdit( {
parseInt( newWidth, 10 ) > 100
? 100
: newWidth;

setAttributes( {
width: parseInt( filteredWidth, 10 ),
} );
Expand All @@ -445,9 +446,8 @@ export default function SearchEdit( {
value={ `${ width }${ widthUnit }` }
units={ units }
/>

<ButtonGroup
className="wp-block-search__components-button-group"
className="wp-block-search__components-button-group" // unused, kept for backwards compatibility
aria-label={ __( 'Percentage Width' ) }
>
{ [ 25, 50, 75, 100 ].map( ( widthValue ) => {
Expand All @@ -473,7 +473,7 @@ export default function SearchEdit( {
);
} ) }
</ButtonGroup>
</BaseControl>
</VStack>
</PanelBody>
</InspectorControls>
</>
Expand Down
7 changes: 5 additions & 2 deletions packages/block-library/src/search/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@
justify-content: center;
text-align: center;
}
}

&__components-button-group {
margin-top: 10px;
.wp-block-search__inspector-controls {
.components-base-control {
// Counteract the margin added by the block inspector.
margin-bottom: 0;
}
}
69 changes: 36 additions & 33 deletions packages/block-library/src/tag-cloud/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
__experimentalUnitControl as UnitControl,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
__experimentalVStack as VStack,
Disabled,
BaseControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
Expand Down Expand Up @@ -119,17 +119,20 @@ function TagCloudEdit( { attributes, setAttributes } ) {
const inspectorControls = (
<InspectorControls>
<PanelBody title={ __( 'Settings' ) }>
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Taxonomy' ) }
options={ getTaxonomyOptions() }
value={ taxonomy }
onChange={ ( selectedTaxonomy ) =>
setAttributes( { taxonomy: selectedTaxonomy } )
}
/>
<BaseControl>
<VStack
spacing={ 4 }
className="wp-block-tag-cloud__inspector-settings"
>
<SelectControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Taxonomy' ) }
options={ getTaxonomyOptions() }
value={ taxonomy }
onChange={ ( selectedTaxonomy ) =>
setAttributes( { taxonomy: selectedTaxonomy } )
}
/>
<Flex gap={ 4 }>
<FlexItem isBlock>
<UnitControl
Expand Down Expand Up @@ -164,27 +167,27 @@ function TagCloudEdit( { attributes, setAttributes } ) {
/>
</FlexItem>
</Flex>
</BaseControl>
<RangeControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Number of tags' ) }
value={ numberOfTags }
onChange={ ( value ) =>
setAttributes( { numberOfTags: value } )
}
min={ MIN_TAGS }
max={ MAX_TAGS }
required
/>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show tag counts' ) }
checked={ showTagCounts }
onChange={ () =>
setAttributes( { showTagCounts: ! showTagCounts } )
}
/>
<RangeControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Number of tags' ) }
value={ numberOfTags }
onChange={ ( value ) =>
setAttributes( { numberOfTags: value } )
}
min={ MIN_TAGS }
max={ MAX_TAGS }
required
/>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Show tag counts' ) }
checked={ showTagCounts }
onChange={ () =>
setAttributes( { showTagCounts: ! showTagCounts } )
}
/>
</VStack>
</PanelBody>
</InspectorControls>
);
Expand Down
8 changes: 8 additions & 0 deletions packages/block-library/src/tag-cloud/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@
border: none;
border-radius: inherit;
}

.wp-block-tag-cloud__inspector-settings {
.components-base-control,
.components-base-control:last-child {
// Cancel out extra margins added by block inspector
margin-bottom: 0;
}
}
1 change: 1 addition & 0 deletions packages/block-library/src/video/edit-common-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const VideoSettings = ( { setAttributes, attributes } ) => {
) }
/>
<SelectControl
__next40pxDefaultSize
__nextHasNoMarginBottom
label={ __( 'Preload' ) }
value={ preload }
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/video/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function VideoEdit( {
attributes={ attributes }
/>
<MediaUploadCheck>
<BaseControl className="editor-video-poster-control">
<div className="editor-video-poster-control">
<BaseControl.VisualLabel>
{ __( 'Poster image' ) }
</BaseControl.VisualLabel>
Expand Down Expand Up @@ -265,7 +265,7 @@ function VideoEdit( {
{ __( 'Remove' ) }
</Button>
) }
</BaseControl>
</div>
</MediaUploadCheck>
</PanelBody>
</InspectorControls>
Expand Down
7 changes: 5 additions & 2 deletions packages/components/src/base-control/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const MyCustomTextareaControl = ({ children, ...baseProps }) => (
const { baseControlProps, controlProps } = useBaseControlProps( baseProps );

return (
<BaseControl { ...baseControlProps } __nextHasNoMarginBottom={ true }>
<BaseControl { ...baseControlProps } __nextHasNoMarginBottom>
<textarea { ...controlProps }>
{ children }
</textarea>
Expand Down Expand Up @@ -92,7 +92,10 @@ It should only be used in cases where the children being rendered inside BaseCon
import { BaseControl } from '@wordpress/components';

const MyBaseControl = () => (
<BaseControl help="This button is already accessibly labeled.">
<BaseControl
__nextHasNoMarginBottom
help="This button is already accessibly labeled."
>
<BaseControl.VisualLabel>Author</BaseControl.VisualLabel>
<Button>Select an author</Button>
</BaseControl>
Expand Down
89 changes: 48 additions & 41 deletions packages/components/src/base-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,6 @@ import { contextConnectWithoutRef, useContextSystem } from '../context';

export { useBaseControlProps } from './hooks';

/**
* `BaseControl` is a component used to generate labels and help text for components handling user inputs.
*
* ```jsx
* import { BaseControl, useBaseControlProps } from '@wordpress/components';
*
* // Render a `BaseControl` for a textarea input
* const MyCustomTextareaControl = ({ children, ...baseProps }) => (
* // `useBaseControlProps` is a convenience hook to get the props for the `BaseControl`
* // and the inner control itself. Namely, it takes care of generating a unique `id`,
* // properly associating it with the `label` and `help` elements.
* const { baseControlProps, controlProps } = useBaseControlProps( baseProps );
*
* return (
* <BaseControl { ...baseControlProps } __nextHasNoMarginBottom={ true }>
* <textarea { ...controlProps }>
* { children }
* </textarea>
* </BaseControl>
* );
* );
* ```
*/
const UnconnectedBaseControl = (
props: WordPressComponentProps< BaseControlProps, null >
) => {
Expand Down Expand Up @@ -105,23 +82,6 @@ const UnconnectedBaseControl = (
);
};

/**
* `BaseControl.VisualLabel` is used to render a purely visual label inside a `BaseControl` component.
*
* It should only be used in cases where the children being rendered inside `BaseControl` are already accessibly labeled,
* e.g., a button, but we want an additional visual label for that section equivalent to the labels `BaseControl` would
* otherwise use if the `label` prop was passed.
*
* @example
* import { BaseControl } from '@wordpress/components';
*
* const MyBaseControl = () => (
* <BaseControl help="This button is already accessibly labeled.">
* <BaseControl.VisualLabel>Author</BaseControl.VisualLabel>
* <Button>Select an author</Button>
* </BaseControl>
* );
*/
const UnforwardedVisualLabel = (
props: WordPressComponentProps< BaseControlVisualLabelProps, 'span' >,
ref: ForwardedRef< any >
Expand All @@ -141,9 +101,56 @@ const UnforwardedVisualLabel = (

export const VisualLabel = forwardRef( UnforwardedVisualLabel );

/**
* `BaseControl` is a component used to generate labels and help text for components handling user inputs.
*
* ```jsx
* import { BaseControl, useBaseControlProps } from '@wordpress/components';
*
* // Render a `BaseControl` for a textarea input
* const MyCustomTextareaControl = ({ children, ...baseProps }) => (
* // `useBaseControlProps` is a convenience hook to get the props for the `BaseControl`
* // and the inner control itself. Namely, it takes care of generating a unique `id`,
* // properly associating it with the `label` and `help` elements.
* const { baseControlProps, controlProps } = useBaseControlProps( baseProps );
*
* return (
* <BaseControl { ...baseControlProps } __nextHasNoMarginBottom>
* <textarea { ...controlProps }>
* { children }
* </textarea>
* </BaseControl>
* );
* );
* ```
*/
export const BaseControl = Object.assign(
contextConnectWithoutRef( UnconnectedBaseControl, 'BaseControl' ),
{ VisualLabel }

{
/**
* `BaseControl.VisualLabel` is used to render a purely visual label inside a `BaseControl` component.
*
* It should only be used in cases where the children being rendered inside `BaseControl` are already accessibly labeled,
* e.g., a button, but we want an additional visual label for that section equivalent to the labels `BaseControl` would
* otherwise use if the `label` prop was passed.
*
* ```jsx
* import { BaseControl } from '@wordpress/components';
*
* const MyBaseControl = () => (
* <BaseControl
* __nextHasNoMarginBottom
* help="This button is already accessibly labeled."
* >
* <BaseControl.VisualLabel>Author</BaseControl.VisualLabel>
* <Button>Select an author</Button>
* </BaseControl>
* );
* ```
*/
VisualLabel,
}
);

export default BaseControl;
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ import {

const DEFAULT_UNITS = [ 'px', 'em', 'rem', 'vw', 'vh' ];

function SizeControl( props ) {
function SizeControl( {
// Do not allow manipulation of margin bottom
__nextHasNoMarginBottom,
...props
} ) {
const { baseControlProps } = useBaseControlProps( props );
const { value, onChange, fallbackValue, disabled } = props;

Expand All @@ -45,7 +49,7 @@ function SizeControl( props ) {
};

return (
<BaseControl { ...baseControlProps }>
<BaseControl { ...baseControlProps } __nextHasNoMarginBottom>
<Flex>
<FlexItem isBlock>
<UnitControl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ function PushChangesToGlobalStylesControl( {

return (
<BaseControl
__nextHasNoMarginBottom
className="edit-site-push-changes-to-global-styles-control"
help={ sprintf(
// translators: %s: Title of the block e.g. 'Heading'.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ export function CreateTemplatePartModalContents( {
required
/>
<BaseControl
__nextHasNoMarginBottom
label={ __( 'Area' ) }
id={ `editor-create-template-part-modal__area-selection-${ instanceId }` }
className="editor-create-template-part-modal__area-base-control"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function PatternOverridesControls( {
<>
<InspectorControls group="advanced">
<BaseControl
__nextHasNoMarginBottom
id={ controlId }
label={ __( 'Overrides' ) }
help={ helpText }
Expand Down

0 comments on commit 0bc3042

Please sign in to comment.