@synerise/ds-utils 0.21.9 → 0.22.1

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
@@ -3,6 +3,33 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.22.1](https://github.com/synerise/synerise-design/compare/@synerise/ds-utils@0.22.0...@synerise/ds-utils@0.22.1) (2022-07-06)
7
+
8
+ **Note:** Version bump only for package @synerise/ds-utils
9
+
10
+
11
+
12
+
13
+
14
+ # [0.22.0](https://github.com/synerise/synerise-design/compare/@synerise/ds-utils@0.21.10...@synerise/ds-utils@0.22.0) (2022-06-15)
15
+
16
+
17
+ ### Features
18
+
19
+ * **utils:** outside click other ignored elements (useful for nesting) ([0ac399d](https://github.com/synerise/synerise-design/commit/0ac399dcd64cbff0158b5b1902ff34efa8aefb3d))
20
+
21
+
22
+
23
+
24
+
25
+ ## [0.21.10](https://github.com/synerise/synerise-design/compare/@synerise/ds-utils@0.21.9...@synerise/ds-utils@0.21.10) (2022-04-29)
26
+
27
+ **Note:** Version bump only for package @synerise/ds-utils
28
+
29
+
30
+
31
+
32
+
6
33
  ## [0.21.9](https://github.com/synerise/synerise-design/compare/@synerise/ds-utils@0.21.8...@synerise/ds-utils@0.21.9) (2022-04-05)
7
34
 
8
35
  **Note:** Version bump only for package @synerise/ds-utils
@@ -22,7 +22,12 @@ var focusWithArrowKeys = function focusWithArrowKeys(keyDownEvent, focusableItem
22
22
  elementToFocusOn = document.querySelector("." + focusableItemClass);
23
23
  }
24
24
 
25
- !elementToFocusOn ? fallback() : elementToFocusOn.focus();
25
+ if (elementToFocusOn) {
26
+ elementToFocusOn.focus();
27
+ } else {
28
+ fallback();
29
+ }
30
+
26
31
  return;
27
32
  }
28
33
 
@@ -36,7 +41,12 @@ var focusWithArrowKeys = function focusWithArrowKeys(keyDownEvent, focusableItem
36
41
  _elementToFocusOn = prevSibling !== null && !hasFocusableElementInside(prevSibling, selector) ? prevSibling : focusableElements[activeElementIndex - 1];
37
42
  }
38
43
 
39
- !_elementToFocusOn ? fallback() : _elementToFocusOn.focus();
44
+ if (_elementToFocusOn) {
45
+ _elementToFocusOn.focus();
46
+ } else {
47
+ fallback();
48
+ }
49
+
40
50
  return;
41
51
  }
42
52
 
@@ -9,5 +9,13 @@ declare type PossibleEvent = {
9
9
  [Type in HandledEventsType]: HTMLElementEventMap[Type];
10
10
  }[HandledEventsType];
11
11
  declare type Handler = (event: PossibleEvent) => void;
12
- export declare const useOnClickOutside: (ref: RefObject<HTMLElement>, handler: Handler | null, customEventsTypes?: ("mousedown" | "touchstart" | "click" | "contextmenu")[] | undefined) => void;
12
+ /**
13
+ * Hook for listening for outside clicks.
14
+ *
15
+ * @param ref main component to out of which outside clicks will be detected
16
+ * @param handler function called on outside click
17
+ * @param customEventsTypes list of events to handle that are happening outside (optional, pass something else than `undefined` to overwrite default `['mousedown', 'touchstart']`)
18
+ * @param ignoreSelectors list of selectors/classes which, in case it is found that any of parent elements of triggering element has this class, event `handler` won't be called
19
+ */
20
+ export declare const useOnClickOutside: (ref: RefObject<HTMLElement>, handler: Handler | null, customEventsTypes?: ("mousedown" | "touchstart" | "click" | "contextmenu")[] | undefined, ignoreSelectors?: string[] | undefined) => void;
13
21
  export {};
@@ -4,7 +4,16 @@ var TOUCHSTART = 'touchstart';
4
4
  var CLICK = 'click';
5
5
  var CONTEXTMENU = 'contextmenu';
6
6
  var defaultEvents = [MOUSEDOWN, TOUCHSTART];
7
- export var useOnClickOutside = function useOnClickOutside(ref, handler, customEventsTypes) {
7
+ /**
8
+ * Hook for listening for outside clicks.
9
+ *
10
+ * @param ref main component to out of which outside clicks will be detected
11
+ * @param handler function called on outside click
12
+ * @param customEventsTypes list of events to handle that are happening outside (optional, pass something else than `undefined` to overwrite default `['mousedown', 'touchstart']`)
13
+ * @param ignoreSelectors list of selectors/classes which, in case it is found that any of parent elements of triggering element has this class, event `handler` won't be called
14
+ */
15
+
16
+ export var useOnClickOutside = function useOnClickOutside(ref, handler, customEventsTypes, ignoreSelectors) {
8
17
  var handlerRef = useRef(handler);
9
18
  var events = customEventsTypes || defaultEvents;
10
19
  useEffect(function () {
@@ -18,7 +27,20 @@ export var useOnClickOutside = function useOnClickOutside(ref, handler, customEv
18
27
  }
19
28
 
20
29
  var listener = function listener(event) {
21
- if (!ref.current || !handlerRef.current || ref.current.contains(event.target)) {
30
+ if (!ref.current || !handlerRef.current) {
31
+ return;
32
+ }
33
+
34
+ if (ref.current.contains(event.target)) {
35
+ return;
36
+ }
37
+
38
+ if (ignoreSelectors != null && ignoreSelectors.some(function (className) {
39
+ var _event$target;
40
+
41
+ return (_event$target = event.target) == null ? void 0 : _event$target.closest(className);
42
+ })) {
43
+ // if any of parent elements contain one of ignored classes - stop proceeding this event
22
44
  return;
23
45
  }
24
46
 
@@ -33,5 +55,5 @@ export var useOnClickOutside = function useOnClickOutside(ref, handler, customEv
33
55
  document.removeEventListener(event, listener);
34
56
  });
35
57
  };
36
- }, [handler, ref, events]);
58
+ }, [handler, ref, events, ignoreSelectors]);
37
59
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@synerise/ds-utils",
3
- "version": "0.21.9",
3
+ "version": "0.22.1",
4
4
  "description": "Utils UI Component for the Synerise Design System",
5
5
  "license": "ISC",
6
6
  "repository": "synerise/synerise-design",
@@ -37,7 +37,7 @@
37
37
  "react": ">=16.9.0 < 17.0.0"
38
38
  },
39
39
  "devDependencies": {
40
- "@synerise/ds-core": "^0.33.0"
40
+ "@synerise/ds-core": "^0.34.1"
41
41
  },
42
- "gitHead": "5793ce09feb88b36cc8ca1923ddfcb272b889cb0"
42
+ "gitHead": "6e09a689031ae03275336274cc4302de669f380d"
43
43
  }