
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
@ryangjchandler/alpine-hooks
Advanced tools
This package contains a variety of hooks for Alpine.js that can be used to improve the developer experience of common front-end development tasks.
You can install this package via npm:
npm install @ryangjchandler/alpine-hooks
You can then register all of the available hooks as an Alpine plugin.
import Alpine from 'alpinejs';
import Hooks from '@ryangjchandler/alpine-hooks';
Alpine.plugin(Hooks);
Alpine.start();
Or if you only need particular hooks, you can import and register them separately.
import Alpine from 'alpinejs';
import { useHover, useWindowSize } from '@ryangjchandler/alpine-hooks';
Alpine.plugin(useHover);
Alpine.plugin(useWindowSize);
Alpine.start();
The table below lists all of the available hooks.
Hook | Description | Example |
---|---|---|
$useHover | Reacts to a mouse hovering over a specific element. | View |
$useFocus | Reacts to a specific element being focused and blurred. | View |
$useHash | Update and react to changes to window.location.hash . | View |
$useWindowSize | Read and react to changes in the window / viewport size. | View |
$useHover
This hook can be used to react to the cursor hovering over a specific element.
<div x-data="{ hovering: $useHover($refs.target) }">
<div id="target" x-ref="target"></div>
</div>
When the #target
element is being hovered over, hovering
will be true
. Otherwise it will default to false.
$useFocus
This hook can be used to react to focus changes on an element.
<div x-data="{ focused: $useFocus($refs.target) }">
<input x-ref="target" />
</div>
When the input
element is focused, the property will be true
. When the blur
event is fired (unfocusing), it will be false
.
$useHash
This hook allows you to modify window.location.hash
and react to external changes too.
<div x-data="{ tab: $useHash('#one') }">
<button x-on:click="tab = '#two'">Two</button>
<!-- More buttons go here... -->
<div x-show="tab === '#two'">
<!-- ... -->
</div>
</div>
Changing the value of the tab
property updates the hash in the URL and is reactive. The tab
property will also read the hash when the component is initialised, defaulting to the value passed in to the hook.
$useWindowSize
This hook lets returns the width
and height
of the viewport and reacts to changes.
<div x-data="{ size: $useWindowSize() }">
<p x-show="size.width < 720">
Your screen is very narrow.
</p>
</div>
FAQs
A collection of hooks for Alpine.js.
The npm package @ryangjchandler/alpine-hooks receives a total of 172 weekly downloads. As such, @ryangjchandler/alpine-hooks popularity was classified as not popular.
We found that @ryangjchandler/alpine-hooks 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.