Skip to content

Instantly share code, notes, and snippets.

@girafffee
Last active June 12, 2024 11:22
Show Gist options
  • Save girafffee/0d4945622847e3d1321857c8780b32f7 to your computer and use it in GitHub Desktop.
Save girafffee/0d4945622847e3d1321857c8780b32f7 to your computer and use it in GitHub Desktop.
Eye icon for password input (JetFormBuilder) with Dashicons (https://developer.wordpress.org/resource/dashicons/)

image

How to apply the code

  1. Enqueue styles from the jet-form-builder-eye-icon.css to your style.css child.theme or custom plugin
  2. Enqueue script from the jet-form-builder-eye-icon.js via your child theme or custom plugin
  3. Add the code from the jet-form-builder-eye-icon.php to the your functions.php child theme or custom plugin

How to apply to existing fields

  1. Select or add Text Field block
  2. Go to the Advanced section
  3. Into the CSS Class Name option add eye-icon

image

Useful links

.jet-form-builder__field-wrap input.eye-icon + .dashicons {
position: absolute;
right: 10px;
top: calc(50% - 10px);
}
/**
* @type {Element}
*/
const visibleIcon = document.createElement( 'span' );
// replace by "dashicons-hidden" to hide
visibleIcon.classList.add( 'dashicons', 'dashicons-visibility' );
/**
* @param input {InputData}
*/
function customHandleJFBField( input ) {
if ( 'input' !== input.inputType ||
'password' !== input.nodes?.[ 0 ]?.type ||
// hasn't "eye-icon" class
!input.nodes?.[ 0 ]?.classList?.contains?.( 'eye-icon' )
) {
return;
}
const [ textInput ] = input.nodes;
const newIcon = visibleIcon.cloneNode();
textInput.parentElement.style.position = 'relative';
textInput.parentElement.append( newIcon );
newIcon.addEventListener(
'click',
/**
* @this {Element}
*/
function () {
// show password
if ( this.classList.contains( 'dashicons-visibility' ) ) {
this.classList.replace(
'dashicons-visibility',
'dashicons-hidden',
);
textInput.type = 'text';
return;
}
// hide password
this.classList.replace(
'dashicons-hidden',
'dashicons-visibility',
);
textInput.type = 'password';
},
);
}
if ( window?.JetPlugins?.hooks?.addAction ) {
JetPlugins.hooks.addAction(
'jet.fb.input.makeReactive',
'custom/password-field/eye-icon',
customHandleJFBField,
);
}
else {
document.addEventListener(
'DOMContentLoaded',
() => window?.JetPlugins?.hooks?.addAction?.(
'jet.fb.input.makeReactive',
'custom/password-field/eye-icon',
customHandleJFBField,
),
);
}
<?php
// this is required for unauthorized users
add_action( 'wp_enqueue_scripts', 'dashicons_front_end' );
if ( ! function_exists( 'dashicons_front_end' ) ) {
function dashicons_front_end() {
wp_enqueue_style( 'dashicons' );
}
}
@girafffee
Copy link
Author

girafffee commented Mar 5, 2024

If you want to use your own SVG instead of Dashicons

  1. Add this css to the existing styles from the jet-form-builder-eye-icon.css
.jet-form-builder__field-wrap input.eye-icon + .dashicons-hidden:before {
    background-image: url('data:image/svg+xml,<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" width="24" height="24" aria-hidden="true" focusable="false"><path d="M4.67 10.664s-2.09 1.11-2.917 1.582l.494.87 1.608-.914.002.002c.343.502.86 1.17 1.563 1.84.348.33.742.663 1.185.976L5.57 16.744l.858.515 1.02-1.701a9.1 9.1 0 0 0 4.051 1.18V19h1v-2.263a9.1 9.1 0 0 0 4.05-1.18l1.021 1.7.858-.514-1.034-1.723c.442-.313.837-.646 1.184-.977.703-.669 1.22-1.337 1.563-1.839l.002-.003 1.61.914.493-.87c-1.75-.994-2.918-1.58-2.918-1.58l-.003.005a8.29 8.29 0 0 1-.422.689 10.097 10.097 0 0 1-1.36 1.598c-1.218 1.16-3.042 2.293-5.544 2.293-2.503 0-4.327-1.132-5.546-2.293a10.099 10.099 0 0 1-1.359-1.599 8.267 8.267 0 0 1-.422-.689l-.003-.005Z"></path></svg>');
    content: '';
    width: 24px;
    height: 24px;
    display: block;
}
.jet-form-builder__field-wrap input.eye-icon + .dashicons-visibility:before {
    background-image: url('data:image/svg+xml,<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" width="24" height="24" aria-hidden="true" focusable="false"><path d="M3.99961 13C4.67043 13.3354 4.6703 13.3357 4.67017 13.3359L4.67298 13.3305C4.67621 13.3242 4.68184 13.3135 4.68988 13.2985C4.70595 13.2686 4.7316 13.2218 4.76695 13.1608C4.8377 13.0385 4.94692 12.8592 5.09541 12.6419C5.39312 12.2062 5.84436 11.624 6.45435 11.0431C7.67308 9.88241 9.49719 8.75 11.9996 8.75C14.502 8.75 16.3261 9.88241 17.5449 11.0431C18.1549 11.624 18.6061 12.2062 18.9038 12.6419C19.0523 12.8592 19.1615 13.0385 19.2323 13.1608C19.2676 13.2218 19.2933 13.2686 19.3093 13.2985C19.3174 13.3135 19.323 13.3242 19.3262 13.3305L19.3291 13.3359C19.3289 13.3357 19.3288 13.3354 19.9996 13C20.6704 12.6646 20.6703 12.6643 20.6701 12.664L20.6697 12.6632L20.6688 12.6614L20.6662 12.6563L20.6583 12.6408C20.6517 12.6282 20.6427 12.6108 20.631 12.5892C20.6078 12.5459 20.5744 12.4852 20.5306 12.4096C20.4432 12.2584 20.3141 12.0471 20.1423 11.7956C19.7994 11.2938 19.2819 10.626 18.5794 9.9569C17.1731 8.61759 14.9972 7.25 11.9996 7.25C9.00203 7.25 6.82614 8.61759 5.41987 9.9569C4.71736 10.626 4.19984 11.2938 3.85694 11.7956C3.68511 12.0471 3.55605 12.2584 3.4686 12.4096C3.42484 12.4852 3.39142 12.5459 3.36818 12.5892C3.35656 12.6108 3.34748 12.6282 3.34092 12.6408L3.33297 12.6563L3.33041 12.6614L3.32948 12.6632L3.32911 12.664C3.32894 12.6643 3.32879 12.6646 3.99961 13ZM11.9996 16C13.9326 16 15.4996 14.433 15.4996 12.5C15.4996 10.567 13.9326 9 11.9996 9C10.0666 9 8.49961 10.567 8.49961 12.5C8.49961 14.433 10.0666 16 11.9996 16Z"></path></svg>');
    content: '';
    width: 24px;
    height: 24px;
    display: block;
}
  1. Remove jet-form-builder-eye-icon.php. Because now we do not need to enqueue native icons

In this example, SVG icons from the @wordpress/icons package were used

You can make/find svg yourself and convert to css declaration for backgroung-image property. It is enough to google "svg to css" to find a conversion tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment