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

Add spinner while google fonts load instead of showing a blank page #214

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 68 additions & 61 deletions src/google-fonts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { __ } from '@wordpress/i18n';
import { useState, useEffect } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { store as coreDataStore } from '@wordpress/core-data';
import { SelectControl } from '@wordpress/components';
import { SelectControl, Spinner } from '@wordpress/components';

import FontVariant from './font-variant';
import { getWeightFromGoogleVariant, getStyleFromGoogleVariant, forceHttps } from './utils';
Expand Down Expand Up @@ -80,72 +80,79 @@ function GoogleFonts () {
setSelectedFont( googleFontsData.items[ value ] ) ;
}

if ( ! googleFontsData?.items ) {
return null;
}

return (
<div className="wrap google-fonts-page">
<h1 className="wp-heading-inline">{ __('Add Google fonts to your theme', 'create-block-theme') }</h1>
<h3>{ __('Add Google fonts assets and font face definitions to your currently active theme', 'create-block-theme')} ({ theme?.name.rendered })</h3>

<div className="select-font">
<SelectControl
label={ __('Select Font', 'create-block-theme') }
name="google-font"
onChange={ handleSelectChange }
>
<option value={null}>{ __('Select a font...', 'create-block-theme') }</option>
{ googleFontsData.items.map( ( font, index ) => (
<option value={ index }>{ font.family }</option>
))}
</SelectControl>
</div>

<DemoTextInput />

{ selectedFont && <p>{ __('Select the font variants you want to include:', 'create-block-theme') }</p> }

{ selectedFont && (
<table className="wp-list-table widefat striped table-view-list" id="google-fonts-table">
<thead>
<tr>
<td className="">
<input
type="checkbox"
onClick={ handleToggleAllVariants }
checked={ selectedVariants.length === selectedFont?.variants.length }
/>
</td>
<td className="">{ __('Weight', 'create-block-theme') }</td>
<td className="">{ __('Style', 'create-block-theme') }</td>
<td className="">{ __('Preview', 'create-block-theme') }</td>
</tr>
</thead>
<tbody>
{selectedFont.variants.map( ( variant, i ) => (
<FontVariant
font={ selectedFont }
variant={ variant }
key={`font-variant-${i}`}
isSelected={ selectedVariants.includes( variant ) }
handleToggle={ () => handleToggleVariant( variant ) }
/>
))}
</tbody>
</table>
{ ! googleFontsData?.items && (
<p>
<Spinner />
<span>{ __('Loading Google fonts data...', 'create-block-theme') }</span>
</p>
) }

{ googleFontsData?.items && (
<>
<div className="select-font">
<SelectControl
label={ __('Select Font', 'create-block-theme') }
name="google-font"
onChange={ handleSelectChange }
>
<option value={null}>{ __('Select a font...', 'create-block-theme') }</option>
{ googleFontsData.items.map( ( font, index ) => (
<option value={ index }>{ font.family }</option>
))}
</SelectControl>
</div>

<DemoTextInput />

{ selectedFont && <p>{ __('Select the font variants you want to include:', 'create-block-theme') }</p> }

{ selectedFont && (
<table className="wp-list-table widefat striped table-view-list" id="google-fonts-table">
<thead>
<tr>
<td className="">
<input
type="checkbox"
onClick={ handleToggleAllVariants }
checked={ selectedVariants.length === selectedFont?.variants.length }
/>
</td>
<td className="">{ __('Weight', 'create-block-theme') }</td>
<td className="">{ __('Style', 'create-block-theme') }</td>
<td className="">{ __('Preview', 'create-block-theme') }</td>
</tr>
</thead>
<tbody>
{selectedFont.variants.map( ( variant, i ) => (
<FontVariant
font={ selectedFont }
variant={ variant }
key={`font-variant-${i}`}
isSelected={ selectedVariants.includes( variant ) }
handleToggle={ () => handleToggleVariant( variant ) }
/>
))}
</tbody>
</table>
) }

<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="selection-data" value={ selectionData } />
<input
type="submit"
value={ __('Add google fonts to your theme', 'create-block-theme') }
className="button button-primary"
disabled={ selectedVariants.length === 0 }
/>
<input type="hidden" name="nonce" value={ nonce } />
</form>
</>
) }

<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="selection-data" value={ selectionData } />
<input
type="submit"
value={ __('Add google fonts to your theme', 'create-block-theme') }
className="button button-primary"
disabled={ selectedVariants.length === 0 }
/>
<input type="hidden" name="nonce" value={ nonce } />
</form>
</div>
)
}
Expand Down