npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

focus-svelte

v0.3.4

Published

Focus lock for svelte with zero dependencies.

Downloads

2,273

Readme

focus-svelte :lock:

Focus lock for svelte with zero dependencies.

Installation

npm install -D focus-svelte
# yarn add -D focus-svelte
# pnpm add -D focus-svelte

Example

https://svelte.dev/repl/4b31b2f4a45c4ee08230f6d47d31db48

Description

focus-svelte works a bit differently than other focus locks I've encounted. Rather than using an event listener to track user activity and overriding the default behavior of the browser, the DOM is manipulated instead. All elements outside of an active focus lock's descendants or ancestory have their tabindex set to -1 if it was 0 or greater previously.

To keep track of changes after the lock is enabled, a MutationObserver monitors the DOM for updates. Once all focus locks are disabled or removed, the observer is stopped and the elements' properties are reset. If a focus lock later becomes active, the observer is restarted and nodes are decorated accordingly.

When a lock becomes active for the first time, the HTMLElement that is assigned focus is dependent upon the options passed to the action / component.

If element is assigned and is tabbable, it will be focused upon. If element is undefined or not tabbable and focusable is true, the HTMLElement with use:focus is granted focus. Finally, if neither of those conditions are met, focus will be set on the first tabbable element.

Usage

There is both an action and a component that can be utilized.

Options

| option | description | type | default | | ------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | ------------------------------------------ | | element | If element is assigned and is tabbable, it will be focused upon when the trap is enabled. string values will be considered a query selector. | Element \| string | undefined | | focusable | The HTMLElement the action is assigned to gets a tabindex of 0 when the trap becomes active | boolean | false | | focusDelay | can either be a number of ms to wait or an async function that resolves (void) when the focus of an element should be set. | number \| () => Promise<void> | tick | | delay | Determines how long to wait before batching updates to tabIndex and ariaHidden. | number \| () => Promise<void> | tick | | assignAriaHidden | When a focus trap becomes enabled and is true, all elements outside of an active trap or their ancestory have their aria-hidden attribute set to "true". | boolean | false | | preventScroll | sets preventScroll when focusing. | boolean | false | | enabled | If true, the focus trap becomes active. | boolean | false |

action

<script>
	import { focus } from "focus-svelte";
	let enabled = true;
	function toggleFocus() {
		enabled = !enabled;
	}
</script>

<button on:click="{toggleFocus}">{enabled ? "disable" : "enable"} focus</button>

<div use:focus="{enabled}">
	<input value={enabled ? "focus is traped here" : "regular tabbable input"} />
</div>

<input value={enabled ? "can't tab here" : "can be tabbed into!"} />

With assignAriaHidden

<script>
	import { focus } from "focus-svelte";
	let enabled = true;
	function toggleFocus() {
		enabled = !enabled;
	}
</script>

<button on:click="{toggleFocus}">{enabled ? "disable" : "enable"} focus</button>

<div use:focus="{{enabled, assignAriaHidden: true}}">
	<input value={enabled ? "focus is traped here" : "regular tabbable input"} />
</div>

<input value={enabled ? "can't tab here" : "can be tabbed into!"} />

component

<script>
	import { Focus } from "focus-svelte";
	let enabled = true;
	function toggleFocus() {
		enabled = !enabled;
	}
</script>

<button on:click="{toggleFocus}">{enabled ? "disable" : "enable"} focus</button>

<Focus {enabled} assignAriaHidden="{true}">
	<input value={enabled ? "focus is traped here" : "regular tabbable input"} />
</Focus>

<input value={enabled ? "can't tab here" : "can be tabbed into!"} />

Note: As the action needs an HTMLElement, the component version wraps your content with a div.

override

If you wish to override the behavior of an element, you can set data-focus-override="true" and it will retain its original tabindex.

Contributing

Pull requests are always welcome.

License

MIT