Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Create a new hook specifically for font size.
  • Loading branch information
ramonjd committed Nov 8, 2022
commit 3795bfc50c3bf9534eb8471d43019935188d6540
25 changes: 10 additions & 15 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,20 @@ export function useStyle( path, blockName, source = 'all' ) {
? `styles.${ path }`
: `styles.blocks.${ blockName }.${ path }`;

const setStyle = ( newValue, metadata ) => {
const setStyle = ( newValue ) => {
setUserConfig( ( currentConfig ) => {
// Deep clone `currentConfig` to avoid mutating it later.
const newUserConfig = JSON.parse( JSON.stringify( currentConfig ) );
let styleValue = getPresetVariableFromValue(
mergedConfig.settings,
blockName,
path,
newValue
set(
newUserConfig,
finalPath,
getPresetVariableFromValue(
mergedConfig.settings,
blockName,
path,
newValue
)
);
// Convert font size styles to fluid if fluid is activated.
if (
finalPath.indexOf( 'typography.fontSize' ) !== -1 &&
!! metadata?.slug
) {
styleValue = `var:preset|font-size|${ metadata?.slug })`;
}

set( newUserConfig, finalPath, styleValue );
return newUserConfig;
} );
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@ function useStyleWithReset( path, blockName ) {
return [ style, setStyle, hasStyle, resetStyle ];
}

function useFontSizeStyleWithReset(
path,
blockName,
isFluidTypographyEnabled
) {
const [ style, setStyleCallback ] = useStyle( path, blockName );
const [ userStyle ] = useStyle( path, blockName, 'user' );
const hasStyle = () => !! userStyle;
const resetStyle = () => setStyleCallback( undefined );
const setStyle = ( newValue, metadata ) => {
// Convert font size styles to fluid if fluid is activated.
if ( isFluidTypographyEnabled && !! metadata?.slug ) {
newValue = `var:preset|font-size|${ metadata?.slug }`;
}
setStyleCallback( newValue );
};
return [ style, setStyle, hasStyle, resetStyle ];
}

function useFontAppearance( prefix, name ) {
const [ fontStyle, setFontStyle ] = useStyle(
prefix + 'typography.fontStyle',
Expand Down Expand Up @@ -192,7 +211,11 @@ export default function TypographyPanel( { name, element, headingLevel } ) {
const [ fontFamily, setFontFamily, hasFontFamily, resetFontFamily ] =
useStyleWithReset( prefix + 'typography.fontFamily', name );
const [ fontSize, setFontSize, hasFontSize, resetFontSize ] =
useStyleWithReset( prefix + 'typography.fontSize', name );
useFontSizeStyleWithReset(
prefix + 'typography.fontSize',
name,
fluidTypography
);
const {
fontStyle,
setFontStyle,
Expand Down