Skip to content

Commit

Permalink
InputControl: Decrease large default padding if has prefix/suffix (#4…
Browse files Browse the repository at this point in the history
…2166)

* InputControl: Decrease padding when prefix/suffix

* UnitControl: Remove padding override

* UnitControl: Clean up unused type

* Tweak story examples for better RTL support

* Add changelog

* Update snapshot

* Add comment

* Fix typo

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

Co-authored-by: Aaron Robertshaw <[email protected]>
  • Loading branch information
mirka and aaronrobertshaw authored Jul 14, 2022
1 parent c240a35 commit a979537
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 18 deletions.
4 changes: 4 additions & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

- `ToggleGroupControl`: Add large size variant ([#42008](https://github.com/WordPress/gutenberg/pull/42008/)).

### Enhancements

- `InputControl`: Ensure that the padding between a `prefix`/`suffix` and the text input stays at a reasonable 8px, even in larger size variants ([#42166](https://github.com/WordPress/gutenberg/pull/42166)).

### Internal

- `Grid`: Convert to TypeScript ([#41923](https://github.com/WordPress/gutenberg/pull/41923)).
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/input-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { useState, forwardRef } from '@wordpress/element';
import InputBase from './input-base';
import InputField from './input-field';
import type { InputControlProps } from './types';
import { space } from '../ui/utils/space';
import { useDraft } from './utils';

const noop = () => {};
Expand Down Expand Up @@ -85,6 +86,8 @@ export function UnforwardedInputControl(
isPressEnterToChange={ isPressEnterToChange }
onKeyDown={ onKeyDown }
onValidate={ onValidate }
paddingInlineStart={ prefix ? space( 2 ) : undefined }
paddingInlineEnd={ suffix ? space( 2 ) : undefined }
ref={ ref }
setIsFocused={ setIsFocused }
size={ size }
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/input-control/stories/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ Default.args = {
export const WithPrefix = Template.bind( {} );
WithPrefix.args = {
...Default.args,
prefix: <span style={ { paddingLeft: 8 } }>@</span>,
prefix: <span style={ { marginInlineStart: 8 } }>@</span>,
};

export const WithSuffix = Template.bind( {} );
WithSuffix.args = {
...Default.args,
suffix: <button style={ { marginRight: 4 } }>Send</button>,
suffix: <button style={ { marginInlineEnd: 4 } }>Send</button>,
};

export const WithSideLabel = Template.bind( {} );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ type InputProps = {
inputSize?: Size;
isDragging?: boolean;
dragCursor?: CSSProperties[ 'cursor' ];
paddingInlineStart?: CSSProperties[ 'paddingInlineStart' ];
paddingInlineEnd?: CSSProperties[ 'paddingInlineEnd' ];
};

const disabledStyles = ( { disabled }: InputProps ) => {
Expand Down Expand Up @@ -145,6 +147,7 @@ const sizeStyles = ( {
inputSize: size,
__next36pxDefaultSize,
}: InputProps ) => {
// Paddings may be overridden by the custom paddings props.
const sizes = {
default: {
height: 36,
Expand Down Expand Up @@ -184,6 +187,13 @@ const sizeStyles = ( {
return css( style );
};

const customPaddings = ( {
paddingInlineStart,
paddingInlineEnd,
}: InputProps ) => {
return css( { paddingInlineStart, paddingInlineEnd } );
};

const dragStyles = ( { isDragging, dragCursor }: InputProps ) => {
let defaultArrowStyles: SerializedStyles | undefined;
let activeDragCursorStyles: SerializedStyles | undefined;
Expand Down Expand Up @@ -235,6 +245,7 @@ export const Input = styled.input< InputProps >`
${ disabledStyles }
${ fontSizeStyles }
${ sizeStyles }
${ customPaddings }
&::-webkit-input-placeholder {
line-height: normal;
Expand Down
13 changes: 10 additions & 3 deletions packages/components/src/input-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ export interface InputFieldProps extends BaseProps {
nextValue: string,
event?: SyntheticEvent< HTMLInputElement >
) => void;
paddingInlineStart?: CSSProperties[ 'paddingInlineStart' ];
paddingInlineEnd?: CSSProperties[ 'paddingInlineEnd' ];
setIsFocused: ( isFocused: boolean ) => void;
stateReducer?: StateReducer;
/**
Expand Down Expand Up @@ -152,12 +154,17 @@ export interface InputControlProps
* be the only prefix prop. Otherwise it tries to do a union of the two prefix properties and you end up
* with an unresolvable type.
*
* `isFocused` and `setIsFocused` are managed internally by the InputControl, but the rest of the props
* for InputField are passed through.
* `isFocused`, `setIsFocused`, `paddingInlineStart`, and `paddingInlineEnd` are managed internally by
* the InputControl, but the rest of the props for InputField are passed through.
*/
Omit<
WordPressComponentProps< InputFieldProps, 'input', false >,
'stateReducer' | 'prefix' | 'isFocused' | 'setIsFocused'
| 'stateReducer'
| 'prefix'
| 'isFocused'
| 'setIsFocused'
| 'paddingInlineStart'
| 'paddingInlineEnd'
> {
__unstableStateReducer?: InputFieldProps[ 'stateReducer' ];
}
Expand Down
10 changes: 0 additions & 10 deletions packages/components/src/unit-control/styles/unit-control-styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,13 @@ type SelectProps = {

type InputProps = {
disableUnits?: boolean;
size: SelectSize;
};

export const Root = styled.div`
box-sizing: border-box;
position: relative;
`;

const paddingStyles = ( { disableUnits }: InputProps ) => {
if ( disableUnits ) return '';

return css`
${ rtl( { paddingRight: 8 } )() };
`;
};

const arrowStyles = ( { disableUnits }: InputProps ) => {
if ( disableUnits ) return '';

Expand All @@ -58,7 +49,6 @@ export const ValueInput = styled( NumberControl )`
width: 100%;
${ arrowStyles };
${ paddingStyles };
}
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Snapshot Diff:
class="components-unit-control-wrapper css-aa2xc3-Root e1bagdl33"
>
<div
- class="components-flex components-input-control components-number-control components-unit-control e1bagdl32 ep48uk90 em5sgkm7 css-18wzek1-View-Flex-sx-Base-sx-Items-ItemsColumn-Root-rootLabelPositionStyles-Input-ValueInput-arrowStyles-paddingStyles em57xhy0"
+ class="components-flex components-input-control components-number-control components-unit-control hello e1bagdl32 ep48uk90 em5sgkm7 css-18wzek1-View-Flex-sx-Base-sx-Items-ItemsColumn-Root-rootLabelPositionStyles-Input-ValueInput-arrowStyles-paddingStyles em57xhy0"
- class="components-flex components-input-control components-number-control components-unit-control e1bagdl32 ep48uk90 em5sgkm7 css-1njfmeu-View-Flex-sx-Base-sx-Items-ItemsColumn-Root-rootLabelPositionStyles-Input-ValueInput-arrowStyles em57xhy0"
+ class="components-flex components-input-control components-number-control components-unit-control hello e1bagdl32 ep48uk90 em5sgkm7 css-1njfmeu-View-Flex-sx-Base-sx-Items-ItemsColumn-Root-rootLabelPositionStyles-Input-ValueInput-arrowStyles em57xhy0"
data-wp-c16t="true"
data-wp-component="Flex"
>
Expand All @@ -22,7 +22,7 @@ Snapshot Diff:
>
<input
autocomplete="off"
class="components-input-control__input css-gzm6eu-Input-dragStyles-fontSizeStyles-sizeStyles em5sgkm5"
class="components-input-control__input css-1vhigq9-Input-dragStyles-fontSizeStyles-sizeStyles-customPaddings em5sgkm5"
- id="inspector-input-control-1"
+ id="inspector-input-control-2"
inputmode="numeric"
Expand Down

0 comments on commit a979537

Please sign in to comment.