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

Font Size Picker: Use a menuitemradio role and better labels. #12372

Merged
merged 3 commits into from
Dec 8, 2018
Merged
Changes from 2 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
45 changes: 26 additions & 19 deletions packages/components/src/font-size-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import { map } from 'lodash';
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { withInstanceId } from '@wordpress/compose';
import { __, _x, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -37,6 +36,7 @@ function FontSizePicker( {
};

const currentFont = fontSizes.find( ( font ) => font.size === value );
const currentFontSizeName = ( currentFont && currentFont.name ) || ( ! value && _x( 'Normal', 'font size name' ) ) || _x( 'Custom', 'font size name' );

return (
<BaseControl label={ __( 'Font Size' ) }>
Expand All @@ -51,26 +51,34 @@ function FontSizePicker( {
isLarge
onClick={ onToggle }
aria-expanded={ isOpen }
aria-label={ __( 'Custom font size' ) }
aria-label={ sprintf(
/* translators: %s: font size name */
__( 'Font size: %s' ), currentFontSizeName
) }
>
{ ( currentFont && currentFont.name ) || ( ! value && _x( 'Normal', 'font size name' ) ) || _x( 'Custom', 'font size name' ) }
{ currentFontSizeName }
</Button>
) }
renderContent={ () => (
<NavigableMenu>
{ map( fontSizes, ( { name, size, slug } ) => (
<Button
key={ slug }
onClick={ () => onChange( slug === 'normal' ? undefined : size ) }
className={ 'is-font-' + slug }
role="menuitem"
>
{ ( value === size || ( ! value && slug === 'normal' ) ) && <Dashicon icon="saved" /> }
<span className="components-font-size-picker__dropdown-text-size" style={ { fontSize: size } }>
{ name }
</span>
</Button>
) ) }
{ map( fontSizes, ( { name, size, slug } ) => {
const isSelected = ( value === size || ( ! value && slug === 'normal' ) );

return (
<Button
key={ slug }
onClick={ () => onChange( slug === 'normal' ? undefined : size ) }
className={ 'is-font-' + slug }
Copy link
Member

Choose a reason for hiding this comment

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

Just a little thing, but we can use template strings here:

Suggested change
className={ 'is-font-' + slug }
className={ `is-font-${ slug }` }

role="menuitemradio"
aria-checked={ isSelected }
>
{ isSelected && <Dashicon icon="saved" /> }
<span className="components-font-size-picker__dropdown-text-size" style={ { fontSize: size } }>
{ name }
</span>
</Button>
);
} ) }
</NavigableMenu>
) }
/>
Expand All @@ -90,7 +98,6 @@ function FontSizePicker( {
onClick={ () => onChange( undefined ) }
isSmall
isDefault
aria-label={ __( 'Reset font size' ) }
>
{ __( 'Reset' ) }
</Button>
Expand All @@ -112,4 +119,4 @@ function FontSizePicker( {
);
}

export default withInstanceId( FontSizePicker );
tofumatt marked this conversation as resolved.
Show resolved Hide resolved
export default FontSizePicker;