trap-focus-svelte
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -1,1 +0,1 @@ | ||
import{listen as t}from"svelte/internal";let e=[],o=0;function n(n,c=1){function u(){const t=[...n.querySelectorAll("*")].filter((t=>t.tabIndex>=0));return[t.at(0)??n,t.at(-1)??n]}let f;function r(){e.push(n),f=document.activeElement,u().at(0).focus()}function s(){e=e.filter((t=>t!=n)),f.focus()}c&&r();const i=t=>e.at(-1)?.contains(t),a=t(n,"focusout",(t=>{if(i(n)){const[e,n]=u();t.target==e&&o?n.focus():t.target!=n||o||e.focus()}})),l=t(document,"focusin",(t=>{if(i(n)&&!i(t.target)){const[t,e]=u();(o?e:t).focus()}}));return{update(t){t?r():s()},destroy(){l(),a(),s()}}}t(document,"keydown",(t=>o=t.shiftKey&&"Tab"==t.key));export{n as trapFocus}; | ||
import{listen as t}from"svelte/internal";let e=[];function o(o,n=1){let c=0;const u=t(document,"keydown",(t=>c=t.shiftKey&&"Tab"==t.key));function f(){const t=[...o.querySelectorAll("*")].filter((t=>t.tabIndex>=0));return[t.at(0)??o,t.at(-1)??o]}let s;function r(){e.push(o),s=document.activeElement,f().at(0).focus()}function i(){e=e.filter((t=>t!=o)),s.focus()}n&&r();const a=t=>e.at(-1)?.contains(t),l=t(o,"focusout",(t=>{if(a(o)){const[e,o]=f();t.target==e&&c?o.focus():t.target!=o||c||e.focus()}})),d=t(document,"focusin",(t=>{if(a(o)&&!a(t.target)){const[t,e]=f();(c?e:t).focus()}}));return{update(t){t?r():i()},destroy(){u(),d(),l(),i()}}}export{o as trapFocus}; |
16
index.ts
@@ -5,9 +5,14 @@ import { listen } from 'svelte/internal' | ||
// true if tabbing backwards with shift + tab | ||
let shiftTab = false | ||
/** Traps focus within a wrapper element */ | ||
function trapFocus(wrap: HTMLElement, active = true) { | ||
// true if tabbing backwards with shift + tab | ||
let shiftTab = false | ||
listen(document, 'keydown', (e: KeyboardEvent) => (shiftTab = e.shiftKey && e.key == 'Tab')) | ||
// set shiftTab to true if shift + tab is pressed | ||
const shiftTabListener = listen( | ||
document, | ||
'keydown', | ||
(e: KeyboardEvent) => (shiftTab = e.shiftKey && e.key == 'Tab') | ||
) | ||
/** Traps focus within a wrapper element */ | ||
function trapFocus(wrap: HTMLElement, active = true) { | ||
// return the first and last focusable children | ||
@@ -76,2 +81,3 @@ function getFirstAndLastFocusable() { | ||
destroy() { | ||
shiftTabListener() | ||
focusListener() | ||
@@ -78,0 +84,0 @@ focusOutListener() |
{ | ||
"name": "trap-focus-svelte", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "description": "Small 0.4kB focus trap that supports stacking, toggling, and dynamic content. Compatible with any framework.", |
@@ -79,3 +79,3 @@ [npm-image]: https://flat.badgen.net/npm/v/trap-focus-svelte?color=blue | ||
const div = document.getElementById('buttons') | ||
const buttons = document.getElementById('buttons') | ||
@@ -82,0 +82,0 @@ // create trap (pass false as a second argument to start disabled) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
162
12001
10