Code Monkey home page Code Monkey logo

trap-focus-svelte's Introduction

trap-focus-svelte

npm File Size MIT license

Small 0.4kB focus trap that supports stacking, toggling, and dynamic content. Designed for Svelte but compatible with plain JavaScript or any framework.

Demo: https://trap-focus-svelte.henrygd.me

Svelte example: https://stackblitz.com/edit/vitejs-vite-k328kh?file=src/App.svelte

Vanilla example with TypeScript: https://stackblitz.com/edit/vitejs-vite-sesxte?file=src/main.ts

Installation

pnpm install trap-focus-svelte

Usage with Svelte

Add directly to an element as an action.

If the element is removed, the trap and event listeners are destroyed automatically.

<script>
	import { trapFocus } from 'trap-focus-svelte'
</script>

<div use:trapFocus>
	<button>Inside trap</button>
	<button>Inside trap</button>
</div>
<button>Outside trap</button>

You can also toggle the trap on they fly:

<script>
	import { trapFocus } from 'trap-focus-svelte'

	let active = true
	const toggleTrap = () => (active = !active)
</script>

<div use:trapFocus={active}>
	<button on:click={toggleTrap}>Toggle trap</button>
	<button>Inside trap</button>
</div>
<button>Outside trap</button>

Usage with vanilla JavaScript or other frameworks

Import from trap-focus-svelte/vanilla

For an example of the demo site made without Svelte (with TypeScript / Vite) see this StackBlitz.

<div id="buttons">
	<button>Inside trap</button>
	<button>Inside trap</button>
</div>
<button>Outside trap</button>
import { trapFocus } from 'trap-focus-svelte/vanilla'

const buttons = document.getElementById('buttons')

// create trap (pass false as a second argument to start disabled)
const buttonTrap = trapFocus(buttons)

// toggle trap
buttonTrap.update(false)

// destory trap
buttonTrap.destroy()

Notes

If you have multiple traps active at the same time, the focus will be within the latest trap created or activated.

When that trap is destroyed or deactivated, the focus will work backwards down the chain, eventually restoring focus to the document.activeElement at the time of the oldest trap's activation.

License

MIT

trap-focus-svelte's People

Contributors

cobypear avatar henrygd avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

cobypear

trap-focus-svelte's Issues

Scroll jumps back up when clicking a checkbox in a trap-focus component

Hi,

I'm having a really weird issue:

The big component uses 'trapfocus'. Clicking a checkbox effectively toggles it, but scrolling and clicking the label of one that was originally out of the visible box, resets the scroll and does not toggle. Clicking the little checkbox itself does work:

Screencast.from.23-04-24.17.59.08.webm

I did some testing - a focusout event is generated and it seems that const inCurrentTrap = (el: HTMLElement) => stack.at(-1)?.contains(el) somehow doesn't realise that the label (containing the checkbox) is not part of the big, floating div and thus refocuses on the first checkbox, resetting the scroll.

Any idea on how to remedy this? (The actual website is here: https://mapcomplete.org/cyclofix.html#filters)

This is a big and complicated project though, I might try to make a minimal example if you want to.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.