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

Attribute Filter block: Enable global style #5557

Merged
merged 16 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 3 additions & 0 deletions assets/js/base/components/dropdown-selector/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import './style.scss';
* @param {Object} props Incoming props for the component.
* @param {string} props.attributeLabel Label for the attributes.
* @param {string} props.className CSS class used.
* @param {Object} props.style CSS style object used.
Copy link
Contributor

Choose a reason for hiding this comment

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

What about using CSSProperties?

* @param {Array} props.checked Which items are checked.
* @param {string} props.inputLabel Label used for the input.
* @param {boolean} props.isDisabled Whether the input is disabled or not.
Expand All @@ -34,6 +35,7 @@ import './style.scss';
const DropdownSelector = ( {
attributeLabel = '',
className,
style = {},
checked = [],
inputLabel = '',
isDisabled = false,
Expand Down Expand Up @@ -105,6 +107,7 @@ const DropdownSelector = ( {
'has-checked': checked.length > 0,
'is-open': isOpen,
} ) }
style={ style }
>
{ /* eslint-disable-next-line jsx-a11y/label-has-for */ }
<label
Expand Down
10 changes: 9 additions & 1 deletion assets/js/blocks/attribute-filter/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ import FilterSubmitButton from '@woocommerce/base-components/filter-submit-butto
import isShallowEqual from '@wordpress/is-shallow-equal';
import { decodeEntities } from '@wordpress/html-entities';
import { Notice } from '@wordpress/components';
import classNames from 'classnames';

/**
* Internal dependencies
*/
import { getAttributeFromID } from '../../utils/attributes';
import { updateAttributeFilter } from '../../utils/attributes-query';
import { previewAttributeObject, previewOptions } from './preview';
import { useBorderProps } from '../../hooks/style-attributes';
import './style.scss';

/**
Expand All @@ -50,6 +52,8 @@ const AttributeFilterBlock = ( {
: []
);

const borderProps = useBorderProps( blockAttributes );

const [ queryState ] = useQueryStateByContext();
const [
productAttributesQuery,
Expand Down Expand Up @@ -381,7 +385,11 @@ const AttributeFilterBlock = ( {
<DropdownSelector
attributeLabel={ attributeObject.label }
checked={ checked }
className={ 'wc-block-attribute-filter-dropdown' }
className={ classNames(
'wc-block-attribute-filter-dropdown',
borderProps.className
) }
style={ { ...borderProps.style, borderStyle: 'none' } }
inputLabel={ blockAttributes.heading }
isLoading={ isLoading }
multiple={ multiple }
Expand Down
12 changes: 9 additions & 3 deletions assets/js/blocks/attribute-filter/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
*/
import { __, sprintf, _n } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { InspectorControls, BlockControls } from '@wordpress/block-editor';
import {
InspectorControls,
BlockControls,
useBlockProps,
} from '@wordpress/block-editor';
import {
Placeholder,
Disabled,
Expand Down Expand Up @@ -47,6 +51,8 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak } ) => {
! attributeId && ! isPreview
);

const blockProps = useBlockProps();

const getBlockControls = () => {
return (
<BlockControls>
Expand Down Expand Up @@ -379,7 +385,7 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak } ) => {
return Object.keys( ATTRIBUTES ).length === 0 ? (
noAttributesPlaceholder()
) : (
<>
<div { ...blockProps }>
{ getBlockControls() }
{ getInspectorControls() }
{ isEditing ? (
Expand All @@ -404,7 +410,7 @@ const Edit = ( { attributes, setAttributes, debouncedSpeak } ) => {
</Disabled>
</div>
) }
</>
</div>
);
};

Expand Down
11 changes: 11 additions & 0 deletions assets/js/blocks/attribute-filter/editor.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
.editor-styles-wrapper .wp-block-woocommerce-attribute-filter {
border-style: none !important;
}


.wc-block-attribute-filter {
.components-placeholder__instructions {
border-bottom: 1px solid #e0e2e6;
Expand Down Expand Up @@ -33,4 +38,10 @@
display: block;
margin-bottom: 1em;
}

.components-disabled {
border-radius: inherit;
border-color: inherit;
}

}
18 changes: 17 additions & 1 deletion assets/js/blocks/attribute-filter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import { __ } from '@wordpress/i18n';
import { registerBlockType } from '@wordpress/blocks';
import { Icon, server } from '@woocommerce/icons';
import { useBlockProps } from '@wordpress/block-editor';
import { isFeaturePluginBuild } from '@woocommerce/block-settings';
import classNames from 'classnames';

/**
Expand All @@ -12,6 +14,7 @@ import classNames from 'classnames';
import edit from './edit.js';

registerBlockType( 'woocommerce/attribute-filter', {
apiVersion: 2,
title: __( 'Filter Products by Attribute', 'woo-gutenberg-products-block' ),
icon: {
src: (
Expand All @@ -29,6 +32,17 @@ registerBlockType( 'woocommerce/attribute-filter', {
),
supports: {
html: false,
color: {
text: true,
background: false,
},
...( isFeaturePluginBuild() && {
__experimentalBorder: {
radius: true,
color: true,
width: false,
},
} ),
},
example: {
attributes: {
Expand Down Expand Up @@ -103,7 +117,9 @@ registerBlockType( 'woocommerce/attribute-filter', {
}
return (
<div
className={ classNames( 'is-loading', className ) }
{ ...useBlockProps.save( {
className: classNames( 'is-loading', className ),
} ) }
{ ...data }
>
<span
Expand Down
12 changes: 12 additions & 0 deletions assets/js/blocks/attribute-filter/style.scss
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
.wp-block-woocommerce-attribute-filter {
border-style: none !important;
}

.wc-block-attribute-filter {
margin-bottom: $gap-large;
border-radius: inherit;
border-color: inherit;

&.style-dropdown {
display: flex;
gap: $gap;
border-radius: inherit;
border-color: inherit;
}

.wc-block-attribute-filter-list {
Expand All @@ -26,6 +34,8 @@
flex-grow: 1;
max-width: unset;
width: 0;
border-radius: inherit;
border-color: inherit;
}

.is-single .wc-block-attribute-filter-list-count,
Expand All @@ -35,5 +45,7 @@

.wc-block-components-dropdown-selector__input-wrapper {
height: 100%;
border-radius: inherit;
border-color: inherit;
}
}
58 changes: 17 additions & 41 deletions assets/js/blocks/featured-category/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import {
InnerBlocks,
InspectorControls,
MediaReplaceFlow,
PanelColorSettings,
withColors,
RichText,
} from '@wordpress/block-editor';
import {
Expand Down Expand Up @@ -56,8 +54,6 @@ import { withCategory } from '../../hocs';
* @param {function(any):any} props.getCategory Function for getting category details.
* @param {boolean} props.isLoading Whether loading or not.
* @param {Object} props.category The product category object.
* @param {Object} props.overlayColor Overlay color object for content.
* @param {function(any):any} props.setOverlayColor Setter for overlay color.
* @param {function(any):any} props.debouncedSpeak Function for delayed speak.
* @param {function():void} props.triggerUrlUpdate Function to update Shop now button Url.
*/
Expand All @@ -69,8 +65,6 @@ const FeaturedCategory = ( {
getCategory,
isLoading,
category,
overlayColor,
setOverlayColor,
debouncedSpeak,
triggerUrlUpdate = () => void null,
} ) => {
Expand Down Expand Up @@ -148,21 +142,14 @@ const FeaturedCategory = ( {
}
/>
</PanelBody>
<PanelColorSettings
title={ __( 'Overlay', 'woo-gutenberg-products-block' ) }
colorSettings={ [
{
value: overlayColor.color,
onChange: setOverlayColor,
label: __(
'Overlay Color',
{ !! url && (
Copy link
Contributor

Choose a reason for hiding this comment

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

I understand you want to cast this as a boolean, but I have to say I've always found the !! confusing and I'm not sure there is any clearer advantage to just having url &&. What are your thoughts?

Copy link
Contributor Author

@gigitux gigitux Jan 21, 2022

Choose a reason for hiding this comment

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

I just moved this code, I didn't edit it. I agree with you that it is not very clear this cast! I will change it!

<>
<PanelBody
title={ __(
'Overlay',
'woo-gutenberg-products-block'
),
},
] }
>
{ !! url && (
<>
) }
>
<RangeControl
label={ __(
'Background Opacity',
Expand All @@ -189,9 +176,9 @@ const FeaturedCategory = ( {
}
/>
) }
</>
) }
</PanelColorSettings>
</PanelBody>
</>
) }
</InspectorControls>
);
};
Expand Down Expand Up @@ -287,14 +274,8 @@ const FeaturedCategory = ( {
};

const renderCategory = () => {
const {
className,
contentAlign,
dimRatio,
focalPoint,
height,
showDesc,
} = attributes;
const { contentAlign, dimRatio, focalPoint, showDesc } = attributes;

const classes = classnames(
'wc-block-featured-category',
{
Expand All @@ -304,14 +285,10 @@ const FeaturedCategory = ( {
'has-background-dim': dimRatio !== 0,
},
dimRatioToClass( dimRatio ),
contentAlign !== 'center' && `has-${ contentAlign }-content`,
className
contentAlign !== 'center' && `has-${ contentAlign }-content`
);
const mediaSrc = attributes.mediaSrc || getCategoryImageSrc( category );
const style = !! category ? getBackgroundImageStyles( mediaSrc ) : {};
if ( overlayColor.color ) {
style.backgroundColor = overlayColor.color;
}
if ( focalPoint ) {
const bgPosX = focalPoint.x * 100;
const bgPosY = focalPoint.y * 100;
Expand All @@ -325,7 +302,10 @@ const FeaturedCategory = ( {
return (
<ResizableBox
className={ classes }
size={ { height } }
size={ {
height: '',
width: '',
} }
minHeight={ getSetting( 'min_height', 500 ) }
enable={ { bottom: true } }
onResizeStop={ onResizeStop }
Expand Down Expand Up @@ -416,17 +396,13 @@ FeaturedCategory.propTypes = {
description: PropTypes.node,
permalink: PropTypes.string,
} ),
// from withColors
overlayColor: PropTypes.object,
setOverlayColor: PropTypes.func.isRequired,
// from withSpokenMessages
debouncedSpeak: PropTypes.func.isRequired,
triggerUrlUpdate: PropTypes.func,
};

export default compose( [
withCategory,
withColors( { overlayColor: 'background-color' } ),
withSpokenMessages,
withSelect( ( select, { clientId }, { dispatch } ) => {
const Block = select( 'core/block-editor' ).getBlock( clientId );
Expand Down
23 changes: 23 additions & 0 deletions assets/js/blocks/featured-category/edit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* External dependencies
*/
import { useBlockProps } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import Block from './block';

export const Edit = ( props: unknown ): JSX.Element => {
Copy link
Contributor

Choose a reason for hiding this comment

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

I haven't worked with blocks much, so forgive me for asking: isn't there a way to know this type at all?

Copy link
Contributor Author

@gigitux gigitux Jan 21, 2022

Choose a reason for hiding this comment

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

I haven't worked with blocks much, so forgive me for asking: isn't there a way to know this type at all?

Yes, we can try to type it, but without the right typeguards, there is the risk that the code will be less safe than now.

const blockProps = useBlockProps();

// The useBlockProps function returns the style with the `color`.
// We need to remove it to avoid the block to be styled with the color.
const { color, ...styles } = blockProps.style;

return (
<div { ...blockProps } style={ styles }>
<Block { ...props } />
</div>
);
};
Loading