Skip to content

Support alpha channel for color picker #6197

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
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
13 changes: 12 additions & 1 deletion classes/fields/color.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function options() {
'dependency' => true,
'developer_mode' => true,
),
static::$type . '_alpha' => array(
'label' => __( 'Enable alpha channel', 'pods' ),
'default' => 0,
'type' => 'boolean',
),
);

return $options;
Expand Down Expand Up @@ -120,14 +125,20 @@ public function validate( $value, $name = null, $options = null, $fields = null,
} else {
$color = str_replace( '#', '', $check );

if ( pods_v( self::$type . '_alpha', $options, false ) ) {
$regex = '/^([0-9A-F]{3,4}){1,2}$/i';
} else {
$regex = '/^([0-9A-F]{3}){1,2}$/i';
}

if ( 0 < strlen( $value ) && '' === $check ) {
if ( $this->is_required( $options ) ) {
$errors[] = __( 'This field is required.', 'pods' );
} else {
// @todo Ask for a specific format in error message
$errors[] = __( 'Invalid value provided for this field.', 'pods' );
}
} elseif ( ! empty( $color ) && ! in_array( strlen( $color ), array( 3, 6 ), true ) ) {
} elseif ( ! empty( $color ) && ! preg_match( $regex, $color ) ) {
$errors[] = __( 'Invalid Hex Color value provided for this field.', 'pods' );
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/js/blocks/pods-blocks-api.min.asset.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dependencies":["wp-api-fetch","wp-autop","wp-block-editor","wp-blocks","wp-components","wp-compose","wp-date","wp-element","wp-i18n","wp-keycodes","wp-server-side-render","wp-url"],"version":"1ae572d3f990891697f42359d4789570"}
{"dependencies":["wp-api-fetch","wp-autop","wp-block-editor","wp-blocks","wp-components","wp-compose","wp-date","wp-element","wp-i18n","wp-keycodes","wp-server-side-render","wp-url"],"version":"eee47e8051ea21b3ee6a152dd4851aad"}
1,270 changes: 1,269 additions & 1 deletion ui/js/blocks/pods-blocks-api.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.asset.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dependencies":["moment","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"24a252ba15d83803f1b7826ab7b44b4c"}
{"dependencies":["moment","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-i18n","wp-keycodes","wp-plugins","wp-primitives","wp-url"],"version":"74863ed2f5aa4b5a71a676e2a25a1f51"}
7,370 changes: 7,369 additions & 1 deletion ui/js/dfv/pods-dfv.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ui/js/dfv/src/config/prop-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ export const FIELD_PROP_TYPE = {

// Color fields
color_repeatable: BOOLEAN_ALL_TYPES,
color_alpha: BOOLEAN_ALL_TYPES,

// Currency fields
currency_decimal_handling: PropTypes.string,
Expand Down
6 changes: 4 additions & 2 deletions ui/js/dfv/src/fields/color/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const Color = ( {
fieldConfig,
setValue,
value,
enableAlpha,
setHasBlurred,
} ) => {
const { name } = fieldConfig;
Expand Down Expand Up @@ -42,10 +43,10 @@ const Color = ( {
<ColorPicker
color={ value }
onChangeComplete={ ( newValue ) => {
setValue( newValue.hex );
setValue( ( 1 > newValue.color._a ) ? newValue.color.toHex8String() : newValue.color.toHexString() );
setHasBlurred();
} }
disableAlpha
enableAlpha={ enableAlpha }
className="pods-color-picker"
/>
) }
Expand All @@ -56,6 +57,7 @@ const Color = ( {
Color.propTypes = {
...FIELD_COMPONENT_BASE_PROPS,
value: PropTypes.string,
enableAlpha: PropTypes.bool,
};

export default Color;