Skip to content

Commit

Permalink
Merge pull request #119 from liip/improvement/better-property-names
Browse files Browse the repository at this point in the history
feat(): Improve button property names
  • Loading branch information
tschortsch committed Nov 15, 2022
2 parents 1846253 + 223f2d7 commit ca922b3
Show file tree
Hide file tree
Showing 22 changed files with 159 additions and 37 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,12 @@ Modify available button styles.

```javascript
function myButtonStyleOptions( styleOptions ) {
styleOptions.push( { label: 'My Option', value: 'my-option', color: '#FF0000' } );
styleOptions.push( {
label: 'My Option',
value: 'my-option',
bgColor: '#ff0000',
textColor: '#ffffff',
} );
return styleOptions;
}
wp.hooks.addFilter(
Expand All @@ -576,6 +581,13 @@ wp.hooks.addFilter(

* `styleOptions` (`Array`) Array with button style options.

Each `styleOption` object should have the following attributes:

* `label` (`string`) Label displayed in the select box.
* `value` (`string`) Value of the chosen option.
* `bgColor` (`string`) Background color of button shown in the editor.
* `textColor` (`string`) Text color of button shown in the editor.

### wpBootstrapBlocks.container.marginAfterOptions

Modify margin after options.
Expand All @@ -598,6 +610,11 @@ wp.hooks.addFilter(

* `marginAfterOptions` (`Array`) Array margin options.

Each `marginAfterOption` object should have the following attributes:

* `label` (`string`) Label displayed in the select box.
* `value` (`string`) Value of the chosen option.

### wpBootstrapBlocks.row.templates

Define block templates.
Expand Down Expand Up @@ -639,7 +656,7 @@ wp.hooks.addFilter(

* `templates` (`array`) List of template objects.

Each template has the following attributes:
Each `template` object should have the following attributes:

* `name` (`string`) Unique identifier of the template
* `title` (`string`) Name of template
Expand Down
2 changes: 1 addition & 1 deletion build/index.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '9b6fd2a0b07ab4882cb6');
<?php return array('dependencies' => array('wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n'), 'version' => '068db33e0cc8e71503c0');
2 changes: 1 addition & 1 deletion build/index.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cypress/e2e/button/button-block.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe( 'Button Block', () => {
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
'have.attr',
'style',
'background-color: rgb(0, 123, 255);'
'background-color: rgb(0, 123, 255); color: rgb(255, 255, 255);'
);

cy.selectButtonBlock();
Expand All @@ -66,7 +66,7 @@ describe( 'Button Block', () => {
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
'have.attr',
'style',
'background-color: rgb(108, 117, 125);'
'background-color: rgb(108, 117, 125); color: rgb(255, 255, 255);'
);

// Editor content should match snapshot
Expand Down
43 changes: 43 additions & 0 deletions cypress/e2e/button/button-filters.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,49 @@ describe( 'Button Block Filters', () => {
// Style option should be applied
cy.getSelectByLabel( 'Style' ).select( 'brand' );

// Style should be visible in UI
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
'have.attr',
'style',
'background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);'
);

// Editor content should match snapshot
cy.postContentMatchesSnapshot();
} );

it( 'Deprecated color attribute should be visible in UI', () => {
cy.insertButtonBlock();
cy.selectButtonBlock();
cy.ensureSidebarOpened();

cy.getSelectByLabel( 'Style' ).select( 'brand-deprecated-color' );

// Style should be visible in UI
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
'have.attr',
'style',
'background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);'
);

// Editor content should match snapshot
cy.postContentMatchesSnapshot();
} );

it( 'Use default colors if textColor or bgColor attributes are missing in styleOption', () => {
cy.insertButtonBlock();
cy.selectButtonBlock();
cy.ensureSidebarOpened();

cy.getSelectByLabel( 'Style' ).select( 'missing-colors' );

// Style should be visible in UI
cy.get( '.wp-block-wp-bootstrap-blocks-button' ).should(
'have.attr',
'style',
'background-color: rgb(0, 123, 255); color: rgb(255, 255, 255);'
);

// Editor content should match snapshot
cy.postContentMatchesSnapshot();
} );
Expand Down
16 changes: 15 additions & 1 deletion e2e-test-plugins/button-filters/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
function buttonFiltersStyleOptions( styleOptions ) {
return [
...styleOptions,
{ label: 'Brand', value: 'brand', color: '#FF0000' },
{
label: 'Brand',
value: 'brand',
bgColor: '#FF0000',
textColor: '#FFFFFF',
},
{
label: 'Brand (Deprecated Color)',
value: 'brand-deprecated-color',
color: '#FF0000',
},
{
label: 'Missing colors',
value: 'missing-colors',
},
];
}
wp.hooks.addFilter(
Expand Down
2 changes: 1 addition & 1 deletion languages/wp-bootstrap-blocks-de_CH.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is distributed under the same license as the Bootstrap Blocks plugin.
msgid ""
msgstr ""
"Project-Id-Version: Bootstrap Blocks 5.0.0\n"
"Project-Id-Version: Bootstrap Blocks 5.1.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/wp-bootstrap-"
"blocks\n"
"POT-Creation-Date: 2022-11-11T09:10:58+00:00\n"
Expand Down
2 changes: 1 addition & 1 deletion languages/wp-bootstrap-blocks-de_DE.po
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is distributed under the same license as the Bootstrap Blocks plugin.
msgid ""
msgstr ""
"Project-Id-Version: Bootstrap Blocks 5.0.0\n"
"Project-Id-Version: Bootstrap Blocks 5.1.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/wp-bootstrap-"
"blocks\n"
"POT-Creation-Date: 2022-11-11T09:10:58+00:00\n"
Expand Down
2 changes: 1 addition & 1 deletion languages/wp-bootstrap-blocks.pot
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is distributed under the GPL2+.
msgid ""
msgstr ""
"Project-Id-Version: Bootstrap Blocks 5.0.0\n"
"Project-Id-Version: Bootstrap Blocks 5.1.0\n"
"Report-Msgid-Bugs-To: https://wordpress.org/support/plugin/wp-bootstrap-blocks\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wp-bootstrap-blocks",
"version": "5.0.0",
"version": "5.1.0",
"private": true,
"description": "Bootstrap Gutenberg Blocks for WordPress",
"author": "Liip AG",
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Tags: gutenberg, blocks, bootstrap
Requires at least: 5.3
Tested up to: 6.1
Requires PHP: 5.6
Stable tag: 5.0.0
Stable tag: 5.1.0
License: GPLv2
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -143,6 +143,11 @@ Please create a new GitHub issue and let us know: [https://github.com/liip/boots

== Changelog ==

= 5.1.0 =

* [CHANGE] Renamed `color` attribute of the `styleOptions` for the button block to `bgColor`. Additionally added `textColor` attribute to be able to change the text color of the button in the editor as well. If you are modifying the `styleOptions` via the [`wpBootstrapBlocks.button.styleOptions`](https://github.com/liip/bootstrap-blocks-wordpress-plugin#wpbootstrapblocksbuttonstyleoptions) JavaScript filter please add the according colors to your style objects.
* [FIX] Remove usage of deprecated `IconButton` component (will be removed in WordPress 6.2).

= 5.0.0 =

Starting with this version the plugin only supports WordPress versions >= 5.3.
Expand Down
6 changes: 6 additions & 0 deletions snapshots.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ module.exports = {
},
"wp_bootstrap_blocks_button_default_attributes should override default attributes": {
"1": "<!-- wp:wp-bootstrap-blocks/button /-->"
},
"Deprecated color attribute should be visible in UI": {
"1": "<!-- wp:wp-bootstrap-blocks/button {\"style\":\"brand-deprecated-color\"} /-->"
},
"Use default colors if textColor or bgColor attributes are missing in styleOption": {
"1": "<!-- wp:wp-bootstrap-blocks/button {\"style\":\"missing-colors\"} /-->"
}
},
"Container Block": {
Expand Down
2 changes: 1 addition & 1 deletion src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"Bootstrap"
],
"textdomain": "wp-bootstrap-blocks",
"version": "5.0.0"
"version": "5.1.0"
}
51 changes: 41 additions & 10 deletions src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { __ } from '@wordpress/i18n';
import {
Dashicon,
IconButton,
Button,
SelectControl,
PanelBody,
ToggleControl,
Expand All @@ -16,26 +16,29 @@ import {
BlockControls,
AlignmentToolbar,
} from '@wordpress/block-editor';
import { colors } from '../constants';
import { bgColors, colors } from '../constants';

let styleOptions = [
{
label: __( 'Primary', 'wp-bootstrap-blocks' ),
value: 'primary',
color: colors.primary,
bgColor: bgColors.primary,
textColor: colors.white,
},
{
label: __( 'Secondary', 'wp-bootstrap-blocks' ),
value: 'secondary',
color: colors.secondary,
bgColor: bgColors.secondary,
textColor: colors.white,
},
];
styleOptions = applyFilters(
'wpBootstrapBlocks.button.styleOptions',
styleOptions
);

const DEFAULT_COLOR = colors.primary;
const DEFAULT_BG_COLOR = bgColors.primary;
const DEFAULT_TEXT_COLOR = colors.white;
const NEW_TAB_REL_DEFAULT_VALUE = 'noreferrer noopener';

const BootstrapButtonEdit = ( {
Expand Down Expand Up @@ -63,19 +66,47 @@ const BootstrapButtonEdit = ( {
} );
};

// Fill empty color values with default values and check for usage of deprecated color attribute in styleOptions
let hasDeprecatedColorAttributes = false;
const styleOptionsWithDefault = styleOptions.map( ( styleOption ) => {
if ( styleOption.color ) {
hasDeprecatedColorAttributes = true;
}
return {
...styleOption,
textColor: styleOption.textColor || DEFAULT_TEXT_COLOR,
bgColor:
styleOption.bgColor || styleOption.color || DEFAULT_BG_COLOR, // Fallback to deprecated color attribute
};
} );

if ( hasDeprecatedColorAttributes ) {
// eslint-disable-next-line no-console
console.warn(
'[wpBootstrapBlocks.button.styleOptions filter] The color attribute in styleOptions is deprecated. Please us bgColor and textColor instead.'
);
}

// Prepare CSS rules for selected button style
let inlineStyle = {
backgroundColor:
styleOptions.length > 0 ? styleOptions[ 0 ].color : DEFAULT_COLOR,
styleOptionsWithDefault.length > 0
? styleOptionsWithDefault[ 0 ].bgColor
: DEFAULT_BG_COLOR,
color:
styleOptionsWithDefault.length > 0
? styleOptionsWithDefault[ 0 ].textColor
: DEFAULT_TEXT_COLOR,
};

if ( style ) {
const selectedButtonColor = styleOptions.find(
const selectedButtonColor = styleOptionsWithDefault.find(
( styleOption ) => styleOption.value === style
);
if ( selectedButtonColor?.color ) {
if ( selectedButtonColor?.bgColor && selectedButtonColor?.textColor ) {
inlineStyle = {
backgroundColor: selectedButtonColor.color,
backgroundColor: selectedButtonColor.bgColor,
color: selectedButtonColor.textColor,
};
}
}
Expand Down Expand Up @@ -151,7 +182,7 @@ const BootstrapButtonEdit = ( {
setAttributes( { url: value } )
}
/>
<IconButton
<Button
icon="editor-break"
label={ __( 'Apply', 'wp-bootstrap-blocks' ) }
type="submit"
Expand Down
2 changes: 1 addition & 1 deletion src/class-wp-bootstrap-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WP_Bootstrap_Blocks {
*
* @var string
*/
public static $version = '5.0.0';
public static $version = '5.1.0';

/**
* The plugin token.
Expand Down
6 changes: 3 additions & 3 deletions src/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
verticalAlignCenter,
verticalAlignTop,
} from '../icons';
import { colors } from '../constants';
import { bgColors } from '../constants';

const contentVerticalAlignmentControls = [
{
Expand Down Expand Up @@ -67,8 +67,8 @@ const ColumnSizeRangeControl = ( {
};

export let bgColorOptions = [
{ name: 'primary', color: colors.primary },
{ name: 'secondary', color: colors.secondary },
{ name: 'primary', color: bgColors.primary },
{ name: 'secondary', color: bgColors.secondary },
];

bgColorOptions = applyFilters(
Expand Down
6 changes: 5 additions & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const colors = {
export const bgColors = {
primary: '#007bff',
secondary: '#6c757d',
};

export const colors = {
white: '#ffffff',
};
Loading

0 comments on commit ca922b3

Please sign in to comment.