flowbite-svelte 0.22.21 → 0.22.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ### [0.22.22](https://github.com/themesberg/flowbite-svelte/compare/v0.22.21...v0.22.22) (2022-07-28)
6
+
7
+
8
+ ### Features
9
+
10
+ * focus trap for modals ([08604ac](https://github.com/themesberg/flowbite-svelte/commit/08604ac3475f28a5a35c037e47ecfc13ee13b877))
11
+
5
12
  ### [0.22.21](https://github.com/themesberg/flowbite-svelte/compare/v0.22.20...v0.22.21) (2022-07-28)
6
13
 
7
14
 
@@ -1,6 +1,7 @@
1
1
  <script>import { createEventDispatcher } from 'svelte';
2
2
  import { setContext } from 'svelte';
3
3
  import CloseButton from '../utils/CloseButton.svelte';
4
+ import focusTrap from '../utils/focusTrap';
4
5
  export let open = false;
5
6
  export let title = undefined;
6
7
  export let size = 'md';
@@ -18,15 +19,14 @@ const allPlacementClasses = [
18
19
  'items-end'
19
20
  ];
20
21
  let backdropEl;
21
- function init(node, _visible) {
22
+ function init(node, _open) {
22
23
  getPlacementClasses().map((c) => node.classList.add(c));
23
- if (_visible)
24
- createBackdrop(node);
24
+ _open && createBackdrop(node);
25
25
  return {
26
- update(_visible) {
26
+ update(_open) {
27
27
  allPlacementClasses.map((c) => node.classList.remove(c));
28
28
  getPlacementClasses().map((c) => node.classList.add(c));
29
- _visible ? createBackdrop(node) : destroyBackdrop(node);
29
+ _open ? createBackdrop(node) : destroyBackdrop(node);
30
30
  },
31
31
  destroy() {
32
32
  destroyBackdrop(node);
@@ -44,17 +44,17 @@ function createBackdrop(node) {
44
44
  const body = document.querySelector('body');
45
45
  body.append(backdropEl);
46
46
  body.style.overflow = 'hidden';
47
- body.addEventListener('keydown', handleEscape, true);
47
+ document.addEventListener('keydown', handleEscape, true);
48
48
  }
49
49
  dispatch('show', node);
50
50
  }
51
51
  function destroyBackdrop(node) {
52
52
  const body = document.querySelector('body');
53
53
  body.style.overflow = 'auto';
54
- body.removeEventListener('keydown', handleEscape, true);
55
54
  if (backdropEl)
56
55
  backdropEl.remove();
57
56
  backdropEl = undefined;
57
+ document.removeEventListener('keydown', handleEscape, true);
58
58
  dispatch('hide', node);
59
59
  }
60
60
  function getPlacementClasses() {
@@ -110,6 +110,7 @@ function hide() {
110
110
  aria-modal={open ? 'true' : undefined}
111
111
  role={open ? 'dialog' : undefined}
112
112
  use:init={open}
113
+ use:focusTrap={open}
113
114
  on:click={onButtonsClick}
114
115
  >
115
116
  <div class="relative p-4 w-full {sizes[size]} h-full md:h-auto">
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flowbite-svelte",
3
- "version": "0.22.21",
3
+ "version": "0.22.22",
4
4
  "description": "Flowbite components for Svelte",
5
5
  "main": "index.js",
6
6
  "author": {
@@ -236,6 +236,7 @@
236
236
  "./utils/CloseButton.svelte": "./utils/CloseButton.svelte",
237
237
  "./utils/OptsButton.svelte": "./utils/OptsButton.svelte",
238
238
  "./utils/clickOutside": "./utils/clickOutside.js",
239
+ "./utils/focusTrap": "./utils/focusTrap.js",
239
240
  "./utils/generateId": "./utils/generateId.js"
240
241
  },
241
242
  "svelte": "./index.js"
@@ -0,0 +1,61 @@
1
+ //
2
+ // Focus trap copied from https://uxdesign.cc/how-to-trap-focus-inside-modal-to-make-it-ada-compliant-6a50f9a70700
3
+ //
4
+
5
+ // add all the elements inside modal which you want to make focusable
6
+ const focusableElements =
7
+ 'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])';
8
+
9
+ function getFocusable(node) {
10
+ const focusableContent = node.querySelectorAll(focusableElements);
11
+ return [focusableContent[0], focusableContent[focusableContent.length - 1]];
12
+ }
13
+
14
+ export default function focusTrap(node, _open) {
15
+ let firstFocusableElement, lastFocusableElement; // first and last element to be focused inside modal
16
+
17
+ function handleFocusTrap(e) {
18
+ let isTabPressed = e.key === 'Tab' || e.keyCode === 9;
19
+
20
+ if (!isTabPressed) {
21
+ return;
22
+ }
23
+
24
+ if (e.shiftKey) {
25
+ // if shift key pressed for shift + tab combination
26
+ if (document.activeElement === firstFocusableElement) {
27
+ lastFocusableElement.focus(); // add focus for the last focusable element
28
+ e.preventDefault();
29
+ }
30
+ } else {
31
+ // if tab key is pressed
32
+ if (document.activeElement === lastFocusableElement) {
33
+ // if focused has reached to last focusable element then focus first focusable element after pressing tab
34
+ firstFocusableElement.focus(); // add focus for the first focusable element
35
+ e.preventDefault();
36
+ }
37
+ }
38
+ }
39
+
40
+ if (_open) {
41
+ [firstFocusableElement, lastFocusableElement] = getFocusable(node);
42
+ document.addEventListener('keydown', handleFocusTrap, true);
43
+ firstFocusableElement.focus();
44
+ }
45
+
46
+ return {
47
+ update(_open) {
48
+ if (_open) {
49
+ [firstFocusableElement, lastFocusableElement] = getFocusable(node);
50
+ document.addEventListener('keydown', handleFocusTrap, true);
51
+ firstFocusableElement.focus();
52
+ } else {
53
+ document.removeEventListener('keydown', handleFocusTrap, true);
54
+ }
55
+ },
56
+
57
+ destroy() {
58
+ document.removeEventListener('keydown', handleFocusTrap, true);
59
+ }
60
+ };
61
+ }