Skip to content
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
33 changes: 13 additions & 20 deletions projects/packages/forms/src/blocks/contact-form/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@
border: var(--jetpack--contact-form--border, 1px solid #8c8f94);
border-color: var(--jetpack--contact-form--border-color, #8c8f94);
border-style: var(--jetpack--contact-form--border-style, solid);
border-width: var(--jetpack--contact-form--border-size, 1px);
border-width: var(--jetpack--contact-form--border-left-size, 1px);
}

.components-base-control__field {
Expand Down Expand Up @@ -525,14 +525,6 @@
}
}

:where(:not(.contact-form)) > .jetpack-field {

.jetpack-field__input, .jetpack-field__textarea {
padding-left: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius));
padding-right: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius));
}
}

// Duplicated to elevate specificity in order to overwrite core styles
.jetpack-field-multiple__list.jetpack-field-multiple__list {
list-style-type: none;
Expand Down Expand Up @@ -857,7 +849,7 @@
}

.notched-label__label {
top: calc(var(--jetpack--contact-form--border-size) * -1);
top: calc(var(--jetpack--contact-form--border-top-size) * -1);
transform: translateY(-50%);
font-size: 0.8em;
}
Expand Down Expand Up @@ -919,15 +911,15 @@
.jetpack-field-dropdown__toggle
{
padding-top: 1.4em;
padding-left: var(--jetpack--contact-form--field-padding);
padding-left: var(--jetpack--contact-form--animated-left-offset);
}

.animated-label__label {
position: absolute;
z-index: 1;
top: 50%;
left: var(--jetpack--contact-form--label-left);
width: calc( 100% - var(--jetpack--contact-form--label-left));
top: var(--jetpack--contact-form--animated-top-offset);
left: calc(var(--jetpack--contact-form--border-left-size, 1px) + var(--jetpack--contact-form--animated-left-offset));
width: auto;
max-width: 100%;
box-sizing: border-box;
margin: 0;
Expand All @@ -939,25 +931,26 @@
}
}

.jetpack-field-multiple__list.jetpack-field-multiple__list--has-border {
padding-left: var(--jetpack--contact-form--animated-left-offset);
}

&.jetpack-field-multiple {

.animated-label__label {
position: static;
}
&.jetpack-field-multiple .animated-label__label {
position: static;
}

&.jetpack-field-textarea .animated-label__label {
transform: unset;
top: var(--jetpack--contact-form--input-padding-top, 16px);
top: calc(2px + var(--jetpack--contact-form--border-top-size, 1px));
}

&.is-selected, &.has-placeholder {

.animated-label__label {
font-size: 0.75em;
transform: translateY(0);
top: calc(2px + var(--jetpack--contact-form--border-size));
top: calc(2px + var(--jetpack--contact-form--border-top-size, 1px));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,45 @@ function getBorderRadiusCssVar( style ) {
}
}

/**
* Returns the border widths for the input block.
*
* @param {object} blockBorderStyles - The attributes of the input block.
* @param {object} globalBorderStyles - The global styles.
* @return {object} The border widths.
*/
function getBorderWidths( blockBorderStyles, globalBorderStyles ) {
/*
* Use `getIntAsPxValue` on the borderWidth as legacy form blocks might not have a value unit.
* Sides (top, right, bottom, left) are available post-upgrade.
*/
const borderWidth =
getIntAsPxValue( blockBorderStyles?.borderWidth ) || globalBorderStyles?.borderWidth;

if ( borderWidth ) {
return {
borderTopWidth: borderWidth,
borderRightWidth: borderWidth,
borderBottomWidth: borderWidth,
borderLeftWidth: borderWidth,
};
}

const borderTopWidth = blockBorderStyles?.borderTopWidth || globalBorderStyles?.borderTopWidth;
const borderRightWidth =
blockBorderStyles?.borderRightWidth || globalBorderStyles?.borderRightWidth;
const borderBottomWidth =
blockBorderStyles?.borderBottomWidth || globalBorderStyles?.borderBottomWidth;
const borderLeftWidth = blockBorderStyles?.borderLeftWidth || globalBorderStyles?.borderLeftWidth;

return {
borderTopWidth,
borderRightWidth,
borderBottomWidth,
borderLeftWidth,
};
}

/**
* Returns properties that help achieve the outlined and animated form styles.
*
Expand Down Expand Up @@ -95,29 +134,31 @@ export default function useVariationStyleProperties( {
[ inputBaseGlobalStyles, inputUserGlobalStyles ]
);

// Add a class to apply padding to option groups that have a border.
const customBorderClasses = useMemo( () => {
return useMemo( () => {
// Access the input block's attributes.
const blockBorderClassesAndStyles = getBorderClassesAndStyles( inputBlockAttributes ?? {} );
const globalBorderClassesAndStyles = getBorderClassesAndStyles( {
style: mergedGlobalBlockStyles,
} );
const borderWidths = getBorderWidths(
blockBorderClassesAndStyles?.style,
globalBorderClassesAndStyles?.style
);

// Add a class to apply padding to option groups that have a border.
const hasBorder =
inputBlockName === 'jetpack/options' &&
( !! inputBlockAttributes?.style?.border?.width ||
!! inputBlockAttributes?.style?.border?.left?.width ||
!! mergedGlobalBlockStyles?.border?.width ||
!! mergedGlobalBlockStyles?.border?.left?.width );
return hasBorder ? 'jetpack-field-multiple__list--has-border' : '';
}, [ inputBlockName, inputBlockAttributes, mergedGlobalBlockStyles ] );
!! borderWidths?.borderLeftWidth &&
parseInt( borderWidths?.borderLeftWidth ) > 0;

const customBorderClasses = hasBorder ? 'jetpack-field-multiple__list--has-border' : '';

return useMemo( () => {
// Only return styles for outlined and animated forms.
if ( formStyle !== FORM_STYLE.OUTLINED && formStyle !== FORM_STYLE.ANIMATED ) {
return {
className: customBorderClasses,
};
}
// Access the input block's attributes.
const blockBorderClassesAndStyles = getBorderClassesAndStyles( inputBlockAttributes ?? {} );
const globalBorderClassesAndStyles = getBorderClassesAndStyles( {
style: mergedGlobalBlockStyles,
} );

// Notched HTML only needs the background color and associated classes.
const attributesWithBackgroundColor = inputBlockAttributes
Expand Down Expand Up @@ -152,17 +193,14 @@ export default function useVariationStyleProperties( {
};
}
if ( formStyle === FORM_STYLE.ANIMATED ) {
const borderLeftSize =
getIntAsPxValue( blockBorderClassesAndStyles?.style?.borderWidth ) ||
blockBorderClassesAndStyles?.style?.borderLeftWidth ||
globalBorderClassesAndStyles?.style?.borderWidth ||
globalBorderClassesAndStyles?.style?.borderLeftWidth ||
'1px';
const hasTopBorder =
!! borderWidths?.borderTopWidth && parseInt( borderWidths?.borderTopWidth ) > 0;
styleSpecificCssVars = {
'--jetpack--contact-form--left-offset': `calc(var(--jetpack--contact-form--input-padding-left, 16px) + ${ borderLeftSize })`,
'--jetpack--contact-form--label-left':
'max(var(--jetpack--contact-form--left-offset), var(--jetpack--contact-form--border-radius))',
'--jetpack--contact-form--field-padding': `calc(var(--jetpack--contact-form--label-left) - ${ borderLeftSize })`,
// For the animated labels.
'--jetpack--contact-form--animated-left-offset': '16px', // Probably can be removed or we use `--jetpack--contact-form--input-padding`
'--jetpack--contact-form--animated-top-offset': hasTopBorder
? 'calc(var(--jetpack--contact-form--border-top-size) + var(--jetpack--contact-form--animated-left-offset))'
: '50%',
};
}

Expand All @@ -174,18 +212,16 @@ export default function useVariationStyleProperties( {
backgroundColor: blockColorClassesAndStyles?.style?.backgroundColor,
},
cssVars: {
// Sets the value of top: calc(var(--jetpack--contact-form--border-size) * -1) for .notched-label__label.
'--jetpack--contact-form--border-size':
getIntAsPxValue( blockBorderClassesAndStyles?.style?.borderWidth ) ||
blockBorderClassesAndStyles?.style?.borderTopWidth ||
globalBorderClassesAndStyles?.style?.borderWidth ||
globalBorderClassesAndStyles?.style?.borderTopWidth,
// Sets the value of --notch-width: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius)); for .notched-label.
'--jetpack--contact-form--border-radius':
getBorderRadiusCssVar( blockBorderClassesAndStyles.style ) ||
getBorderRadiusCssVar( globalBorderClassesAndStyles.style ),
'--jetpack--contact-form--border-left-size': borderWidths?.borderLeftWidth,
'--jetpack--contact-form--border-right-size': borderWidths?.borderRightWidth,
'--jetpack--contact-form--border-top-size': borderWidths?.borderTopWidth,
'--jetpack--contact-form--border-bottom-size': borderWidths?.borderBottomWidth,
...styleSpecificCssVars,
},
};
}, [ inputBlockAttributes, mergedGlobalBlockStyles, formStyle, customBorderClasses ] );
}, [ inputBlockAttributes, mergedGlobalBlockStyles, formStyle, inputBlockName ] );
}
128 changes: 73 additions & 55 deletions projects/packages/forms/src/contact-form/class-contact-form-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public function render() {
// For Outline style support.
$form_style = $this->get_form_style();
if ( 'outlined' === $form_style || 'animated' === $form_style ) {
$output_data = $this->get_outline_styles( $form_style );
$output_data = $this->get_form_variation_style_properties( $form_style );
$this->block_styles .= $output_data['css_vars'];
}
} else {
Expand Down Expand Up @@ -1506,7 +1506,7 @@ public function render_outline_label( $id, $label, $required, $required_field_te
$classes .= $this->is_error() ? ' form-error' : '';
$classes .= $this->label_classes ? ' ' . $this->label_classes : '';

$output_data = $this->get_outline_styles();
$output_data = $this->get_form_variation_style_properties();

return '
<div class="notched-label">
Expand All @@ -1528,6 +1528,7 @@ class=" ' . $classes . '"

/**
* Returns the styles, classes and CSS vars necessary to render fields in the "Outlined" style.
* The "Animated" style variation shares the CSS vars, which require similar calculations for the left offset and label left position.
* At the block level, the styles are extracted and added to the shortcode attributes in
* Contact_Form_Plugin::get_outlined_style_attributes().
* This function extracts those styles and applies them to the field,
Expand All @@ -1541,7 +1542,7 @@ class=" ' . $classes . '"
* @type string $class_name The class name.
* }
*/
private function get_outline_styles( $form_style = 'outlined' ) {
private function get_form_variation_style_properties( $form_style = 'outlined' ) {
$style_attrs = '';
$css_vars = '';
$outline_styles = $this->get_attribute( 'outlinestyledata' );
Expand All @@ -1552,61 +1553,78 @@ private function get_outline_styles( $form_style = 'outlined' ) {
$block_name = 'jetpack/options';
}

if ( ! empty( $outline_styles ) ) {
$outline_styles = json_decode( html_entity_decode( $outline_styles, ENT_COMPAT ), true );
$input_styles = $this->get_attribute( 'inputstyles' );

if ( ! empty( $input_styles ) ) {
$style_attrs = $input_styles;
}

$global_styles = wp_get_global_styles(
array( 'border' ),
array(
'block_name' => $block_name,
'transforms' => array( 'resolve-variables' ),
)
);

$legacy_border_size = $outline_styles['border']['width'] ?? null;
$legacy_border_size = is_numeric( $legacy_border_size ) ? $legacy_border_size . 'px' : $legacy_border_size;
$legacy_border_radius = $outline_styles['border']['radius'] ?? null;
$legacy_border_radius = is_numeric( $legacy_border_radius ) ? $legacy_border_radius . 'px' : $legacy_border_radius;

$border_size = $legacy_border_size ??
$outline_styles['border']['top']['width'] ??
$global_styles['width'] ??
$global_styles['top']['width'];

$border_radius = $legacy_border_radius ??
$global_styles['radius'];

$css_vars = $border_size ? '--jetpack--contact-form--border-size: ' . $border_size . ';' : '';
// Check if border radius is split or a single value.
if ( is_array( $border_radius ) ) {
// If corner radii are set on the top-left or bottom-left of the block, take the maximum of the two.
// We check the left side due to writing direction—this variable is used to offset text.
// TODO: this should factor in RTL languages.
$css_vars .= $border_radius ? '--jetpack--contact-form--border-radius: max(' . $border_radius['topLeft'] . ',' . $border_radius['bottomLeft'] . ');' : '';
} elseif ( isset( $border_radius ) ) {
$css_vars .= $border_radius ? '--jetpack--contact-form--border-radius: ' . $border_radius . ';' : '';
}
$outline_styles = ! empty( $outline_styles ) ? json_decode( html_entity_decode( $outline_styles, ENT_COMPAT ), true ) : array();
$input_styles = $this->get_attribute( 'inputstyles' );

if ( 'outlined' === $form_style ) {
$css_vars .= '--jetpack--contact-form--notch-width: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius));';
} elseif ( 'animated' === $form_style ) {
$legacy_border_left_size = $outline_styles['border']['width'] ?? null;
$legacy_border_left_size = is_numeric( $legacy_border_left_size ) ? $legacy_border_left_size . 'px' : $legacy_border_left_size;
if ( ! empty( $input_styles ) ) {
$style_attrs = $input_styles;
}

$border_left_size = $legacy_border_left_size ??
$outline_styles['border']['left']['width'] ??
$global_styles['width'] ??
$global_styles['left']['width'];
$global_styles = wp_get_global_styles(
array( 'border' ),
array(
'block_name' => $block_name,
'transforms' => array( 'resolve-variables' ),
)
);

$css_vars .= "--jetpack--contact-form--left-offset: calc(var(--jetpack--contact-form--input-padding-left, 16px) + {$border_left_size});";
$css_vars .= '--jetpack--contact-form--label-left: max(var(--jetpack--contact-form--left-offset), var(--jetpack--contact-form--border-radius));';
$css_vars .= "--jetpack--contact-form--field-padding: calc(var(--jetpack--contact-form--label-left) - {$border_left_size});";
}
/*
* The `borderwidth` attribute contains the border value that forms used before the migration to global styles.
* Any old forms saved in a post will still use this attribute, so it needs to be factored into the css vars for border
* to properly support backwards compatibility. So we check if the attribute is set and if it's not empty or '0', which is a valid width value.
* For newer forms that use global styles or the block supports styles, this value will be empty and is ignored.
*/
$border_width_attribute = $this->get_attribute( 'borderwidth' );
$legacy_border_size = ! empty( $border_width_attribute ) || $border_width_attribute === '0' ? $border_width_attribute . 'px' : $outline_styles['border']['width'] ?? null;
$legacy_border_size = is_numeric( $legacy_border_size ) ? $legacy_border_size . 'px' : $legacy_border_size;
$legacy_border_radius = $outline_styles['border']['radius'] ?? null;
$legacy_border_radius = is_numeric( $legacy_border_radius ) ? $legacy_border_radius . 'px' : $legacy_border_radius;

$border_top_size = $legacy_border_size ??
$outline_styles['border']['top']['width'] ??
$global_styles['width'] ??
$global_styles['top']['width'] ?? null;

$border_right_size = $legacy_border_size ??
$outline_styles['border']['right']['width'] ??
$global_styles['width'] ??
$global_styles['right']['width'] ?? null;

$border_bottom_size = $legacy_border_size ??
$outline_styles['border']['bottom']['width'] ??
$global_styles['width'] ??
$global_styles['bottom']['width'] ?? null;

$border_left_size = $legacy_border_size ??
$outline_styles['border']['left']['width'] ??
$global_styles['width'] ??
$global_styles['left']['width'] ?? null;

$border_radius = $legacy_border_radius ??
$global_styles['radius'] ?? null;

$css_vars = $border_top_size ? '--jetpack--contact-form--border-size: ' . $border_top_size . ';' : '';
$css_vars .= $border_top_size ? '--jetpack--contact-form--border-top-size: ' . $border_top_size . ';' : '';
$css_vars .= $border_right_size ? '--jetpack--contact-form--border-right-size: ' . $border_right_size . ';' : '';
$css_vars .= $border_bottom_size ? '--jetpack--contact-form--border-bottom-size: ' . $border_bottom_size . ';' : '';
$css_vars .= $border_left_size ? '--jetpack--contact-form--border-left-size: ' . $border_left_size . ';' : '';

// Check if border radius is split or a single value.
if ( is_array( $border_radius ) ) {
// If corner radii are set on the top-left or bottom-left of the block, take the maximum of the two.
// We check the left side due to writing direction—this variable is used to offset text.
// TODO: this should factor in RTL languages.
$css_vars .= $border_radius ? '--jetpack--contact-form--border-radius: max(' . $border_radius['topLeft'] . ',' . $border_radius['bottomLeft'] . ');' : '';
} elseif ( isset( $border_radius ) ) {
$css_vars .= $border_radius ? '--jetpack--contact-form--border-radius: ' . $border_radius . ';' : '';
}

if ( 'outlined' === $form_style ) {
$css_vars .= '--jetpack--contact-form--notch-width: max(var(--jetpack--contact-form--input-padding-left, 16px), var(--jetpack--contact-form--border-radius));';
} elseif ( 'animated' === $form_style ) {
$css_vars .= '--jetpack--contact-form--animated-left-offset: 16px;';
$css_vars .= '--jetpack--contact-form--animated-top-offset:' . $border_top_size ? 'calc(var(--jetpack--contact-form--border-top-size) + var(--jetpack--contact-form--animated-left-offset));'
: '50%;';
}

return array(
Expand Down
Loading