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

(Custom)SelectControl: Refresh and refactor chevron #42962

Merged
merged 11 commits into from
Aug 11, 2022
Merged
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

### Enhancements

- `SelectControl`, `CustomSelectControl`: Refresh and refactor chevron down icon ([#42962](https://github.com/WordPress/gutenberg/pull/42962)).
- `FontSizePicker`: Add large size variant ([#42716](https://github.com/WordPress/gutenberg/pull/42716/)).
- `Popover`: tidy up code, add more comments ([#42944](https://github.com/WordPress/gutenberg/pull/42944)).
- Add `box-sizing` reset style mixin to utils ([#42754](https://github.com/WordPress/gutenberg/pull/42754)).
Expand Down
26 changes: 10 additions & 16 deletions packages/components/src/custom-select-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { Icon, check, chevronDown } from '@wordpress/icons';
import { Icon, check } from '@wordpress/icons';
import { __, sprintf } from '@wordpress/i18n';
import { useCallback, useState } from '@wordpress/element';

Expand All @@ -16,7 +16,8 @@ import { useCallback, useState } from '@wordpress/element';
*/
import { VisuallyHidden } from '../';
import { Select as SelectControlSelect } from '../select-control/styles/select-control-styles';
import InputBase from '../input-control/input-base';
import SelectControlChevronDown from '../select-control/chevron-down';
import { InputBaseWithBackCompatMinWidth } from './styles';
import { StyledLabel } from '../base-control/styles/base-control-styles';

const itemToString = ( item ) => item?.name;
Expand Down Expand Up @@ -147,12 +148,16 @@ export default function CustomSelectControl( {
{ label }
</StyledLabel>
) }
<InputBase
<InputBaseWithBackCompatMinWidth
__next36pxDefaultSize={ __next36pxDefaultSize }
__nextUnconstrainedWidth={ __nextUnconstrainedWidth }
mirka marked this conversation as resolved.
Show resolved Hide resolved
isFocused={ isOpen || isFocused }
__unstableInputWidth={
__nextUnconstrainedWidth ? undefined : 'auto'
}
labelPosition={ __nextUnconstrainedWidth ? undefined : 'top' }
size={ size }
suffix={ <SelectControlChevronDown /> }
>
<SelectControlSelect
as="button"
Expand All @@ -164,24 +169,13 @@ export default function CustomSelectControl( {
// This is needed because some speech recognition software don't support `aria-labelledby`.
'aria-label': label,
'aria-labelledby': undefined,
className: classnames(
'components-custom-select-control__button',
{
'is-next-unconstrained-width':
__nextUnconstrainedWidth,
}
),
className: 'components-custom-select-control__button',
describedBy: getDescribedBy(),
} ) }
>
{ itemToString( selectedItem ) }
<Icon
icon={ chevronDown }
className="components-custom-select-control__button-icon"
size={ 18 }
/>
</SelectControlSelect>
</InputBase>
</InputBaseWithBackCompatMinWidth>
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
<ul { ...menuProps } onKeyDown={ onKeyDownHandler }>
{ isOpen &&
Expand Down
12 changes: 0 additions & 12 deletions packages/components/src/custom-select-control/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,6 @@
position: relative;
text-align: left;
outline: 0; // focus ring is handled elsewhere

&:not(.is-next-unconstrained-width) {
min-width: 130px;
}
mirka marked this conversation as resolved.
Show resolved Hide resolved

.components-custom-select-control__button-icon {
height: 100%;
padding: 0;
position: absolute;
right: $grid-unit-05;
top: 0;
}
}

.components-custom-select-control__menu {
Expand Down
28 changes: 28 additions & 0 deletions packages/components/src/custom-select-control/styles.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* External dependencies
*/
import { css } from '@emotion/react';
import styled from '@emotion/styled';

/**
* Internal dependencies
*/
import InputBase from '../input-control/input-base';
import { Container as InputControlContainer } from '../input-control/styles/input-control-styles';

type BackCompatMinWidthProps = {
__nextUnconstrainedWidth: boolean;
};

const backCompatMinWidth = ( props: BackCompatMinWidthProps ) =>
! props.__nextUnconstrainedWidth
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like InputBaseWithBackCompatMinWidth never receives the __nextUnconstrainedWidth prop in packages/components/src/custom-select-control/index.js

Copy link
Member Author

Choose a reason for hiding this comment

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

Lol, I like how this still accidentally works even without the correct prop 😂 (Also how do people code without end-to-end typescript checking...)

? css`
${ InputControlContainer } {
min-width: 130px;
}
ciampo marked this conversation as resolved.
Show resolved Hide resolved
`
: '';

export const InputBaseWithBackCompatMinWidth = styled( InputBase )`
${ backCompatMinWidth }
`;
25 changes: 25 additions & 0 deletions packages/components/src/select-control/chevron-down.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* WordPress dependencies
*/
import { chevronDown, Icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import {
chevronIconSize,
DownArrowWrapper,
InputControlSuffixWrapperWithClickThrough,
} from './styles/select-control-styles';

const SelectControlChevronDown = () => {
return (
<InputControlSuffixWrapperWithClickThrough>
<DownArrowWrapper>
<Icon icon={ chevronDown } size={ chevronIconSize } />
</DownArrowWrapper>
</InputControlSuffixWrapperWithClickThrough>
);
};

export default SelectControlChevronDown;
15 changes: 6 additions & 9 deletions packages/components/src/select-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import type { ChangeEvent, FocusEvent, ForwardedRef } from 'react';
*/
import { useInstanceId } from '@wordpress/compose';
import { useState, forwardRef } from '@wordpress/element';
import { Icon, chevronDown } from '@wordpress/icons';

/**
* Internal dependencies
*/
import BaseControl from '../base-control';
import InputBase from '../input-control/input-base';
import { Select, DownArrowWrapper } from './styles/select-control-styles';
import { Select } from './styles/select-control-styles';
import type { WordPressComponentProps } from '../ui/context';
import type { SelectControlProps } from './types';
import SelectControlChevronDown from './chevron-down';

const noop = () => {};

Expand Down Expand Up @@ -48,6 +48,7 @@ function UnforwardedSelectControl(
children,
prefix,
suffix,
__next36pxDefaultSize = false,
__nextHasNoMarginBottom = false,
...props
}: WordPressComponentProps< SelectControlProps, 'select', false >,
Expand Down Expand Up @@ -100,18 +101,14 @@ function UnforwardedSelectControl(
isFocused={ isFocused }
label={ label }
size={ size }
suffix={
suffix || (
<DownArrowWrapper>
<Icon icon={ chevronDown } size={ 18 } />
</DownArrowWrapper>
)
}
suffix={ suffix || <SelectControlChevronDown /> }
prefix={ prefix }
labelPosition={ labelPosition }
__next36pxDefaultSize={ __next36pxDefaultSize }
>
<Select
{ ...props }
__next36pxDefaultSize={ __next36pxDefaultSize }
aria-describedby={ helpId }
className="components-select-control__input"
disabled={ disabled }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import styled from '@emotion/styled';
* Internal dependencies
*/
import { COLORS, rtl } from '../../utils';
import { space } from '../../ui/utils/space';
import type { SelectControlProps } from '../types';
import InputControlSuffixWrapper from '../../input-control/input-suffix-wrapper';

interface SelectProps
extends Pick< SelectControlProps, '__next36pxDefaultSize' | 'disabled' > {
Expand Down Expand Up @@ -85,29 +87,33 @@ const sizeStyles = ( {
return css( style );
};

export const chevronIconSize = 18;

const sizePaddings = ( {
__next36pxDefaultSize,
selectSize = 'default',
}: SelectProps ) => {
const iconWidth = chevronIconSize;

const sizes = {
default: {
paddingLeft: 16,
paddingRight: 32,
paddingRight: 16 + iconWidth,
},
small: {
paddingLeft: 8,
paddingRight: 24,
paddingRight: 8 + iconWidth,
},
'__unstable-large': {
paddingLeft: 16,
paddingRight: 32,
paddingRight: 16 + iconWidth,
},
};

if ( ! __next36pxDefaultSize ) {
sizes.default = {
paddingLeft: 8,
paddingRight: 24,
paddingRight: 8 + iconWidth,
};
}

Expand All @@ -129,6 +135,8 @@ export const Select = styled.select< SelectProps >`
font-family: inherit;
margin: 0;
width: 100%;
max-width: none;
cursor: pointer;

${ disabledStyles };
${ fontSizeStyles };
Expand All @@ -138,17 +146,15 @@ export const Select = styled.select< SelectProps >`
`;

export const DownArrowWrapper = styled.div`
align-items: center;
bottom: 0;
display: flex;
padding: 0 4px;
pointer-events: none;
margin-inline-end: ${ space( -1 ) }; // optically adjust the icon
line-height: 0;
`;

export const InputControlSuffixWrapperWithClickThrough = styled(
InputControlSuffixWrapper
)`
position: absolute;
top: 0;
pointer-events: none;

${ rtl( { right: 0 } ) }

svg {
display: block;
}
`;