Skip to content

Commit

Permalink
Add elements support to the typography panel in global styles (#36718)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 24, 2021
1 parent 97629ab commit 8d258e5
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/typography.js
Expand Up @@ -231,7 +231,7 @@ export function TypographyPanel( props ) {
);
}

const hasTypographySupport = ( blockName ) => {
export const hasTypographySupport = ( blockName ) => {
return TYPOGRAPHY_SUPPORT_KEYS.some( ( key ) =>
hasBlockSupport( blockName, key )
);
Expand Down
@@ -0,0 +1,39 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import TypographyPanel from './typography-panel';
import ScreenHeader from './header';

const elements = {
text: {
description: __( 'Manage the fonts used on the site.' ),
title: __( 'Text' ),
},
link: {
description: __( 'Manage the fonts and typography used on the links.' ),
title: __( 'Link' ),
},
};

function ScreenTypographyElement( { name, element } ) {
const parentMenu =
name === undefined ? '/typography' : '/blocks/' + name + '/typography';

return (
<>
<ScreenHeader
back={ parentMenu }
title={ elements[ element ].title }
description={ elements[ element ].description }
/>
<TypographyPanel name={ name } element={ element } />
</>
);
}

export default ScreenTypographyElement;
Expand Up @@ -2,12 +2,69 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
__experimentalItemGroup as ItemGroup,
__experimentalVStack as VStack,
__experimentalHStack as HStack,
FlexItem,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import TypographyPanel from './typography-panel';
import ScreenHeader from './header';
import NavigationButton from './navigation-button';
import { useStyle } from './hooks';
import Subtitle from './subtitle';
import TypographyPanel from './typography-panel';

function Item( { name, parentMenu, element, label } ) {
const hasSupport = ! name;
const prefix =
element === 'text' || ! element ? '' : `elements.${ element }.`;
const extraStyles =
element === 'link'
? {
textDecoration: 'underline',
}
: {};
const [ fontFamily ] = useStyle( prefix + 'typography.fontFamily', name );
const [ fontStyle ] = useStyle( prefix + 'typography.fontStyle', name );
const [ fontWeight ] = useStyle( prefix + 'typography.fontWeight', name );
const [ letterSpacing ] = useStyle(
prefix + 'typography.letterSpacing',
name
);
const [ backgroundColor ] = useStyle( prefix + 'color.background', name );
const [ gradientValue ] = useStyle( prefix + 'color.gradient', name );
const [ color ] = useStyle( prefix + 'color.text', name );

if ( ! hasSupport ) {
return null;
}

return (
<NavigationButton path={ parentMenu + '/typography/' + element }>
<HStack justify="flex-start">
<FlexItem
className="edit-site-global-styles-screen-typography__indicator"
style={ {
fontFamily: fontFamily ?? 'serif',
background: gradientValue ?? backgroundColor,
color,
fontStyle,
fontWeight,
letterSpacing,
...extraStyles,
} }
>
{ __( 'Aa' ) }
</FlexItem>
<FlexItem>{ label }</FlexItem>
</HStack>
</NavigationButton>
);
}

function ScreenTypography( { name } ) {
const parentMenu = name === undefined ? '' : '/blocks/' + name;
Expand All @@ -18,10 +75,34 @@ function ScreenTypography( { name } ) {
back={ parentMenu ? parentMenu : '/' }
title={ __( 'Typography' ) }
description={ __(
'Manage the fonts used on the site and the default aspect of different global elements.'
'Manage the typography settings for different elements.'
) }
/>
<TypographyPanel name={ name } />

{ ! name && (
<div className="edit-site-global-styles-screen-typography">
<VStack spacing={ 3 }>
<Subtitle>{ __( 'Elements' ) }</Subtitle>
<ItemGroup isBordered isSeparated>
<Item
name={ name }
parentMenu={ parentMenu }
element="text"
label={ __( 'Text' ) }
/>
<Item
name={ name }
parentMenu={ parentMenu }
element="link"
label={ __( 'Link' ) }
/>
</ItemGroup>
</VStack>
</div>
) }

{ /* no typogrpahy elements support yet for blocks */ }
{ !! name && <TypographyPanel name={ name } element="text" /> }
</>
);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/edit-site/src/components/global-styles/style.scss
Expand Up @@ -15,6 +15,30 @@
}
}

.edit-site-typography-panel__preview {
display: flex;
align-items: center;
justify-content: center;
min-height: 100px;
margin-bottom: $grid-unit-20;
background: $gray-100;
border-radius: $radius-block-ui;
}

.edit-site-global-styles-screen-typography {
margin: $grid-unit-20;
}

.edit-site-global-styles-screen-typography__indicator {
height: 24px;
width: 24px;
font-size: 14px;
display: flex !important;
align-items: center;
justify-content: center;
border-radius: $radius-block-ui;
}

.edit-site-global-styles-screen-colors {
margin: $grid-unit-20;

Expand Down
Expand Up @@ -54,8 +54,10 @@ function useHasLetterSpacingControl( name ) {
);
}

export default function TypographyPanel( { name } ) {
export default function TypographyPanel( { name, element } ) {
const supports = getSupportedGlobalStylesPanels( name );
const prefix =
element === 'text' || ! element ? '' : `elements.${ element }.`;
const [ fontSizes ] = useSetting( 'typography.fontSizes', name );
const disableCustomFontSizes = ! useSetting(
'typography.customFontSize',
Expand All @@ -73,30 +75,58 @@ export default function TypographyPanel( { name } ) {
const hasLetterSpacingControl = useHasLetterSpacingControl( name );

const [ fontFamily, setFontFamily ] = useStyle(
'typography.fontFamily',
prefix + 'typography.fontFamily',
name
);
const [ fontSize, setFontSize ] = useStyle(
prefix + 'typography.fontSize',
name
);
const [ fontSize, setFontSize ] = useStyle( 'typography.fontSize', name );

const [ fontStyle, setFontStyle ] = useStyle(
'typography.fontStyle',
prefix + 'typography.fontStyle',
name
);
const [ fontWeight, setFontWeight ] = useStyle(
'typography.fontWeight',
prefix + 'typography.fontWeight',
name
);
const [ lineHeight, setLineHeight ] = useStyle(
'typography.lineHeight',
prefix + 'typography.lineHeight',
name
);
const [ letterSpacing, setLetterSpacing ] = useStyle(
'typography.letterSpacing',
prefix + 'typography.letterSpacing',
name
);
const [ backgroundColor ] = useStyle( prefix + 'color.background', name );
const [ gradientValue ] = useStyle( prefix + 'color.gradient', name );
const [ color ] = useStyle( prefix + 'color.text', name );
const extraStyles =
element === 'link'
? {
textDecoration: 'underline',
}
: {};

return (
<PanelBody className="edit-site-typography-panel" initialOpen={ true }>
<div
className="edit-site-typography-panel__preview"
style={ {
fontFamily: fontFamily ?? 'serif',
background: gradientValue ?? backgroundColor,
color,
fontSize,
fontStyle,
fontWeight,
letterSpacing,
...extraStyles,
} }
>
Aa
</div>

{ supports.includes( 'fontFamily' ) && (
<FontFamilyControl
fontFamilies={ fontFamilies }
Expand Down
9 changes: 9 additions & 0 deletions packages/edit-site/src/components/global-styles/ui.js
Expand Up @@ -14,6 +14,7 @@ import ScreenRoot from './screen-root';
import ScreenBlockList from './screen-block-list';
import ScreenBlock from './screen-block';
import ScreenTypography from './screen-typography';
import ScreenTypographyElement from './screen-typography-element';
import ScreenColors from './screen-colors';
import ScreenColorPalette from './screen-color-palette';
import ScreenBackgroundColor from './screen-background-color';
Expand All @@ -30,6 +31,14 @@ function ContextScreens( { name } ) {
<ScreenTypography name={ name } />
</NavigatorScreen>

<NavigatorScreen path={ parentMenu + '/typography/text' }>
<ScreenTypographyElement name={ name } element="text" />
</NavigatorScreen>

<NavigatorScreen path={ parentMenu + '/typography/link' }>
<ScreenTypographyElement name={ name } element="link" />
</NavigatorScreen>

<NavigatorScreen path={ parentMenu + '/colors' }>
<ScreenColors name={ name } />
</NavigatorScreen>
Expand Down

0 comments on commit 8d258e5

Please sign in to comment.