
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Toggle elements with CSS transitions and animations the easy way.
This library addresses the issue of not being able to transition/animate an element while toggling the display property at the same time. See here for a full explanation: https://www.impressivewebs.com/animate-display-block-none/
is-open
, is-opening
and is-closing
classes at the exact right times to ensure that CSS transitions and animations can be used while toggling display: none;
Onion can be bundled with your project or added directly to the browser.
Add onion to your project
# NPM
npm install onion
# yarn
yarn add onion
# pnpm
pnpm add onion
Import the package to your script
// ESM
import onion from "onion";
// CommonJS
const onion = require("onion");
From unpkg CDN
<script src="https://unpkg.com/onion@latest/dist/onion.umd.js"></script>
Self-hosted
<script src="/path/to/onion.umd.js"></script>
See /example
for a more detailed example.
<button class="toggle">Toggle</div>
<div class="box">Example</div>
.box {
display: none;
opacity: 0;
transition: opacity 0.4s ease-out;
}
.box.is-open {
display: block;
}
.box.is-opening {
opacity: 1;
}
const toggle = document.querySelector(".toggle");
const box = document.querySelector(".box");
toggle.addEventListener("click", function() {
onion.toggle(box);
});
Onion does not ship with any CSS styles. You should add your own stylesheet to make it work.
You should add display: none;
to the element by default, as well as any styles for the initial closed state.
The is-open
class is added when the element is shown. You should override the default display: none;
with display: block;
, for example.
The is-opening
class is added on the next event cycle after is-open
is added. A CSS transition or animation should be added to this class. The 1-cycle delay ensures that the transition/animation can be played immediately after display: none;
is removed.
Note: is-opening
is kept on the element until it is closing.
The is-closing
class is added when the element is hiding. A CSS transition or animation should be added to this class. The is-open
class will not be removed until the transition or animation ends.
onion.show(el, token?);
Parameter | Type | Description |
---|---|---|
el | HTMLElement | The element to be shown |
token | string | Optional. A custom class to be added while opening |
onion.hide(el, token?);
Parameter | Type | Description |
---|---|---|
el | HTMLElement | The element to be hidden |
token | string | Optional. A custom class to be added while closing |
onion.toggle(el, force?, openingToken?, closingToken?);
Parameter | Type | Description |
---|---|---|
el | HTMLElement | The element to be toggled |
force | boolean | Optional. If true, the element will be shown. Otherwise, it will be hidden. |
openingToken | string | Optional. A custom class to be added while opening |
closingToken | string | Optional. A custom class to be added while closing |
Note: you can pass undefined
to skip an optional argument.
FAQs
Toggle elements with CSS transitions and animations the easy way.
The npm package onion receives a total of 162 weekly downloads. As such, onion popularity was classified as not popular.
We found that onion 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
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.