Tailwind CSS Strong Password Plugin API
Headless, unstyled strong password plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.
Installation
To get started, install Strong Password plugin via npm, else you can skip this step if you are already using Preline UI as a package.
-
Install the plugin
Install
@preline/strong-passwordvia npmTerminalnpm i @preline/strong-password -
Add the plugin CSS
Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.
main.css@import "tailwindcss"; /* @preline/strong-password */ /* [!code highlight:3] */ @source "../node_modules/@preline/strong-password/*.js"; @import "./node_modules/@preline/strong-password/variants.css"; @import "./node_modules/@preline/strong-password/theme.css"; -
Add the plugin JavaScript
Include the JavaScript
<script>that powers the interactive elements near the end of your</body>tag:HTML
Additional Initialization Options
Use the
non-autoentry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.HTML (non-auto)
Via bundler
When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.
JavaScript (auto)import "@preline/strong-password";JavaScript (non-auto)import HSStrongPassword from "@preline/strong-password/non-auto"; HSStrongPassword.autoInit(); // Or initialize a specific element manually const el = document.querySelector("#strong-password"); if (el) new HSStrongPassword(el);
Example
Here is an example of a strong password with level of weakness, hints and progress.
Basic usage
Prefer to create your own style? Here is a completely unstylized example.
<div class="flex-1">
<input type="password" id="hs-strong-password-input" class="py-3 px-4 block w-full border-stone-200 rounded-lg text-sm focus:border-blue-500 focus:ring-blue-500 dark:bg-neutral-900 dark:border-neutral-700 dark:text-neutral-400"
/>
<div data-hs-strong-password='{
"target": "#hs-strong-password-input",
"stripClasses": "hs-strong-password:opacity-100 hs-strong-password-accepted:bg-green-500 h-2 flex-auto rounded-full bg-blue-500 opacity-50 mx-1"
}' id="strong-password" class="flex mt-2 -mx-1"
></div>
</div>
<div class="mb-3">
<span id="strong-password-weakness"></span>
</div>
Methods
The HSStrongPassword object is contained within the global window object
Force dropdown hint position to be recalculated when scrolling (public method).
const strongPassword = new HSStrongPassword(document.querySelector('#strong-password'));
document.addEventListener('scroll', () => {
if (strongPassword) strongPassword.recalculateDirection();
});
Force dropdown hint position to be recalculated when scrolling (mixed).
const strongPassword = HSStrongPassword.getInstance('#strong-password');
document.addEventListener('scroll', () => {
if (strongPassword) strongPassword.recalculateDirection();
});
Events
An example where a function is run every time a value changes.
const el = HSStrongPassword.getInstance('#strong-password');
el.on('change', ({strength, rules}) => {...});
Ready to use Components
Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.
Strong Password
Explore ready-to-use Tailwind CSS examples built with Preline UI.
View Strong Password examples