Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
material-input
Advanced tools
An easy drop-in material design vanilla custom input element. HTML form ready, no framework dependencies, small footprint.
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:
<script src="webcomponents-lite.js"></script>
<script src="./node_modules/material-input/dist/material-input.js"></script>
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.
<material-input name="username"></material-input>
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.
<material-input name="username" label="Choose your username"></material-input>
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.
<material-input name="username" label="username" placeholder="Choose your username"></material-input>
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 -->
<material-input name="animal" value="cat"></material-input>
// javascript
document.querySelector('material-input.animal').value = 'cat';
document.querySelector('material-input.animal').setAttribute('value','cat');
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.querySelector('material-input.customValidatedItem').setCustomValidity('This is not valid.');
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.
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.
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.
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 */
material-input.some-class{
/* the text color of the input */
--material-input-text-color: black;
/* the text color of the placeholder or the floating label on an empty field */
--material-input-placeholder-color: grey;
/* the color of the border and label when the field is focused */
--material-input-highlight-color: indigo;
/* the color of the border and label when the field is in an invalid state */
--material-input-invalid-color: red;
/* the color of the border and label when the field is in a valid state */
--material-input-valid-color: green;
/* the default color of the bottom border */
--material-input-border-color: orange;
/* the height of the bottom border when the field is not focused */
--material-input-line-height: 1px;
/* the height of the bottom border when the field is focused */
--material-input-highlight-line-height: 3px;
}
FAQs
An easy drop-in material-input solution as a custom element
We found that material-input demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.