-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
FontSizePicker: add pickerMode #63040
Changes from all commits
c501f37
2582369
c3c7b47
99134db
3f68837
4468d0d
f666b55
b64c5e4
81d3936
cfb5c29
99495e4
79175aa
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 |
---|---|---|
|
@@ -23,6 +23,7 @@ const DEFAULT_FONT_SIZE = 16; | |
|
||
function FontSizePicker( { | ||
fontSizes = [], | ||
// Can this be kept as-is? | ||
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. @WordPress/native-mobile @dcalhoun this is still a WIP, but I wanted to check with you about the best thing to do here in case we move forward — we're basically considering deprecating 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. Thanks for your patience while I found time to respond. 🙇🏻 I'm catching up on this project after being absent for a few months. From reviewing the UI, current implementation, and proposed changes, it seems like we could likely apply the proposed changes to the native mobile editor as well. I do not perceive a specific blocker for doing so. I quickly tested the proposed changes on a Samsung Galaxy S20 and encountered an unexpected custom field and an editor crash when:
Font size picker crash
Since the Are you willing to replace 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. Thank you @dcalhoun 🙏 Should we go ahead with the PR, I will apply your suggested changes and ping you again for review |
||
disableCustomFontSizes = false, | ||
onChange, | ||
value: selectedValue, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ import type { ForwardedRef } from 'react'; | |
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { settings } from '@wordpress/icons'; | ||
import { useState, useMemo, forwardRef } from '@wordpress/element'; | ||
import { useState, forwardRef, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -23,7 +23,6 @@ import { | |
} from '../unit-control'; | ||
import { VisuallyHidden } from '../visually-hidden'; | ||
import { getCommonSizeUnit } from './utils'; | ||
import type { FontSizePickerProps } from './types'; | ||
import { | ||
Container, | ||
Header, | ||
|
@@ -35,9 +34,62 @@ import { Spacer } from '../spacer'; | |
import FontSizePickerSelect from './font-size-picker-select'; | ||
import FontSizePickerToggleGroup from './font-size-picker-toggle-group'; | ||
import { T_SHIRT_NAMES } from './constants'; | ||
import type { | ||
FontSize, | ||
FontSizePickerProps, | ||
FontSizePickerMode, | ||
FontSizePickerType, | ||
} from './types'; | ||
|
||
const DEFAULT_UNITS = [ 'px', 'em', 'rem', 'vw', 'vh' ]; | ||
|
||
const shouldUseSelectOverToggle = ( howManyfontSizes: number ) => | ||
howManyfontSizes > 5; | ||
|
||
const getPickerType = ( | ||
pickerMode: FontSizePickerMode, | ||
isCustomValue: boolean, | ||
fontSizes: FontSize[] | ||
): FontSizePickerType => { | ||
if ( | ||
pickerMode === 'custom' || | ||
( pickerMode !== 'predefined' && isCustomValue ) | ||
) { | ||
return 'custom'; | ||
} | ||
|
||
return shouldUseSelectOverToggle( fontSizes.length ) | ||
? 'select' | ||
: 'togglegroup'; | ||
}; | ||
|
||
const getHeaderHint = ( | ||
currentPickerType: FontSizePickerType, | ||
selectedFontSize: FontSize | undefined, | ||
fontSizes: FontSize[] | ||
) => { | ||
if ( currentPickerType === 'custom' ) { | ||
return __( 'Custom' ); | ||
} | ||
|
||
if ( ! shouldUseSelectOverToggle( fontSizes.length ) ) { | ||
if ( selectedFontSize ) { | ||
return ( | ||
selectedFontSize.name || | ||
T_SHIRT_NAMES[ fontSizes.indexOf( selectedFontSize ) ] | ||
); | ||
} | ||
return ''; | ||
} | ||
|
||
const commonUnit = getCommonSizeUnit( fontSizes ); | ||
if ( commonUnit ) { | ||
return `(${ commonUnit })`; | ||
} | ||
|
||
return ''; | ||
}; | ||
|
||
const UnforwardedFontSizePicker = ( | ||
props: FontSizePickerProps, | ||
ref: ForwardedRef< any > | ||
|
@@ -46,61 +98,52 @@ const UnforwardedFontSizePicker = ( | |
__next40pxDefaultSize = false, | ||
fallbackFontSize, | ||
fontSizes = [], | ||
disableCustomFontSizes = false, | ||
onChange, | ||
pickerMode = 'all', | ||
size = 'default', | ||
units: unitsProp = DEFAULT_UNITS, | ||
value, | ||
withSlider = false, | ||
withReset = true, | ||
|
||
// deprecated | ||
disableCustomFontSizes, | ||
} = props; | ||
|
||
const units = useCustomUnits( { | ||
availableUnits: unitsProp, | ||
} ); | ||
Comment on lines
-58
to
-60
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. Moved further down the file, away from picker mode / type logic |
||
let computedPickerMode = pickerMode; | ||
if ( disableCustomFontSizes !== undefined ) { | ||
computedPickerMode = disableCustomFontSizes ? 'predefined' : 'all'; | ||
} | ||
|
||
const shouldUseSelectControl = fontSizes.length > 5; | ||
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. Moved to a separate |
||
const selectedFontSize = fontSizes.find( | ||
( fontSize ) => fontSize.size === value | ||
); | ||
const isCustomValue = !! value && ! selectedFontSize; | ||
|
||
const [ showCustomValueControl, setShowCustomValueControl ] = useState( | ||
! disableCustomFontSizes && isCustomValue | ||
const [ currentPickerType, setCurrentPickerType ] = useState( | ||
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. While the |
||
getPickerType( computedPickerMode, isCustomValue, fontSizes ) | ||
); | ||
|
||
const headerHint = useMemo( () => { | ||
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. Moved |
||
if ( showCustomValueControl ) { | ||
return __( 'Custom' ); | ||
} | ||
|
||
if ( ! shouldUseSelectControl ) { | ||
if ( selectedFontSize ) { | ||
return ( | ||
selectedFontSize.name || | ||
T_SHIRT_NAMES[ fontSizes.indexOf( selectedFontSize ) ] | ||
); | ||
} | ||
return ''; | ||
} | ||
|
||
const commonUnit = getCommonSizeUnit( fontSizes ); | ||
if ( commonUnit ) { | ||
return `(${ commonUnit })`; | ||
} | ||
useEffect( () => { | ||
setCurrentPickerType( | ||
getPickerType( computedPickerMode, isCustomValue, fontSizes ) | ||
); | ||
}, [ computedPickerMode, isCustomValue, fontSizes ] ); | ||
|
||
return ''; | ||
}, [ | ||
showCustomValueControl, | ||
shouldUseSelectControl, | ||
selectedFontSize, | ||
fontSizes, | ||
] ); | ||
const units = useCustomUnits( { | ||
availableUnits: unitsProp, | ||
} ); | ||
|
||
if ( fontSizes.length === 0 && disableCustomFontSizes ) { | ||
if ( fontSizes.length === 0 && computedPickerMode === 'predefined' ) { | ||
return null; | ||
} | ||
|
||
const headerHint = getHeaderHint( | ||
currentPickerType, | ||
selectedFontSize, | ||
fontSizes | ||
); | ||
|
||
// If neither the value or first font size is a string, then FontSizePicker | ||
// operates in a legacy "unitless" mode where UnitControl can only be used | ||
// to select px values and onChange() is always called with number values. | ||
|
@@ -130,56 +173,60 @@ const UnforwardedFontSizePicker = ( | |
</HeaderHint> | ||
) } | ||
</HeaderLabel> | ||
{ ! disableCustomFontSizes && ( | ||
{ /* Show toggle button only when all picker modes are enabled */ } | ||
{ computedPickerMode === 'all' && ( | ||
<HeaderToggle | ||
label={ | ||
showCustomValueControl | ||
currentPickerType === 'custom' | ||
? __( 'Use size preset' ) | ||
: __( 'Set custom size' ) | ||
} | ||
icon={ settings } | ||
onClick={ () => { | ||
setShowCustomValueControl( | ||
! showCustomValueControl | ||
setCurrentPickerType( | ||
getPickerType( | ||
currentPickerType === 'custom' | ||
? 'predefined' | ||
: 'custom', | ||
isCustomValue, | ||
fontSizes | ||
) | ||
); | ||
} } | ||
isPressed={ showCustomValueControl } | ||
isPressed={ currentPickerType === 'custom' } | ||
size="small" | ||
/> | ||
) } | ||
</Header> | ||
</Spacer> | ||
<div> | ||
{ !! fontSizes.length && | ||
shouldUseSelectControl && | ||
! showCustomValueControl && ( | ||
Comment on lines
-153
to
-155
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. The logic for what UI should be shown to the user has been consolidated and moved in the Also, the |
||
<FontSizePickerSelect | ||
__next40pxDefaultSize={ __next40pxDefaultSize } | ||
fontSizes={ fontSizes } | ||
value={ value } | ||
disableCustomFontSizes={ disableCustomFontSizes } | ||
size={ size } | ||
onChange={ ( newValue ) => { | ||
if ( newValue === undefined ) { | ||
onChange?.( undefined ); | ||
} else { | ||
onChange?.( | ||
hasUnits | ||
? newValue | ||
: Number( newValue ), | ||
fontSizes.find( | ||
( fontSize ) => | ||
fontSize.size === newValue | ||
) | ||
); | ||
} | ||
} } | ||
onSelectCustom={ () => | ||
setShowCustomValueControl( true ) | ||
{ currentPickerType === 'select' && ( | ||
<FontSizePickerSelect | ||
__next40pxDefaultSize={ __next40pxDefaultSize } | ||
fontSizes={ fontSizes } | ||
value={ value } | ||
pickerMode={ computedPickerMode } | ||
size={ size } | ||
onChange={ ( newValue ) => { | ||
if ( newValue === undefined ) { | ||
onChange?.( undefined ); | ||
} else { | ||
onChange?.( | ||
hasUnits ? newValue : Number( newValue ), | ||
fontSizes.find( | ||
( fontSize ) => | ||
fontSize.size === newValue | ||
) | ||
); | ||
} | ||
/> | ||
) } | ||
{ ! shouldUseSelectControl && ! showCustomValueControl && ( | ||
} } | ||
onSelectCustom={ () => | ||
setCurrentPickerType( 'custom' ) | ||
} | ||
/> | ||
) } | ||
|
||
{ currentPickerType === 'togglegroup' && ( | ||
<FontSizePickerToggleGroup | ||
fontSizes={ fontSizes } | ||
value={ value } | ||
|
@@ -200,7 +247,8 @@ const UnforwardedFontSizePicker = ( | |
} } | ||
/> | ||
) } | ||
{ ! disableCustomFontSizes && showCustomValueControl && ( | ||
|
||
{ currentPickerType === 'custom' && ( | ||
<Flex className="components-font-size-picker__custom-size-control"> | ||
<FlexItem isBlock> | ||
<UnitControl | ||
|
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.
Moved to bottom of the list of props