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

Adding Font size presets UI #63057

Merged
merged 35 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
45959e4
adding ui for size presets
matiasbenedetto Jun 28, 2024
923bc76
add disable prop
matiasbenedetto Jun 28, 2024
f27fc7d
Merge branch 'trunk' into add/font-size-preset-ui-to-editor
matiasbenedetto Jul 1, 2024
a8cc36b
removes font size custom ordering
matiasbenedetto Jul 1, 2024
0585007
use origins
matiasbenedetto Jul 2, 2024
8c8f8cd
fix spelling
matiasbenedetto Jul 3, 2024
cea269f
fixing font size presets count button size
matiasbenedetto Jul 3, 2024
f7df1da
size of font size preset item
matiasbenedetto Jul 4, 2024
57ecea1
edit wording
matiasbenedetto Jul 4, 2024
e2b3653
style on font size preset item
matiasbenedetto Jul 4, 2024
a8e816d
focus on input on the rename preset modal
matiasbenedetto Jul 4, 2024
2d1f957
redirect to font sizes
matiasbenedetto Jul 4, 2024
7cc1260
Add confirm dialog to reset font size presets
matiasbenedetto Jul 4, 2024
535953f
update wording
matiasbenedetto Jul 4, 2024
316b24f
Merge branch 'trunk' into add/font-size-preset-ui-to-editor
matiasbenedetto Jul 4, 2024
7752e21
remove size prop for ItemGroup
matiasbenedetto Jul 4, 2024
1a22c99
remove number in font size presets drilldown
matiasbenedetto Jul 4, 2024
679122e
improve item look
matiasbenedetto Jul 4, 2024
dee2355
update label
matiasbenedetto Jul 10, 2024
7cc001d
make markup more consistent with other rename dialogs
matiasbenedetto Jul 10, 2024
46b0dd2
update label
matiasbenedetto Jul 10, 2024
6d065f2
Merge branch 'trunk' into add/font-size-preset-ui-to-editor
matiasbenedetto Jul 10, 2024
908da98
disalow rename and delete of default font size presets
matiasbenedetto Jul 10, 2024
b93998a
add missing imports
matiasbenedetto Jul 10, 2024
fb1eec7
disable font main size component when custom fluid is set
matiasbenedetto Jul 10, 2024
fd86a59
use getComputedFluidTypographyValue to compute the size of the preview
matiasbenedetto Jul 10, 2024
4e1b2bd
remove not needed prop
matiasbenedetto Jul 10, 2024
dd1b70a
remove not needed prop
matiasbenedetto Jul 11, 2024
573296a
allow to rename and delete only custom font size presets
matiasbenedetto Jul 11, 2024
948bbd4
improve styles
matiasbenedetto Jul 11, 2024
d94d75b
remove not needed classes
matiasbenedetto Jul 11, 2024
ef707f0
revert unwated changes
matiasbenedetto Jul 11, 2024
82c1b9a
removing not needed navigation
matiasbenedetto Jul 11, 2024
81e61ee
removing not needed condition
matiasbenedetto Jul 11, 2024
3f84d8c
improve array comparison
matiasbenedetto Jul 11, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';

function ConfirmDeleteFontSizeDialog( {
fontSize,
isOpen,
toggleOpen,
handleRemoveFontSize,
} ) {
const handleConfirm = async () => {
toggleOpen();
handleRemoveFontSize( fontSize );
};

const handleCancel = () => {
toggleOpen();
};

return (
<ConfirmDialog
isOpen={ isOpen }
cancelButtonText={ __( 'Cancel' ) }
confirmButtonText={ __( 'Delete' ) }
onCancel={ handleCancel }
onConfirm={ handleConfirm }
size="medium"
>
{ fontSize &&
sprintf(
/* translators: %s: Name of the font size preset. */
__(
'Are you sure you want to delete "%s" font size preset?'
),
fontSize.name
) }
</ConfirmDialog>
);
}

export default ConfirmDeleteFontSizeDialog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* WordPress dependencies
*/
import { __experimentalConfirmDialog as ConfirmDialog } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

function ConfirmResetFontSizesDialog( {
text,
confirmButtonText,
isOpen,
toggleOpen,
onConfirm,
} ) {
const handleConfirm = async () => {
toggleOpen();
onConfirm();
};

const handleCancel = () => {
toggleOpen();
};

return (
<ConfirmDialog
isOpen={ isOpen }
cancelButtonText={ __( 'Cancel' ) }
confirmButtonText={ confirmButtonText }
onCancel={ handleCancel }
onConfirm={ handleConfirm }
size="medium"
>
{ text }
</ConfirmDialog>
);
}

export default ConfirmResetFontSizesDialog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import {
getComputedFluidTypographyValue,
privateApis as blockEditorPrivateApis,
} from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { unlock } from '../../../lock-unlock';
const { useGlobalStyle } = unlock( blockEditorPrivateApis );

function FontSizePreview( { fontSize } ) {
const [ font ] = useGlobalStyle( 'typography' );

const input =
fontSize?.fluid?.min && fontSize?.fluid?.max
? {
minimumFontSize: fontSize.fluid.min,
maximumFontSize: fontSize.fluid.max,
}
: {
fontSize: fontSize.size,
};

const computedFontSize = getComputedFluidTypographyValue( input );
return (
<div
className="edit-site-typography-preview"
style={ {
fontSize: computedFontSize,
fontFamily: font?.fontFamily ?? 'serif',
} }
>
{ __( 'Aa' ) }
</div>
);
}

export default FontSizePreview;
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
/**
* WordPress dependencies
*/
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import {
__experimentalSpacer as Spacer,
__experimentalUseNavigator as useNavigator,
__experimentalView as View,
__experimentalHStack as HStack,
__experimentalVStack as VStack,
privateApis as componentsPrivateApis,
Button,
FlexItem,
ToggleControl,
} from '@wordpress/components';
import { moreVertical } from '@wordpress/icons';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { unlock } from '../../../lock-unlock';
const {
DropdownMenuV2: DropdownMenu,
DropdownMenuItemV2: DropdownMenuItem,
DropdownMenuItemLabelV2: DropdownMenuItemLabel,
} = unlock( componentsPrivateApis );
const { useGlobalSetting } = unlock( blockEditorPrivateApis );
import ScreenHeader from '../header';
import FontSizePreview from './font-size-preview';
import ConfirmDeleteFontSizeDialog from './confirm-delete-font-size-dialog';
import RenameFontSizeDialog from './rename-font-size-dialog';
import SizeControl from '../size-control';

function FontSize() {
const [ isDeleteConfirmOpen, setIsDeleteConfirmOpen ] = useState( false );
const [ isRenameDialogOpen, setIsRenameDialogOpen ] = useState( false );

const {
params: { origin, slug },
goBack,
goTo,
} = useNavigator();

const [ fontSizes, setFontSizes ] = useGlobalSetting(
'typography.fontSizes'
);

// Get the font sizes from the origin, default to empty array.
const sizes = fontSizes[ origin ] ?? [];

// Get the font size by slug.
const fontSize = sizes.find( ( size ) => size.slug === slug );

// Whether fluid is true or an object, set it to true, otherwise false.
const isFluid = !! fontSize.fluid ?? false;

// Whether custom fluid values are used.
const isCustomFluid = typeof fontSize.fluid === 'object';

const handleNameChange = ( value ) => {
updateFontSize( 'name', value );
};

const handleFontSizeChange = ( value ) => {
updateFontSize( 'size', value );
};

const handleFluidChange = ( value ) => {
updateFontSize( 'fluid', value );
};

const handleCustomFluidValues = ( value ) => {
if ( value ) {
// If custom values are used, init the values with the current ones.
updateFontSize( 'fluid', {
min: fontSize.size,
max: fontSize.size,
} );
} else {
// If custom fluid values are disabled, set fluid to true.
updateFontSize( 'fluid', true );
}
};

const handleMinChange = ( value ) => {
updateFontSize( 'fluid', { ...fontSize.fluid, min: value } );
};

const handleMaxChange = ( value ) => {
updateFontSize( 'fluid', { ...fontSize.fluid, max: value } );
};

const updateFontSize = ( key, value ) => {
const newFontSizes = sizes.map( ( size ) => {
if ( size.slug === slug ) {
return { ...size, [ key ]: value }; // Create a new object with updated key
}
return size;
} );

setFontSizes( {
...fontSizes,
[ origin ]: newFontSizes,
} );
};

const handleRemoveFontSize = () => {
// Navigate to the font sizes list.
goBack();

const newFontSizes = sizes.filter( ( size ) => size.slug !== slug );
setFontSizes( {
...fontSizes,
[ origin ]: newFontSizes,
} );
};

const toggleDeleteConfirm = () => {
setIsDeleteConfirmOpen( ! isDeleteConfirmOpen );
};

const toggleRenameDialog = () => {
setIsRenameDialogOpen( ! isRenameDialogOpen );
};

return (
<>
<ConfirmDeleteFontSizeDialog
fontSize={ fontSize }
isOpen={ isDeleteConfirmOpen }
toggleOpen={ toggleDeleteConfirm }
handleRemoveFontSize={ handleRemoveFontSize }
/>

{ isRenameDialogOpen && (
<RenameFontSizeDialog
fontSize={ fontSize }
toggleOpen={ toggleRenameDialog }
handleRename={ handleNameChange }
/>
) }

<VStack spacing={ 4 }>
<HStack justify="space-between" align="flex-start">
<ScreenHeader
title={ fontSize.name }
description={ sprintf(
/* translators: %s: font size preset name. */
__( 'Manage the font size %s.' ),
fontSize.name
) }
onBack={ () => goTo( '/typography/font-sizes/' ) }
/>
{ origin === 'custom' && (
<FlexItem>
<Spacer
marginTop={ 3 }
marginBottom={ 0 }
paddingX={ 4 }
>
<DropdownMenu
trigger={
<Button
size="small"
icon={ moreVertical }
label={ __( 'Font size options' ) }
/>
}
>
<DropdownMenuItem
onClick={ toggleRenameDialog }
>
<DropdownMenuItemLabel>
{ __( 'Rename' ) }
</DropdownMenuItemLabel>
</DropdownMenuItem>
<DropdownMenuItem
onClick={ toggleDeleteConfirm }
>
<DropdownMenuItemLabel>
{ __( 'Delete' ) }
</DropdownMenuItemLabel>
</DropdownMenuItem>
</DropdownMenu>
</Spacer>
</FlexItem>
) }
</HStack>

<View>
<Spacer paddingX={ 4 }>
<VStack spacing={ 4 }>
<FlexItem>
<FontSizePreview fontSize={ fontSize } />
</FlexItem>

<SizeControl
label={ __( 'Size' ) }
value={ ! isCustomFluid ? fontSize.size : '' }
onChange={ handleFontSizeChange }
disabled={ isCustomFluid }
/>

<ToggleControl
label={ __( 'Fluid typography' ) }
help={ __(
'Scale the font size dynamically to fit the screen or viewport.'
) }
checked={ isFluid }
onChange={ handleFluidChange }
__nextHasNoMarginBottom
/>

{ isFluid && (
<ToggleControl
label={ __( 'Custom fluid values' ) }
help={ __(
'Set custom min and max values for the fluid font size.'
) }
checked={ isCustomFluid }
onChange={ handleCustomFluidValues }
__nextHasNoMarginBottom
/>
) }

{ isCustomFluid && (
<>
<SizeControl
label={ __( 'Minimum' ) }
value={ fontSize.fluid?.min }
onChange={ handleMinChange }
/>
<SizeControl
label={ __( 'Maximum' ) }
value={ fontSize.fluid?.max }
onChange={ handleMaxChange }
/>
</>
) }
</VStack>
</Spacer>
</View>
</VStack>
</>
);
}

export default FontSize;
Loading
Loading