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

Make color editable #14

Merged
merged 1 commit into from
Feb 7, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
"category": "woocommerce",
"parent": [ "woocommerce/cart-totals-block" ],
"supports": {
"html": false
"html": false,
"color": {
"__experimentalSkipSerialization": true
nielslange marked this conversation as resolved.
Show resolved Hide resolved
}
},
"attributes": {
"currentTotal": {
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"lint:css": "wp-scripts lint-style",
"lint:js": "wp-scripts lint-js",
"start": "wp-scripts start",
"test": "wp-scripts test-unit-js",
"test:coverage": "wp-scripts test-unit-js --coverage",
"test": "wp-scripts test-unit-js --verbose",
"test:coverage": "wp-scripts test-unit-js --coverage --verbose",
"test:help": "wp-scripts test-unit-js --help",
"test:watch": "wp-scripts test-unit-js --watchAll",
"phpcbf": "composer run phpcbf",
Expand All @@ -32,7 +32,10 @@
"dependencies": {
"@wordpress/block-editor": "^8.0.0",
"@wordpress/blocks": "^11.1.0",
"autoprefixer": "10.4.2",
"classnames": "^2.3.1",
"eslint": "^7.32.0",
"lodash.kebabcase": "^4.1.1",
"prettier": "npm:[email protected]"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import ProgressBar from './components/progress-bar';

export default function Block( attributes ) {
return (
<>
<div className="wc-free-shipping-progress-bar">
<ProgressLabel { ...attributes } />
<ProgressBar { ...attributes } />
</>
</div>
);
}
46 changes: 30 additions & 16 deletions src/components/progress-bar/__test__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,53 @@ import '@testing-library/jest-dom';
/**
* Internal dependencies
*/
import ProgressBar from './../index';
import ProgressBar from '../index';

const outer = '.wc-free-shipping-progress-bar__outer';
const inner = '.wc-free-shipping-progress-bar__inner';
const progressbar = '.wc-free-shipping-progress-bar__progress';

describe( 'The ProgressBar component', () => {
it( 'shows the classnames of the outer div correctly', () => {
render( <ProgressBar /> );
expect( document.querySelector( outer ) ).toBeInTheDocument();
} );

it( 'shows the classnames of the inner div correctly', () => {
render( <ProgressBar /> );
expect( document.querySelector( inner ) ).toBeInTheDocument();
it( 'shows the classnames of the div correctly', () => {
render( <ProgressBar freeShippingFrom="1" currentTotal="0" /> );
expect( document.querySelector( progressbar ) ).toBeInTheDocument();
} );

it( 'shows the style for a full bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="4" /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 100%' );
expect( document.querySelector( progressbar ) ).toHaveAttribute(
'value',
'100'
);
} );

it( 'shows the style for a threequarter bar correctly', () => {
it( 'shows the style for a three quarter bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="3" /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 75%' );
expect( document.querySelector( progressbar ) ).toHaveAttribute(
'value',
'75'
);
} );

it( 'shows the style for a half bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="2" /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 50%' );
expect( document.querySelector( progressbar ) ).toHaveAttribute(
'value',
'50'
);
} );

it( 'shows the style for a one quarter bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="1" /> );
expect( document.querySelector( progressbar ) ).toHaveAttribute(
'value',
'25'
);
} );

it( 'shows the style for an empty bar correctly', () => {
render( <ProgressBar freeShippingFrom="4" currentTotal="0" /> );
expect( document.querySelector( inner ) ).toHaveStyle( 'width: 0%' );
expect( document.querySelector( progressbar ) ).toHaveAttribute(
'value',
'0'
);
} );
} );
42 changes: 33 additions & 9 deletions src/components/progress-bar/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,39 @@
const ProgressBar = ( { currentTotal, freeShippingFrom } ) => {
/*
* External dependencies
*/
import classnames from 'classnames';

/**
* Internal dependencies
*/
import { getColorClass, getColorCode } from '../../util';

const ProgressBar = ( {
currentTotal,
freeShippingFrom,
backgroundColor,
colorProps,
} ) => {
const progressClass = getColorClass( backgroundColor, 'color' );
const progressColor = getColorCode(
backgroundColor,
colorProps,
'backgroundColor'
);
const progress = ( currentTotal / freeShippingFrom ) * 100;
const divWidth = ( progress > 100 ? 100 : progress ) + '%';
const divStyle = { width: divWidth };
const width = progress > 100 ? 100 : progress;
const style = { color: progressColor };

return (
<div className="wc-free-shipping-progress-bar__outer">
<div
className="wc-free-shipping-progress-bar__inner"
style={ divStyle }
></div>
</div>
<progress
className={ classnames(
'wc-free-shipping-progress-bar__progress',
progressClass
) }
max="100"
style={ style }
value={ width }
/>
);
};

Expand Down
25 changes: 24 additions & 1 deletion src/components/progress-label/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
/*
* External dependencies
*/
import classnames from 'classnames';

/**
* Internal dependencies
*/
import { getColorClass, getColorCode } from '../../util';

const ProgressLabel = ( {
currentTotal,
freeShippingFrom,
labelInsufficientTotals,
labelSufficientTotals,
textColor,
colorProps,
} ) => {
const messageClass = getColorClass( textColor, 'color' );
const messageColor = getColorCode( textColor, colorProps, 'color' );
const style = { color: messageColor };
const remaining = Number( freeShippingFrom - currentTotal ).toFixed( 2 );
const message =
remaining > 0
? labelInsufficientTotals.replace( '{amount}', remaining )
: labelSufficientTotals;

return (
<div className="wc-free-shipping-progress-bar__label">{ message }</div>
<div
className={ classnames(
'wc-free-shipping-progress-bar__label',
messageClass
) }
style={ style }
>
{ message }
</div>
);
};

Expand Down
21 changes: 19 additions & 2 deletions src/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
import { __ } from '@wordpress/i18n';
import {
__experimentalUseColorProps as useColorProps,
BlockControls,
InspectorControls,
PlainText,
Expand All @@ -19,6 +20,7 @@ import useViewSwitcher from './components/use-view-switcher';
import { notice } from './constants';
import { Icon, progressBarHalf, progressBarFull } from './icons';
import './style.scss';
import { getColorCode } from './util';

const BlockSettings = ( { attributes, setAttributes } ) => {
const { freeShippingFrom } = attributes;
Expand Down Expand Up @@ -51,7 +53,11 @@ const BlockSettings = ( { attributes, setAttributes } ) => {
};

const Edit = ( { attributes, setAttributes, clientId } ) => {
const { labelInsufficientTotals, labelSufficientTotals } = attributes;
const {
labelInsufficientTotals,
labelSufficientTotals,
textColor,
} = attributes;
const { currentView, component: ViewSwitcherComponent } = useViewSwitcher(
clientId,
[
Expand All @@ -74,6 +80,10 @@ const Edit = ( { attributes, setAttributes, clientId } ) => {
]
);

const colorProps = useColorProps( attributes );
const messageColor = getColorCode( textColor, colorProps, 'color' );
const messageStyle = { color: messageColor };

return (
<div { ...useBlockProps() }>
<BlockErrorBoundary
Expand Down Expand Up @@ -102,6 +112,7 @@ const Edit = ( { attributes, setAttributes, clientId } ) => {
/>
<PlainText
className="wc-free-shipping-progress-bar__label"
style={ messageStyle }
value={ labelInsufficientTotals }
onChange={ ( value ) =>
setAttributes( {
Expand Down Expand Up @@ -135,13 +146,18 @@ const Edit = ( { attributes, setAttributes, clientId } ) => {
</p>
</Notice>
) }
<ProgressBar { ...attributes } />

<ProgressBar
{ ...attributes }
colorProps={ colorProps }
/>
</>
) }
{ currentView === 'sufficient-cart-totals' && (
<>
<PlainText
className="wc-free-shipping-progress-bar__label"
style={ messageStyle }
value={ labelSufficientTotals }
onChange={ ( value ) =>
setAttributes( {
Expand All @@ -151,6 +167,7 @@ const Edit = ( { attributes, setAttributes, clientId } ) => {
/>
<ProgressBar
{ ...attributes }
colorProps={ colorProps }
currentTotal="1"
freeShippingFrom="1"
/>
Expand Down
32 changes: 17 additions & 15 deletions src/style.scss
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
.wc-free-shipping-progress-bar__outer {
align-items: center;
.wc-free-shipping-progress-bar__progress[value] {
appearance: none;
background: transparent;
border: 2px solid #ddd;
border-radius: 4px;
display: flex;
height: 2em;
justify-content: center;
border: 1px solid currentColor;
border-radius: 3px;
height: 20px;
margin: 0.5em 0 2em;
padding: 0.1em;
position: relative;
width: 100%;
}
.wc-free-shipping-progress-bar__inner {
background: #4c566a;
border-radius: 3px;
height: 100%;
left: 0;
position: absolute;

.wc-free-shipping-progress-bar__progress[value]::-webkit-progress-bar {
background: transparent;
}

.wc-free-shipping-progress-bar__progress[value]::-webkit-progress-value {
background: currentColor;
}

.wc-free-shipping-progress-bar__progress[value]::-moz-progress-bar {
background: currentColor;
}
22 changes: 22 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import kebabCase from 'lodash.kebabcase';
nielslange marked this conversation as resolved.
Show resolved Hide resolved

export function getColorClass( colorSlug, colorContextName ) {
if ( ! colorSlug || ! colorContextName ) {
return undefined;
}

return `has-${ kebabCase(
colorSlug
) }-${ colorContextName } has-text-color`;
}

export function getColorCode( colorCode, colorProps, scope ) {
if ( colorProps !== undefined && colorProps.style !== undefined ) {
return colorProps.style[ scope ];
}

return colorCode || 'currentColor';
}
Loading