Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Commit

Permalink
try: render placeholder serverside
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhtungdu committed Aug 11, 2022
1 parent dee7dc6 commit 98f08ef
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 28 deletions.
18 changes: 14 additions & 4 deletions assets/js/base/components/loading-placeholder/index.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
interface Props {
isLoading: boolean;
width: string;
isLoading?: boolean;
width?: string;
height: string;
style?: Record< string, string | number >;
}

const LoadingPlaceHolder = ( { isLoading, width, height }: Props ) => {
const LoadingPlaceHolder = ( {
isLoading = true,
width,
height,
style,
}: Props ) => {
if ( ! isLoading ) return null;
return (
<div
className="wc-block-loading-placeholder"
style={ { width, height } }
style={ {
width,
height,
...style,
} }
/>
);
};
Expand Down
18 changes: 0 additions & 18 deletions assets/js/base/components/price-slider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import './style.scss';
import { constrainRangeSliderValues } from './constrain-range-slider-values';
import FilterSubmitButton from '../filter-submit-button';
import { isValidMaxValue, isValidMinValue } from './utils';
import LoadingPlaceHolder from '../loading-placeholder';

export interface PriceSliderProps {
/**
Expand Down Expand Up @@ -374,11 +373,6 @@ const PriceSlider = ( {
{ ( ! inlineInput || ! showInputFields ) && slider }
{ showInputFields && (
<div className="wc-block-price-filter__controls wc-block-components-price-slider__controls">
<LoadingPlaceHolder
isLoading={ isLoading && ! hasValidConstraints }
width="120px"
height="34px"
/>
<FormattedMonetaryAmount
currency={ currency }
displayType="input"
Expand Down Expand Up @@ -426,11 +420,6 @@ const PriceSlider = ( {
disabled={ isLoading || ! hasValidConstraints }
value={ maxPriceInput }
/>
<LoadingPlaceHolder
isLoading={ isLoading && ! hasValidConstraints }
width="120px"
height="34px"
/>
</div>
) }
<div className="wc-block-price-filter__controls wc-block-components-price-slider__controls">
Expand All @@ -452,13 +441,6 @@ const PriceSlider = ( {
/>
</div>
) }
<LoadingPlaceHolder
isLoading={
isLoading && ! showInputFields && ! hasValidConstraints
}
width="200px"
height="25px"
/>
{ showFilterButton && (
<FilterSubmitButton
className="wc-block-price-filter__button wc-block-components-price-slider__button"
Expand Down
2 changes: 0 additions & 2 deletions assets/js/base/components/price-slider/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
.wc-block-components-price-slider__range-input-wrapper,
.wc-block-components-filter-reset-button,
.wc-block-components-filter-submit-button {
@include placeholder();
box-shadow: none;
}

Expand Down Expand Up @@ -261,7 +260,6 @@
.wc-block-components-price-slider {
&.is-loading.is-disabled {
.wc-block-components-price-slider__range-input-wrapper {
@include placeholder();
box-shadow: none;
}
}
Expand Down
57 changes: 57 additions & 0 deletions assets/js/blocks/price-filter/PriceFilterLoadingPlaceholder.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/**
* External dependencies
*/
import LoadingPlaceHolder from '@woocommerce/base-components/loading-placeholder';

interface Props {
showInputFields: boolean;
inlineInput: boolean;
showFilterButton: boolean;
isEditor?: boolean;
}

const PriceFilterLoadingPlaceholder = ( {
showInputFields,
inlineInput,
showFilterButton,
isEditor = false,
}: Props ) => (
<>
{ ! isEditor && (
<LoadingPlaceHolder
height="35px"
width="200px"
style={ { marginBottom: '20px' } }
/>
) }

<div className="wc-block-price-slider">
<LoadingPlaceHolder
height="8px"
isLoading={ ! inlineInput || ! showInputFields }
/>
<div className="wc-block-components-price-slider__controls">
<LoadingPlaceHolder width="120px" height="34px" />
<LoadingPlaceHolder height="8px" isLoading={ inlineInput } />
<LoadingPlaceHolder width="120px" height="34px" />
</div>
<div className="wc-block-components-price-slider__controls">
<LoadingPlaceHolder
isLoading={ ! showInputFields }
width="200px"
height="25px"
/>
<LoadingPlaceHolder
isLoading={ showFilterButton }
width="100px"
height="50px"
style={ {
marginLeft: 'auto',
} }
/>
</div>
</div>
</>
);

export default PriceFilterLoadingPlaceholder;
15 changes: 15 additions & 0 deletions assets/js/blocks/price-filter/block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import usePriceConstraints from './use-price-constraints';
import './style.scss';
import { Attributes } from './types';
import PriceFilterLoadingPlaceholder from './PriceFilterLoadingPlaceholder';

/**
* Formats filter values into a string for the URL parameters needed for filtering PHP templates.
Expand Down Expand Up @@ -300,6 +301,20 @@ const PriceFilterBlock = ( {
return null;
}

if (
isLoading &&
! ( isFinite( minConstraint ) && isFinite( maxConstraint ) )
) {
return (
<PriceFilterLoadingPlaceholder
showInputFields={ attributes.showInputFields }
inlineInput={ attributes.inlineInput }
showFilterButton={ attributes.showFilterButton }
isEditor={ isEditor }
/>
);
}

const TagName =
`h${ attributes.headingLevel }` as keyof JSX.IntrinsicElements;

Expand Down
8 changes: 4 additions & 4 deletions assets/js/blocks/price-filter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { useBlockProps } from '@wordpress/block-editor';
import edit from './edit';
import metadata from './block.json';
import { blockAttributes } from './attributes';
import PriceFilterLoadingPlaceholder from './PriceFilterLoadingPlaceholder';

registerBlockType( metadata, {
title: __( 'Filter Products by Price', 'woo-gutenberg-products-block' ),
Expand Down Expand Up @@ -72,6 +73,7 @@ registerBlockType( metadata, {
className,
showInputFields,
showFilterButton,
inlineInput,
heading,
headingLevel,
} = attributes;
Expand All @@ -80,6 +82,7 @@ registerBlockType( metadata, {
'data-showfilterbutton': showFilterButton,
'data-heading': heading,
'data-heading-level': headingLevel,
'data-inline-input': inlineInput,
};
return (
<div
Expand All @@ -88,10 +91,7 @@ registerBlockType( metadata, {
} ) }
{ ...data }
>
<span
aria-hidden
className="wc-block-product-categories__placeholder"
/>
<PriceFilterLoadingPlaceholder { ...attributes } />
</div>
);
},
Expand Down

0 comments on commit 98f08ef

Please sign in to comment.