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

Add/relative units for font size #25825

Closed
Closed
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
70 changes: 56 additions & 14 deletions packages/components/src/font-size-picker/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/**
* External dependencies
*/
import { isNil, isNumber } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useInstanceId } from '@wordpress/compose';
import { textColor } from '@wordpress/icons';
import { useMemo } from '@wordpress/element';
import { useMemo, useState } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -13,6 +19,7 @@ import Button from '../button';
import RangeControl from '../range-control';
import CustomSelectControl from '../custom-select-control';
import VisuallyHidden from '../visually-hidden';
import ToggleControl from '../toggle-control';

const DEFAULT_FONT_SIZE = 'default';
const CUSTOM_FONT_SIZE = 'custom';
Expand Down Expand Up @@ -60,6 +67,10 @@ export default function FontSizePicker( {
[ fontSizes, disableCustomFontSizes ]
);

const [ advancedFontSizing, setAdvancedFontSizing ] = useState(
! isNumber( value ) && ! isNil( value )
);

if ( ! options ) {
return null;
}
Expand All @@ -68,11 +79,40 @@ export default function FontSizePicker( {

const fontSizePickerNumberId = `components-font-size-picker__number#${ instanceId }`;

const customInputProps = advancedFontSizing
? {
id: fontSizePickerNumberId,
className: classnames(
'components-font-size-picker__number',
advancedFontSizing &&
'components-font-size-picker__number--full-size'
),
onChange: ( event ) => onChange( event.target.value ),
ariaLabel: __( 'Custom' ),
value: value || '',
placeholder: __( 'e.g. calc(2rem - 13.5px)' ),
}
: {
id: fontSizePickerNumberId,
className: 'components-font-size-picker__number',
type: 'number',
min: 1,
onChange: ( event ) => onChange( Number( event.target.value ) ),
ariaLabel: __( 'Custom' ),
value: value || '',
};

return (
<fieldset className="components-font-size-picker">
<VisuallyHidden as="legend">{ __( 'Font size' ) }</VisuallyHidden>
<div className="components-font-size-picker__controls">
{ fontSizes.length > 0 && (
<div
className={ classnames(
'components-font-size-picker__controls',
advancedFontSizing &&
'components-font-size-picker__controls--full-size'
) }
>
{ fontSizes.length > 0 && ! advancedFontSizing && (
<CustomSelectControl
className={ 'components-font-size-picker__select' }
label={ __( 'Font size' ) }
Expand All @@ -93,17 +133,7 @@ export default function FontSizePicker( {
<label htmlFor={ fontSizePickerNumberId }>
{ __( 'Custom' ) }
</label>
<input
id={ fontSizePickerNumberId }
className="components-font-size-picker__number"
type="number"
min={ 1 }
onChange={ ( event ) => {
onChange( Number( event.target.value ) );
} }
aria-label={ __( 'Custom' ) }
value={ value || '' }
/>
<input { ...customInputProps } />
</div>
) }
<Button
Expand All @@ -118,6 +148,18 @@ export default function FontSizePicker( {
{ __( 'Reset' ) }
</Button>
</div>
<div>
<ToggleControl
label={ __( 'Advanced font sizing' ) }
onChange={ () =>
setAdvancedFontSizing( ! advancedFontSizing )
}
help={ __(
'Toggle to activate custom font-sizing settings'
) }
checked={ advancedFontSizing }
/>
</div>
{ withSlider && (
<RangeControl
className="components-font-size-picker__custom-input"
Expand Down
11 changes: 10 additions & 1 deletion packages/components/src/font-size-picker/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
margin-left: 0;
margin-right: $grid-unit-10;
margin-top: $grid-unit-10;
width: 54px;
width: 56px;

// Show the reset button as disabled until a value is entered.
&[value=""] + .components-button {
Expand All @@ -27,6 +27,11 @@
&-container {
display: flex;
flex-direction: column;
flex-grow: 1;
}

&--full-size {
width: auto;
}
}

Expand All @@ -39,6 +44,10 @@
height: 30px;
margin-top: 26px;
}

&--full-size {
flex: 0 0 100%;
}
}

.components-font-size-picker__custom-input {
Expand Down