
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
alpinejs-twmerge
Advanced tools
Minimal AlpineJS wrapper for twMerge and clsx. magic and directive for Alpine.js
x-twmergeAlpine JS plugin x-twmerge is a minimal wrapper designed to integrate twMerge and clsx with AlpineJS, offering both a directive x-twmerge for dynamic class management and a magic method $twMerge for computed class merging. This plugin enables you to leverage the utility-first CSS approach of Tailwind CSS dynamically within your Alpine.js components.
x-twmerge Directive: Dynamically apply and manage Tailwind CSS classes based on component state.$twMerge Magic Method: Use this magic method to compute and merge classes programmatically within Alpine.js expressions.x-twmerge BehaviorWhen x-twmerge is initialized on an element, it takes note of the original classes defined on that element and always uses them as the base/first set of classes sent to twMerge. This ensures that the initial styling of the element is preserved, and only the additional dynamic classes are merged or toggled based on component state.
Example of x-twmerge preserving original classes:
<div x-data="{ highlight: false }" class="text-gray-800">
<!-- Original class "text-gray-800" is always included -->
<div class="w-10 h-10 bg-gray-200" x-twmerge="{'bg-yellow-200': highlight}"></div>
<button @click="highlight = !highlight">Toggle Highlight</button>
</div>
In contrast, the $twMerge magic method does not automatically include the element's original classes. It only processes the classes provided to it and returns the merged class string. All the dynamic classes need to be sent to the $twMerge magic in order for it to correctly merge them as it does not know the element original classes.
Example of $twMerge without preserving original classes:
<div x-data="{ active: false }" class="text-gray-800">
<!-- When using :class with $twmerge to dynamically merge classes you need to provide the base classes as well -->
<div class="w-10 h-10" :class="$twMerge(['bg-gray-200', {'bg-yellow-200': active}])"></div>
<button @click="active = !active">Toggle Active</button>
</div>
Toggle a class based on component state using the x-twmerge directive:
<div x-data="{ active: false }" class="p-4">
<div class="w-24 h-24 bg-gray-200" x-twmerge="active && 'bg-blue-500'"></div>
<button @click="active = !active">Toggle Active</button>
</div>
Pass an array of classes to x-twmerge:
<div x-data="{ open: false }" class="p-4">
<div x-twmerge="['bg-red-500', open && 'bg-blue-500']"></div>
<button @click="open = !open">Toggle Open</button>
</div>
clsx ObjectUtilize a clsx-like object syntax with x-twmerge for more complex class logic:
<div x-data="{ error: true, warning: false }" class="p-4">
<div
x-twmerge="{
'text-white': true,
'bg-red-500': error,
'bg-yellow-500': warning && !error,
}"></div>
<button @click="error = !error">Toggle Error</button>
<button @click="warning = !warning">Toggle Warning</button>
</div>
:class with $twMerge Magic MethodUse $twMerge within the :class binding for computed class strings:
<div x-data="{ primary: false, secondary: true }" class="p-4">
<div
:class="$twMerge([
'text-base',
'font-semibold',
primary ? 'text-blue-500' : 'text-gray-800',
secondary && 'bg-yellow-200'
])"></div>
<button @click="primary = !primary">Toggle Primary</button>
<button @click="secondary = !secondary">Toggle Secondary</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/alpinejs-twmerge@latest/dist/alpinejs-twmerge.cdn.js"></script>
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
npm i -D alpinejs-twmerge
# or
yarn add -D alpinejs-twmerge
Then, integrate it into your project:
import Alpine from "alpinejs";
import twmerge from "alpinejs-twmerge";
Alpine.plugin(twmerge);
window.Alpine = Alpine;
Alpine.start();
FAQs
Minimal AlpineJS wrapper for twMerge and clsx. magic and directive for Alpine.js
We found that alpinejs-twmerge 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
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.