@artsy/palette 29.7.0 → 29.7.2

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.
@@ -25,9 +25,15 @@ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Sy
25
25
 
26
26
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
27
27
 
28
+ var MUTATION_OBSERVER_OPTIONS = {
29
+ attributes: true,
30
+ subtree: true,
31
+ attributeFilter: ["disabled"]
32
+ };
28
33
  /**
29
34
  * Locks focus within the given element
30
35
  */
36
+
31
37
  var useFocusLock = function useFocusLock(_ref) {
32
38
  var ref = _ref.ref,
33
39
  _ref$active = _ref.active,
@@ -54,21 +60,23 @@ var useFocusLock = function useFocusLock(_ref) {
54
60
  clearTimeout(timeout);
55
61
  };
56
62
  }, [updateFocusableEls]);
57
- var skipUpdateFocusRef = (0, _react.useRef)(false); // Detects when DOM changes and updates focusable elements
63
+ var skipUpdateFocusRef = (0, _react.useRef)(false);
64
+ var handleMutate = (0, _react.useCallback)(function (mutations) {
65
+ // Check to see if any of the mutations has either added or removed nodes
66
+ var hasMeaningfullyMutated = mutations.some(function (mutation) {
67
+ return mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0 || mutation.attributeName === "disabled";
68
+ });
69
+
70
+ if (hasMeaningfullyMutated) {
71
+ skipUpdateFocusRef.current = true;
72
+ updateFocusableEls();
73
+ }
74
+ }, [updateFocusableEls]); // Detects when DOM changes and updates focusable elements
58
75
 
59
76
  (0, _useMutationObserver.useMutationObserver)({
60
77
  ref: ref,
61
- onMutate: function onMutate(mutations) {
62
- // Check to see if any of the mutations has either added or removed nodes
63
- var hasMeaningfullyMutated = mutations.some(function (mutation) {
64
- return mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0;
65
- });
66
-
67
- if (hasMeaningfullyMutated) {
68
- skipUpdateFocusRef.current = true;
69
- updateFocusableEls();
70
- }
71
- }
78
+ onMutate: handleMutate,
79
+ options: MUTATION_OBSERVER_OPTIONS
72
80
  });
73
81
 
74
82
  var _useCursor = (0, _useCursor2.useCursor)({
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/useFocusLock.ts"],"names":["useFocusLock","ref","active","focusableEls","setFocusableEls","updateFocusableEls","current","timeout","setTimeout","clearTimeout","skipUpdateFocusRef","onMutate","mutations","hasMeaningfullyMutated","some","mutation","addedNodes","length","removedNodes","max","focusableIndex","index","handlePrev","handleNext","setCursor","focus","handleKeydown","event","key","preventDefault","stopPropagation","shiftKey","focusedElPriorToOpen","document","activeElement","addEventListener","removeEventListener","handleClick","target","findIndex","node","handleFocusIn","focusableEl","stopImmediatePropagation"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;AASA;AACA;AACA;AACO,IAAMA,YAAY,GAAG,SAAfA,YAAe,OAA0C;AAAA,MAAvCC,GAAuC,QAAvCA,GAAuC;AAAA,yBAAlCC,MAAkC;AAAA,MAAlCA,MAAkC,4BAAzB,IAAyB;;AACpE,kBAAwC,qBAA6B,EAA7B,CAAxC;AAAA;AAAA,MAAOC,YAAP;AAAA,MAAqBC,eAArB;;AAEA,MAAMC,kBAAkB,GAAG,wBAAY,YAAM;AAC3C,QAAIJ,GAAG,CAACK,OAAJ,KAAgB,IAApB,EAA0B;AAE1BF,IAAAA,eAAe,CAAC,wBAASH,GAAG,CAACK,OAAb,CAAD,CAAf,CAH2C,CAK3C;AACA;AACA;AACD,GAR0B,EAQxB,CAACJ,MAAD,CARwB,CAA3B,CAHoE,CAapE;;AACA,wBAAU,YAAM;AACd;AACA,QAAMK,OAAO,GAAGC,UAAU,CAAC,YAAM;AAC/BH,MAAAA,kBAAkB;AACnB,KAFyB,EAEvB,CAFuB,CAA1B;AAIA,WAAO,YAAM;AACXI,MAAAA,YAAY,CAACF,OAAD,CAAZ;AACD,KAFD;AAGD,GATD,EASG,CAACF,kBAAD,CATH;AAWA,MAAMK,kBAAkB,GAAG,mBAAO,KAAP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;;AAWAQ,IAAAA,QAAQ,CAACE,gBAAT,CAA0B,OAA1B,EAAmCE,WAAnC;AACAJ,IAAAA,QAAQ,CAACE,gBAAT,CAA0B,SAA1B,EAAqCM,aAArC;AACA,WAAO,YAAM;AACXR,MAAAA,QAAQ,CAACG,mBAAT,CAA6B,OAA7B,EAAsCC,WAAtC;AACAJ,MAAAA,QAAQ,CAACG,mBAAT,CAA6B,SAA7B,EAAwCK,aAAxC;AACD,KAHD;AAID,GA9BD,EA8BG,CAACtC,YAAD,EAAeiB,cAAf,EAA+BI,SAA/B,CA9BH;AA+BD,CAhIM","sourcesContent":["import React, { useCallback, useEffect, useRef, useState } from \"react\"\nimport { useCursor } from \"use-cursor\"\nimport { useMutationObserver } from \"./useMutationObserver\"\nimport { tabbable } from \"tabbable\"\n\ninterface UseFocusLock {\n ref: React.MutableRefObject<HTMLElement | null>\n active?: boolean\n}\n\ntype FocusableElement = HTMLElement | SVGElement\n\n/**\n * Locks focus within the given element\n */\nexport const useFocusLock = ({ ref, active = true }: UseFocusLock) => {\n const [focusableEls, setFocusableEls] = useState<FocusableElement[]>([])\n\n const updateFocusableEls = useCallback(() => {\n if (ref.current === null) return\n\n setFocusableEls(tabbable(ref.current))\n\n // When `active` changes that typically means our target ref\n // is being inserted into the DOM, so we need to update the focusable elements.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [active])\n\n // Set initial focusable elements on mount\n useEffect(() => {\n // Wait for the next tick to ensure focusable elements are in the DOM\n const timeout = setTimeout(() => {\n updateFocusableEls()\n }, 0)\n\n return () => {\n clearTimeout(timeout)\n }\n }, [updateFocusableEls])\n\n const skipUpdateFocusRef = useRef(false)\n\n // Detects when DOM changes and updates focusable elements\n useMutationObserver({\n ref,\n onMutate: (mutations) => {\n // Check to see if any of the mutations has either added or removed nodes\n const hasMeaningfullyMutated = mutations.some((mutation) => {\n return (\n mutation.addedNodes.length > 0 || mutation.removedNodes.length > 0\n )\n })\n\n if (hasMeaningfullyMutated) {\n skipUpdateFocusRef.current = true\n updateFocusableEls()\n }\n },\n })\n\n const {\n index: focusableIndex,\n handlePrev,\n handleNext,\n setCursor,\n } = useCursor({ max: focusableEls.length })\n\n // Moves focus when index changes\n useEffect(() => {\n if (!focusableEls.length) return\n\n // In order to avoid a loop when a focus might be the cause of a mutation,\n // we skip focusing the el.\n if (skipUpdateFocusRef.current) {\n skipUpdateFocusRef.current = false\n return\n }\n\n focusableEls[focusableIndex].focus()\n }, [focusableEls, focusableIndex])\n\n // Handle keyboard navigation\n useEffect(() => {\n const handleKeydown = (event: KeyboardEvent) => {\n if (!active) return\n\n switch (event.key) {\n case \"Tab\":\n // Lock focus within element\n event.preventDefault()\n event.stopPropagation()\n\n // Move focus up or down\n event.shiftKey ? handlePrev() : handleNext()\n break\n default:\n break\n }\n }\n\n const focusedElPriorToOpen = document.activeElement as HTMLElement\n\n document.addEventListener(\"keydown\", handleKeydown)\n\n return () => {\n // Return the focus\n focusedElPriorToOpen.focus()\n\n document.removeEventListener(\"keydown\", handleKeydown)\n }\n }, [active, handleNext, handlePrev])\n\n useEffect(() => {\n // Update the index when any focusable is clicked\n const handleClick = (event: MouseEvent) => {\n if (event.target !== document.activeElement) return\n\n const index = focusableEls.findIndex((node) => node === event.target)\n\n if (index === -1) return\n\n setCursor(index)\n }\n\n // If focus escapes the trap, pull it back in\n const handleFocusIn = (event: FocusEvent) => {\n const index = focusableEls.findIndex((node) => node === event.target)\n const focusableEl = focusableEls[focusableIndex]\n\n if (index === -1 && !!focusableEl) {\n event.stopImmediatePropagation()\n focusableEl.focus()\n return\n }\n }\n\n document.addEventListener(\"click\", handleClick)\n document.addEventListener(\"focusin\", handleFocusIn)\n return () => {\n document.removeEventListener(\"click\", handleClick)\n document.removeEventListener(\"focusin\", handleFocusIn)\n }\n }, [focusableEls, focusableIndex, setCursor])\n}\n"],"file":"useFocusLock.js"}
1
+ {"version":3,"sources":["../../src/utils/useFocusLock.ts"],"names":["MUTATION_OBSERVER_OPTIONS","attributes","subtree","attributeFilter","useFocusLock","ref","active","focusableEls","setFocusableEls","updateFocusableEls","current","timeout","setTimeout","clearTimeout","skipUpdateFocusRef","handleMutate","mutations","hasMeaningfullyMutated","some","mutation","addedNodes","length","removedNodes","attributeName","onMutate","options","max","focusableIndex","index","handlePrev","handleNext","setCursor","focus","handleKeydown","event","key","preventDefault","stopPropagation","shiftKey","focusedElPriorToOpen","document","activeElement","addEventListener","removeEventListener","handleClick","target","findIndex","node","handleFocusIn","focusableEl","stopImmediatePropagation"],"mappings":";;;;;;;AAAA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;AASA,IAAMA,yBAAyB,GAAG;AAChCC,EAAAA,UAAU,EAAE,IADoB;AAEhCC,EAAAA,OAAO,EAAE,IAFuB;AAGhCC,EAAAA,eAAe,EAAE,CAAC,UAAD;AAHe,CAAlC;AAMA;AACA;AACA;;AACO,IAAMC,YAAY,GAAG,SAAfA,YAAe,OAA0C;AAAA,MAAvCC,GAAuC,QAAvCA,GAAuC;AAAA,yBAAlCC,MAAkC;AAAA,MAAlCA,MAAkC,4BAAzB,IAAyB;;AACpE,kBAAwC,qBAA6B,EAA7B,CAAxC;AAAA;AAAA,MAAOC,YAAP;AAAA,MAAqBC,eAArB;;AAEA,MAAMC,kBAAkB,GAAG,wBAAY,YAAM;AAC3C,QAAIJ,GAAG,CAACK,OAAJ,KAAgB,IAApB,EAA0B;AAE1BF,IAAAA,eAAe,CAAC,wBAASH,GAAG,CAACK,OAAb,CAAD,CAAf,CAH2C,CAK3C;AACA;AACA;AACD,GAR0B,EAQxB,CAACJ,MAAD,CARwB,CAA3B,CAHoE,CAapE;;AACA,wBAAU,YAAM;AACd;AACA,QAAMK,OAAO,GAAGC,UAAU,CAAC,YAAM;AAC/BH,MAAAA,kBAAkB;AACnB,KAFyB,EAEvB,CAFuB,CAA1B;AAIA,WAAO,YAAM;AACXI,MAAAA,YAAY,CAACF,OAAD,CAAZ;AACD,KAFD;AAGD,GATD,EASG,CAACF,kBAAD,CATH;A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GAAG1C,YAAY,CAACoB,cAAD,CAAhC;;AAEA,UAAIC,KAAK,KAAK,CAAC,CAAX,IAAgB,CAAC,CAACqB,WAAtB,EAAmC;AACjCf,QAAAA,KAAK,CAACgB,wBAAN;AACAD,QAAAA,WAAW,CAACjB,KAAZ;AACA;AACD;AACF,KATD;;AAWAQ,IAAAA,QAAQ,CAACE,gBAAT,CAA0B,OAA1B,EAAmCE,WAAnC;AACAJ,IAAAA,QAAQ,CAACE,gBAAT,CAA0B,SAA1B,EAAqCM,aAArC;AACA,WAAO,YAAM;AACXR,MAAAA,QAAQ,CAACG,mBAAT,CAA6B,OAA7B,EAAsCC,WAAtC;AACAJ,MAAAA,QAAQ,CAACG,mBAAT,CAA6B,SAA7B,EAAwCK,aAAxC;AACD,KAHD;AAID,GA9BD,EA8BG,CAACzC,YAAD,EAAeoB,cAAf,EAA+BI,SAA/B,CA9BH;AA+BD,CAxIM","sourcesContent":["import React, { useCallback, useEffect, useRef, useState } from \"react\"\nimport { useCursor } from \"use-cursor\"\nimport { useMutationObserver } from \"./useMutationObserver\"\nimport { tabbable } from \"tabbable\"\n\ninterface UseFocusLock {\n ref: React.MutableRefObject<HTMLElement | null>\n active?: boolean\n}\n\ntype FocusableElement = HTMLElement | SVGElement\n\nconst MUTATION_OBSERVER_OPTIONS = {\n attributes: true,\n subtree: true,\n attributeFilter: [\"disabled\"],\n}\n\n/**\n * Locks focus within the given element\n */\nexport const useFocusLock = ({ ref, active = true }: UseFocusLock) => {\n const [focusableEls, setFocusableEls] = useState<FocusableElement[]>([])\n\n const updateFocusableEls = useCallback(() => {\n if (ref.current === null) return\n\n setFocusableEls(tabbable(ref.current))\n\n // When `active` changes that typically means our target ref\n // is being inserted into the DOM, so we need to update the focusable elements.\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [active])\n\n // Set initial focusable elements on mount\n useEffect(() => {\n // Wait for the next tick to ensure focusable elements are in the DOM\n const timeout = setTimeout(() => {\n updateFocusableEls()\n }, 0)\n\n return () => {\n clearTimeout(timeout)\n }\n }, [updateFocusableEls])\n\n const skipUpdateFocusRef = useRef(false)\n\n const handleMutate = useCallback(\n (mutations: MutationRecord[]) => {\n // Check to see if any of the mutations has either added or removed nodes\n const hasMeaningfullyMutated = mutations.some((mutation) => {\n return (\n mutation.addedNodes.length > 0 ||\n mutation.removedNodes.length > 0 ||\n mutation.attributeName === \"disabled\"\n )\n })\n\n if (hasMeaningfullyMutated) {\n skipUpdateFocusRef.current = true\n updateFocusableEls()\n }\n },\n [updateFocusableEls]\n )\n\n // Detects when DOM changes and updates focusable elements\n useMutationObserver({\n ref,\n onMutate: handleMutate,\n options: MUTATION_OBSERVER_OPTIONS,\n })\n\n const {\n index: focusableIndex,\n handlePrev,\n handleNext,\n setCursor,\n } = useCursor({ max: focusableEls.length })\n\n // Moves focus when index changes\n useEffect(() => {\n if (!focusableEls.length) return\n\n // In order to avoid a loop when a focus might be the cause of a mutation,\n // we skip focusing the el.\n if (skipUpdateFocusRef.current) {\n skipUpdateFocusRef.current = false\n return\n }\n\n focusableEls[focusableIndex].focus()\n }, [focusableEls, focusableIndex])\n\n // Handle keyboard navigation\n useEffect(() => {\n const handleKeydown = (event: KeyboardEvent) => {\n if (!active) return\n\n switch (event.key) {\n case \"Tab\":\n // Lock focus within element\n event.preventDefault()\n event.stopPropagation()\n\n // Move focus up or down\n event.shiftKey ? handlePrev() : handleNext()\n break\n default:\n break\n }\n }\n\n const focusedElPriorToOpen = document.activeElement as HTMLElement\n\n document.addEventListener(\"keydown\", handleKeydown)\n\n return () => {\n // Return the focus\n focusedElPriorToOpen.focus()\n\n document.removeEventListener(\"keydown\", handleKeydown)\n }\n }, [active, handleNext, handlePrev])\n\n useEffect(() => {\n // Update the index when any focusable is clicked\n const handleClick = (event: MouseEvent) => {\n if (event.target !== document.activeElement) return\n\n const index = focusableEls.findIndex((node) => node === event.target)\n\n if (index === -1) return\n\n setCursor(index)\n }\n\n // If focus escapes the trap, pull it back in\n const handleFocusIn = (event: FocusEvent) => {\n const index = focusableEls.findIndex((node) => node === event.target)\n const focusableEl = focusableEls[focusableIndex]\n\n if (index === -1 && !!focusableEl) {\n event.stopImmediatePropagation()\n focusableEl.focus()\n return\n }\n }\n\n document.addEventListener(\"click\", handleClick)\n document.addEventListener(\"focusin\", handleFocusIn)\n return () => {\n document.removeEventListener(\"click\", handleClick)\n document.removeEventListener(\"focusin\", handleFocusIn)\n }\n }, [focusableEls, focusableIndex, setCursor])\n}\n"],"file":"useFocusLock.js"}
@@ -4,3 +4,4 @@ declare const _default: {
4
4
  export default _default;
5
5
  export declare const Default: () => JSX.Element;
6
6
  export declare const WithAutocompleteInput: () => JSX.Element;
7
+ export declare const DisableToEnable: () => JSX.Element;
@@ -5,7 +5,7 @@ function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" =
5
5
  Object.defineProperty(exports, "__esModule", {
6
6
  value: true
7
7
  });
8
- exports.default = exports.WithAutocompleteInput = exports.Default = void 0;
8
+ exports.default = exports.WithAutocompleteInput = exports.DisableToEnable = exports.Default = void 0;
9
9
 
10
10
  var _react = _interopRequireWildcard(require("react"));
11
11
 
@@ -21,6 +21,18 @@ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "functio
21
21
 
22
22
  function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
23
23
 
24
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
25
+
26
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
27
+
28
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
29
+
30
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
31
+
32
+ function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
33
+
34
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
35
+
24
36
  var _default = {
25
37
  title: "Hooks/useFocusLock"
26
38
  };
@@ -87,4 +99,30 @@ var WithAutocompleteInput = function WithAutocompleteInput() {
87
99
 
88
100
  exports.WithAutocompleteInput = WithAutocompleteInput;
89
101
  WithAutocompleteInput.displayName = "WithAutocompleteInput";
102
+
103
+ var DisableToEnable = function DisableToEnable() {
104
+ var _useState = (0, _react.useState)(""),
105
+ _useState2 = _slicedToArray(_useState, 2),
106
+ value = _useState2[0],
107
+ setValue = _useState2[1];
108
+
109
+ var ref = (0, _react.useRef)(null);
110
+ (0, _useFocusLock.useFocusLock)({
111
+ ref: ref
112
+ });
113
+ return /*#__PURE__*/_react.default.createElement("div", {
114
+ ref: ref
115
+ }, /*#__PURE__*/_react.default.createElement(_Input.Input, {
116
+ placeholder: "Value",
117
+ value: value,
118
+ onChange: function onChange(e) {
119
+ return setValue(e.target.value);
120
+ }
121
+ }), /*#__PURE__*/_react.default.createElement(_Button.Button, {
122
+ disabled: !value
123
+ }, "Submit"));
124
+ };
125
+
126
+ exports.DisableToEnable = DisableToEnable;
127
+ DisableToEnable.displayName = "DisableToEnable";
90
128
  //# sourceMappingURL=useFocusLock.story.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/useFocusLock.story.tsx"],"names":["title","Default","ref","WithAutocompleteInput","text","value"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;;;;;eAEe;AACbA,EAAAA,KAAK,EAAE;AADM,C;;;AAIR,IAAMC,OAAO,GAAG,SAAVA,OAAU,GAAM;AAC3B,MAAMC,GAAG,GAAG,mBAA8B,IAA9B,CAAZ;AAEA,kCAAa;AAAEA,IAAAA,GAAG,EAAHA;AAAF,GAAb;AAEA,sBACE,yEACE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IADF,eAEE;AAAK,IAAA,GAAG,EAAEA;AAAV,kBACE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IADF,eAEE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IAFF,eAGE;AAAG,IAAA,IAAI,EAAC,GAAR;AAAY,IAAA,QAAQ,EAAE,CAAC;AAAvB,eAHF,eAME;AAAG,IAAA,IAAI,EAAC;AAAR,iBANF,eAOE,6BAAC,cAAD;AAAQ,IAAA,OAAO,EAAC,aAAhB;AAA8B,IAAA,QAAQ,EAAE,CAAC;AAAzC,eAPF,eAUE,6BAAC,cAAD;AAAQ,IAAA,OAAO,EAAC,aAAhB;AAA8B,IAAA,QAAQ;AAAtC,gBAVF,eAaE,6BAAC,cAAD;AAAQ,IAAA,OAAO,EAAC;AAAhB,iBAbF,CAFF,eAiBE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IAjBF,CADF;AAqBD,CA1BM;;;;AA4BA,IAAMC,qBAAqB,GAAG,SAAxBA,qBAAwB,GAAM;AACzC,MAAMD,GAAG,GAAG,mBAA8B,IAA9B,CAAZ;AAEA,kCAAa;AAAEA,IAAAA,GAAG,EAAHA;AAAF,GAAb;AAEA,sBACE;AAAK,IAAA,GAAG,EAAEA;AAAV,kBACE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IADF,eAEE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IAFF,eAGE,6BAAC,oCAAD;AACE,IAAA,WAAW,EAAC,WADd;AAEE,IAAA,OAAO,EAAE,CACP;AAAEE,MAAAA,IAAI,EAAE,KAAR;AAAeC,MAAAA,KAAK,EAAE;AAAtB,KADO,EAEP;AAAED,MAAAA,IAAI,EAAE,KAAR;AAAeC,MAAAA,KAAK,EAAE;AAAtB,KAFO,EAGP;AAAED,MAAAA,IAAI,EAAE,OAAR;AAAiBC,MAAAA,KAAK,EAAE;AAAxB,KAHO;AAFX,IAHF,CADF;AAcD,CAnBM;;;AAAMF,qB","sourcesContent":["import React, { useRef } from \"react\"\nimport { useFocusLock } from \"./useFocusLock\"\nimport { Input } from \"../elements/Input\"\nimport { Button } from \"../elements/Button\"\nimport { AutocompleteInput } from \"../elements/AutocompleteInput\"\n\nexport default {\n title: \"Hooks/useFocusLock\",\n}\n\nexport const Default = () => {\n const ref = useRef<HTMLDivElement | null>(null)\n\n useFocusLock({ ref })\n\n return (\n <>\n <Input placeholder=\"Not focusable\" />\n <div ref={ref}>\n <Input placeholder=\"Focusable\" />\n <Input placeholder=\"Focusable\" />\n <a href=\"#\" tabIndex={-1}>\n Skipped\n </a>\n <a href=\"#\">Focusable</a>\n <Button variant=\"primaryGray\" tabIndex={-1}>\n Skipped\n </Button>\n <Button variant=\"primaryGray\" disabled>\n Disabled\n </Button>\n <Button variant=\"primaryGray\">Focusable</Button>\n </div>\n <Input placeholder=\"Not focusable\" />\n </>\n )\n}\n\nexport const WithAutocompleteInput = () => {\n const ref = useRef<HTMLDivElement | null>(null)\n\n useFocusLock({ ref })\n\n return (\n <div ref={ref}>\n <Input placeholder=\"Focusable\" />\n <Input placeholder=\"Focusable\" />\n <AutocompleteInput\n placeholder=\"Focusable\"\n options={[\n { text: \"One\", value: \"one\" },\n { text: \"Two\", value: \"two\" },\n { text: \"Three\", value: \"three\" },\n ]}\n />\n </div>\n )\n}\n"],"file":"useFocusLock.story.js"}
1
+ {"version":3,"sources":["../../src/utils/useFocusLock.story.tsx"],"names":["title","Default","ref","WithAutocompleteInput","text","value","DisableToEnable","setValue","e","target"],"mappings":";;;;;;;;;AAAA;;AACA;;AACA;;AACA;;AACA;;;;;;;;;;;;;;;;;;eAEe;AACbA,EAAAA,KAAK,EAAE;AADM,C;;;AAIR,IAAMC,OAAO,GAAG,SAAVA,OAAU,GAAM;AAC3B,MAAMC,GAAG,GAAG,mBAA8B,IAA9B,CAAZ;AAEA,kCAAa;AAAEA,IAAAA,GAAG,EAAHA;AAAF,GAAb;AAEA,sBACE,yEACE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IADF,eAEE;AAAK,IAAA,GAAG,EAAEA;AAAV,kBACE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IADF,eAEE,6BAAC,YAAD;AAAO,IAAA,WAAW,EAAC;AAAnB,IAFF,eAGE;AAAG,IAAA,IAAI,EAAC,GAAR;AAAY,IAAA,QAAQ,EAAE,CAAC;AAAvB,eAHF,eAME;AAAG,IAAA,IAAI,EAAC;AAAR,iBANF,eAOE,6BAAC,cAAD;AAAQ,IAAA,OAAO,EAAC,aAAhB;AAA8B,IAAA,QAAQ,EAAE,CAAC;AAAzC,eAPF,eAUE,6BAAC,cAAD;AAA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Q,EAAE,kBAACG,CAAD;AAAA,aAAOD,QAAQ,CAACC,CAAC,CAACC,MAAF,CAASJ,KAAV,CAAf;AAAA;AAHZ,IADF,eAME,6BAAC,cAAD;AAAQ,IAAA,QAAQ,EAAE,CAACA;AAAnB,cANF,CADF;AAUD,CAjBM;;;AAAMC,e","sourcesContent":["import React, { useRef, useState } from \"react\"\nimport { useFocusLock } from \"./useFocusLock\"\nimport { Input } from \"../elements/Input\"\nimport { Button } from \"../elements/Button\"\nimport { AutocompleteInput } from \"../elements/AutocompleteInput\"\n\nexport default {\n title: \"Hooks/useFocusLock\",\n}\n\nexport const Default = () => {\n const ref = useRef<HTMLDivElement | null>(null)\n\n useFocusLock({ ref })\n\n return (\n <>\n <Input placeholder=\"Not focusable\" />\n <div ref={ref}>\n <Input placeholder=\"Focusable\" />\n <Input placeholder=\"Focusable\" />\n <a href=\"#\" tabIndex={-1}>\n Skipped\n </a>\n <a href=\"#\">Focusable</a>\n <Button variant=\"primaryGray\" tabIndex={-1}>\n Skipped\n </Button>\n <Button variant=\"primaryGray\" disabled>\n Disabled\n </Button>\n <Button variant=\"primaryGray\">Focusable</Button>\n </div>\n <Input placeholder=\"Not focusable\" />\n </>\n )\n}\n\nexport const WithAutocompleteInput = () => {\n const ref = useRef<HTMLDivElement | null>(null)\n\n useFocusLock({ ref })\n\n return (\n <div ref={ref}>\n <Input placeholder=\"Focusable\" />\n <Input placeholder=\"Focusable\" />\n <AutocompleteInput\n placeholder=\"Focusable\"\n options={[\n { text: \"One\", value: \"one\" },\n { text: \"Two\", value: \"two\" },\n { text: \"Three\", value: \"three\" },\n ]}\n />\n </div>\n )\n}\n\nexport const DisableToEnable = () => {\n const [value, setValue] = useState(\"\")\n\n const ref = useRef<HTMLDivElement | null>(null)\n\n useFocusLock({ ref })\n\n return (\n <div ref={ref}>\n <Input\n placeholder=\"Value\"\n value={value}\n onChange={(e) => setValue(e.target.value)}\n />\n <Button disabled={!value}>Submit</Button>\n </div>\n )\n}\n"],"file":"useFocusLock.story.js"}
@@ -13,18 +13,20 @@ function _objectWithoutProperties(source, excluded) { if (source == null) return
13
13
 
14
14
  function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
15
15
 
16
+ var DEFAULT_OPTIONS = {
17
+ attributes: true,
18
+ characterData: true,
19
+ childList: true,
20
+ subtree: true
21
+ };
16
22
  /**
17
23
  * Accepts a ref and calls the `onMutate` callback when mutations are observed.
18
24
  */
25
+
19
26
  var useMutationObserver = function useMutationObserver(_ref) {
20
27
  var onMutate = _ref.onMutate,
21
28
  _ref$options = _ref.options,
22
- options = _ref$options === void 0 ? {
23
- attributes: true,
24
- characterData: true,
25
- childList: true,
26
- subtree: true
27
- } : _ref$options,
29
+ options = _ref$options === void 0 ? DEFAULT_OPTIONS : _ref$options,
28
30
  rest = _objectWithoutProperties(_ref, _excluded);
29
31
 
30
32
  (0, _react.useEffect)(function () {
@@ -33,16 +35,13 @@ var useMutationObserver = function useMutationObserver(_ref) {
33
35
  }
34
36
 
35
37
  var el = "ref" in rest ? rest.ref.current : rest.element;
38
+ if (!el) return;
39
+ var observer = new MutationObserver(onMutate); // Start observing the target node for configured mutations
36
40
 
37
- if (el) {
38
- var observer = new MutationObserver(onMutate); // Start observing the target node for configured mutations
39
-
40
- observer.observe(el, options);
41
- return function () {
42
- observer.disconnect();
43
- };
44
- } // eslint-disable-next-line react-hooks/exhaustive-deps
45
-
41
+ observer.observe(el, options);
42
+ return function () {
43
+ observer.disconnect();
44
+ }; // eslint-disable-next-line react-hooks/exhaustive-deps
46
45
  }, [onMutate, options]);
47
46
  };
48
47
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utils/useMutationObserver.ts"],"names":["useMutationObserver","onMutate","options","attributes","characterData","childList","subtree","rest","MutationObserver","el","ref","current","element","observer","observe","disconnect"],"mappings":";;;;;;;AACA;;;;;;;;AAUA;AACA;AACA;AACO,IAAMA,mBAAmB,GAAG,SAAtBA,mBAAsB,OASR;AAAA,MARzBC,QAQyB,QARzBA,QAQyB;AAAA,0BAPzBC,OAOyB;AAAA,MAPzBA,OAOyB,6BAPf;AACRC,IAAAA,UAAU,EAAE,IADJ;AAERC,IAAAA,aAAa,EAAE,IAFP;AAGRC,IAAAA,SAAS,EAAE,IAHH;AAIRC,IAAAA,OAAO,EAAE;AAJD,GAOe;AAAA,MADtBC,IACsB;;AACzB,wBAAU,YAAM;AACd,QAAI,OAAOC,gBAAP,KAA4B,WAAhC,EAA6C;AAC3C;AACD;;AAED,QAAMC,EAAE,GAAG,SAASF,IAAT,GAAgBA,IAAI,CAACG,GAAL,CAASC,OAAzB,GAAmCJ,IAAI,CAACK,OAAnD;;AAEA,QAAIH,EAAJ,EAAQ;AACN,UAAMI,QAAQ,GAAG,IAAIL,gBAAJ,CAAqBP,QAArB,CAAjB,CADM,CAGN;;AACAY,MAAAA,QAAQ,CAACC,OAAT,CAAiBL,EAAjB,EAAqBP,OAArB;AAEA,aAAO,YAAM;AACXW,QAAAA,QAAQ,CAACE,UAAT;AACD,OAFD;AAGD,KAhBa,CAiBd;;AACD,GAlBD,EAkBG,CAACd,QAAD,EAAWC,OAAX,CAlBH;AAmBD,CA7BM","sourcesContent":["import type { MutableRefObject } from \"react\"\nimport { useEffect } from \"react\"\n\ntype UseMutationObserver = {\n onMutate: MutationCallback\n options?: MutationObserverInit\n} & (\n | { ref: MutableRefObject<HTMLElement | null> }\n | { element?: HTMLElement | null }\n)\n\n/**\n * Accepts a ref and calls the `onMutate` callback when mutations are observed.\n */\nexport const useMutationObserver = ({\n onMutate,\n options = {\n attributes: true,\n characterData: true,\n childList: true,\n subtree: true,\n },\n ...rest\n}: UseMutationObserver) => {\n useEffect(() => {\n if (typeof MutationObserver === \"undefined\") {\n return\n }\n\n const el = \"ref\" in rest ? rest.ref.current : rest.element\n\n if (el) {\n const observer = new MutationObserver(onMutate)\n\n // Start observing the target node for configured mutations\n observer.observe(el, options)\n\n return () => {\n observer.disconnect()\n }\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [onMutate, options])\n}\n"],"file":"useMutationObserver.js"}
1
+ {"version":3,"sources":["../../src/utils/useMutationObserver.ts"],"names":["DEFAULT_OPTIONS","attributes","characterData","childList","subtree","useMutationObserver","onMutate","options","rest","MutationObserver","el","ref","current","element","observer","observe","disconnect"],"mappings":";;;;;;;AACA;;;;;;;;AAUA,IAAMA,eAAe,GAAG;AACtBC,EAAAA,UAAU,EAAE,IADU;AAEtBC,EAAAA,aAAa,EAAE,IAFO;AAGtBC,EAAAA,SAAS,EAAE,IAHW;AAItBC,EAAAA,OAAO,EAAE;AAJa,CAAxB;AAOA;AACA;AACA;;AACO,IAAMC,mBAAmB,GAAG,SAAtBA,mBAAsB,OAIR;AAAA,MAHzBC,QAGyB,QAHzBA,QAGyB;AAAA,0BAFzBC,OAEyB;AAAA,MAFzBA,OAEyB,6BAFfP,eAEe;AAAA,MADtBQ,IACsB;;AACzB,wBAAU,YAAM;AACd,QAAI,OAAOC,gBAAP,KAA4B,WAAhC,EAA6C;AAC3C;AACD;;AAED,QAAMC,EAAE,GAAG,SAASF,IAAT,GAAgBA,IAAI,CAACG,GAAL,CAASC,OAAzB,GAAmCJ,IAAI,CAACK,OAAnD;AAEA,QAAI,CAACH,EAAL,EAAS;AACT,QAAMI,QAAQ,GAAG,IAAIL,gBAAJ,CAAqBH,QAArB,CAAjB,CARc,CAUd;;AACAQ,IAAAA,QAAQ,CAACC,OAAT,CAAiBL,EAAjB,EAAqBH,OAArB;AAEA,WAAO,YAAM;AACXO,MAAAA,QAAQ,CAACE,UAAT;AACD,KAFD,CAbc,CAgBd;AACD,GAjBD,EAiBG,CAACV,QAAD,EAAWC,OAAX,CAjBH;AAkBD,CAvBM","sourcesContent":["import type { MutableRefObject } from \"react\"\nimport { useEffect } from \"react\"\n\ntype UseMutationObserver = {\n onMutate: MutationCallback\n options?: MutationObserverInit\n} & (\n | { ref: MutableRefObject<HTMLElement | null> }\n | { element?: HTMLElement | null }\n)\n\nconst DEFAULT_OPTIONS = {\n attributes: true,\n characterData: true,\n childList: true,\n subtree: true,\n}\n\n/**\n * Accepts a ref and calls the `onMutate` callback when mutations are observed.\n */\nexport const useMutationObserver = ({\n onMutate,\n options = DEFAULT_OPTIONS,\n ...rest\n}: UseMutationObserver) => {\n useEffect(() => {\n if (typeof MutationObserver === \"undefined\") {\n return\n }\n\n const el = \"ref\" in rest ? rest.ref.current : rest.element\n\n if (!el) return\n const observer = new MutationObserver(onMutate)\n\n // Start observing the target node for configured mutations\n observer.observe(el, options)\n\n return () => {\n observer.disconnect()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [onMutate, options])\n}\n"],"file":"useMutationObserver.js"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@artsy/palette",
3
- "version": "29.7.0",
3
+ "version": "29.7.2",
4
4
  "description": "Design system library for react components",
5
5
  "main": "dist/index.js",
6
6
  "publishConfig": {
@@ -175,5 +175,5 @@
175
175
  "<rootDir>/www/"
176
176
  ]
177
177
  },
178
- "gitHead": "daf6864c2d993295d2f1d57104dd7a1cf1954696"
178
+ "gitHead": "12d35a8d6495b473b6ccb0e65990d003d44c0b3b"
179
179
  }