I want to create a custom element for a password input that has an “eye” icon that allows the user to show/hide the password. I did the following:
password.html
<template bindable="password">
<require from="resources/elements/password.css"></require>
<input class="password" type="${showPass ? 'text' : 'password'}" value.bind="password" />
<i class="fas ${showPass ? 'fa-eye-slash' : 'fa-eye'}" click.delegate="showPass = !showPass"></i>
<template>
This works great, but I began to realize that going about it this way keeps me from accessing the attributes of the input – placeholder, disabled, etc.
I could add bindable
values for each attribute of the input element that I wanted to bind to – but that seems a bit of a pain.
I thought about using content projection
using a slot
so I could add the <input>
element myself like so:
<password >
<input type="password" />
</password>
However, now my CustomElement has no way of changing the type of the <input>
element. I can do the following:
<password showpass.two-way="myshowvar">
<input type="password" type="${myshowvar ? 'text' : 'password'}" />
</password>
But I want that logic embeded in the Custom Element.
Is there a way to pass a reference to my input into the Custom Element? Something along the lines of this?
<template bindable="elementref">
<slot></slot>
<i class="fas ${showPass ? 'fa-eye-slash' : 'fa-eye'}"
click.delegate="showPass = !showPass; elementref.type = showPass ? 'text' : 'password';"></i>
</template>
Thanks for your help!