/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';
import {
__experimentalSpacer as Spacer,
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, useEffect } from '@wordpress/element';
import type {
TypographyPreset,
FluidTypographySettings,
FontSize,
FluidTypographyConfig,
} from '@wordpress/global-styles-engine';
/**
* Internal dependencies
*/
import { ScreenHeader } from '../screen-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';
import { useSetting } from '../hooks';
import { unlock } from '../lock-unlock';
const { Menu } = unlock( componentsPrivateApis );
function FontSize() {
const [ isDeleteConfirmOpen, setIsDeleteConfirmOpen ] = useState( false );
const [ isRenameDialogOpen, setIsRenameDialogOpen ] = useState( false );
const {
params: { origin, slug },
goBack,
} = useNavigator();
const [ fontSizes, setFontSizes ] = useSetting<
Record< string, TypographyPreset[] > | undefined
>( 'typography.fontSizes' );
const [ globalFluid ] = useSetting<
boolean | FluidTypographySettings | undefined
>( 'typography.fluid' );
// Get the font sizes from the origin, default to empty array.
const sizes = fontSizes?.[ origin as string ] ?? [];
// Get the font size by slug.
const fontSize: FontSize | undefined = sizes.find(
( size ) => size.slug === slug
);
// Navigate to the font sizes list if the font size is not available.
useEffect( () => {
if ( !! slug && ! fontSize ) {
goBack();
}
}, [ slug, fontSize, goBack ] );
if ( ! origin || ! slug || ! fontSize ) {
return null;
}
// Whether the font size is fluid. If not defined, use the global fluid value of the theme.
const isFluid =
fontSize?.fluid !== undefined ? !! fontSize.fluid : !! globalFluid;
// Whether custom fluid values are used.
const isCustomFluid = typeof fontSize?.fluid === 'object';
const handleNameChange = ( value: string ) => {
updateFontSize( 'name', value );
};
const handleFontSizeChange = ( value: string | undefined ) => {
updateFontSize( 'size', value );
};
const handleFluidChange = ( value: boolean ) => {
updateFontSize( 'fluid', value );
};
const handleCustomFluidValues = ( value: boolean ) => {
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: string | undefined ) => {
const fluid: FluidTypographyConfig =
typeof fontSize.fluid === 'object' ? fontSize.fluid : {};
updateFontSize( 'fluid', { ...fluid, min: value } );
};
const handleMaxChange = ( value: string | undefined ) => {
const fluid: FluidTypographyConfig =
typeof fontSize.fluid === 'object' ? fontSize.fluid : {};
updateFontSize( 'fluid', { ...fluid, max: value } );
};
const updateFontSize = ( key: string, value: any ) => {
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 as string ]: newFontSizes,
} );
};
const handleRemoveFontSize = () => {
const newFontSizes = sizes.filter( ( size ) => size.slug !== slug );
setFontSizes( {
...fontSizes,
[ origin as string ]: newFontSizes,
} );
};
const toggleDeleteConfirm = () => {
setIsDeleteConfirmOpen( ! isDeleteConfirmOpen );
};
const toggleRenameDialog = () => {
setIsRenameDialogOpen( ! isRenameDialogOpen );
};
return (
<>
{ isRenameDialogOpen && (
) }
{ origin === 'custom' && (
) }
{ isFluid && (
) }
{ isCustomFluid && (
<>
>
) }
>
);
}
export default FontSize;