-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Updates to the Font Size Picker #9802
Changes from all commits
713526e
16a84de
3e7c044
3ab378d
af3dfc3
132680f
7848506
5f2da9a
18ba6db
5cf9607
62cb488
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,23 @@ | ||
.components-base-control { | ||
font-family: $default-font; | ||
font-size: $default-font-size; | ||
} | ||
|
||
.components-base-control__field { | ||
margin-bottom: $grid-size; | ||
.components-base-control__field { | ||
margin-bottom: $grid-size; | ||
|
||
.components-panel__row & { | ||
margin-bottom: inherit; | ||
.components-panel__row & { | ||
margin-bottom: inherit; | ||
} | ||
} | ||
} | ||
|
||
.components-base-control__label { | ||
display: block; | ||
margin-bottom: $grid-size-small; | ||
} | ||
.components-base-control__label { | ||
display: block; | ||
margin-bottom: $grid-size-small; | ||
} | ||
|
||
.components-base-control__help { | ||
font-style: italic; | ||
margin-bottom: 0; | ||
.components-base-control__help { | ||
margin-top: -$grid-size; | ||
font-style: italic; | ||
margin-bottom: 0; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,51 +6,110 @@ import { map } from 'lodash'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Fragment } from '@wordpress/element'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { withInstanceId } from '@wordpress/compose'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import Dashicon from '../dashicon'; | ||
import BaseControl from '../base-control'; | ||
import Button from '../button'; | ||
import ButtonGroup from '../button-group'; | ||
import Dropdown from '../dropdown'; | ||
import RangeControl from '../range-control'; | ||
import { NavigableMenu } from '../navigable-container'; | ||
|
||
function FontSizePicker( { | ||
fallbackFontSize, | ||
fontSizes = [], | ||
onChange, | ||
value, | ||
withSlider, | ||
} ) { | ||
const onChangeValue = ( event ) => { | ||
const newValue = event.target.value; | ||
if ( newValue === '' ) { | ||
onChange( undefined ); | ||
return; | ||
} | ||
onChange( Number( newValue ) ); | ||
}; | ||
|
||
const currentFont = fontSizes.find( ( font ) => font.size === value ); | ||
|
||
export default function FontSizePicker( { fontSizes = [], fallbackFontSize, value, onChange } ) { | ||
return ( | ||
<Fragment> | ||
<BaseControl label={ __( 'Font Size' ) }> | ||
<div className="components-font-size-picker__buttons"> | ||
<ButtonGroup aria-label={ __( 'Font Size' ) }> | ||
{ map( fontSizes, ( { name, size, shortName } ) => ( | ||
<Dropdown | ||
className="components-font-size-picker__dropdown" | ||
contentClassName="components-font-size-picker__dropdown-content" | ||
position="bottom" | ||
renderToggle={ ( { isOpen, onToggle } ) => ( | ||
<Button | ||
key={ size } | ||
className="components-font-size-picker__selector" | ||
isLarge | ||
isPrimary={ value === size } | ||
aria-pressed={ value === size } | ||
onClick={ () => onChange( size ) } | ||
onClick={ onToggle } | ||
aria-expanded={ isOpen } | ||
aria-label={ __( 'Custom font size' ) } | ||
> | ||
{ shortName || name } | ||
{ ( currentFont && currentFont.name ) || ( ! value && 'Normal' ) || 'Custom' } | ||
</Button> | ||
) ) } | ||
</ButtonGroup> | ||
) } | ||
renderContent={ () => ( | ||
<NavigableMenu> | ||
{ map( fontSizes, ( { name, size, slug } ) => ( | ||
<Button | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be great to have a lint rule that checks for |
||
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> | ||
) ) } | ||
</NavigableMenu> | ||
) } | ||
/> | ||
{ ! withSlider && | ||
<input | ||
className="components-range-control__number" | ||
type="number" | ||
onChange={ onChangeValue } | ||
aria-label={ __( 'Custom font size' ) } | ||
value={ value || '' } | ||
/> | ||
} | ||
<Button | ||
isLarge | ||
className="components-color-palette__clear" | ||
type="button" | ||
disabled={ value === undefined } | ||
onClick={ () => onChange( undefined ) } | ||
isButton | ||
isSmall | ||
isDefault | ||
aria-label={ __( 'Reset font size' ) } | ||
> | ||
{ __( 'Reset' ) } | ||
youknowriad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
</Button> | ||
</div> | ||
<RangeControl | ||
className="components-font-size-picker__custom-input" | ||
label={ __( 'Custom Size' ) } | ||
value={ value || '' } | ||
initialPosition={ fallbackFontSize } | ||
onChange={ onChange } | ||
min={ 12 } | ||
max={ 100 } | ||
beforeIcon="editor-textcolor" | ||
afterIcon="editor-textcolor" | ||
/> | ||
</Fragment> | ||
{ withSlider && | ||
<RangeControl | ||
className="components-font-size-picker__custom-input" | ||
label={ __( 'Custom Size' ) } | ||
value={ value || '' } | ||
initialPosition={ fallbackFontSize } | ||
onChange={ onChange } | ||
min={ 12 } | ||
max={ 100 } | ||
beforeIcon="editor-textcolor" | ||
afterIcon="editor-textcolor" | ||
/> | ||
} | ||
</BaseControl> | ||
); | ||
} | ||
|
||
export default withInstanceId( FontSizePicker ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use-case does not feel legitimate. I don't think we should support
BaseControl
with alabel
and not anid
. The label should always direct to an input, even in the case of the font size picker.This leaves open the possibility for error like described at https://github.com/WordPress/gutenberg/pull/10925/files#r257248594 .