Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
focus-within-polyfill
Advanced tools
Small vanilla javascript that feature detect the :focus-within pseudo selector and polyfills it with a standard css class if necessary.
:focus-within
Pseudo-Class Polyfill Find this usefull? ☕ Buy me a coffee!
The :focus-within
CSS pseudo-class represents an element that has received focus or contains an element that has received focus. In other words, it represents an element that is itself matched by the :focus pseudo-class or has a descendant that is matched by :focus
(This includes descendants in shadow trees).
This selector is useful, to take a common example, for highlighting an entire <form>
container when the user focuses on one of its <input>
fields.
More information on MDN.
This package is available both as production ready script and as a package. The script can be downloaded here, or installed with a package manager.
# npm
npm install focus-within-polyfill --save
# yarn
yarn add focus-within-polyfill
When included the polyfill will auto-initialize.
/* ES5 */
<script src='https://unpkg.com/focus-within-polyfill/dist/focus-within-polyfill.js'></script>
In addition this library is available as a es6 module that can be imported and bundled up with any build tool. If you are importing it as a dependency, make sure to call the polyfill method:
/* ES6 */
import 'focus-within-polyfill'
After import and initialization the polyfill will kick in only if :focus-within
is not supported in the current browser. By default the .focus-within
class will be added automatically to every element that should have the pseudo-class. Additionally in the initialization fase a .js-focus-within
class will be appended to the body.
This package will add two event listeners, one on the focus event and one on the blur event to trigger the automatic apply and remove of a custom attribute to indicate wheter the Element should have a :focus-within
pseudo-class. In order to do so and be compatible with older version of IE and EDGE the getAttribute
, setAttribute
and removeAttribute
methods are used to set both attributes and classes. Then requestAnimationFrame
is used to apply the modification to the DOM.
This polyfill is compatible with native Shadow DOM and with the webcomponents polyfill. This means that even on IE11 and EDGE you will be able to use the :focus-within
pseudo-class to style elements even outside a shadowRoot.
Polyfill | Edge | IE11+ | Chrome | Firefox | Safari |
---|---|---|---|---|---|
:focus-within | ✓ | ✓ | ✓ | ✓ | ✓ |
* This polyfill maybe work on older versions of the browsers.
The demos folder contains 10+ demos that uses this polyfill.
Type | Title | Code | Live demo |
---|---|---|---|
Content | :focus-within for <button> elements | Code | Live |
Content | :focus-within for <input type="radio"> elements | Code | Live |
Content | :focus-within for <input type="checkbox"> elements | Code | Live |
Content | :focus-within for <div contenteditable> elements | Code | Live |
Content | :focus-within for <div tabindex> elements | Code | Live |
Content | :focus-within for <select> elements | Code | Live |
Content | :focus-within for <svg> elements | Code | Live |
Content | :focus-within for <web-components> elements | Code | Live |
Content | :focus-within for other elements | Code | Live |
FAQs
Small vanilla javascript that feature detect the :focus-within pseudo selector and polyfills it with a standard css class if necessary.
The npm package focus-within-polyfill receives a total of 13,506 weekly downloads. As such, focus-within-polyfill popularity was classified as popular.
We found that focus-within-polyfill 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.