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

feat/typography typescale #3482

Closed
wants to merge 54 commits into from
Closed

feat/typography typescale #3482

wants to merge 54 commits into from

Conversation

Arukuen
Copy link
Contributor

@Arukuen Arukuen commented Apr 4, 2025

No description provided.

[email protected] and others added 30 commits March 7, 2025 21:08
Copy link

github-actions bot commented Apr 4, 2025

🤖 Pull request artifacts

file commit
pr3482-stackable-3482-merge.zip 842c743

github-actions bot added a commit that referenced this pull request Apr 4, 2025
github-actions bot added a commit that referenced this pull request Apr 6, 2025
github-actions bot added a commit that referenced this pull request Apr 10, 2025
github-actions bot added a commit that referenced this pull request Apr 10, 2025
github-actions bot added a commit that referenced this pull request Apr 10, 2025
@@ -284,6 +287,8 @@ export const Controls = props => {
video: 'column-gap',
description: __( 'Sets the distance between two or more columns', i18n ),
} }
marks={ presetMarks }
hasCSSVariableValue={ true }
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we had a discussion before to remove this prop hasCSSVariableValue since it can just be detected from the value

$this->form_tag_selector( 'p' ), // Core text.
$this->form_tag_selector( 'li' ), // Core lists.
$this->form_tag_selector( 'td' ) // Core table cells.
$is_apply_body_to_html = get_option( 'stackable_is_apply_body_to_html' ) ?? false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe get_option() is never null since it returns false if the value is not yet set. So we can do just:

Suggested change
$is_apply_body_to_html = get_option( 'stackable_is_apply_body_to_html' ) ?? false;
$is_apply_body_to_html = get_option( 'stackable_is_apply_body_to_html' );

Can you also move this code to the if statement where it is used to make it closer and easier to read.


private function load_presets( $json_path ) {
if ( file_exists( $json_path ) ) {
$json_data = file_get_contents( $json_path );
Copy link
Contributor

Choose a reason for hiding this comment

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

Use wp_json_file_decode instead

if ( customPresets && typeof customPresets === 'object' ) {
renderGlobalStyles( customPresets, setStyles )
}
}, [ JSON.stringify( customPresets ) ] )
Copy link
Contributor

Choose a reason for hiding this comment

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

This might work without needing to do JSON.stringify()

Suggested change
}, [ JSON.stringify( customPresets ) ] )
}, [ customPresets ] )

Comment on lines +45 to +51
$this->custom_presets = get_option( 'stackable_global_custom_preset_controls' );
$this->theme_presets = WP_Theme_JSON_Resolver::get_theme_data()->get_settings();
$this->default_presets = WP_Theme_JSON_Resolver::get_core_data()->get_settings();
$this->stackable_presets = $this->load_presets( __DIR__ . '/presets.json');

add_filter( 'stackable_inline_styles_nodep', array( $this, 'add_preset_controls_styles' ) );
add_filter( 'stackable_inline_editor_styles', array( $this, 'add_preset_controls_styles' ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

These will be called even in the front end where I don't think the presets are needed.

I recommend to place the code the populates custom_presets, theme_presets etc into a new function:

public function load_presets() {
    $this->custom_presets = get_option( 'stackable_global_custom_preset_controls' );
    // ...
}

Then calling $this->load_presets() when the values are about to be used.

This is so that the loading process will only be called when needed (currently this performed for every page load)

return ! is_array( $input ) ? array( array() ) : $input;
}

private function load_presets( $json_path ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe change this function name since it's not actually loading presets, it's just loading the provided json file

? $preset['size']
: "var(--wp--preset--$prefix--$slug)";

$css .= "--stk--preset--$prefix--$slug: $value;\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of building the css from strings, you can return an object here instead, then later on use wp_style_engine_get_stylesheet_from_css_rules to build the css so we get the benefit of sanitation.

Comment on lines +121 to +123
public function deepGet( $array, $keys ) {
return array_reduce( $keys, fn( $value, $key ) => $value[ $key ] ?? null, $array );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

If you use wp_style_engine_get_stylesheet_from_css_rules from the previous comment, you might not need to use this anymore

disabled: true,
},
{
label: __( '1.067 - Minor Second', i18n ),
Copy link
Contributor

Choose a reason for hiding this comment

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

Make the English portion only as translatable:

Suggested change
label: __( '1.067 - Minor Second', i18n ),
// Translators: First %s is a unit, second %s is a name of the unit
label: sprintf( __( '%s - %s', i18n ), '1.067', __( 'Minor Second', i18n ) ),

Comment on lines +202 to +243
// Update the custom presets when using typography as presets
if ( useTypographyAsPresets ) {
const fontSizePresets = TYPOGRAPHY_TAGS
.filter( ( { presetSlug } ) => !! presetSlug )
.map( ( {
selector, presetName, presetSlug,
} ) => {
const size = typographySettings[ selector ]?.fontSize ?? getDefaultFontSize( selector ) ?? 16
const unit = typographySettings[ selector ]?.fontSizeUnit ?? 'px'
return {
name: presetName,
slug: presetSlug,
size: `${ size }${ unit }`,
}
} )
// Add the preset for extra small
let xSmallSize = typographySettings[ '.stk-subtitle' ]?.fontSize ?? getDefaultFontSize( '.stk-subtitle' ) ?? 16
let xSmallUnit = typographySettings[ '.stk-subtitle' ]?.fontSizeUnit ?? 'px'
if ( xSmallUnit === 'px' ) {
xSmallSize = Math.pow( xSmallSize / 16, 2 )
xSmallUnit = 'rem'
} else {
xSmallSize = Math.pow( xSmallSize, 2 )
}

fontSizePresets.push( {
name: 'XS',
slug: 'x-small',
size: `${ xSmallSize }${ xSmallUnit ?? 'px' }`,
} )
// Reverse the presets so it's from smallest to biggest
fontSizePresets.reverse()

const newSettings = { ...allCustomPresets, fontSizes: fontSizePresets }

clearTimeout( saveTypographyAsPresetsThrottle )
saveTypographyAsPresetsThrottle = setTimeout( () => {
const settings = new models.Settings( { stackable_global_custom_preset_controls: newSettings } ) // eslint-disable-line camelcase
settings.save()
}, 300 )

dispatch( 'stackable/global-preset-controls.custom' ).updateCustomPresetControls( newSettings )
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this code updates the preset controls and belongs to the preset controls area. In the preset controls area, you can just listen to calls to stackable.global-settings.typography.update-trigger and perform preset changes accordingly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants