Skip to content

Commit

Permalink
Search block: Add typography supports (#43499)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd authored Sep 12, 2022
1 parent 74cd9f9 commit dff616f
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 17 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ Help visitors find your content. ([Source](https://github.com/WordPress/gutenber

- **Name:** core/search
- **Category:** widgets
- **Supports:** align (center, left, right), color (background, gradients, text), ~~html~~
- **Supports:** align (center, left, right), color (background, gradients, text), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** buttonPosition, buttonText, buttonUseIcon, label, placeholder, query, showLabel, width, widthUnit

## Separator
Expand Down
13 changes: 13 additions & 0 deletions packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ _Returns_

- `string`: returns the cssUnit value in a simple px format.

### getTypographyClassesAndStyles

Provides the CSS class names and inline styles for a block's typography support
attributes.

_Parameters_

- _attributes_ `Object`: Block attributes.

_Returns_

- `Object`: Typography block support derived CSS classes & styles.

### InnerBlocks

_Related_
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export { useCustomSides } from './dimensions';
export { getBorderClassesAndStyles, useBorderProps } from './use-border-props';
export { getColorClassesAndStyles, useColorProps } from './use-color-props';
export { getSpacingClassesAndStyles } from './use-spacing-props';
export { getTypographyClassesAndStyles } from './use-typography-props';
export { getGapCSSValue } from './gap';
export { useCachedTruthy } from './use-cached-truthy';
28 changes: 28 additions & 0 deletions packages/block-editor/src/hooks/test/use-typography-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Internal dependencies
*/
import { getTypographyClassesAndStyles } from '../use-typography-props';

describe( 'getTypographyClassesAndStyles', () => {
it( 'should return styles and classes', () => {
const attributes = {
fontFamily: 'tofu',
fontSize: 'large',
style: {
typography: {
letterSpacing: '22px',
fontSize: '2rem',
textTransform: 'uppercase',
},
},
};
expect( getTypographyClassesAndStyles( attributes ) ).toEqual( {
className: 'has-tofu-font-family has-large-font-size',
style: {
letterSpacing: '22px',
fontSize: '2rem',
textTransform: 'uppercase',
},
} );
} );
} );
41 changes: 41 additions & 0 deletions packages/block-editor/src/hooks/use-typography-props.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* External dependencies
*/
import { kebabCase } from 'lodash';
import classnames from 'classnames';

/**
* Internal dependencies
*/
import { getInlineStyles } from './style';
import { getFontSizeClass } from '../components/font-sizes';

// This utility is intended to assist where the serialization of the typography
// block support is being skipped for a block but the typography related CSS
// styles still need to be generated so they can be applied to inner elements.

/**
* Provides the CSS class names and inline styles for a block's typography support
* attributes.
*
* @param {Object} attributes Block attributes.
*
* @return {Object} Typography block support derived CSS classes & styles.
*/
export function getTypographyClassesAndStyles( attributes ) {
const typographyStyles = attributes?.style?.typography || {};
const style = getInlineStyles( { typography: typographyStyles } );
const fontFamilyClassName = !! attributes?.fontFamily
? `has-${ kebabCase( attributes.fontFamily ) }-font-family`
: '';

const className = classnames(
fontFamilyClassName,
getFontSizeClass( attributes?.fontSize )
);

return {
className,
style,
};
}
1 change: 1 addition & 0 deletions packages/block-editor/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export {
getBorderClassesAndStyles as __experimentalGetBorderClassesAndStyles,
useBorderProps as __experimentalUseBorderProps,
getColorClassesAndStyles as __experimentalGetColorClassesAndStyles,
getTypographyClassesAndStyles,
useColorProps as __experimentalUseColorProps,
useCustomSides as __experimentalUseCustomSides,
getSpacingClassesAndStyles as __experimentalGetSpacingClassesAndStyles,
Expand Down
15 changes: 15 additions & 0 deletions packages/block-library/src/search/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@
"text": true
}
},
"typography": {
"__experimentalSkipSerialization": true,
"__experimentalSelector": ".wp-block-search__label, .wp-block-search__input, .wp-block-search__button",
"fontSize": true,
"lineHeight": true,
"__experimentalFontFamily": true,
"__experimentalFontWeight": true,
"__experimentalFontStyle": true,
"__experimentalTextTransform": true,
"__experimentalTextDecoration": true,
"__experimentalLetterSpacing": true,
"__experimentalDefaultControls": {
"fontSize": true
}
},
"__experimentalBorder": {
"color": true,
"radius": true,
Expand Down
30 changes: 25 additions & 5 deletions packages/block-library/src/search/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
RichText,
__experimentalUseBorderProps as useBorderProps,
__experimentalUseColorProps as useColorProps,
getTypographyClassesAndStyles as useTypographyProps,
store as blockEditorStore,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';
Expand Down Expand Up @@ -112,6 +113,7 @@ export default function SearchEdit( {
}

const colorProps = useColorProps( attributes );
const typographyProps = useTypographyProps( attributes );
const unitControlInstanceId = useInstanceId( UnitControl );
const unitControlInputId = `wp-block-search__width-${ unitControlInstanceId }`;
const isButtonPositionInside = 'button-inside' === buttonPosition;
Expand Down Expand Up @@ -208,11 +210,16 @@ export default function SearchEdit( {
// If the input is inside the wrapper, the wrapper gets the border color styles/classes, not the input control.
const textFieldClasses = classnames(
'wp-block-search__input',
isButtonPositionInside ? undefined : borderProps.className
isButtonPositionInside ? undefined : borderProps.className,
typographyProps.className
);
const textFieldStyles = isButtonPositionInside
? { borderRadius }
: borderProps.style;
const textFieldStyles = {
...( isButtonPositionInside
? { borderRadius }
: borderProps.style ),
...typographyProps.style,
textDecoration: undefined,
};

return (
<input
Expand All @@ -239,12 +246,14 @@ export default function SearchEdit( {
const buttonClasses = classnames(
'wp-block-search__button',
colorProps.className,
typographyProps.className,
isButtonPositionInside ? undefined : borderProps.className,
buttonUseIcon ? 'has-icon' : undefined,
__experimentalGetElementClassName( 'button' )
);
const buttonStyles = {
...colorProps.style,
...typographyProps.style,
...( isButtonPositionInside
? { borderRadius }
: borderProps.style ),
Expand Down Expand Up @@ -443,20 +452,31 @@ export default function SearchEdit( {

const blockProps = useBlockProps( {
className: getBlockClassNames(),
style: {
...typographyProps.style,
// Input opts out of text decoration.
textDecoration: undefined,
},
} );

const labelClassnames = classnames(
'wp-block-search__label',
typographyProps.className
);

return (
<div { ...blockProps }>
{ controls }

{ showLabel && (
<RichText
className="wp-block-search__label"
className={ labelClassnames }
aria-label={ __( 'Label text' ) }
placeholder={ __( 'Add label…' ) }
withoutInteractiveFormatting
value={ label }
onChange={ ( html ) => setAttributes( { label: html } ) }
style={ typographyProps.style }
/>
) }

Expand Down
Loading

0 comments on commit dff616f

Please sign in to comment.