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

InputBase: Add isBorderless prop #58750

Merged
merged 4 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion packages/components/src/input-control/backdrop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ import { memo } from '@wordpress/element';
*/
import { BackdropUI } from './styles/input-control-styles';

function Backdrop( { disabled = false, isFocused = false } ) {
function Backdrop( {
disabled = false,
isBorderless = false,
isFocused = false,
} ) {
return (
<BackdropUI
aria-hidden="true"
className="components-input-control__backdrop"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, we can already just use this .components-input-control__backdrop selector to override border styles from any consumer. Still, I think it's better to control this centrally because it's intertwined with the border styles in the focused and disabled states, as you can see in the input-control-styles.tsx file.

disabled={ disabled }
isBorderless={ isBorderless }
isFocused={ isFocused }
/>
);
Expand Down
19 changes: 14 additions & 5 deletions packages/components/src/input-control/input-base.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { ForwardedRef } from 'react';
* WordPress dependencies
*/
import { useInstanceId } from '@wordpress/compose';
import { forwardRef, useMemo } from '@wordpress/element';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -23,7 +23,11 @@ import {
} from './styles/input-control-styles';
import type { InputBaseProps, LabelPosition } from './types';
import type { WordPressComponentProps } from '../context';
import { ContextSystemProvider } from '../context';
import {
ContextSystemProvider,
contextConnect,
useContextSystem,
} from '../context';
import { useDeprecated36pxDefaultSizeProp } from '../utils/use-deprecated-props';

function useUniqueId( idProp?: string ) {
Expand Down Expand Up @@ -73,14 +77,15 @@ export function InputBase(
hideLabelFromVision = false,
labelPosition,
id: idProp,
isBorderless = false,
isFocused = false,
label,
prefix,
size = 'default',
suffix,
...restProps
} = useDeprecated36pxDefaultSizeProp(
props,
useContextSystem( props, 'InputBase' ),
'wp.components.InputBase',
'6.4'
);
Expand Down Expand Up @@ -138,10 +143,14 @@ export function InputBase(
</Suffix>
) }
</ContextSystemProvider>
<Backdrop disabled={ disabled } isFocused={ isFocused } />
<Backdrop
disabled={ disabled }
isBorderless={ isBorderless }
isFocused={ isFocused }
/>
</Container>
</Root>
);
}

export default forwardRef( InputBase );
export default contextConnect( InputBase, 'InputBase' );
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const Input = styled.input< InputProps >`
box-sizing: border-box;
border: none;
box-shadow: none !important;
color: ${ COLORS.gray[ 900 ] };
color: ${ COLORS.theme.foreground };
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to borderless, but I'd like to sneak in this small enhancement for better themability if that's ok 🥺

display: block;
font-family: inherit;
margin: 0;
Expand Down Expand Up @@ -263,28 +263,31 @@ export const LabelWrapper = styled( FlexItem )`

type BackdropProps = {
disabled?: boolean;
isBorderless?: boolean;
isFocused?: boolean;
};

const backdropFocusedStyles = ( {
disabled,
isBorderless,
isFocused,
}: BackdropProps ): SerializedStyles => {
let borderColor = isFocused ? COLORS.ui.borderFocus : COLORS.ui.border;
let borderColor = isBorderless ? 'transparent' : COLORS.ui.border;

let boxShadow;
let outline;
let outlineOffset;

if ( isFocused ) {
borderColor = COLORS.ui.borderFocus;
boxShadow = CONFIG.controlBoxShadowFocus;
// Windows High Contrast mode will show this outline, but not the box-shadow.
outline = `2px solid transparent`;
outlineOffset = `-2px`;
}

if ( disabled ) {
borderColor = COLORS.ui.borderDisabled;
borderColor = isBorderless ? 'transparent' : COLORS.ui.borderDisabled;
}

return css( {
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/input-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ export interface InputBaseProps extends BaseProps, FlexProps {
* If this property is added, a label will be generated using label property as the content.
*/
label?: ReactNode;
/**
* Whether to hide the border when not focused.
*
* @default false
*/
isBorderless?: boolean;
Comment on lines +179 to +184
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This prop shouldn't be exposed in the APIs of any public component.

}

export interface InputControlProps
Expand Down
Loading