-
Notifications
You must be signed in to change notification settings - Fork 328
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
js_vars add class function #1137
Conversation
Added possibility to set element class within the js_vars
Added possibility to callback a custom function containing the dom element, new value and old value. Example of usage: 'js_vars' => array( array( 'element' => '.woofc', 'function' => 'function(elem, val, oldval) { if(val) { jQuery(elem).addClass("visible"); }else{ jQuery(elem).removeClass("hidden"); } }' ) )
…ses new Function() which is similar to eval().
Hi Guys, I am new to pull requests and first time hearing of the Travis CI. Will you consider adding this feature in the next update ? I am currently using it but I rather include kirki as a plugin instead of directly within my project. Thanks |
You can now use I opted to do it this way instead of adding a new function as this is more abstract and will allow greater flexibility. So your example above would become like this: 'js_vars' => array(
array(
'element' => '.selector',
'function' => 'html',
'attr' => 'class',
'value_pattern' => '%value% pos-$',
),
), |
I tried this using your example above:
I'm getting: Instead of: Also upon each setting change the class concatenates rather than replacing the previous value:
So when this works and $ does return the value, the result would still be as follows:
It should be:
|
After a lot of experimentation, getting the previous classes via JS is quite problematic and it looks a bit overkill. It works fine like this: Kirki::add_field( 'kirki_demo', array(
'type' => 'dashicons',
'settings' => 'dashicons0',
'label' => __( 'Dashicons Control', 'my_textdomain' ),
'description' => esc_attr__( 'This control is using postMessage & js_vars', 'kirki-demo' ),
'section' => 'dashicons',
'default' => 'format-status',
'priority' => 10,
'transport' => 'postMessage',
'js_vars' => array(
array(
'element' => '.control-section-demo.dashicons .dashicons0 .dashicons',
'function' => 'html',
'attr' => 'class',
'value_pattern' => 'dashicons dashicons-$',
),
),
) ); In other words, just add the full value in the Example implementation: |
I needed to be able to set the value as a class using the js_vars, I know that it's possible using function => "html" with the "attr" => "class" argument, however this will override all classes, not very convenient. So I added a new function "class".
Example of usage:
This will apply the appropriate class based on the value. Also supports prefix and suffix.