Skip to content

Commit

Permalink
Use autocomplete for font selection.
Browse files Browse the repository at this point in the history
  • Loading branch information
spacedmonkey committed Aug 30, 2019
1 parent 91d59a1 commit 673d31c
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 39 deletions.
30 changes: 30 additions & 0 deletions assets/src/stories-editor/components/font-family-picker/edit.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.autocomplete__wrapper .autocomplete__menu {
border-color: #007cba;
width: 101%;
border-bottom-left-radius: 4px !important;
border-bottom-right-radius: 4px !important;
margin-top: -3px;
padding-top: 3px;
}
.autocomplete__icon {
position: absolute;
top: 4px;
right: 4px;
}

.autocomplete__option{
border-bottom: 0px none;
padding: 6px 8px;
font-size: 13px;
}
.autocomplete__option--focused,
.autocomplete__option:hover {
background-color: #0071a1;
border-color: #0071a1;
}
.autocomplete__wrapper .autocomplete__input.autocomplete__input--focused{
color: #191e23;
border-color: #007cba;
box-shadow: 0 0 0 1px #007cba;
outline: 2px solid transparent;
}
92 changes: 57 additions & 35 deletions assets/src/stories-editor/components/font-family-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { __, sprintf } from '@wordpress/i18n';

import { __, _n, sprintf } from '@wordpress/i18n';
import { BaseControl } from '@wordpress/components';
/**
* Internal dependencies
*/
import { maybeEnqueueFontStyle } from '../../helpers';
/**
* Internal dependencies
*/
import { AMP_STORY_FONT_IMAGES } from '../../constants';
import { PreviewPicker } from '../';
import Autocomplete from 'accessible-autocomplete/react';

import 'accessible-autocomplete/src/autocomplete.css';
import './edit.css';

/**
* Font Family Picker component.
Expand All @@ -24,44 +30,60 @@ function FontFamilyPicker( {
onChange = () => {},
value = '',
} ) {
const defaultOption = {
value: '',
label: __( 'None', 'amp' ),
const results = fonts;
const suggest = ( query, syncResults ) => {
const searchResults = query ? results.filter( function( result ) {
return result.name.toLowerCase().indexOf( query.toLowerCase() ) !== -1;
} ) :
[];
syncResults( searchResults );
};

const options = fonts.map( ( font ) => ( {
value: font.name,
label: font.name,
} ) );
const suggestionTemplate = ( result ) => {
maybeEnqueueFontStyle( result.name );
const fallbacks = ( result.fallbacks ) ? ', ' + result.fallbacks.join( ', ' ) : '';
return result && `<span style='font-family: ${ result.name }${ fallbacks }'>${ result.name }</span>`;
};

const fontLabel = ( familyName ) => AMP_STORY_FONT_IMAGES[ familyName ] ?
AMP_STORY_FONT_IMAGES[ familyName ]( { height: 13 } ) :
familyName;
const inputValueTemplate = ( result ) => {
return result && result.name;
};

return (
<PreviewPicker
value={ value }
options={ options }
defaultOption={ defaultOption }
onChange={ ( { value: selectedValue } ) => onChange( '' === selectedValue ? undefined : selectedValue ) }
<BaseControl
label={ __( 'Font Family', 'amp' ) }
id="amp-stories-font-family-picker"
ariaLabel={ ( currentOption ) => {
return sprintf(
/* translators: %s: font name */
__( 'Font Family: %s', 'amp' ),
currentOption.label
);
} }
renderToggle={ ( { label } ) => fontLabel( label ) }
renderOption={ ( option ) => {
return (
<span className="components-preview-picker__dropdown-label" data-font-family={ option.value === '' ? undefined : option.value }>
{ fontLabel( option.label ) }
</span>
);
} }
/>
>
<Autocomplete
id="amp-stories-font-family-picker"
source={ suggest }
templates={
{ suggestion: suggestionTemplate, inputValue: inputValueTemplate }
}
minLength={ 2 }
onConfirm={ onChange }
showAllValues={ false }
confirmOnBlur={ false }
defaultValue={ value }
dropdownArrow={ () => '' }
preserveNullOptions={ true }
placeholder={ __( 'None', 'amp' ) }
showNoOptionsFound={ false }
displayMenu="overlay"
tStatusQueryTooShort={ ( minQueryLength ) =>
// translators: %d: the number characters required to initiate an author search.
sprintf( __( 'Type in %d or more characters for results', 'amp' ), minQueryLength )
}
// translators: 1: the index of thre selected result. 2: The total number of results.
tStatusSelectedOption={ ( selectedOption, length ) => sprintf( __( '%1$s (1 of %2$s) is selected', 'amp' ), selectedOption, length ) }
tStatusResults={ ( length, contentSelectedOption ) => {
return (
_n( '%d font is available.', '%d fonts are available.', length, 'amp' ) +
' ' + contentSelectedOption
);
} }
/>
</BaseControl>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import {
BLOCK_ROTATION_SNAPS,
BLOCK_ROTATION_SNAP_GAP,
} from '../../constants';
import { getBlockOrderDescription, maybeEnqueueFontStyle, getCallToActionBlock } from '../../helpers';
import { getBlockOrderDescription, getCallToActionBlock } from '../../helpers';
import bringForwardIcon from '../../../../images/stories-editor/bring-forward.svg';
import sendBackwardIcon from '../../../../images/stories-editor/send-backwards.svg';
import bringFrontIcon from '../../../../images/stories-editor/bring-front.svg';
Expand Down Expand Up @@ -432,9 +432,8 @@ export default createHigherOrderComponent(
<FontFamilyPicker
fonts={ ampStoriesFonts }
value={ ampFontFamily }
onChange={ ( value ) => {
maybeEnqueueFontStyle( value );
setAttributes( { ampFontFamily: value } );
onChange={ ( font ) => {
setAttributes( { ampFontFamily: font.name } );
} }
/>
<ToggleControl
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"@wordpress/server-side-render": "1.1.0",
"@wordpress/url": "2.7.0",
"@wordpress/wordcount": "2.6.0",
"accessible-autocomplete": "1.6.2",
"autoprefixer": "9.6.1",
"babel-eslint": "10.0.3",
"babel-jest": "24.9.0",
Expand Down

0 comments on commit 673d31c

Please sign in to comment.