@openedx/paragon 23.1.1 → 23.2.0

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.
@@ -0,0 +1,10 @@
1
+ /// <reference types="react" />
2
+ export interface ArrowKeyNavProps {
3
+ /** e.g. 'a,button,input' */
4
+ selectors?: string;
5
+ ignoredKeys?: string[];
6
+ }
7
+ /**
8
+ * A React hook to enable arrow key navigation on a component.
9
+ */
10
+ export default function useArrowKeyNavigation(props?: ArrowKeyNavProps): import("react").MutableRefObject<HTMLElement | undefined>;
@@ -5,11 +5,6 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
7
  import { useRef, useEffect } from 'react';
8
-
9
- /**
10
- * A React hook to enable arrow key navigation on a component.
11
- */
12
-
13
8
  function handleEnter(_ref) {
14
9
  var event = _ref.event,
15
10
  currentIndex = _ref.currentIndex,
@@ -48,7 +43,6 @@ function handleArrowKey(_ref2) {
48
43
  (_nextElement = nextElement) === null || _nextElement === void 0 ? void 0 : _nextElement.focus();
49
44
  event.preventDefault();
50
45
  }
51
-
52
46
  /**
53
47
  * Implement arrow key navigation for the given parentNode
54
48
  */
@@ -86,7 +80,7 @@ function handleEvents(_ref3) {
86
80
  var currentIndex = Array.from(availableElements).findIndex(function (availableElement) {
87
81
  return availableElement === activeElement;
88
82
  });
89
- if (key === 'Enter') {
83
+ if (key === 'Enter' && activeElement) {
90
84
  handleEnter({
91
85
  event: event,
92
86
  currentIndex: currentIndex,
@@ -99,10 +93,13 @@ function handleEvents(_ref3) {
99
93
  availableElements: availableElements
100
94
  });
101
95
  }
102
- export default function useArrowKeyNavigation(props) {
103
- var _ref4 = props || {},
104
- selectors = _ref4.selectors,
105
- ignoredKeys = _ref4.ignoredKeys;
96
+ /**
97
+ * A React hook to enable arrow key navigation on a component.
98
+ */
99
+ export default function useArrowKeyNavigation() {
100
+ var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
101
+ var selectors = props.selectors,
102
+ ignoredKeys = props.ignoredKeys;
106
103
  var parentNode = useRef();
107
104
  useEffect(function () {
108
105
  var eventHandler = function eventHandler(event) {
@@ -1 +1 @@
1
- {"version":3,"file":"useArrowKeyNavigation.js","names":["useRef","useEffect","handleEnter","_ref","event","currentIndex","activeElement","click","preventDefault","handleArrowKey","_ref2","_nextElement","availableElements","focus","nextElement","key","length","_availableElements","_slicedToArray","handleEvents","_ref3","_ref3$ignoredKeys","ignoredKeys","parentNode","_ref3$selectors","selectors","includes","_document","document","contains","querySelectorAll","Array","from","findIndex","availableElement","useArrowKeyNavigation","props","_ref4","eventHandler","current","addEventListener","removeEventListener"],"sources":["../../src/hooks/useArrowKeyNavigation.jsx"],"sourcesContent":["import { useRef, useEffect } from 'react';\n\n/**\n * A React hook to enable arrow key navigation on a component.\n */\n\nfunction handleEnter({ event, currentIndex, activeElement }) {\n if (currentIndex === -1) { return; }\n activeElement.click();\n event.preventDefault();\n}\n\nfunction handleArrowKey({ event, currentIndex, availableElements }) {\n // If the focus isn't in the container, focus on the first thing\n if (currentIndex === -1) { availableElements[0].focus(); }\n\n // Move the focus up or down. Wrap around ends of list.\n let nextElement;\n\n if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {\n nextElement = availableElements[(currentIndex + 1) % availableElements.length];\n }\n if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {\n nextElement = currentIndex - 1 < 0\n ? availableElements[currentIndex - 1 + availableElements.length]\n : availableElements[currentIndex - 1];\n }\n if (event.key === 'End') {\n nextElement = availableElements[availableElements.length - 1];\n }\n if (event.key === 'Home') {\n [nextElement] = availableElements;\n }\n\n nextElement?.focus();\n event.preventDefault();\n}\n\n/**\n * Implement arrow key navigation for the given parentNode\n */\nfunction handleEvents({\n event,\n ignoredKeys = [],\n parentNode,\n selectors = 'a,button,input',\n}) {\n if (!parentNode) { return; }\n\n const { key } = event;\n\n if (!['ArrowUp', 'ArrowDown', 'ArrowRight', 'ArrowLeft', 'Enter', 'Home', 'End'].includes(key)\n || ignoredKeys.includes(key)) {\n return;\n }\n\n const { activeElement } = document;\n\n // If we're not inside the container, don't do anything\n if (!parentNode.contains(activeElement)) { return; }\n\n // Get the list of elements we're allowed to scroll through\n const availableElements = parentNode.querySelectorAll(selectors);\n\n // No elements are available to loop through.\n if (!availableElements.length) { return; }\n\n // Which index is currently selected\n const currentIndex = Array.from(availableElements).findIndex(\n (availableElement) => availableElement === activeElement,\n );\n\n if (key === 'Enter') {\n handleEnter({ event, currentIndex, activeElement });\n }\n handleArrowKey({ event, currentIndex, availableElements });\n}\n\nexport default function useArrowKeyNavigation(props) {\n const { selectors, ignoredKeys } = props || {};\n const parentNode = useRef();\n\n useEffect(() => {\n const eventHandler = (event) => {\n handleEvents({\n event, ignoredKeys, parentNode: parentNode.current, selectors,\n });\n };\n\n document.addEventListener('keydown', eventHandler);\n\n return () => document.removeEventListener('keydown', eventHandler);\n }, [ignoredKeys, selectors]);\n\n return parentNode;\n}\n"],"mappings":";;;;;;AAAA,SAASA,MAAM,EAAEC,SAAS,QAAQ,OAAO;;AAEzC;AACA;AACA;;AAEA,SAASC,WAAWA,CAAAC,IAAA,EAAyC;EAAA,IAAtCC,KAAK,GAAAD,IAAA,CAALC,KAAK;IAAEC,YAAY,GAAAF,IAAA,CAAZE,YAAY;IAAEC,aAAa,GAAAH,IAAA,CAAbG,aAAa;EACvD,IAAID,YAAY,KAAK,CAAC,CAA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AAY;MAAEC,aAAa,EAAbA;IAAc,CAAC,CAAC;EACrD;EACAG,cAAc,CAAC;IAAEL,KAAK,EAALA,KAAK;IAAEC,YAAY,EAAZA,YAAY;IAAEO,iBAAiB,EAAjBA;EAAkB,CAAC,CAAC;AAC5D;AAEA,eAAe,SAASuB,qBAAqBA,CAACC,KAAK,EAAE;EACnD,IAAAC,KAAA,GAAmCD,KAAK,IAAI,CAAC,CAAC;IAAtCX,SAAS,GAAAY,KAAA,CAATZ,SAAS;IAAEH,WAAW,GAAAe,KAAA,CAAXf,WAAW;EAC9B,IAAMC,UAAU,GAAGvB,MAAM,CAAC,CAAC;EAE3BC,SAAS,CAAC,YAAM;IACd,IAAMqC,YAAY,GAAG,SAAfA,YAAYA,CAAIlC,KAAK,EAAK;MAC9Be,YAAY,CAAC;QACXf,KAAK,EAALA,KAAK;QAAEkB,WAAW,EAAXA,WAAW;QAAEC,UAAU,EAAEA,UAAU,CAACgB,OAAO;QAAEd,SAAS,EAATA;MACtD,CAAC,CAAC;IACJ,CAAC;IAEDG,QAAQ,CAACY,gBAAgB,CAAC,SAAS,EAAEF,YAAY,CAAC;IAElD,OAAO;MAAA,OAAMV,QAAQ,CAACa,mBAAmB,CAAC,SAAS,EAAEH,YAAY,CAAC;IAAA;EACpE,CAAC,EAAE,CAAChB,WAAW,EAAEG,SAAS,CAAC,CAAC;EAE5B,OAAOF,UAAU;AACnB","ignoreList":[]}
1
+ {"version":3,"file":"useArrowKeyNavigation.js","names":["useRef","useEffect","handleEnter","_ref","event","currentIndex","activeElement","click","preventDefault","handleArrowKey","_ref2","_nextElement","availableElements","focus","nextElement","key","length","_availableElements","_slicedToArray","handleEvents","_ref3","_ref3$ignoredKeys","ignoredKeys","parentNode","_ref3$selectors","selectors","includes","_document","document","contains","querySelectorAll","Array","from","findIndex","availableElement","useArrowKeyNavigation","props","arguments","undefined","eventHandler","current","addEventListener","removeEventListener"],"sources":["../../src/hooks/useArrowKeyNavigation.tsx"],"sourcesContent":["import { useRef, useEffect } from 'react';\n\ninterface HandleEnterArgs {\n event: KeyboardEvent;\n currentIndex: number;\n activeElement: HTMLElement;\n}\n\nfunction handleEnter({ event, currentIndex, activeElement }: HandleEnterArgs) {\n if (currentIndex === -1) { return; }\n activeElement.click();\n event.preventDefault();\n}\n\ninterface HandleArrowKeyArgs {\n event: KeyboardEvent;\n currentIndex: number;\n availableElements: NodeListOf<HTMLElement>;\n}\n\nfunction handleArrowKey({ event, currentIndex, availableElements }: HandleArrowKeyArgs) {\n // If the focus isn't in the container, focus on the first thing\n if (currentIndex === -1) { availableElements[0].focus(); }\n\n // Move the focus up or down. Wrap around ends of list.\n let nextElement;\n\n if (event.key === 'ArrowDown' || event.key === 'ArrowRight') {\n nextElement = availableElements[(currentIndex + 1) % availableElements.length];\n }\n if (event.key === 'ArrowUp' || event.key === 'ArrowLeft') {\n nextElement = currentIndex - 1 < 0\n ? availableElements[currentIndex - 1 + availableElements.length]\n : availableElements[currentIndex - 1];\n }\n if (event.key === 'End') {\n nextElement = availableElements[availableElements.length - 1];\n }\n if (event.key === 'Home') {\n [nextElement] = availableElements;\n }\n\n nextElement?.focus();\n event.preventDefault();\n}\n\ninterface HandleEventsArgs {\n event: KeyboardEvent;\n ignoredKeys?: string[];\n parentNode: HTMLElement | undefined;\n selectors?: string;\n}\n\n/**\n * Implement arrow key navigation for the given parentNode\n */\nfunction handleEvents({\n event,\n ignoredKeys = [],\n parentNode,\n selectors = 'a,button,input',\n}: HandleEventsArgs) {\n if (!parentNode) { return; }\n\n const { key } = event;\n\n if (!['ArrowUp', 'ArrowDown', 'ArrowRight', 'ArrowLeft', 'Enter', 'Home', 'End'].includes(key)\n || ignoredKeys.includes(key)) {\n return;\n }\n\n const { activeElement } = document;\n\n // If we're not inside the container, don't do anything\n if (!parentNode.contains(activeElement)) { return; }\n\n // Get the list of elements we're allowed to scroll through\n const availableElements = parentNode.querySelectorAll<HTMLElement>(selectors);\n\n // No elements are available to loop through.\n if (!availableElements.length) { return; }\n\n // Which index is currently selected\n const currentIndex = Array.from(availableElements).findIndex(\n (availableElement) => availableElement === activeElement,\n );\n\n if (key === 'Enter' && activeElement) {\n handleEnter({ event, currentIndex, activeElement: activeElement as HTMLElement });\n }\n handleArrowKey({ event, currentIndex, availableElements });\n}\n\nexport interface ArrowKeyNavProps {\n /** e.g. 'a,button,input' */\n selectors?: string;\n ignoredKeys?: string[];\n}\n\n/**\n * A React hook to enable arrow key navigation on a component.\n */\nexport default function useArrowKeyNavigation(props: ArrowKeyNavProps = {}) {\n const { selectors, ignoredKeys } = props;\n const parentNode = useRef<HTMLElement>();\n\n useEffect(() => {\n const eventHandler = (event: KeyboardEvent) => {\n handleEvents({\n event, ignoredKeys, parentNode: parentNode.current, selectors,\n });\n };\n\n document.addEventListener('keydown', eventHandler);\n\n return () => document.removeEventListener('keydown', eventHandler);\n }, [ignoredKeys, selectors]);\n\n return parentNode;\n}\n"],"mappings":";;;;;;AAAA,SAASA,MAAM,EAAEC,SAAS,QAAQ,OAAO;AAQzC,SAASC,WAAWA,CAAAC,IAAA,EAA0D;EAAA,IAAvDC,KAAK,GAAAD,IAAA,CAALC,KAAK;IAAEC,YAAY,GAAAF,IAAA,CAAZE,YAAY;IAAEC,aAAa,GAAAH,IAAA,CAAbG,aAAa;EACvD,IAAID,YAAY,KAAK,CAAC,CAAC,EAAE;IAAE;EAAQ;EACnCC,aAAa,CAACC,KAAK,CAAC,CAAC;EACrBH,KAAK,CAACI,cAAc,CAAC,CAAC;AACxB;AAQA,SAASC,cAAcA,CAAAC,KAAA,EAAiE;EAAA,IAAAC,YAAA;EAAA,IAA9DP,KAAK,GAAAM,KAAA,CAALN,KAAK;IAAEC,YAAY,GAAAK,KAAA,CAAZL,YAAY;IAAEO,iBAAiB,GAAAF,KAAA,CAAjBE,iBAAiB;EAC9D;EACA,IAAIP,YAAY,KAAK,CAAC,CAAC,EAAE;IAAEO,iBAAiB,CAAC,CAAC,CAAC,CAACC,KAAK,CAAC,CAAC;EAAE;;EA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,GAAKc,KAAK,CAArBd,WAAW;EAC9B,IAAMC,UAAU,GAAGvB,MAAM,CAAc,CAAC;EAExCC,SAAS,CAAC,YAAM;IACd,IAAMsC,YAAY,GAAG,SAAfA,YAAYA,CAAInC,KAAoB,EAAK;MAC7Ce,YAAY,CAAC;QACXf,KAAK,EAALA,KAAK;QAAEkB,WAAW,EAAXA,WAAW;QAAEC,UAAU,EAAEA,UAAU,CAACiB,OAAO;QAAEf,SAAS,EAATA;MACtD,CAAC,CAAC;IACJ,CAAC;IAEDG,QAAQ,CAACa,gBAAgB,CAAC,SAAS,EAAEF,YAAY,CAAC;IAElD,OAAO;MAAA,OAAMX,QAAQ,CAACc,mBAAmB,CAAC,SAAS,EAAEH,YAAY,CAAC;IAAA;EACpE,CAAC,EAAE,CAACjB,WAAW,EAAEG,SAAS,CAAC,CAAC;EAE5B,OAAOF,UAAU;AACnB","ignoreList":[]}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * This hook will find the index of the last child of a containing element
3
+ * that fits within its bounding rectangle. This is done by summing the widths
4
+ * of the children until they exceed the width of the container.
5
+ *
6
+ * The hook returns the index of the last visible child.
7
+ *
8
+ * @param containerElementRef - container element
9
+ * @param overflowElementRef - overflow element
10
+ */
11
+ declare const useIndexOfLastVisibleChild: (containerElementRef: HTMLElement | null, overflowElementRef: HTMLElement | null) => number;
12
+ export default useIndexOfLastVisibleChild;
@@ -11,10 +11,10 @@ import { useLayoutEffect, useState } from 'react';
11
11
  * that fits within its bounding rectangle. This is done by summing the widths
12
12
  * of the children until they exceed the width of the container.
13
13
  *
14
- * @param {Element} containerElementRef - container element
15
- * @param {Element} overflowElementRef - overflow element
16
- *
17
14
  * The hook returns the index of the last visible child.
15
+ *
16
+ * @param containerElementRef - container element
17
+ * @param overflowElementRef - overflow element
18
18
  */
19
19
  var useIndexOfLastVisibleChild = function useIndexOfLastVisibleChild(containerElementRef, overflowElementRef) {
20
20
  var _useState = useState(-1),
@@ -22,7 +22,11 @@ var useIndexOfLastVisibleChild = function useIndexOfLastVisibleChild(containerEl
22
22
  indexOfLastVisibleChild = _useState2[0],
23
23
  setIndexOfLastVisibleChild = _useState2[1];
24
24
  useLayoutEffect(function () {
25
+ if (!containerElementRef) {
26
+ return undefined;
27
+ }
25
28
  function updateLastVisibleChildIndex() {
29
+ var _overflowElementRef$g;
26
30
  // Get array of child nodes from NodeList form
27
31
  var childNodesArr = Array.prototype.slice.call(containerElementRef.children);
28
32
  var _childNodesArr$filter = childNodesArr
@@ -42,23 +46,20 @@ var useIndexOfLastVisibleChild = function useIndexOfLastVisibleChild(containerEl
42
46
  // sometimes we'll show a dropdown with one item in it when it would fit,
43
47
  // but allowing this case dramatically simplifies the calculations we need
44
48
  // to do above.
45
- sumWidth: overflowElementRef ? overflowElementRef.getBoundingClientRect().width : 0,
49
+ sumWidth: (_overflowElementRef$g = overflowElementRef === null || overflowElementRef === void 0 ? void 0 : overflowElementRef.getBoundingClientRect().width) !== null && _overflowElementRef$g !== void 0 ? _overflowElementRef$g : 0,
46
50
  nextIndexOfLastVisibleChild: -1
47
51
  }),
48
52
  nextIndexOfLastVisibleChild = _childNodesArr$filter.nextIndexOfLastVisibleChild;
49
53
  setIndexOfLastVisibleChild(nextIndexOfLastVisibleChild);
50
54
  }
51
- if (containerElementRef) {
52
- updateLastVisibleChildIndex();
53
- var resizeObserver = new ResizeObserver(function () {
54
- return updateLastVisibleChildIndex();
55
- });
56
- resizeObserver.observe(containerElementRef);
57
- return function () {
58
- return resizeObserver.disconnect();
59
- };
60
- }
61
- return undefined;
55
+ updateLastVisibleChildIndex();
56
+ var resizeObserver = new ResizeObserver(function () {
57
+ return updateLastVisibleChildIndex();
58
+ });
59
+ resizeObserver.observe(containerElementRef);
60
+ return function () {
61
+ return resizeObserver.disconnect();
62
+ };
62
63
  }, [containerElementRef, overflowElementRef]);
63
64
  return indexOfLastVisibleChild;
64
65
  };
@@ -1 +1 @@
1
- {"version":3,"file":"useIndexOfLastVisibleChild.js","names":["useLayoutEffect","useState","useIndexOfLastVisibleChild","containerElementRef","overflowElementRef","_useState","_useState2","_slicedToArray","indexOfLastVisibleChild","setIndexOfLastVisibleChild","updateLastVisibleChildIndex","childNodesArr","Array","prototype","slice","call","children","_childNodesArr$filter","filter","childNode","reduce","acc","index","sumWidth","getBoundingClientRect","width","nextIndexOfLastVisibleChild","resizeObserver","ResizeObserver","observe","disconnect","undefined"],"sources":["../../src/hooks/useIndexOfLastVisibleChild.jsx"],"sourcesContent":["import { useLayoutEffect, useState } from 'react';\n\n/**\n * This hook will find the index of the last child of a containing element\n * that fits within its bounding rectangle. This is done by summing the widths\n * of the children until they exceed the width of the container.\n *\n * @param {Element} containerElementRef - container element\n * @param {Element} overflowElementRef - overflow element\n *\n * The hook returns the index of the last visible child.\n */\nconst useIndexOfLastVisibleChild = (containerElementRef, overflowElementRef) => {\n const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1);\n\n useLayoutEffect(() => {\n function updateLastVisibleChildIndex() {\n // Get array of child nodes from NodeList form\n const childNodesArr = Array.prototype.slice.call(containerElementRef.children);\n const { nextIndexOfLastVisibleChild } = childNodesArr\n // filter out the overflow element\n .filter(childNode => childNode !== overflowElementRef)\n // sum the widths to find the last visible element's index\n .reduce((acc, childNode, index) => {\n acc.sumWidth += childNode.getBoundingClientRect().width;\n if (acc.sumWidth <= containerElementRef.getBoundingClientRect().width) {\n acc.nextIndexOfLastVisibleChild = index;\n }\n return acc;\n }, {\n // Include the overflow element's width to begin with. Doing this means\n // sometimes we'll show a dropdown with one item in it when it would fit,\n // but allowing this case dramatically simplifies the calculations we need\n // to do above.\n sumWidth: overflowElementRef ? overflowElementRef.getBoundingClientRect().width : 0,\n nextIndexOfLastVisibleChild: -1,\n });\n\n setIndexOfLastVisibleChild(nextIndexOfLastVisibleChild);\n }\n\n if (containerElementRef) {\n updateLastVisibleChildIndex();\n\n const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex());\n resizeObserver.observe(containerElementRef);\n\n return () => resizeObserver.disconnect();\n }\n\n return undefined;\n }, [containerElementRef, overflowElementRef]);\n\n return indexOfLastVisibleChild;\n};\n\nexport default useIndexOfLastVisibleChild;\n"],"mappings":";;;;;;AAAA,SAASA,eAAe,EAAEC,QAAQ,QAAQ,OAAO;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAMC,0BAA0B,GAAG,SAA7BA,0BAA0BA,CAAIC,mBAAmB,EAAEC,kBAAkB,EAAK;EAC9E,IAAAC,SAAA,GAA8DJ,QAAQ,CAAC,CAAC,CAAC,CAAC;IAAAK,UAAA,GAAAC,cAAA,CAAAF,SAAA;IAAnEG,uBAAuB,GAAAF,UAAA;IAAEG,0BAA0B,GAAAH,UAAA;EAE1DN,eAAe,CAAC,YAAM;IACpB,SAASU,2BAA2BA,CAAA,EAAG;MACrC;MACA,IAAMC,aAAa,GAAGC,KAAK,CAACC,SAAS,CAACC,KAAK,CAACC,IAAI,CAACZ,mBAAmB,CAACa,QAAQ,CAAC;MAC9E,IAAAC,qBAAA,GAAwCN;QACtC;QAAA,CACCO,MAAM,CAAC,UAAAC,SAAS;UAAA,OAAIA,SAAS,KAAKf,kBAAkB;QAAA;QACrD;QAAA,CACCgB,MAAM,CAAC,UAACC,GAAG,EAAEF,SAAS,EAAEG,KAAK,EAAK;UACjCD,GAAG,CAACE,QAAQ,IAAIJ,SAAS,CAACK,qBAAqB,CAAC,CAAC,CAACC,KAAK;UACvD,IAAIJ,GAAG,CAACE,QAAQ,IAAIpB,mBAAmB,CAACqB,qBAAqB,CAAC,CAAC,CAACC,KAAK,EAAE;YACrEJ,GAAG,CAACK,2BAA2B,GAAGJ,KAAK;UACzC;UACA,OAAOD,GAAG;QACZ,CAAC,EAAE;UACD;UACA;UACA;UACA;UACAE,QAAQ,EAAEnB,kBAAkB,GAAGA,kBAAkB,CAACoB,qBAAqB,CAAC,CAAC,CAACC,KAAK,GAAG,CAAC;UACnFC,2BAA2B,EAAE,CAAC;QAChC,CAAC,CAAC;QAjBIA,2BAA2B,GAAAT,qBAAA,CAA3BS,2BAA2B;MAmBnCjB,0BAA0B,CAACiB,2BAA2B,CAAC;IACzD;IAEA,IAAIvB,mBAAmB,EAAE;MACvBO,2BAA2B,CAAC,CAAC;MAE7B,IAAMiB,cAAc,GAAG,IAAIC,cAAc,CAAC;QAAA,OAAMlB,2BAA2B,CAAC,CAAC;MAAA,EAAC;MAC9EiB,cAAc,CAACE,OAAO,CAAC1B,mBAAmB,CAAC;MAE3C,OAAO;QAAA,OAAMwB,cAAc,CAACG,UAAU,CAAC,CAAC;MAAA;IAC1C;IAEA,OAAOC,SAAS;EAClB,CAAC,EAAE,CAAC5B,mBAAmB,EAAEC,kBAAkB,CAAC,CAAC;EAE7C,OAAOI,uBAAuB;AAChC,CAAC;AAED,eAAeN,0BAA0B","ignoreList":[]}
1
+ {"version":3,"file":"useIndexOfLastVisibleChild.js","names":["useLayoutEffect","useState","useIndexOfLastVisibleChild","containerElementRef","overflowElementRef","_useState","_useState2","_slicedToArray","indexOfLastVisibleChild","setIndexOfLastVisibleChild","undefined","updateLastVisibleChildIndex","_overflowElementRef$g","childNodesArr","Array","prototype","slice","call","children","_childNodesArr$filter","filter","childNode","reduce","acc","index","sumWidth","getBoundingClientRect","width","nextIndexOfLastVisibleChild","resizeObserver","ResizeObserver","observe","disconnect"],"sources":["../../src/hooks/useIndexOfLastVisibleChild.tsx"],"sourcesContent":["import { useLayoutEffect, useState } from 'react';\n\n/**\n * This hook will find the index of the last child of a containing element\n * that fits within its bounding rectangle. This is done by summing the widths\n * of the children until they exceed the width of the container.\n *\n * The hook returns the index of the last visible child.\n *\n * @param containerElementRef - container element\n * @param overflowElementRef - overflow element\n */\nconst useIndexOfLastVisibleChild = (\n containerElementRef: HTMLElement | null,\n overflowElementRef: HTMLElement | null,\n): number => {\n const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1);\n\n useLayoutEffect(() => {\n if (!containerElementRef) {\n return undefined;\n }\n\n function updateLastVisibleChildIndex() {\n // Get array of child nodes from NodeList form\n const childNodesArr = Array.prototype.slice.call(containerElementRef!.children);\n const { nextIndexOfLastVisibleChild } = childNodesArr\n // filter out the overflow element\n .filter(childNode => childNode !== overflowElementRef)\n // sum the widths to find the last visible element's index\n .reduce((acc, childNode, index) => {\n acc.sumWidth += childNode.getBoundingClientRect().width;\n if (acc.sumWidth <= containerElementRef!.getBoundingClientRect().width) {\n acc.nextIndexOfLastVisibleChild = index;\n }\n return acc;\n }, {\n // Include the overflow element's width to begin with. Doing this means\n // sometimes we'll show a dropdown with one item in it when it would fit,\n // but allowing this case dramatically simplifies the calculations we need\n // to do above.\n sumWidth: overflowElementRef?.getBoundingClientRect().width ?? 0,\n nextIndexOfLastVisibleChild: -1,\n });\n\n setIndexOfLastVisibleChild(nextIndexOfLastVisibleChild);\n }\n\n updateLastVisibleChildIndex();\n\n const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex());\n resizeObserver.observe(containerElementRef);\n return () => resizeObserver.disconnect();\n }, [containerElementRef, overflowElementRef]);\n\n return indexOfLastVisibleChild;\n};\n\nexport default useIndexOfLastVisibleChild;\n"],"mappings":";;;;;;AAAA,SAASA,eAAe,EAAEC,QAAQ,QAAQ,OAAO;;AAEjD;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,IAAMC,0BAA0B,GAAG,SAA7BA,0BAA0BA,CAC9BC,mBAAuC,EACvCC,kBAAsC,EAC3B;EACX,IAAAC,SAAA,GAA8DJ,QAAQ,CAAC,CAAC,CAAC,CAAC;IAAAK,UAAA,GAAAC,cAAA,CAAAF,SAAA;IAAnEG,uBAAuB,GAAAF,UAAA;IAAEG,0BAA0B,GAAAH,UAAA;EAE1DN,eAAe,CAAC,YAAM;IACpB,IAAI,CAACG,mBAAmB,EAAE;MACxB,OAAOO,SAAS;IAClB;IAEA,SAASC,2BAA2BA,CAAA,EAAG;MAAA,IAAAC,qBAAA;MACrC;MACA,IAAMC,aAAa,GA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+1,3 @@
1
+ import React from 'react';
2
+ declare const useIsVisible: (defaultIsVisible?: boolean) => [isVisible: boolean, sentinelRef: React.MutableRefObject<HTMLElement | null>];
3
+ export default useIsVisible;
@@ -7,7 +7,7 @@ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
7
  import { useRef, useState, useEffect } from 'react';
8
8
  var useIsVisible = function useIsVisible() {
9
9
  var defaultIsVisible = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
10
- var sentinelRef = useRef();
10
+ var sentinelRef = useRef(null);
11
11
  var _useState = useState(defaultIsVisible),
12
12
  _useState2 = _slicedToArray(_useState, 2),
13
13
  isVisible = _useState2[0],
@@ -1 +1 @@
1
- {"version":3,"file":"useIsVisible.js","names":["useRef","useState","useEffect","useIsVisible","defaultIsVisible","arguments","length","undefined","sentinelRef","_useState","_useState2","_slicedToArray","isVisible","setIsVisible","current","observer","IntersectionObserver","entries","forEach","_ref","isIntersecting","observe","disconnect","e","isReferenceError","ReferenceError"],"sources":["../../src/hooks/useIsVisible.jsx"],"sourcesContent":["import { useRef, useState, useEffect } from 'react';\n\nconst useIsVisible = (defaultIsVisible = true) => {\n const sentinelRef = useRef();\n const [isVisible, setIsVisible] = useState(defaultIsVisible);\n\n useEffect(() => {\n try {\n if (sentinelRef.current) {\n const observer = new IntersectionObserver((entries) => {\n entries.forEach(({ isIntersecting }) => {\n setIsVisible(isIntersecting);\n });\n }, {});\n observer.observe(sentinelRef.current);\n return () => {\n observer.disconnect();\n };\n }\n } catch (e) {\n const isReferenceError = e instanceof ReferenceError;\n if (!isReferenceError) {\n throw e;\n }\n // Do nothing if an intersection observer can't be created.\n }\n return () => {};\n }, []);\n\n return [isVisible, sentinelRef];\n};\n\nexport default useIsVisible;\n"],"mappings":";;;;;;AAAA,SAASA,MAAM,EAAEC,QAAQ,EAAEC,SAAS,QAAQ,OAAO;AAEnD,IAAMC,YAAY,GAAG,SAAfA,YAAYA,CAAA,EAAgC;EAAA,IAA5BC,gBAAgB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,IAAI;EAC3C,IAAMG,WAAW,GAAGR,MAAM,CAAC,CAAC;EAC5B,IAAAS,SAAA,GAAkCR,QAAQ,CAACG,gBAAgB,CAAC;IAAAM,UAAA,GAAAC,cAAA,CAAAF,SAAA;IAArDG,SAAS,GAAAF,UAAA;IAAEG,YAAY,GAAAH,UAAA;EAE9BR,SAAS,CAAC,YAAM;IACd,IAAI;MACF,IAAIM,WAAW,CAACM,OAAO,EAAE;QACvB,IAAMC,QAAQ,GAAG,IAAIC,oBAAoB,CAAC,UAACC,OAAO,EAAK;UACrDA,OAAO,CAACC,OAAO,CAAC,UAAAC,IAAA,EAAwB;YAAA,IAArBC,cAAc,GAAAD,IAAA,CAAdC,cAAc;YAC/BP,YAAY,CAACO,cAAc,CAAC;UAC9B,CAAC,CAAC;QACJ,CAAC,EAAE,CAAC,CAAC,CAAC;QACNL,QAAQ,CAACM,OAAO,CAACb,WAAW,CAACM,OAAO,CAAC;QACrC,OAAO,YAAM;UACXC,QAAQ,CAACO,UAAU,CAAC,CAAC;QACvB,CAAC;MACH;IACF,CAAC,CAAC,OAAOC,CAAC,EAAE;MACV,IAAMC,gBAAgB,GAAGD,CAAC,YAAYE,cAAc;MACpD,IAAI,CAACD,gBAAgB,EAAE;QACrB,MAAMD,CAAC;MACT;MACA;IACF;IACA,OAAO,YAAM,CAAC,CAAC;EACjB,CAAC,EAAE,EAAE,CAAC;EAEN,OAAO,CAACX,SAAS,EAAEJ,WAAW,CAAC;AACjC,CAAC;AAED,eAAeL,YAAY","ignoreList":[]}
1
+ {"version":3,"file":"useIsVisible.js","names":["useRef","useState","useEffect","useIsVisible","defaultIsVisible","arguments","length","undefined","sentinelRef","_useState","_useState2","_slicedToArray","isVisible","setIsVisible","current","observer","IntersectionObserver","entries","forEach","_ref","isIntersecting","observe","disconnect","e","isReferenceError","ReferenceError"],"sources":["../../src/hooks/useIsVisible.tsx"],"sourcesContent":["import React, { useRef, useState, useEffect } from 'react';\n\nconst useIsVisible = (defaultIsVisible = true): [\n isVisible: boolean,\n sentinelRef: React.MutableRefObject<HTMLElement | null>,\n] => {\n const sentinelRef = useRef<HTMLElement | null>(null);\n const [isVisible, setIsVisible] = useState(defaultIsVisible);\n\n useEffect(() => {\n try {\n if (sentinelRef.current) {\n const observer = new IntersectionObserver((entries) => {\n entries.forEach(({ isIntersecting }) => {\n setIsVisible(isIntersecting);\n });\n }, {});\n observer.observe(sentinelRef.current);\n return () => {\n observer.disconnect();\n };\n }\n } catch (e) {\n const isReferenceError = e instanceof ReferenceError;\n if (!isReferenceError) {\n throw e;\n }\n // Do nothing if an intersection observer can't be created.\n }\n return () => {};\n }, []);\n\n return [isVisible, sentinelRef];\n};\n\nexport default useIsVisible;\n"],"mappings":";;;;;;AAAA,SAAgBA,MAAM,EAAEC,QAAQ,EAAEC,SAAS,QAAQ,OAAO;AAE1D,IAAMC,YAAY,GAAG,SAAfA,YAAYA,CAAA,EAGb;EAAA,IAHiBC,gBAAgB,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,IAAI;EAI3C,IAAMG,WAAW,GAAGR,MAAM,CAAqB,IAAI,CAAC;EACpD,IAAAS,SAAA,GAAkCR,QAAQ,CAACG,gBAAgB,CAAC;IAAAM,UAAA,GAAAC,cAAA,CAAAF,SAAA;IAArDG,SAAS,GAAAF,UAAA;IAAEG,YAAY,GAAAH,UAAA;EAE9BR,SAAS,CAAC,YAAM;IACd,IAAI;MACF,IAAIM,WAAW,CAACM,OAAO,EAAE;QACvB,IAAMC,QAAQ,GAAG,IAAIC,oBAAoB,CAAC,UAACC,OAAO,EAAK;UACrDA,OAAO,CAACC,OAAO,CAAC,UAAAC,IAAA,EAAwB;YAAA,IAArBC,cAAc,GAAAD,IAAA,CAAdC,cAAc;YAC/BP,YAAY,CAACO,cAAc,CAAC;UAC9B,CAAC,CAAC;QACJ,CAAC,EAAE,CAAC,CAAC,CAAC;QACNL,QAAQ,CAACM,OAAO,CAACb,WAAW,CAACM,OAAO,CAAC;QACrC,OAAO,YAAM;UACXC,QAAQ,CAACO,UAAU,CAAC,CAAC;QACvB,CAAC;MACH;IACF,CAAC,CAAC,OAAOC,CAAC,EAAE;MACV,IAAMC,gBAAgB,GAAGD,CAAC,YAAYE,cAAc;MACpD,IAAI,CAACD,gBAAgB,EAAE;QACrB,MAAMD,CAAC;MACT;MACA;IACF;IACA,OAAO,YAAM,CAAC,CAAC;EACjB,CAAC,EAAE,EAAE,CAAC;EAEN,OAAO,CAACX,SAAS,EAAEJ,WAAW,CAAC;AACjC,CAAC;AAED,eAAeL,YAAY","ignoreList":[]}
@@ -0,0 +1,12 @@
1
+ export type Toggler = [
2
+ isOn: boolean,
3
+ setOn: () => void,
4
+ setOff: () => void,
5
+ toggle: () => void
6
+ ];
7
+ export interface ToggleHandlers {
8
+ handleToggleOn?: () => void;
9
+ handleToggleOff?: () => void;
10
+ handleToggle?: (newStatus: boolean) => void;
11
+ }
12
+ export default function useToggle(defaultIsOn?: boolean, handlers?: ToggleHandlers): Toggler;
@@ -5,36 +5,25 @@ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len
5
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
7
  import { useState, useCallback } from 'react';
8
- export default function useToggle(defaultIsOn) {
8
+ export default function useToggle() {
9
+ var defaultIsOn = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
9
10
  var handlers = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
10
11
  var handleToggleOn = handlers.handleToggleOn,
11
12
  handleToggleOff = handlers.handleToggleOff,
12
13
  handleToggle = handlers.handleToggle;
13
- var _useState = useState(defaultIsOn || false),
14
+ var _useState = useState(defaultIsOn),
14
15
  _useState2 = _slicedToArray(_useState, 2),
15
16
  isOn = _useState2[0],
16
17
  setIsOn = _useState2[1];
17
18
  var setOn = useCallback(function () {
18
19
  setIsOn(true);
19
- // istanbul ignore else
20
- if (handleToggleOn) {
21
- handleToggleOn();
22
- }
23
- // istanbul ignore else
24
- if (handleToggle) {
25
- handleToggle(true);
26
- }
20
+ handleToggleOn === null || handleToggleOn === void 0 ? void 0 : handleToggleOn();
21
+ handleToggle === null || handleToggle === void 0 ? void 0 : handleToggle(true);
27
22
  }, [handleToggleOn, handleToggle]);
28
23
  var setOff = useCallback(function () {
29
24
  setIsOn(false);
30
- // istanbul ignore else
31
- if (handleToggleOff) {
32
- handleToggleOff();
33
- }
34
- // istanbul ignore else
35
- if (handleToggle) {
36
- handleToggle(false);
37
- }
25
+ handleToggleOff === null || handleToggleOff === void 0 ? void 0 : handleToggleOff();
26
+ handleToggle === null || handleToggle === void 0 ? void 0 : handleToggle(false);
38
27
  }, [handleToggleOff, handleToggle]);
39
28
  var toggle = useCallback(function () {
40
29
  var doToggle = isOn ? setOff : setOn;
@@ -1 +1 @@
1
- {"version":3,"file":"useToggle.js","names":["useState","useCallback","useToggle","defaultIsOn","handlers","arguments","length","undefined","handleToggleOn","handleToggleOff","handleToggle","_useState","_useState2","_slicedToArray","isOn","setIsOn","setOn","setOff","toggle","doToggle"],"sources":["../../src/hooks/useToggle.jsx"],"sourcesContent":["import { useState, useCallback } from 'react';\n\nexport default function useToggle(defaultIsOn, handlers = {}) {\n const { handleToggleOn, handleToggleOff, handleToggle } = handlers;\n const [isOn, setIsOn] = useState(defaultIsOn || false);\n\n const setOn = useCallback(() => {\n setIsOn(true);\n // istanbul ignore else\n if (handleToggleOn) {\n handleToggleOn();\n }\n // istanbul ignore else\n if (handleToggle) {\n handleToggle(true);\n }\n }, [handleToggleOn, handleToggle]);\n\n const setOff = useCallback(() => {\n setIsOn(false);\n // istanbul ignore else\n if (handleToggleOff) {\n handleToggleOff();\n }\n // istanbul ignore else\n if (handleToggle) {\n handleToggle(false);\n }\n }, [handleToggleOff, handleToggle]);\n\n const toggle = useCallback(() => {\n const doToggle = isOn ? setOff : setOn;\n doToggle();\n }, [isOn, setOn, setOff]);\n\n return [isOn, setOn, setOff, toggle];\n}\n"],"mappings":";;;;;;AAAA,SAASA,QAAQ,EAAEC,WAAW,QAAQ,OAAO;AAE7C,eAAe,SAASC,SAASA,CAACC,WAAW,EAAiB;EAAA,IAAfC,QAAQ,GAAAC,SAAA,CAAAC,MAAA,QAAAD,SAAA,QAAAE,SAAA,GAAAF,SAAA,MAAG,CAAC,CAAC;EAC1D,IAAQG,cAAc,GAAoCJ,QAAQ,CAA1DI,cAAc;IAAEC,eAAe,GAAmBL,QAAQ,CAA1CK,eAAe;IAAEC,YAAY,GAAKN,QAAQ,CAAzBM,YAAY;EACrD,IAAAC,SAAA,GAAwBX,QAAQ,CAACG,WAAW,IAAI,KAAK,CAAC;IAAAS,UAAA,GAAAC,cAAA,CAAAF,SAAA;IAA/CG,IAAI,GAAAF,UAAA;IAAEG,OAAO,GAAAH,UAAA;EAEpB,IAAMI,KAAK,GAAGf,WAAW,CAAC,YAAM;IAC9Bc,OAAO,CAAC,IAAI,CAAC;IACb;IACA,IAAIP,cAAc,EAAE;MAClBA,cAAc,CAAC,CAAC;IAClB;IACA;IACA,IAAIE,YAAY,EAAE;MAChBA,YAAY,CAAC,IAAI,CAAC;IACpB;EACF,CAAC,EAAE,CAACF,cAAc,EAAEE,YAAY,CAAC,CAAC;EAElC,IAAMO,MAAM,GAAGhB,WAAW,CAAC,YAAM;IAC/Bc,OAAO,CAAC,KAAK,CAAC;IACd;IACA,IAAIN,eAAe,EAAE;MACnBA,eAAe,CAAC,CAAC;IACnB;IACA;IACA,IAAIC,YAAY,EAAE;MAChBA,YAAY,CAAC,KAAK,CAAC;IACrB;EACF,CAAC,EAAE,CAACD,eAAe,EAAEC,YAAY,CAAC,CAAC;EAEnC,IAAMQ,MAAM,GAAGjB,WAAW,CAAC,YAAM;IAC/B,IAAMkB,QAAQ,GAAGL,IAAI,GAAGG,MAAM,GAAGD,KAAK;IACtCG,QAAQ,CAAC,CAAC;EACZ,CAAC,EAAE,CAACL,IAAI,EAAEE,KAAK,EAAEC,MAAM,CAAC,CAAC;EAEzB,OAAO,CAACH,IAAI,EAAEE,KAAK,EAAEC,MAAM,EAAEC,MAAM,CAAC;AACtC","ignoreList":[]}
1
+ {"version":3,"file":"useToggle.js","names":["useState","useCallback","useToggle","defaultIsOn","arguments","length","undefined","handlers","handleToggleOn","handleToggleOff","handleToggle","_useState","_useState2","_slicedToArray","isOn","setIsOn","setOn","setOff","toggle","doToggle"],"sources":["../../src/hooks/useToggle.tsx"],"sourcesContent":["import { useState, useCallback } from 'react';\n\nexport type Toggler = [\n isOn: boolean,\n setOn: () => void,\n setOff: () => void,\n toggle: () => void,\n];\n\nexport interface ToggleHandlers {\n handleToggleOn?: () => void;\n handleToggleOff?: () => void;\n handleToggle?: (newStatus: boolean) => void;\n}\n\nexport default function useToggle(defaultIsOn = false, handlers: ToggleHandlers = {}): Toggler {\n const { handleToggleOn, handleToggleOff, handleToggle } = handlers;\n const [isOn, setIsOn] = useState(defaultIsOn);\n\n const setOn = useCallback(() => {\n setIsOn(true);\n handleToggleOn?.();\n handleToggle?.(true);\n }, [handleToggleOn, handleToggle]);\n\n const setOff = useCallback(() => {\n setIsOn(false);\n handleToggleOff?.();\n handleToggle?.(false);\n }, [handleToggleOff, handleToggle]);\n\n const toggle = useCallback(() => {\n const doToggle = isOn ? setOff : setOn;\n doToggle();\n }, [isOn, setOn, setOff]);\n\n return [isOn, setOn, setOff, togg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,EAAE,CAACL,IAAI,EAAEE,KAAK,EAAEC,MAAM,CAAC,CAAC;EAEzB,OAAO,CAACH,IAAI,EAAEE,KAAK,EAAEC,MAAM,EAAEC,MAAM,CAAC;AACtC","ignoreList":[]}
@@ -0,0 +1,6 @@
1
+ export interface WindowSizeData {
2
+ width: number | undefined;
3
+ height: number | undefined;
4
+ }
5
+ declare function useWindowSize(): WindowSizeData;
6
+ export default useWindowSize;
@@ -1 +1 @@
1
- {"version":3,"file":"useWindowSize.js","names":["useState","useLayoutEffect","useWindowSize","_useState","width","undefined","height","_useState2","_slicedToArray","windowSize","setWindowSize","handleResize","global","innerWidth","innerHeight","addEventListener","removeEventListener"],"sources":["../../src/hooks/useWindowSize.jsx"],"sourcesContent":["import { useState, useLayoutEffect } from 'react';\n\nfunction useWindowSize() {\n // Initialize state with undefined width/height so server and client renders match\n // Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/\n const [windowSize, setWindowSize] = useState({\n width: undefined,\n height: undefined,\n });\n\n useLayoutEffect(() => {\n // Handler to call on window resize\n function handleResize() {\n // Set window width/height to state\n setWindowSize({\n width: global.innerWidth,\n height: global.innerHeight,\n });\n }\n\n // Add event listener\n global.addEventListener('resize', handleResize);\n\n // Call handler right away so state gets updated with initial window size\n handleResize();\n\n // Remove event listener on cleanup\n return () => global.removeEventListener('resize', handleResize);\n }, []); // Empty array ensures that effect is only run on mount\n\n return windowSize;\n}\n\nexport default useWindowSize;\n"],"mappings":";;;;;;AAAA,SAASA,QAAQ,EAAEC,eAAe,QAAQ,OAAO;AAEjD,SAASC,aAAaA,CAAA,EAAG;EACvB;EACA;EACA,IAAAC,SAAA,GAAoCH,QAAQ,CAAC;MAC3CI,KAAK,EAAEC,SAAS;MAChBC,MAAM,EAAED;IACV,CAAC,CAAC;IAAAE,UAAA,GAAAC,cAAA,CAAAL,SAAA;IAHKM,UAAU,GAAAF,UAAA;IAAEG,aAAa,GAAAH,UAAA;EAKhCN,eAAe,CAAC,YAAM;IACpB;IACA,SAASU,YAAYA,CAAA,EAAG;MACtB;MACAD,aAAa,CAAC;QACZN,KAAK,EAAEQ,MAAM,CAACC,UAAU;QACxBP,MAAM,EAAEM,MAAM,CAACE;MACjB,CAAC,CAAC;IACJ;;IAEA;IACAF,MAAM,CAACG,gBAAgB,CAAC,QAAQ,EAAEJ,YAAY,CAAC;;IAE/C;IACAA,YAAY,CAAC,CAAC;;IAEd;IACA,OAAO;MAAA,OAAMC,MAAM,CAACI,mBAAmB,CAAC,QAAQ,EAAEL,YAAY,CAAC;IAAA;EACjE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;;EAER,OAAOF,UAAU;AACnB;AAEA,eAAeP,aAAa","ignoreList":[]}
1
+ {"version":3,"file":"useWindowSize.js","names":["useState","useLayoutEffect","useWindowSize","_useState","width","undefined","height","_useState2","_slicedToArray","windowSize","setWindowSize","handleResize","global","innerWidth","innerHeight","addEventListener","removeEventListener"],"sources":["../../src/hooks/useWindowSize.tsx"],"sourcesContent":["import { useState, useLayoutEffect } from 'react';\n\nexport interface WindowSizeData {\n width: number | undefined;\n height: number | undefined;\n}\n\nfunction useWindowSize(): WindowSizeData {\n // Initialize state with undefined width/height so server and client renders match\n // Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/\n const [windowSize, setWindowSize] = useState<WindowSizeData>({\n width: undefined,\n height: undefined,\n });\n\n useLayoutEffect(() => {\n // Handler to call on window resize\n function handleResize() {\n // Set window width/height to state\n setWindowSize({\n width: global.innerWidth,\n height: global.innerHeight,\n });\n }\n\n // Add event listener\n global.addEventListener('resize', handleResize);\n\n // Call handler right away so state gets updated with initial window size\n handleResize();\n\n // Remove event listener on cleanup\n return () => global.removeEventListener('resize', handleResize);\n }, []); // Empty array ensures that effect is only run on mount\n\n return windowSize;\n}\n\nexport default useWindowSize;\n"],"mappings":";;;;;;AAAA,SAASA,QAAQ,EAAEC,eAAe,QAAQ,OAAO;AAOjD,SAASC,aAAaA,CAAA,EAAmB;EACvC;EACA;EACA,IAAAC,SAAA,GAAoCH,QAAQ,CAAiB;MAC3DI,KAAK,EAAEC,SAAS;MAChBC,MAAM,EAAED;IACV,CAAC,CAAC;IAAAE,UAAA,GAAAC,cAAA,CAAAL,SAAA;IAHKM,UAAU,GAAAF,UAAA;IAAEG,aAAa,GAAAH,UAAA;EAKhCN,eAAe,CAAC,YAAM;IACpB;IACA,SAASU,YAAYA,CAAA,EAAG;MACtB;MACAD,aAAa,CAAC;QACZN,KAAK,EAAEQ,MAAM,CAACC,UAAU;QACxBP,MAAM,EAAEM,MAAM,CAACE;MACjB,CAAC,CAAC;IACJ;;IAEA;IACAF,MAAM,CAACG,gBAAgB,CAAC,QAAQ,EAAEJ,YAAY,CAAC;;IAE/C;IACAA,YAAY,CAAC,CAAC;;IAEd;IACA,OAAO;MAAA,OAAMC,MAAM,CAACI,mBAAmB,CAAC,QAAQ,EAAEL,YAAY,CAAC;IAAA;EACjE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;;EAER,OAAOF,UAAU;AACnB;AAEA,eAAeP,aAAa","ignoreList":[]}
package/dist/index.d.ts CHANGED
@@ -18,6 +18,11 @@ export { default as ModalLayer } from './Modal/ModalLayer';
18
18
  export { default as Overlay, OverlayTrigger } from './Overlay';
19
19
  export { default as Portal } from './Modal/Portal';
20
20
  export { default as Tooltip } from './Tooltip';
21
+ export { default as useWindowSize, type WindowSizeData } from './hooks/useWindowSize';
22
+ export { default as useToggle, type Toggler, type ToggleHandlers } from './hooks/useToggle';
23
+ export { default as useArrowKeyNavigation, type ArrowKeyNavProps } from './hooks/useArrowKeyNavigation';
24
+ export { default as useIndexOfLastVisibleChild } from './hooks/useIndexOfLastVisibleChild';
25
+ export { default as useIsVisible } from './hooks/useIsVisible';
21
26
 
22
27
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
23
28
  // Things that don't have types
@@ -187,11 +192,6 @@ export const Sticky: any; // from './Sticky';
187
192
  export const SelectableBox: any; // from './SelectableBox';
188
193
  export const breakpoints: any; // from './utils/breakpoints';
189
194
  export const Variant: any; // from './utils/constants';
190
- export const useWindowSize: any; // from './hooks/useWindowSize';
191
- export const useToggle: any; // from './hooks/useToggle';
192
- export const useArrowKeyNavigation: any; // from './hooks/useArrowKeyNavigation';
193
- export const useIndexOfLastVisibleChild: any; // from './hooks/useIndexOfLastVisibleChild';
194
- export const useIsVisible: any; // from './hooks/useIsVisible';
195
195
  export const
196
196
  OverflowScrollContext: any,
197
197
  OverflowScroll: any,
package/dist/index.js CHANGED
@@ -18,6 +18,11 @@ export { default as ModalLayer } from './Modal/ModalLayer';
18
18
  export { default as Overlay, OverlayTrigger } from './Overlay';
19
19
  export { default as Portal } from './Modal/Portal';
20
20
  export { default as Tooltip } from './Tooltip';
21
+ export { default as useWindowSize } from './hooks/useWindowSize';
22
+ export { default as useToggle } from './hooks/useToggle';
23
+ export { default as useArrowKeyNavigation } from './hooks/useArrowKeyNavigation';
24
+ export { default as useIndexOfLastVisibleChild } from './hooks/useIndexOfLastVisibleChild';
25
+ export { default as useIsVisible } from './hooks/useIsVisible';
21
26
 
22
27
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
23
28
  // Things that don't have types
@@ -159,11 +164,6 @@ export { default as Sticky } from './Sticky';
159
164
  export { default as SelectableBox } from './SelectableBox';
160
165
  export { default as breakpoints } from './utils/breakpoints';
161
166
  export { default as Variant } from './utils/constants';
162
- export { default as useWindowSize } from './hooks/useWindowSize';
163
- export { default as useToggle } from './hooks/useToggle';
164
- export { default as useArrowKeyNavigation } from './hooks/useArrowKeyNavigation';
165
- export { default as useIndexOfLastVisibleChild } from './hooks/useIndexOfLastVisibleChild';
166
- export { default as useIsVisible } from './hooks/useIsVisible';
167
167
  export {
168
168
  OverflowScrollContext,
169
169
  OverflowScroll,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@openedx/paragon",
3
- "version": "23.1.1",
3
+ "version": "23.2.0",
4
4
  "description": "Accessible, responsive UI component library based on Bootstrap.",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -12,8 +12,8 @@ window.ResizeObserver = window.ResizeObserver
12
12
  }));
13
13
 
14
14
  function TestComponent() {
15
- const [containerElementRef, setContainerElementRef] = React.useState(null);
16
- const overflowElementRef = React.useRef(null);
15
+ const [containerElementRef, setContainerElementRef] = React.useState<HTMLDivElement | null>(null);
16
+ const overflowElementRef = React.useRef<HTMLDivElement>(null);
17
17
  const indexOfLastVisibleChild = useIndexOfLastVisibleChild(containerElementRef, overflowElementRef.current);
18
18
 
19
19
  return (
@@ -4,6 +4,7 @@ import { render, screen } from '@testing-library/react';
4
4
  import userEvent from '@testing-library/user-event';
5
5
 
6
6
  import { useToggle } from '../..';
7
+ import { ToggleHandlers } from '../useToggle';
7
8
 
8
9
  const TOGGLE_IS_ON = 'on';
9
10
  const TOGGLE_IS_OFF = 'off';
@@ -19,7 +20,7 @@ const resetHandlerMocks = () => {
19
20
  };
20
21
 
21
22
  // eslint-disable-next-line react/prop-types
22
- function FakeComponent({ defaultIsOn, handlers }) {
23
+ function FakeComponent({ defaultIsOn, handlers }: { defaultIsOn: boolean, handlers: ToggleHandlers }) {
23
24
  const [isOn, setOn, setOff, toggle] = useToggle(defaultIsOn, handlers);
24
25
 
25
26
  return (
@@ -1,16 +1,24 @@
1
1
  import { useRef, useEffect } from 'react';
2
2
 
3
- /**
4
- * A React hook to enable arrow key navigation on a component.
5
- */
3
+ interface HandleEnterArgs {
4
+ event: KeyboardEvent;
5
+ currentIndex: number;
6
+ activeElement: HTMLElement;
7
+ }
6
8
 
7
- function handleEnter({ event, currentIndex, activeElement }) {
9
+ function handleEnter({ event, currentIndex, activeElement }: HandleEnterArgs) {
8
10
  if (currentIndex === -1) { return; }
9
11
  activeElement.click();
10
12
  event.preventDefault();
11
13
  }
12
14
 
13
- function handleArrowKey({ event, currentIndex, availableElements }) {
15
+ interface HandleArrowKeyArgs {
16
+ event: KeyboardEvent;
17
+ currentIndex: number;
18
+ availableElements: NodeListOf<HTMLElement>;
19
+ }
20
+
21
+ function handleArrowKey({ event, currentIndex, availableElements }: HandleArrowKeyArgs) {
14
22
  // If the focus isn't in the container, focus on the first thing
15
23
  if (currentIndex === -1) { availableElements[0].focus(); }
16
24
 
@@ -36,6 +44,13 @@ function handleArrowKey({ event, currentIndex, availableElements }) {
36
44
  event.preventDefault();
37
45
  }
38
46
 
47
+ interface HandleEventsArgs {
48
+ event: KeyboardEvent;
49
+ ignoredKeys?: string[];
50
+ parentNode: HTMLElement | undefined;
51
+ selectors?: string;
52
+ }
53
+
39
54
  /**
40
55
  * Implement arrow key navigation for the given parentNode
41
56
  */
@@ -44,7 +59,7 @@ function handleEvents({
44
59
  ignoredKeys = [],
45
60
  parentNode,
46
61
  selectors = 'a,button,input',
47
- }) {
62
+ }: HandleEventsArgs) {
48
63
  if (!parentNode) { return; }
49
64
 
50
65
  const { key } = event;
@@ -60,7 +75,7 @@ function handleEvents({
60
75
  if (!parentNode.contains(activeElement)) { return; }
61
76
 
62
77
  // Get the list of elements we're allowed to scroll through
63
- const availableElements = parentNode.querySelectorAll(selectors);
78
+ const availableElements = parentNode.querySelectorAll<HTMLElement>(selectors);
64
79
 
65
80
  // No elements are available to loop through.
66
81
  if (!availableElements.length) { return; }
@@ -70,18 +85,27 @@ function handleEvents({
70
85
  (availableElement) => availableElement === activeElement,
71
86
  );
72
87
 
73
- if (key === 'Enter') {
74
- handleEnter({ event, currentIndex, activeElement });
88
+ if (key === 'Enter' && activeElement) {
89
+ handleEnter({ event, currentIndex, activeElement: activeElement as HTMLElement });
75
90
  }
76
91
  handleArrowKey({ event, currentIndex, availableElements });
77
92
  }
78
93
 
79
- export default function useArrowKeyNavigation(props) {
80
- const { selectors, ignoredKeys } = props || {};
81
- const parentNode = useRef();
94
+ export interface ArrowKeyNavProps {
95
+ /** e.g. 'a,button,input' */
96
+ selectors?: string;
97
+ ignoredKeys?: string[];
98
+ }
99
+
100
+ /**
101
+ * A React hook to enable arrow key navigation on a component.
102
+ */
103
+ export default function useArrowKeyNavigation(props: ArrowKeyNavProps = {}) {
104
+ const { selectors, ignoredKeys } = props;
105
+ const parentNode = useRef<HTMLElement>();
82
106
 
83
107
  useEffect(() => {
84
- const eventHandler = (event) => {
108
+ const eventHandler = (event: KeyboardEvent) => {
85
109
  handleEvents({
86
110
  event, ignoredKeys, parentNode: parentNode.current, selectors,
87
111
  });
@@ -5,25 +5,32 @@ import { useLayoutEffect, useState } from 'react';
5
5
  * that fits within its bounding rectangle. This is done by summing the widths
6
6
  * of the children until they exceed the width of the container.
7
7
  *
8
- * @param {Element} containerElementRef - container element
9
- * @param {Element} overflowElementRef - overflow element
10
- *
11
8
  * The hook returns the index of the last visible child.
9
+ *
10
+ * @param containerElementRef - container element
11
+ * @param overflowElementRef - overflow element
12
12
  */
13
- const useIndexOfLastVisibleChild = (containerElementRef, overflowElementRef) => {
13
+ const useIndexOfLastVisibleChild = (
14
+ containerElementRef: HTMLElement | null,
15
+ overflowElementRef: HTMLElement | null,
16
+ ): number => {
14
17
  const [indexOfLastVisibleChild, setIndexOfLastVisibleChild] = useState(-1);
15
18
 
16
19
  useLayoutEffect(() => {
20
+ if (!containerElementRef) {
21
+ return undefined;
22
+ }
23
+
17
24
  function updateLastVisibleChildIndex() {
18
25
  // Get array of child nodes from NodeList form
19
- const childNodesArr = Array.prototype.slice.call(containerElementRef.children);
26
+ const childNodesArr = Array.prototype.slice.call(containerElementRef!.children);
20
27
  const { nextIndexOfLastVisibleChild } = childNodesArr
21
28
  // filter out the overflow element
22
29
  .filter(childNode => childNode !== overflowElementRef)
23
30
  // sum the widths to find the last visible element's index
24
31
  .reduce((acc, childNode, index) => {
25
32
  acc.sumWidth += childNode.getBoundingClientRect().width;
26
- if (acc.sumWidth <= containerElementRef.getBoundingClientRect().width) {
33
+ if (acc.sumWidth <= containerElementRef!.getBoundingClientRect().width) {
27
34
  acc.nextIndexOfLastVisibleChild = index;
28
35
  }
29
36
  return acc;
@@ -32,23 +39,18 @@ const useIndexOfLastVisibleChild = (containerElementRef, overflowElementRef) =>
32
39
  // sometimes we'll show a dropdown with one item in it when it would fit,
33
40
  // but allowing this case dramatically simplifies the calculations we need
34
41
  // to do above.
35
- sumWidth: overflowElementRef ? overflowElementRef.getBoundingClientRect().width : 0,
42
+ sumWidth: overflowElementRef?.getBoundingClientRect().width ?? 0,
36
43
  nextIndexOfLastVisibleChild: -1,
37
44
  });
38
45
 
39
46
  setIndexOfLastVisibleChild(nextIndexOfLastVisibleChild);
40
47
  }
41
48
 
42
- if (containerElementRef) {
43
- updateLastVisibleChildIndex();
44
-
45
- const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex());
46
- resizeObserver.observe(containerElementRef);
47
-
48
- return () => resizeObserver.disconnect();
49
- }
49
+ updateLastVisibleChildIndex();
50
50
 
51
- return undefined;
51
+ const resizeObserver = new ResizeObserver(() => updateLastVisibleChildIndex());
52
+ resizeObserver.observe(containerElementRef);
53
+ return () => resizeObserver.disconnect();
52
54
  }, [containerElementRef, overflowElementRef]);
53
55
 
54
56
  return indexOfLastVisibleChild;
@@ -1,7 +1,10 @@
1
- import { useRef, useState, useEffect } from 'react';
1
+ import React, { useRef, useState, useEffect } from 'react';
2
2
 
3
- const useIsVisible = (defaultIsVisible = true) => {
4
- const sentinelRef = useRef();
3
+ const useIsVisible = (defaultIsVisible = true): [
4
+ isVisible: boolean,
5
+ sentinelRef: React.MutableRefObject<HTMLElement | null>,
6
+ ] => {
7
+ const sentinelRef = useRef<HTMLElement | null>(null);
5
8
  const [isVisible, setIsVisible] = useState(defaultIsVisible);
6
9
 
7
10
  useEffect(() => {
@@ -0,0 +1,38 @@
1
+ import { useState, useCallback } from 'react';
2
+
3
+ export type Toggler = [
4
+ isOn: boolean,
5
+ setOn: () => void,
6
+ setOff: () => void,
7
+ toggle: () => void,
8
+ ];
9
+
10
+ export interface ToggleHandlers {
11
+ handleToggleOn?: () => void;
12
+ handleToggleOff?: () => void;
13
+ handleToggle?: (newStatus: boolean) => void;
14
+ }
15
+
16
+ export default function useToggle(defaultIsOn = false, handlers: ToggleHandlers = {}): Toggler {
17
+ const { handleToggleOn, handleToggleOff, handleToggle } = handlers;
18
+ const [isOn, setIsOn] = useState(defaultIsOn);
19
+
20
+ const setOn = useCallback(() => {
21
+ setIsOn(true);
22
+ handleToggleOn?.();
23
+ handleToggle?.(true);
24
+ }, [handleToggleOn, handleToggle]);
25
+
26
+ const setOff = useCallback(() => {
27
+ setIsOn(false);
28
+ handleToggleOff?.();
29
+ handleToggle?.(false);
30
+ }, [handleToggleOff, handleToggle]);
31
+
32
+ const toggle = useCallback(() => {
33
+ const doToggle = isOn ? setOff : setOn;
34
+ doToggle();
35
+ }, [isOn, setOn, setOff]);
36
+
37
+ return [isOn, setOn, setOff, toggle];
38
+ }
@@ -1,9 +1,14 @@
1
1
  import { useState, useLayoutEffect } from 'react';
2
2
 
3
- function useWindowSize() {
3
+ export interface WindowSizeData {
4
+ width: number | undefined;
5
+ height: number | undefined;
6
+ }
7
+
8
+ function useWindowSize(): WindowSizeData {
4
9
  // Initialize state with undefined width/height so server and client renders match
5
10
  // Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
6
- const [windowSize, setWindowSize] = useState({
11
+ const [windowSize, setWindowSize] = useState<WindowSizeData>({
7
12
  width: undefined,
8
13
  height: undefined,
9
14
  });
package/src/index.d.ts CHANGED
@@ -18,6 +18,11 @@ export { default as ModalLayer } from './Modal/ModalLayer';
18
18
  export { default as Overlay, OverlayTrigger } from './Overlay';
19
19
  export { default as Portal } from './Modal/Portal';
20
20
  export { default as Tooltip } from './Tooltip';
21
+ export { default as useWindowSize, type WindowSizeData } from './hooks/useWindowSize';
22
+ export { default as useToggle, type Toggler, type ToggleHandlers } from './hooks/useToggle';
23
+ export { default as useArrowKeyNavigation, type ArrowKeyNavProps } from './hooks/useArrowKeyNavigation';
24
+ export { default as useIndexOfLastVisibleChild } from './hooks/useIndexOfLastVisibleChild';
25
+ export { default as useIsVisible } from './hooks/useIsVisible';
21
26
 
22
27
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
23
28
  // Things that don't have types
@@ -187,11 +192,6 @@ export const Sticky: any; // from './Sticky';
187
192
  export const SelectableBox: any; // from './SelectableBox';
188
193
  export const breakpoints: any; // from './utils/breakpoints';
189
194
  export const Variant: any; // from './utils/constants';
190
- export const useWindowSize: any; // from './hooks/useWindowSize';
191
- export const useToggle: any; // from './hooks/useToggle';
192
- export const useArrowKeyNavigation: any; // from './hooks/useArrowKeyNavigation';
193
- export const useIndexOfLastVisibleChild: any; // from './hooks/useIndexOfLastVisibleChild';
194
- export const useIsVisible: any; // from './hooks/useIsVisible';
195
195
  export const
196
196
  OverflowScrollContext: any,
197
197
  OverflowScroll: any,
package/src/index.js CHANGED
@@ -18,6 +18,11 @@ export { default as ModalLayer } from './Modal/ModalLayer';
18
18
  export { default as Overlay, OverlayTrigger } from './Overlay';
19
19
  export { default as Portal } from './Modal/Portal';
20
20
  export { default as Tooltip } from './Tooltip';
21
+ export { default as useWindowSize } from './hooks/useWindowSize';
22
+ export { default as useToggle } from './hooks/useToggle';
23
+ export { default as useArrowKeyNavigation } from './hooks/useArrowKeyNavigation';
24
+ export { default as useIndexOfLastVisibleChild } from './hooks/useIndexOfLastVisibleChild';
25
+ export { default as useIsVisible } from './hooks/useIsVisible';
21
26
 
22
27
  // // // // // // // // // // // // // // // // // // // // // // // // // // //
23
28
  // Things that don't have types
@@ -159,11 +164,6 @@ export { default as Sticky } from './Sticky';
159
164
  export { default as SelectableBox } from './SelectableBox';
160
165
  export { default as breakpoints } from './utils/breakpoints';
161
166
  export { default as Variant } from './utils/constants';
162
- export { default as useWindowSize } from './hooks/useWindowSize';
163
- export { default as useToggle } from './hooks/useToggle';
164
- export { default as useArrowKeyNavigation } from './hooks/useArrowKeyNavigation';
165
- export { default as useIndexOfLastVisibleChild } from './hooks/useIndexOfLastVisibleChild';
166
- export { default as useIsVisible } from './hooks/useIsVisible';
167
167
  export {
168
168
  OverflowScrollContext,
169
169
  OverflowScroll,
@@ -1,37 +0,0 @@
1
- import { useState, useCallback } from 'react';
2
-
3
- export default function useToggle(defaultIsOn, handlers = {}) {
4
- const { handleToggleOn, handleToggleOff, handleToggle } = handlers;
5
- const [isOn, setIsOn] = useState(defaultIsOn || false);
6
-
7
- const setOn = useCallback(() => {
8
- setIsOn(true);
9
- // istanbul ignore else
10
- if (handleToggleOn) {
11
- handleToggleOn();
12
- }
13
- // istanbul ignore else
14
- if (handleToggle) {
15
- handleToggle(true);
16
- }
17
- }, [handleToggleOn, handleToggle]);
18
-
19
- const setOff = useCallback(() => {
20
- setIsOn(false);
21
- // istanbul ignore else
22
- if (handleToggleOff) {
23
- handleToggleOff();
24
- }
25
- // istanbul ignore else
26
- if (handleToggle) {
27
- handleToggle(false);
28
- }
29
- }, [handleToggleOff, handleToggle]);
30
-
31
- const toggle = useCallback(() => {
32
- const doToggle = isOn ? setOff : setOn;
33
- doToggle();
34
- }, [isOn, setOn, setOff]);
35
-
36
- return [isOn, setOn, setOff, toggle];
37
- }