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 all 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,38 +156,36 @@ 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={ 2 } />
<Notice
isDismissible={ false }
status={ notice.type }
className="font-library-modal__upload-area__notice"
>
{ notice.message }
</Notice>
<div className="font-library-modal__upload-area">
<ProgressBar />
</div>
</FlexItem>
) }
<Spacer margin={ 2 } />
Expand All @@ -194,6 +198,18 @@ function LocalFonts() {
supportedFormats
) }
</Text>
{ ! isUploading && notice && (
<FlexItem>
<Spacer margin={ 2 } />
<Notice
isDismissible={ false }
status={ notice.type }
className="font-library-modal__upload-area__notice"
>
{ notice.message }
</Notice>
</FlexItem>
) }
</VStack>
</>
);
Expand Down
Expand Up @@ -94,6 +94,9 @@
justify-content: center;
height: 250px;
width: 100%;
}

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

Expand Down