Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@elvia/elvis-toolbox

Package Overview
Dependencies
Maintainers
7
Versions
91
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elvia/elvis-toolbox - npm Package Compare versions

Comparing version 4.2.0 to 5.0.0

15

CHANGELOG.json

@@ -6,2 +6,17 @@ {

"date": "18.10.22",
"version": "5.0.0",
"changelog": [
{
"type": "breaking_changes",
"changes": [
"The <span class=\"code-text\">useFocusTrap</span> is now a proper hook that returns a toggle for enabling/disabling focus trap."
],
"fixes": [
"Instead of calling the <span class=\"code-text\">useFocusTrap</span> each time you enable/disable the focus trap, you use the returned method."
]
}
]
},
{
"date": "18.10.22",
"version": "4.2.0",

@@ -8,0 +23,0 @@ "changelog": [

5

dist/hooks/useFocusTrap.d.ts
import { RefObject } from 'react';
export declare const useFocusTrap: (focusTrapContainer: RefObject<HTMLElement>, removeEventListener?: boolean) => void;
export declare const useFocusTrap: () => {
trapFocus: (focusTrapContainer: RefObject<HTMLElement>) => void;
releaseFocusTrap: () => void;
};

79

dist/hooks/useFocusTrap.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFocusTrap = void 0;
const useFocusTrap = (focusTrapContainer, removeEventListener) => {
setTimeout(() => {
if (!focusTrapContainer.current) {
return;
const useFocusTrap = () => {
let firstItem;
let lastItem;
const handleFirstItemTab = (e) => {
if (e.key === 'Tab' && e.shiftKey) {
if (lastItem) {
lastItem.focus();
}
e.preventDefault();
}
const focusableItems = Array.from(focusTrapContainer.current.querySelectorAll('a[href], button, textarea, input, select, details, [tabindex]:not([tabindex="-1"]')).filter((element) => {
var _a;
return (!element.hasAttribute('disabled') &&
element.getAttribute('aria-hidden') !== 'true' &&
parseInt((_a = element.getAttribute('tabindex')) !== null && _a !== void 0 ? _a : '0') >= 0);
});
const firstItem = focusableItems[0];
const lastItem = focusableItems[focusableItems.length - 1];
const handleFirstItemTab = (e) => {
if (e.key === 'Tab' && e.shiftKey) {
if (lastItem) {
lastItem.focus();
}
e.preventDefault();
};
const handleLastItemTab = (e) => {
if (e.key === 'Tab' && !e.shiftKey) {
if (firstItem) {
firstItem.focus();
}
};
const handleLastItemTab = (e) => {
if (e.key === 'Tab' && !e.shiftKey) {
if (firstItem) {
firstItem.focus();
}
e.preventDefault();
e.preventDefault();
}
};
const trapFocus = (focusTrapContainer) => {
setTimeout(() => {
if (!focusTrapContainer.current) {
return;
}
};
if (firstItem) {
firstItem.focus();
firstItem.addEventListener('keydown', handleFirstItemTab);
}
if (lastItem) {
lastItem.addEventListener('keydown', handleLastItemTab);
}
if (removeEventListener) {
const focusableItems = Array.from(focusTrapContainer.current.querySelectorAll('a[href], button, textarea, input, select, details, [tabindex]:not([tabindex="-1"]')).filter((element) => {
var _a;
return (!element.hasAttribute('disabled') &&
element.getAttribute('aria-hidden') !== 'true' &&
parseInt((_a = element.getAttribute('tabindex')) !== null && _a !== void 0 ? _a : '0') >= 0);
});
firstItem = focusableItems[0];
lastItem = focusableItems[focusableItems.length - 1];
if (firstItem) {
firstItem.removeEventListener('keydown', handleFirstItemTab);
firstItem.focus();
firstItem.addEventListener('keydown', handleFirstItemTab);
}
if (lastItem) {
lastItem.removeEventListener('keydown', handleLastItemTab);
lastItem.addEventListener('keydown', handleLastItemTab);
}
});
};
const releaseFocusTrap = () => {
if (firstItem) {
firstItem.removeEventListener('keydown', handleFirstItemTab);
}
});
if (lastItem) {
lastItem.removeEventListener('keydown', handleLastItemTab);
}
};
return { trapFocus, releaseFocusTrap };
};
exports.useFocusTrap = useFocusTrap;
{
"name": "@elvia/elvis-toolbox",
"version": "4.2.0",
"version": "5.0.0",
"description": "",

@@ -5,0 +5,0 @@ "license": "MIT",

# Toolbox

@@ -0,0 +0,0 @@ import { isSsr } from './isSsr';

@@ -0,0 +0,0 @@ import { useEffect, useState } from 'react';

@@ -0,0 +0,0 @@ import { Dispatch, RefObject, SetStateAction, useEffect, useState } from 'react';

import { RefObject } from 'react';
export const useFocusTrap = (
focusTrapContainer: RefObject<HTMLElement>,
removeEventListener?: boolean,
): void => {
setTimeout(() => {
if (!focusTrapContainer.current) {
return;
export const useFocusTrap = (): {
trapFocus: (focusTrapContainer: RefObject<HTMLElement>) => void;
releaseFocusTrap: () => void;
} => {
let firstItem: Element;
let lastItem: Element;
const handleFirstItemTab = (e: KeyboardEvent) => {
if (e.key === 'Tab' && e.shiftKey) {
if (lastItem) {
(lastItem as HTMLElement).focus();
}
e.preventDefault();
}
const focusableItems = Array.from(
focusTrapContainer.current.querySelectorAll(
'a[href], button, textarea, input, select, details, [tabindex]:not([tabindex="-1"]',
),
).filter((element) => {
return (
!element.hasAttribute('disabled') &&
element.getAttribute('aria-hidden') !== 'true' &&
parseInt(element.getAttribute('tabindex') ?? '0') >= 0
);
});
};
const firstItem = focusableItems[0];
const lastItem = focusableItems[focusableItems.length - 1];
const handleLastItemTab = (e: KeyboardEvent) => {
if (e.key === 'Tab' && !e.shiftKey) {
if (firstItem) {
(firstItem as HTMLElement).focus();
}
e.preventDefault();
}
};
const handleFirstItemTab = (e: KeyboardEvent) => {
if (e.key === 'Tab' && e.shiftKey) {
if (lastItem) {
(lastItem as HTMLElement).focus();
}
e.preventDefault();
const trapFocus = (focusTrapContainer: RefObject<HTMLElement>) => {
setTimeout(() => {
if (!focusTrapContainer.current) {
return;
}
};
const handleLastItemTab = (e: KeyboardEvent) => {
if (e.key === 'Tab' && !e.shiftKey) {
if (firstItem) {
(firstItem as HTMLElement).focus();
}
e.preventDefault();
const focusableItems = Array.from(
focusTrapContainer.current.querySelectorAll(
'a[href], button, textarea, input, select, details, [tabindex]:not([tabindex="-1"]',
),
).filter((element) => {
return (
!element.hasAttribute('disabled') &&
element.getAttribute('aria-hidden') !== 'true' &&
parseInt(element.getAttribute('tabindex') ?? '0') >= 0
);
});
firstItem = focusableItems[0];
lastItem = focusableItems[focusableItems.length - 1];
if (firstItem) {
(firstItem as HTMLElement).focus();
firstItem.addEventListener('keydown', handleFirstItemTab);
}
};
if (lastItem) {
lastItem.addEventListener('keydown', handleLastItemTab);
}
});
};
const releaseFocusTrap = () => {
if (firstItem) {
(firstItem as HTMLElement).focus();
firstItem.addEventListener('keydown', handleFirstItemTab);
firstItem.removeEventListener('keydown', handleFirstItemTab);
}
if (lastItem) {
lastItem.addEventListener('keydown', handleLastItemTab);
lastItem.removeEventListener('keydown', handleLastItemTab);
}
};
if (removeEventListener) {
if (firstItem) {
firstItem.removeEventListener('keydown', handleFirstItemTab);
}
if (lastItem) {
lastItem.removeEventListener('keydown', handleLastItemTab);
}
}
});
return { trapFocus, releaseFocusTrap };
};

@@ -0,0 +0,0 @@ import React, { useLayoutEffect, useRef, useState } from 'react';

@@ -0,0 +0,0 @@ import { RefObject, useEffect, useState } from 'react';

@@ -0,0 +0,0 @@ export * from './elvis-toolbox';

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc