Material-input
An easy drop-in material design vanilla custom input element. HTML form ready, no framework dependencies, small footprint.
Demo
Installation
npm install --save material-input
You need the webcomponents-lite polyfill.
Load the polyfill
and the material-input.js
in your html page or however you load you javascript dependencies:
Usage
Just drop the <material-input>
element into you html <form>
element and you are ready to go. A visually hidden <input>
field will be created which syncs its values with the <material-input>
to allow for normal forms to pick up the value.
Labels
By default material inputs use floating labels that can be defined via the label
attribute. This means that the label looks like a placeholder, but when the field is focuses or filled it is moved above the users text.
Placeholders
You can use normal input placeholders instead of, or in combination with labels. The label will always be above the input field, if you choose to use both.
Value
Like a normal input field, you can set the value using the attribute value
in html or via javascript by either setting the attribute or by directly setting the value
property.
<!-- html -->
// javascriptdocumentvalue = 'cat'; document;
Validation
Validation works just like with any default <input>
element. Add a required
or set the type
to email
and you will get the browsers validation notifications. Additionally the material-input
will have a valid
or invalid
style.
Additionally it is possible to explicitly set a field to be invalid
by using the default setCustomValidity
method on the material-input
. You can read more about the setCustomValidity feature on MDN.
document;
Backend validation
If you validate your forms via your backend (which you should!), you cann add an invalid
or valid
attribute to the material-input
. This will set the field in the defined state.
Error messages
You can set the message
attribute, which will add an error message below the field. This is always an error message, no matter what state the field is in.
Autovalidate
When adding the autovalidate
attribute to the material-input
, the field will be validated on every keydown
event. However, this means a field with no validation rules will always be valid and receive the valid
immediately.
Custom styling
Apart from basic styling on the material-input
like margins
or the font-size
you can use the following css properties
to for custom styling.
/* select your specific input or all */