Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Font Library: add progress-bar while uploading font assets #57463

Merged
merged 5 commits into from Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -11,6 +11,7 @@ import {
FormFileUpload,
Notice,
FlexItem,
privateApis as componentsPrivateApis,
} from '@wordpress/components';
import { useContext, useState, useEffect } from '@wordpress/element';

Expand All @@ -23,10 +24,14 @@ import { Font } from '../../../../lib/lib-font.browser';
import makeFamiliesFromFaces from './utils/make-families-from-faces';
import { loadFontFaceInBrowser } from './utils';
import { getNoticeFromInstallResponse } from './utils/get-notice-from-response';
import { unlock } from '../../../lock-unlock';

const { ProgressBar } = unlock( componentsPrivateApis );

function LocalFonts() {
const { installFonts } = useContext( FontLibraryContext );
const [ notice, setNotice ] = useState( null );
const [ isUploading, setIsUploading ] = useState( false );
const supportedFormats =
ALLOWED_FILE_EXTENSIONS.slice( 0, -1 )
.map( ( extension ) => `.${ extension }` )
Expand Down Expand Up @@ -58,6 +63,7 @@ function LocalFonts() {
*/
const handleFilesUpload = ( files ) => {
setNotice( null );
setIsUploading( true );
const uniqueFilenames = new Set();
const selectedFiles = [ ...files ];
const allowedFiles = selectedFiles.filter( ( file ) => {
Expand Down Expand Up @@ -150,29 +156,39 @@ function LocalFonts() {
const response = await installFonts( fontFamilies );
const installNotice = getNoticeFromInstallResponse( response );
setNotice( installNotice );
setIsUploading( false );
};

return (
<>
<Spacer margin={ 16 } />
<DropZone onFilesDrop={ handleDropZone } />
<VStack className="font-library-modal__local-fonts">
<FormFileUpload
accept={ ALLOWED_FILE_EXTENSIONS.map(
( ext ) => `.${ ext }`
).join( ',' ) }
multiple={ true }
onChange={ onFilesUpload }
render={ ( { openFileDialog } ) => (
<Button
className="font-library-modal__upload-area"
onClick={ openFileDialog }
>
<span>{ __( 'Upload font' ) }</span>
</Button>
) }
/>
{ notice && (
{ ! isUploading && (
<FormFileUpload
accept={ ALLOWED_FILE_EXTENSIONS.map(
( ext ) => `.${ ext }`
).join( ',' ) }
multiple={ true }
onChange={ onFilesUpload }
render={ ( { openFileDialog } ) => (
<Button
className="font-library-modal__upload-area"
onClick={ openFileDialog }
>
<span>{ __( 'Upload font' ) }</span>
</Button>
) }
/>
) }
{ isUploading && (
<FlexItem>
<Spacer margin={ 32 } />
<ProgressBar className="font-library-modal__upload-area__progress-bar" />
<Spacer margin={ 32 } />
</FlexItem>
) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can achieve a more consistent layout by re-using the font-library-modal__upload-area class, as this already has a fixed height. Something like this:

Suggested change
{ isUploading && (
<FlexItem>
<Spacer margin={ 32 } />
<ProgressBar className="font-library-modal__upload-area__progress-bar" />
<Spacer margin={ 32 } />
</FlexItem>
) }
{ isUploading && (
<FlexItem>
<div className="font-library-modal__upload-area">
<ProgressBar />
</div>
</FlexItem>
) }

We don't want the progress bar to have a grey background, so we could adjust the CSS so the background colour is specific to the upload button:

button.font-library-modal__upload-area {
	background-color: #f0f0f0;

}

This should create a more consistent layout between the not-uploading is isUploading states:

Not-uploading isUploading
image image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure would be looking onto this.

Thanks.

{ ! isUploading && notice && (
<FlexItem>
<Spacer margin={ 2 } />
<Notice
Expand All @@ -185,15 +201,17 @@ function LocalFonts() {
</FlexItem>
) }
<Spacer margin={ 2 } />
<Text className="font-library-modal__upload-area__text">
{ sprintf(
/* translators: %s: supported font formats: ex: .ttf, .woff and .woff2 */
__(
'Uploaded fonts appear in your library and can be used in your theme. Supported formats: %s.'
),
supportedFormats
) }
</Text>
{ ! isUploading && (
<Text className="font-library-modal__upload-area__text">
{ sprintf(
/* translators: %s: supported font formats: ex: .ttf, .woff and .woff2 */
__(
'Uploaded fonts appear in your library and can be used in your theme. Supported formats: %s.'
),
supportedFormats
) }
</Text>
) }
</VStack>
</>
);
Expand Down
Expand Up @@ -101,6 +101,10 @@
margin: 0 auto;
width: 80%;

.font-library-modal__upload-area__progress-bar {
margin: 0 auto; // Inherit does not work here, because it's inside a flex Item.
}

.font-library-modal__upload-area__text {
color: #6e6e6e;
}
Expand Down