All Projects → chanced → focus-svelte

chanced / focus-svelte

Licence: MIT license
focus lock for svelte

Programming Languages

typescript
32286 projects
Svelte
593 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to focus-svelte

svelte-progressbar
A multiseries, SVG progressbar component made with Svelte
Stars: ✭ 85 (+372.22%)
Mutual labels:  svelte-components, svelte, sveltejs
sveltekit-blog
Sveltekit blog starter project created with sveltekit, typescript, tailwindcss, postcss, husky, and storybook. The project has the structure set up for the scaleable web application.
Stars: ✭ 100 (+455.56%)
Mutual labels:  svelte, sveltejs, sveltekit
Svelte Material Ui
Svelte Material UI Components
Stars: ✭ 2,081 (+11461.11%)
Mutual labels:  svelte-components, svelte, sveltejs
svelte-headlessui
Unofficial Svelte port of Headless UI components
Stars: ✭ 564 (+3033.33%)
Mutual labels:  svelte-components, svelte, sveltejs
svelte-material
Modular and customizable Material Design UI components for Svelte.js
Stars: ✭ 30 (+66.67%)
Mutual labels:  svelte-components, svelte, sveltejs
svelte-toy
A toy for svelte data stores
Stars: ✭ 73 (+305.56%)
Mutual labels:  svelte, sveltejs, sveltekit
fa-svelte
Font Awesome 5 for svelte.js
Stars: ✭ 72 (+300%)
Mutual labels:  svelte-components, svelte, sveltejs
ui-svelte
A component library for Svelte
Stars: ✭ 18 (+0%)
Mutual labels:  svelte-components, svelte, sveltejs
s-date-range-picker
📅 A date range picker built with Svelte
Stars: ✭ 13 (-27.78%)
Mutual labels:  svelte-components, svelte, sveltejs
kahi-ui
Straight-forward Svelte UI for the Web
Stars: ✭ 169 (+838.89%)
Mutual labels:  svelte-components, svelte, sveltejs
programmingtil-svelte
No description or website provided.
Stars: ✭ 59 (+227.78%)
Mutual labels:  svelte, sveltejs, sveltekit
svelte-accessible-dialog
An accessible dialog component for Svelte apps
Stars: ✭ 24 (+33.33%)
Mutual labels:  accessibility, svelte, sveltejs
vue-focus-loop
Vue component that helps you to to trap focus in an element.
Stars: ✭ 23 (+27.78%)
Mutual labels:  accessibility, focus, focus-trap
hagura-sveltekit
A minimal markdown blog template built using SvelteKit
Stars: ✭ 51 (+183.33%)
Mutual labels:  svelte, sveltejs, sveltekit
generator-jhipster-svelte
Generate Svelte powered JHipster web applications
Stars: ✭ 44 (+144.44%)
Mutual labels:  svelte, sveltejs, sveltekit
svelte-starter-kit
Svelte with brilliant bells and useful whistles
Stars: ✭ 384 (+2033.33%)
Mutual labels:  svelte, sveltejs, sveltekit
kickstart
Ruby on Rails application templates
Stars: ✭ 61 (+238.89%)
Mutual labels:  svelte, sveltekit
svelte-quotes
A small app to demonstrate use of SvelteJS
Stars: ✭ 16 (-11.11%)
Mutual labels:  svelte, sveltejs
its-ok-i-guess
🧐 Guess the game from the Steam review!
Stars: ✭ 41 (+127.78%)
Mutual labels:  svelte, sveltejs
nomie5
Nomie v5 Source Code
Stars: ✭ 528 (+2833.33%)
Mutual labels:  svelte, sveltejs

focus-svelte 🔒

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].