Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@cloudfour/transition-hidden-element
Advanced tools
A JavaScript utility to help you use CSS transitions when showing and hiding elements with the `hidden` attribute.
A JavaScript utility to help you use CSS transitions when showing and hiding elements with the hidden
attribute.
To properly hide elements from all users including screen reader users, elements should be hidden using the hidden
attribute. However, this prevents elements from being transitioned with CSS. If you'd like to use CSS transitions to show and hide these elements you'll need to use JavaScript to do so. This utility wraps that JavaScript into a small, easy-to-use module.
To allow transitions when showing an element the utility performs a few steps:
hidden
attribute.To allow transitions when hiding an element the utility performs a few steps:
hidden
attribute.This library can be used to show or hide an element with transitioned children. For example, when opening a menu, each child link may animate in one-by-one in a staggered fashion. This utility includes API options to support this use case.
Animation can cause health consequences for some users and they may prefer reduced motion. If a user's OS settings signal they prefer reduced motion you should disable your CSS transitions:
@media (prefers-reduced-motion: reduce) {
* {
transition: none !important;
}
}
First, install the package from npm:
npm i @cloudfour/transition-hidden-element --save
Then you can get started. Here's a simple example showing importing the module, initializing a menu, and then showing and hiding it based on user interaction:
// Import our dependency
import { transitionHiddenElement } from '@cloudfour/transition-hidden-element';
// Initialize our menu
const menuTransitioner = transitionHiddenElement({
element: document.querySelector('#menu'),
visibleClass: 'is-open',
});
document.querySelector('#open-menu-button').addEventListener('click', () => {
menuTransitioner.show();
});
document.querySelector('#close-menu-button').addEventListener('click', () => {
menuTransitioner.close();
});
When initializing transitionHiddenElement
, there are two required parameters and two optional parameters:
const simpleFader = transitionHiddenElement({
element: document.querySelector('.js-simple-fade'), // Required
visibleClass: 'is-shown', // Required
hideMode: 'transitionend', // Optional — defaults to `'transitionend'`
timeoutDuration: null // Optional — defaults to `null`
});
{HTMLElement}
element
should be the primary element we're showing and hiding. It will be the element that we'll be adding and removing classes and the hidden
attribute from.
{String}
visibleClass
is the class that will be added when showing our element
. Adding the class should trigger a transition on our element
or its child elements.
{String}
hideMode
determines when the utility should re-apply the hidden
attribute. It defaults to transitionend
but has a few options:
transitionend
— Wait for the element
's transitionend
event to fire. This works if the element has a transition that will be triggered by removing the visibleClass
.timeout
— Wait a certain number of milliseconds. This is useful when your element
is not the only element transitioning. For example, if removing your visibleClass
triggers transitions on child elements, then you should use this option. When using this option be sure to pass in a number for the timeoutDuration
parameter.immediate
— Don't wait at all.Regardless of which setting you choose, it will be converted to immediate
if a user's OS settings signal they prefer reduced motion. You should disable other transitions in your CSS for these users as mentioned above.
{Number}
When using the timeout
option for hideMode
you should be sure to pass in the length of the timeout in milliseconds.
After initializing your transitionHiddenElement
it will return an object with a few methods.
Shows your element
. Removes hidden
, triggers a document reflow, and applies your visibleClass
.
Hides your element
. Removes your visibleClass
and adds hidden
.
Toggles the visibility of your element
. Shows it if it's hidden and hides it if it's visible.
Returns the current hidden status of your element
. It returns true
if the element has the hidden
attribute or is missing the visibleClass
.
Feel free to fork the repo and submit a PR with any helpful additions or changes. After cloning the repository run the following commands:
npm i
— Install dependenciesnpm start
- Build and serve a demo server with hot reloading.localhost:3000
Testing is done in the browser using Cypress, since virtual DOM libraries like jsdom don't handle transitions well.
In order to run the tests do the following:
npm start
— launch the servernpm test
— launch CypressTests will also automatically be run when pull requests are created.
FAQs
A JavaScript utility to help you use CSS transitions when showing and hiding elements with the `hidden` attribute.
We found that @cloudfour/transition-hidden-element demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 8 open source maintainers 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.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.