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
72 changes: 70 additions & 2 deletions classes/helpers/FrmStylesHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -852,10 +852,12 @@ public static function get_base_font_size_settings() {
/**
* Get the style settings that the styler preview can update with JavaScript, without requesting new CSS.
* A setting only belongs here when its value is output as a CSS variable and nothing else.
* Settings used directly in a CSS template (in a condition, a calculation, or a raw value) need the server.
* This includes the templates from Pro versions that are still supported, so those settings are left out until Pro outputs them only as CSS variables.
* Settings used directly in a CSS template (in a calculation or a raw value) need the server.
* A template may still check if a setting is empty, since the preview requests new CSS when a value is or was empty.
* This includes the templates from Pro versions that are still supported, so the settings Pro uses directly are only added when Pro can remove them again.
*
* @since 7.1
* @since x.x Added the settings the Lite templates now output only as CSS variables.
*
* @return array<string>
*/
Expand Down Expand Up @@ -921,8 +923,39 @@ public static function get_live_preview_settings() {
'success_border_color',
'success_font_size',
'progress_border_size',
'font',
'width',
'description_font_size',
'description_color',
'description_weight',
'description_style',
'description_align',
'description_margin',
'field_width',
'field_margin',
'border_color',
'border_radius',
'border_width_error',
'check_label_color',
'check_weight',
'check_font_size',
'submit_width',
'submit_height',
'submit_border_width',
'submit_shadow_color',
);

$pro_outputs_style = has_action( 'frm_output_single_style', 'FrmProStylesController::output_single_style' );

if ( ! $pro_outputs_style ) {
$settings = array_merge( $settings, self::get_live_preview_settings_without_pro() );
}

// Pro 7.0.1 and later remove the settings their templates use directly with the filter below.
if ( ! $pro_outputs_style || has_filter( 'frm_style_live_preview_settings', 'FrmProStylesController::live_preview_settings' ) ) {
$settings = array_merge( $settings, self::get_live_preview_settings_filtered_by_pro() );
}

/**
* Filter the style settings that the styler preview can update with JavaScript.
* Remove any setting that a frm_output_single_style callback uses directly, so it still gets new CSS from the server.
Expand All @@ -938,6 +971,41 @@ public static function get_live_preview_settings() {
return is_array( $settings ) ? array_values( array_unique( array_filter( $settings, 'is_string' ) ) ) : array();
}

/**
* Get the style settings that every Pro version uses directly in its style template.
* Lite only uses these in CSS variables, so the preview updates them with JavaScript when Pro is not active.
*
* @since x.x
*
* @return array<string>
*/
private static function get_live_preview_settings_without_pro() {
return array(
'field_height',
'field_border_width',
);
}

/**
* Get the style settings that Pro versions before 7.0.1 use directly in their style template.
* Pro 7.0.1 and later remove the ones they still use directly with the frm_style_live_preview_settings filter.
*
* @since x.x
*
* @return array<string>
*/
private static function get_live_preview_settings_filtered_by_pro() {
return array(
'font_size',
'label_color',
'text_color',
'border_color_active',
'field_pad',
'error_text',
'success_text_color',
);
}

/**
* Get style font size scale value.
*
Expand Down
49 changes: 27 additions & 22 deletions css/_single_theme.css.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,28 @@
.<?php echo esc_html( $style_class ); ?> .frm_error,
.<?php echo esc_html( $style_class ); ?> .frm_pro_max_limit_desc{
<?php if ( ! empty( $description_margin ) ) { ?>
margin:<?php echo esc_html( $description_margin . $important ); ?>;
margin:var(--description-margin)<?php echo esc_html( $important ); ?>;
<?php } else { ?>
margin-top: 6px;
<?php } ?>
padding:0;
<?php if ( ! empty( $font ) ) { ?>
font-family:<?php FrmAppHelper::kses_echo( $font . $important ); ?>;
font-family:var(--font)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $description_font_size ) ) { ?>
font-size:<?php echo esc_html( $description_font_size . $important ); ?>;
font-size:var(--description-font-size)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $description_color ) ) { ?>
color:<?php echo esc_html( $description_color . $important ); ?>;
color:var(--description-color)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $description_weight ) ) { ?>
font-weight:<?php echo esc_html( $description_weight . $important ); ?>;
font-weight:var(--description-weight)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $description_align ) ) { ?>
text-align:<?php echo esc_html( $description_align . $important ); ?>;
text-align:var(--description-align)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $description_style ) ) { ?>
font-style:<?php echo esc_html( $description_style . $important ); ?>;
font-style:var(--description-style)<?php echo esc_html( $important ); ?>;
<?php } ?>
max-width:100%;
}
Expand All @@ -103,11 +103,11 @@
<?php } ?>

.<?php echo esc_html( $style_class ); ?> .frm_form_field.frm_left_container{
grid-template-columns: <?php echo esc_html( $width ); ?> auto;
grid-template-columns: var(--width) auto;
}

.<?php echo esc_html( $style_class ); ?> .frm_form_field.frm_right_container{
grid-template-columns: auto <?php echo esc_html( $width ); ?>;
grid-template-columns: auto var(--width);
}

.<?php echo esc_html( $style_class ); ?> .frm_inline_container.frm_dynamic_select_container .frm_data_container,
Expand All @@ -128,16 +128,16 @@
<?php if ( $pro_is_installed ) : ?>
.<?php echo esc_html( $style_class ); ?> .frm_scale label{
<?php if ( ! empty( $check_weight ) ) { ?>
font-weight:<?php echo esc_html( $check_weight . $important ); ?>;
font-weight:var(--check-weight)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $font ) ) { ?>
font-family:<?php FrmAppHelper::kses_echo( $font . $important ); ?>;
font-family:var(--font)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $check_font_size ) ) { ?>
font-size:<?php echo esc_html( $check_font_size . $important ); ?>;
font-size:var(--check-font-size)<?php echo esc_html( $important ); ?>;
<?php } ?>
<?php if ( ! empty( $check_label_color ) ) { ?>
color:<?php echo esc_html( $check_label_color . $important ); ?>;
color:var(--check-label-color)<?php echo esc_html( $important ); ?>;
<?php } ?>
}
<?php endif; ?>
Expand Down Expand Up @@ -183,6 +183,10 @@
outline: none;
<?php } else { ?>
box-shadow:0px 0px 5px 0px rgba(<?php echo esc_html( FrmStylesHelper::hex2rgb( $border_color_active ) ); ?>, 0.6);
<?php
// The same glow from the CSS variable, so the styler preview can update it. Browsers without color-mix() keep the line above.
?>
box-shadow:0px 0px 5px 0px color-mix(in srgb, var(--border-color-active) 60%, transparent);
<?php } ?>
}

Expand All @@ -191,6 +195,7 @@
-webkit-box-shadow: none<?php echo esc_html( $important ); ?>;
<?php } else { ?>
-webkit-box-shadow: 0px 0px 5px 0px rgba(<?php echo esc_html( FrmStylesHelper::hex2rgb( $border_color_active ) ); ?>, 0.6);
-webkit-box-shadow: 0px 0px 5px 0px color-mix(in srgb, var(--border-color-active) 60%, transparent);
<?php } ?>
}

Expand All @@ -203,16 +208,16 @@
.<?php echo esc_html( $style_class ); ?> .frm_submit input[type=button],
.<?php echo esc_html( $style_class ); ?> .frm_submit button,
.frm_form_submit_style {
width:<?php echo esc_html( ( $submit_width == '' ? 'auto' : $submit_width ) . $important ); // phpcs:ignore Universal.Operators.StrictComparisons ?>;
width:var(--submit-width,<?php echo esc_html( '' === $submit_width ? 'auto' : $submit_width ); ?>)<?php echo esc_html( $important ); ?>;
<?php if ( ! empty( $font ) ) { ?>
font-family:<?php FrmAppHelper::kses_echo( $font ); ?>;
font-family:var(--font,<?php FrmAppHelper::kses_echo( $font ); ?>);
<?php } ?>
<?php
// These use CSS variables so the styler preview can update them without new CSS from the server.
// The fallback values are for .frm_form_submit_style when it is used outside of a styled form.
?>
font-size:var(--submit-font-size,<?php echo esc_html( $submit_font_size ); ?>)<?php echo esc_html( $important ); ?>;
height:<?php echo esc_html( $submit_height . $important ); ?>;
height:var(--submit-height,<?php echo esc_html( $submit_height ); ?>)<?php echo esc_html( $important ); ?>;
line-height:normal<?php echo esc_html( $important ); ?>;
text-align:center;
background:
Expand All @@ -226,7 +231,7 @@
?>
;
<?php if ( '' !== $submit_border_width ) : ?>
border-width:<?php echo esc_html( $submit_border_width ); ?>;
border-width:var(--submit-border-width,<?php echo esc_html( $submit_border_width ); ?>);
<?php endif; ?>
border-color:var(--submit-border-color,<?php echo esc_html( $submit_border_color ); ?>)<?php echo esc_html( $important ); ?>;
border-style:solid;
Expand All @@ -238,7 +243,7 @@
padding:var(--submit-padding,<?php echo esc_html( $submit_padding ); ?>)<?php echo esc_html( $important ); ?>;
box-sizing:border-box;
<?php if ( ! empty( $submit_shadow_color ) ) { ?>
box-shadow:0 1px 1px <?php echo esc_html( $submit_shadow_color ); ?>;
box-shadow:0 1px 1px var(--submit-shadow-color,<?php echo esc_html( $submit_shadow_color ); ?>);
<?php } ?>
margin:<?php echo esc_html( $submit_margin ); ?>;
<?php
Expand Down Expand Up @@ -331,7 +336,7 @@
.<?php echo esc_html( $style_class ); ?> .frm_submit.frm_inline_submit::before {
content:"before";
<?php if ( ! empty( $font ) ) { ?>
font-family:<?php FrmAppHelper::kses_echo( $font ); ?>;
font-family:var(--font);
<?php } ?>
font-size:var(--font-size)<?php echo esc_html( $important ); ?>;
color:var(--label-color)<?php echo esc_html( $important ); ?>;
Expand All @@ -356,7 +361,7 @@
?>
.<?php echo esc_html( $style_class ); ?> #frm_field_cptch_number_container{
<?php if ( ! empty( $font ) ) { ?>
font-family:<?php FrmAppHelper::kses_echo( $font ); ?>;
font-family:var(--font);
<?php } ?>
font-size:var(--font-size)<?php echo esc_html( $important ); ?>;
color:var(--label-color)<?php echo esc_html( $important ); ?>;
Expand Down Expand Up @@ -481,7 +486,7 @@

<?php if ( $pro_is_installed ) { ?>
.<?php echo esc_html( $style_class ); ?> #frm_loading .progress-striped .progress-bar{
background-image:linear-gradient(45deg, <?php echo esc_html( $border_color ); ?> 25%, rgba(0, 0, 0, 0) 25%, rgba(0, 0, 0, 0) 50%, <?php echo esc_html( $border_color ); ?> 50%, <?php echo esc_html( $border_color ); ?> 75%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0));
background-image:linear-gradient(45deg, var(--border-color) 25%, rgba(0, 0, 0, 0) 25%, rgba(0, 0, 0, 0) 50%, var(--border-color) 50%, var(--border-color) 75%, rgba(0, 0, 0, 0) 75%, rgba(0, 0, 0, 0));
}

.<?php echo esc_html( $style_class ); ?> #frm_loading .progress-bar{
Expand All @@ -493,7 +498,7 @@
.<?php echo esc_html( $style_class ); ?> .frm_form_field.frm_total_big textarea,
.<?php echo esc_html( $style_class ); ?> .frm_form_field.frm_total input,
.<?php echo esc_html( $style_class ); ?> .frm_form_field.frm_total textarea{
color: <?php echo esc_html( $text_color . $important ); ?>;
color: var(--text-color)<?php echo esc_html( $important ); ?>;
background-color:transparent<?php echo esc_html( $important ); ?>;
border:none<?php echo esc_html( $important ); ?>;
display:inline<?php echo esc_html( $important ); ?>;
Expand Down
87 changes: 79 additions & 8 deletions js/admin/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -1528,9 +1528,10 @@
return;
}

const previousValues = livePreview.values;
livePreview.values = values;

const liveKeys = changedKeys.filter( key => canUpdateLive( key, values ) );
const liveKeys = changedKeys.filter( key => canUpdateLive( key, values, previousValues ) );
if ( liveKeys.length !== changedKeys.length ) {
debouncedChangeStyling();
}
Expand All @@ -1541,9 +1542,12 @@

livePreview.updateCount++;
liveKeys.forEach( key => {
livePreview.vars.set( getCssVarName( key ), {
value: prepareLiveValue( key, values[ key ] ),
updateCount: livePreview.updateCount
const value = prepareLiveValue( key, values[ key ] );
getCssVarNames( key, values ).forEach( name => {
livePreview.vars.set( name, {
value,
updateCount: livePreview.updateCount
} );
} );
} );
renderLiveVars();
Expand Down Expand Up @@ -1601,20 +1605,69 @@
* Check if a setting can be updated without new CSS from the server.
*
* @since 7.1
* @since x.x Added the previousValues parameter.
*
* @param {string} key The setting key.
* @param {Object} values All current setting values.
* @param {string} key The setting key.
* @param {Object} values All current setting values.
* @param {Object} previousValues The setting values the preview reflected before this change.
* @return {boolean} True if the CSS variable can be set with JavaScript.
*/
function canUpdateLive( key, values ) {
if ( ! livePreview.settings.has( key ) || ! isSafeLiveValue( values[ key ] ) ) {
function canUpdateLive( key, values, previousValues ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This signature change breaks tests/js/style-live-preview.test.mjs (14/14 on master, 5/14 here; CI skipped it). Fix in that file:

  • names (line 12): add 'switchesTemplateBranch', 'usesFieldShapeValue', 'getCssVarNames' so the extracted canUpdateLive and updatePreview can find them.
  • loadDecisions(): pass a previous-values object, e.g. canUpdateLive( key, values, values ) in the existing cases, and add getCssVarNames to the updatePreview globals in loadPreview() (getCssVarNames: key => [ key ]).
  • Add cases for: a change from '' to a value and a value to '' needs the server; field_height/submit_width/submit_height set to auto (new or previous) needs the server; border_radius under circle/regular/underline and field_border_width/border_width_error under underline need the server; getCssVarNames( 'field_width', { auto_width: '0' } ) returns both variables and with auto_width: '1' returns one.

if ( ! livePreview.settings.has( key ) ) {
return false;
}

// The templates leave out some rules when a value is empty, so a change to or from an empty value needs the server.
if ( ! isSafeLiveValue( values[ key ] ) || ! isSafeLiveValue( previousValues[ key ] ) ) {
return false;
}

if ( switchesTemplateBranch( key, values[ key ] ) || switchesTemplateBranch( key, previousValues[ key ] ) ) {
return false;
}

if ( usesFieldShapeValue( key, values ) ) {
return false;
}

// The server scales these font sizes from the base font size when it is in use.
return ! livePreview.baseFontSizeSettings.has( key ) || ! usesBaseFontSize( values );
}

/**
* The CSS templates add or skip rules when these settings are set to auto, so those values need the server.
* This matches the checks for field_height, submit_width and submit_height in css/_single_theme.css.php.
*
* @since x.x
*
* @param {string} key The setting key.
* @param {string} value The setting value.
* @return {boolean} True if the value changes which rules the template outputs.
*/
function switchesTemplateBranch( key, value ) {
const autoSettings = [ 'field_height', 'submit_width', 'submit_height' ];
return autoSettings.includes( key ) && 'auto' === value.trim();
}

/**
* This matches the field shape overrides in FrmStylesHelper::css_var_prepare_value.
*
* @since x.x
*
* @param {string} key The setting key.
* @param {Object} values All current setting values.
* @return {boolean} True if the server replaces the setting value because of the field shape.
*/
function usesFieldShapeValue( key, values ) {
const shape = values.field_shape_type;

if ( 'border_radius' === key ) {
return [ 'underline', 'regular', 'circle' ].includes( shape );
}

return [ 'field_border_width', 'border_width_error' ].includes( key ) && 'underline' === shape;
}

/**
* This matches the check in FrmStylesHelper::update_base_font_size.
*
Expand Down Expand Up @@ -1674,6 +1727,24 @@
return `--${ key.replace( /_/g, '-' ).replace( /[^a-zA-Z0-9_-]/g, '' ) }`;
}

/**
* Get every CSS variable that a setting sets.
* The auto width variable uses the field width unless automatic width is on, as in FrmStylesHelper::get_settings_for_output.
*
* @since x.x
*
* @param {string} key The setting key.
* @param {Object} values All current setting values.
* @return {string[]} The CSS variable names.
*/
function getCssVarNames( key, values ) {
const names = [ getCssVarName( key ) ];
if ( 'field_width' === key && ( ! values.auto_width || '0' === values.auto_width ) ) {
names.push( getCssVarName( 'auto_width' ) );
}
return names;
}

/**
* Write the CSS variables set with JavaScript to a style tag after #this_css, so they take priority over the last server response.
*
Expand Down
Loading
Loading