@chakra-ui/descendant
Advanced tools
Comparing version 2.1.0 to 2.1.1
# Change Log | ||
## 2.1.1 | ||
### Patch Changes | ||
- [#5075](https://github.com/chakra-ui/chakra-ui/pull/5075) | ||
[`b28142946`](https://github.com/chakra-ui/chakra-ui/commit/b281429462a099b7fd7f9352e837cd28d1a2da0e) | ||
Thanks [@cschroeter](https://github.com/cschroeter)! - Update babel config to | ||
transpile soruces for older browsers. This fixes issues with CRA and | ||
Storybook. | ||
- Updated dependencies | ||
[[`b28142946`](https://github.com/chakra-ui/chakra-ui/commit/b281429462a099b7fd7f9352e837cd28d1a2da0e)]: | ||
- @chakra-ui/react-utils@1.2.1 | ||
## 2.1.0 | ||
@@ -4,0 +17,0 @@ |
@@ -8,2 +8,20 @@ 'use strict'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
/** | ||
@@ -15,4 +33,4 @@ * Sort an array of DOM nodes according to the HTML tree order | ||
function sortNodes(nodes) { | ||
return nodes.sort((a, b) => { | ||
const compare = a.compareDocumentPosition(b); | ||
return nodes.sort(function (a, b) { | ||
var compare = a.compareDocumentPosition(b); | ||
@@ -36,5 +54,7 @@ if (compare & Node.DOCUMENT_POSITION_FOLLOWING || compare & Node.DOCUMENT_POSITION_CONTAINED_BY) { | ||
} | ||
const isElement = el => typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
var isElement = function isElement(el) { | ||
return typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
}; | ||
function getNextIndex(current, max, loop) { | ||
let next = current + 1; | ||
var next = current + 1; | ||
if (loop && next >= max) next = 0; | ||
@@ -44,8 +64,10 @@ return next; | ||
function getPrevIndex(current, max, loop) { | ||
let next = current - 1; | ||
var next = current - 1; | ||
if (loop && next < 0) next = max; | ||
return next; | ||
} | ||
const useSafeLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect; | ||
const cast = value => value; | ||
var useSafeLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect; | ||
var cast = function cast(value) { | ||
return value; | ||
}; | ||
@@ -58,127 +80,169 @@ /** | ||
*/ | ||
class DescendantsManager { | ||
constructor() { | ||
this.descendants = new Map(); | ||
var DescendantsManager = function DescendantsManager() { | ||
var _this = this; | ||
this.register = nodeOrOptions => { | ||
if (nodeOrOptions == null) return; | ||
this.descendants = new Map(); | ||
if (isElement(nodeOrOptions)) { | ||
return this.registerNode(nodeOrOptions); | ||
} | ||
this.register = function (nodeOrOptions) { | ||
if (nodeOrOptions == null) return; | ||
return node => { | ||
this.registerNode(node, nodeOrOptions); | ||
}; | ||
}; | ||
if (isElement(nodeOrOptions)) { | ||
return _this.registerNode(nodeOrOptions); | ||
} | ||
this.unregister = node => { | ||
this.descendants.delete(node); | ||
const sorted = sortNodes(Array.from(this.descendants.keys())); | ||
this.assignIndex(sorted); | ||
return function (node) { | ||
_this.registerNode(node, nodeOrOptions); | ||
}; | ||
}; | ||
this.destroy = () => { | ||
this.descendants.clear(); | ||
}; | ||
this.unregister = function (node) { | ||
_this.descendants["delete"](node); | ||
this.assignIndex = descendants => { | ||
this.descendants.forEach(descendant => { | ||
const index = descendants.indexOf(descendant.node); | ||
descendant.index = index; | ||
descendant.node.dataset.index = descendant.index.toString(); | ||
}); | ||
}; | ||
var sorted = sortNodes(Array.from(_this.descendants.keys())); | ||
this.count = () => this.descendants.size; | ||
_this.assignIndex(sorted); | ||
}; | ||
this.enabledCount = () => this.enabledValues().length; | ||
this.destroy = function () { | ||
_this.descendants.clear(); | ||
}; | ||
this.values = () => { | ||
const values = Array.from(this.descendants.values()); | ||
return values.sort((a, b) => a.index - b.index); | ||
}; | ||
this.assignIndex = function (descendants) { | ||
_this.descendants.forEach(function (descendant) { | ||
var index = descendants.indexOf(descendant.node); | ||
descendant.index = index; | ||
descendant.node.dataset.index = descendant.index.toString(); | ||
}); | ||
}; | ||
this.enabledValues = () => { | ||
return this.values().filter(descendant => !descendant.disabled); | ||
}; | ||
this.count = function () { | ||
return _this.descendants.size; | ||
}; | ||
this.item = index => { | ||
if (this.count() === 0) return undefined; | ||
return this.values()[index]; | ||
}; | ||
this.enabledCount = function () { | ||
return _this.enabledValues().length; | ||
}; | ||
this.enabledItem = index => { | ||
if (this.enabledCount() === 0) return undefined; | ||
return this.enabledValues()[index]; | ||
}; | ||
this.values = function () { | ||
var values = Array.from(_this.descendants.values()); | ||
return values.sort(function (a, b) { | ||
return a.index - b.index; | ||
}); | ||
}; | ||
this.first = () => this.item(0); | ||
this.enabledValues = function () { | ||
return _this.values().filter(function (descendant) { | ||
return !descendant.disabled; | ||
}); | ||
}; | ||
this.firstEnabled = () => this.enabledItem(0); | ||
this.item = function (index) { | ||
if (_this.count() === 0) return undefined; | ||
return _this.values()[index]; | ||
}; | ||
this.last = () => this.item(this.descendants.size - 1); | ||
this.enabledItem = function (index) { | ||
if (_this.enabledCount() === 0) return undefined; | ||
return _this.enabledValues()[index]; | ||
}; | ||
this.lastEnabled = () => { | ||
const lastIndex = this.enabledValues().length - 1; | ||
return this.enabledItem(lastIndex); | ||
}; | ||
this.first = function () { | ||
return _this.item(0); | ||
}; | ||
this.indexOf = node => { | ||
if (!node) return -1; | ||
return this.descendants.get(node)?.index ?? -1; | ||
}; | ||
this.firstEnabled = function () { | ||
return _this.enabledItem(0); | ||
}; | ||
this.enabledIndexOf = node => { | ||
if (node == null) return -1; | ||
return this.enabledValues().findIndex(i => i.node.isSameNode(node)); | ||
}; | ||
this.last = function () { | ||
return _this.item(_this.descendants.size - 1); | ||
}; | ||
this.next = (index, loop = true) => { | ||
const next = getNextIndex(index, this.count(), loop); | ||
return this.item(next); | ||
}; | ||
this.lastEnabled = function () { | ||
var lastIndex = _this.enabledValues().length - 1; | ||
return _this.enabledItem(lastIndex); | ||
}; | ||
this.nextEnabled = (index, loop = true) => { | ||
const item = this.item(index); | ||
if (!item) return; | ||
const enabledIndex = this.enabledIndexOf(item.node); | ||
const nextEnabledIndex = getNextIndex(enabledIndex, this.enabledCount(), loop); | ||
return this.enabledItem(nextEnabledIndex); | ||
}; | ||
this.indexOf = function (node) { | ||
var _this$descendants$get, _this$descendants$get2; | ||
this.prev = (index, loop = true) => { | ||
const prev = getPrevIndex(index, this.count() - 1, loop); | ||
return this.item(prev); | ||
}; | ||
if (!node) return -1; | ||
return (_this$descendants$get = (_this$descendants$get2 = _this.descendants.get(node)) == null ? void 0 : _this$descendants$get2.index) != null ? _this$descendants$get : -1; | ||
}; | ||
this.prevEnabled = (index, loop = true) => { | ||
const item = this.item(index); | ||
if (!item) return; | ||
const enabledIndex = this.enabledIndexOf(item.node); | ||
const prevEnabledIndex = getPrevIndex(enabledIndex, this.enabledCount() - 1, loop); | ||
return this.enabledItem(prevEnabledIndex); | ||
}; | ||
this.enabledIndexOf = function (node) { | ||
if (node == null) return -1; | ||
return _this.enabledValues().findIndex(function (i) { | ||
return i.node.isSameNode(node); | ||
}); | ||
}; | ||
this.registerNode = (node, options) => { | ||
if (!node || this.descendants.has(node)) return; | ||
const keys = Array.from(this.descendants.keys()).concat(node); | ||
const sorted = sortNodes(keys); | ||
this.next = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
if (options?.disabled) { | ||
options.disabled = !!options.disabled; | ||
} | ||
var next = getNextIndex(index, _this.count(), loop); | ||
return _this.item(next); | ||
}; | ||
const descendant = { | ||
node, | ||
index: -1, | ||
...options | ||
}; | ||
this.descendants.set(node, descendant); | ||
this.assignIndex(sorted); | ||
}; | ||
} | ||
this.nextEnabled = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
} | ||
var item = _this.item(index); | ||
if (!item) return; | ||
var enabledIndex = _this.enabledIndexOf(item.node); | ||
var nextEnabledIndex = getNextIndex(enabledIndex, _this.enabledCount(), loop); | ||
return _this.enabledItem(nextEnabledIndex); | ||
}; | ||
this.prev = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
var prev = getPrevIndex(index, _this.count() - 1, loop); | ||
return _this.item(prev); | ||
}; | ||
this.prevEnabled = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
var item = _this.item(index); | ||
if (!item) return; | ||
var enabledIndex = _this.enabledIndexOf(item.node); | ||
var prevEnabledIndex = getPrevIndex(enabledIndex, _this.enabledCount() - 1, loop); | ||
return _this.enabledItem(prevEnabledIndex); | ||
}; | ||
this.registerNode = function (node, options) { | ||
if (!node || _this.descendants.has(node)) return; | ||
var keys = Array.from(_this.descendants.keys()).concat(node); | ||
var sorted = sortNodes(keys); | ||
if (options != null && options.disabled) { | ||
options.disabled = !!options.disabled; | ||
} | ||
var descendant = _extends({ | ||
node: node, | ||
index: -1 | ||
}, options); | ||
_this.descendants.set(node, descendant); | ||
_this.assignIndex(sorted); | ||
}; | ||
}; | ||
/** | ||
@@ -190,5 +254,7 @@ * @internal | ||
function useDescendants() { | ||
const descendants = react.useRef(new DescendantsManager()); | ||
useSafeLayoutEffect(() => { | ||
return () => descendants.current.destroy(); | ||
var descendants = react.useRef(new DescendantsManager()); | ||
useSafeLayoutEffect(function () { | ||
return function () { | ||
return descendants.current.destroy(); | ||
}; | ||
}); | ||
@@ -205,6 +271,8 @@ return descendants.current; | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const [DescendantsContextProvider, useDescendantsContext] = reactUtils.createContext({ | ||
var _createContext = reactUtils.createContext({ | ||
name: "DescendantsProvider", | ||
errorMessage: "useDescendantsContext must be used within DescendantsProvider" | ||
}); | ||
}), | ||
DescendantsContextProvider = _createContext[0], | ||
useDescendantsContext = _createContext[1]; | ||
/** | ||
@@ -218,8 +286,13 @@ * @internal | ||
function useDescendant(options) { | ||
const descendants = useDescendantsContext(); | ||
const [index, setIndex] = react.useState(-1); | ||
const ref = react.useRef(null); | ||
useSafeLayoutEffect(() => { | ||
return () => { | ||
var descendants = useDescendantsContext(); | ||
var _useState = react.useState(-1), | ||
index = _useState[0], | ||
setIndex = _useState[1]; | ||
var ref = react.useRef(null); | ||
useSafeLayoutEffect(function () { | ||
return function () { | ||
if (!ref.current) return; | ||
@@ -229,5 +302,5 @@ descendants.unregister(ref.current); | ||
}, []); | ||
useSafeLayoutEffect(() => { | ||
useSafeLayoutEffect(function () { | ||
if (!ref.current) return; | ||
const dataIndex = Number(ref.current.dataset.index); | ||
var dataIndex = Number(ref.current.dataset.index); | ||
@@ -238,6 +311,6 @@ if (index != dataIndex && !Number.isNaN(dataIndex)) { | ||
}); | ||
const refCallback = options ? cast(descendants.register(options)) : cast(descendants.register); | ||
var refCallback = options ? cast(descendants.register(options)) : cast(descendants.register); | ||
return { | ||
descendants, | ||
index, | ||
descendants: descendants, | ||
index: index, | ||
enabledIndex: descendants.enabledIndexOf(ref.current), | ||
@@ -254,9 +327,15 @@ register: reactUtils.mergeRefs(refCallback, ref) | ||
function createDescendantContext() { | ||
const ContextProvider = cast(DescendantsContextProvider); | ||
var ContextProvider = cast(DescendantsContextProvider); | ||
const _useDescendantsContext = () => cast(useDescendantsContext()); | ||
var _useDescendantsContext = function _useDescendantsContext() { | ||
return cast(useDescendantsContext()); | ||
}; | ||
const _useDescendant = options => useDescendant(options); | ||
var _useDescendant = function _useDescendant(options) { | ||
return useDescendant(options); | ||
}; | ||
const _useDescendants = () => useDescendants(); | ||
var _useDescendants = function _useDescendants() { | ||
return useDescendants(); | ||
}; | ||
@@ -263,0 +342,0 @@ return [// context provider |
@@ -8,2 +8,20 @@ 'use strict'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
/** | ||
@@ -15,4 +33,4 @@ * Sort an array of DOM nodes according to the HTML tree order | ||
function sortNodes(nodes) { | ||
return nodes.sort((a, b) => { | ||
const compare = a.compareDocumentPosition(b); | ||
return nodes.sort(function (a, b) { | ||
var compare = a.compareDocumentPosition(b); | ||
@@ -36,5 +54,7 @@ if (compare & Node.DOCUMENT_POSITION_FOLLOWING || compare & Node.DOCUMENT_POSITION_CONTAINED_BY) { | ||
} | ||
const isElement = el => typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
var isElement = function isElement(el) { | ||
return typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
}; | ||
function getNextIndex(current, max, loop) { | ||
let next = current + 1; | ||
var next = current + 1; | ||
if (loop && next >= max) next = 0; | ||
@@ -44,8 +64,10 @@ return next; | ||
function getPrevIndex(current, max, loop) { | ||
let next = current - 1; | ||
var next = current - 1; | ||
if (loop && next < 0) next = max; | ||
return next; | ||
} | ||
const useSafeLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect; | ||
const cast = value => value; | ||
var useSafeLayoutEffect = typeof window !== "undefined" ? react.useLayoutEffect : react.useEffect; | ||
var cast = function cast(value) { | ||
return value; | ||
}; | ||
@@ -58,127 +80,169 @@ /** | ||
*/ | ||
class DescendantsManager { | ||
constructor() { | ||
this.descendants = new Map(); | ||
var DescendantsManager = function DescendantsManager() { | ||
var _this = this; | ||
this.register = nodeOrOptions => { | ||
if (nodeOrOptions == null) return; | ||
this.descendants = new Map(); | ||
if (isElement(nodeOrOptions)) { | ||
return this.registerNode(nodeOrOptions); | ||
} | ||
this.register = function (nodeOrOptions) { | ||
if (nodeOrOptions == null) return; | ||
return node => { | ||
this.registerNode(node, nodeOrOptions); | ||
}; | ||
}; | ||
if (isElement(nodeOrOptions)) { | ||
return _this.registerNode(nodeOrOptions); | ||
} | ||
this.unregister = node => { | ||
this.descendants.delete(node); | ||
const sorted = sortNodes(Array.from(this.descendants.keys())); | ||
this.assignIndex(sorted); | ||
return function (node) { | ||
_this.registerNode(node, nodeOrOptions); | ||
}; | ||
}; | ||
this.destroy = () => { | ||
this.descendants.clear(); | ||
}; | ||
this.unregister = function (node) { | ||
_this.descendants["delete"](node); | ||
this.assignIndex = descendants => { | ||
this.descendants.forEach(descendant => { | ||
const index = descendants.indexOf(descendant.node); | ||
descendant.index = index; | ||
descendant.node.dataset.index = descendant.index.toString(); | ||
}); | ||
}; | ||
var sorted = sortNodes(Array.from(_this.descendants.keys())); | ||
this.count = () => this.descendants.size; | ||
_this.assignIndex(sorted); | ||
}; | ||
this.enabledCount = () => this.enabledValues().length; | ||
this.destroy = function () { | ||
_this.descendants.clear(); | ||
}; | ||
this.values = () => { | ||
const values = Array.from(this.descendants.values()); | ||
return values.sort((a, b) => a.index - b.index); | ||
}; | ||
this.assignIndex = function (descendants) { | ||
_this.descendants.forEach(function (descendant) { | ||
var index = descendants.indexOf(descendant.node); | ||
descendant.index = index; | ||
descendant.node.dataset.index = descendant.index.toString(); | ||
}); | ||
}; | ||
this.enabledValues = () => { | ||
return this.values().filter(descendant => !descendant.disabled); | ||
}; | ||
this.count = function () { | ||
return _this.descendants.size; | ||
}; | ||
this.item = index => { | ||
if (this.count() === 0) return undefined; | ||
return this.values()[index]; | ||
}; | ||
this.enabledCount = function () { | ||
return _this.enabledValues().length; | ||
}; | ||
this.enabledItem = index => { | ||
if (this.enabledCount() === 0) return undefined; | ||
return this.enabledValues()[index]; | ||
}; | ||
this.values = function () { | ||
var values = Array.from(_this.descendants.values()); | ||
return values.sort(function (a, b) { | ||
return a.index - b.index; | ||
}); | ||
}; | ||
this.first = () => this.item(0); | ||
this.enabledValues = function () { | ||
return _this.values().filter(function (descendant) { | ||
return !descendant.disabled; | ||
}); | ||
}; | ||
this.firstEnabled = () => this.enabledItem(0); | ||
this.item = function (index) { | ||
if (_this.count() === 0) return undefined; | ||
return _this.values()[index]; | ||
}; | ||
this.last = () => this.item(this.descendants.size - 1); | ||
this.enabledItem = function (index) { | ||
if (_this.enabledCount() === 0) return undefined; | ||
return _this.enabledValues()[index]; | ||
}; | ||
this.lastEnabled = () => { | ||
const lastIndex = this.enabledValues().length - 1; | ||
return this.enabledItem(lastIndex); | ||
}; | ||
this.first = function () { | ||
return _this.item(0); | ||
}; | ||
this.indexOf = node => { | ||
if (!node) return -1; | ||
return this.descendants.get(node)?.index ?? -1; | ||
}; | ||
this.firstEnabled = function () { | ||
return _this.enabledItem(0); | ||
}; | ||
this.enabledIndexOf = node => { | ||
if (node == null) return -1; | ||
return this.enabledValues().findIndex(i => i.node.isSameNode(node)); | ||
}; | ||
this.last = function () { | ||
return _this.item(_this.descendants.size - 1); | ||
}; | ||
this.next = (index, loop = true) => { | ||
const next = getNextIndex(index, this.count(), loop); | ||
return this.item(next); | ||
}; | ||
this.lastEnabled = function () { | ||
var lastIndex = _this.enabledValues().length - 1; | ||
return _this.enabledItem(lastIndex); | ||
}; | ||
this.nextEnabled = (index, loop = true) => { | ||
const item = this.item(index); | ||
if (!item) return; | ||
const enabledIndex = this.enabledIndexOf(item.node); | ||
const nextEnabledIndex = getNextIndex(enabledIndex, this.enabledCount(), loop); | ||
return this.enabledItem(nextEnabledIndex); | ||
}; | ||
this.indexOf = function (node) { | ||
var _this$descendants$get, _this$descendants$get2; | ||
this.prev = (index, loop = true) => { | ||
const prev = getPrevIndex(index, this.count() - 1, loop); | ||
return this.item(prev); | ||
}; | ||
if (!node) return -1; | ||
return (_this$descendants$get = (_this$descendants$get2 = _this.descendants.get(node)) == null ? void 0 : _this$descendants$get2.index) != null ? _this$descendants$get : -1; | ||
}; | ||
this.prevEnabled = (index, loop = true) => { | ||
const item = this.item(index); | ||
if (!item) return; | ||
const enabledIndex = this.enabledIndexOf(item.node); | ||
const prevEnabledIndex = getPrevIndex(enabledIndex, this.enabledCount() - 1, loop); | ||
return this.enabledItem(prevEnabledIndex); | ||
}; | ||
this.enabledIndexOf = function (node) { | ||
if (node == null) return -1; | ||
return _this.enabledValues().findIndex(function (i) { | ||
return i.node.isSameNode(node); | ||
}); | ||
}; | ||
this.registerNode = (node, options) => { | ||
if (!node || this.descendants.has(node)) return; | ||
const keys = Array.from(this.descendants.keys()).concat(node); | ||
const sorted = sortNodes(keys); | ||
this.next = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
if (options?.disabled) { | ||
options.disabled = !!options.disabled; | ||
} | ||
var next = getNextIndex(index, _this.count(), loop); | ||
return _this.item(next); | ||
}; | ||
const descendant = { | ||
node, | ||
index: -1, | ||
...options | ||
}; | ||
this.descendants.set(node, descendant); | ||
this.assignIndex(sorted); | ||
}; | ||
} | ||
this.nextEnabled = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
} | ||
var item = _this.item(index); | ||
if (!item) return; | ||
var enabledIndex = _this.enabledIndexOf(item.node); | ||
var nextEnabledIndex = getNextIndex(enabledIndex, _this.enabledCount(), loop); | ||
return _this.enabledItem(nextEnabledIndex); | ||
}; | ||
this.prev = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
var prev = getPrevIndex(index, _this.count() - 1, loop); | ||
return _this.item(prev); | ||
}; | ||
this.prevEnabled = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
var item = _this.item(index); | ||
if (!item) return; | ||
var enabledIndex = _this.enabledIndexOf(item.node); | ||
var prevEnabledIndex = getPrevIndex(enabledIndex, _this.enabledCount() - 1, loop); | ||
return _this.enabledItem(prevEnabledIndex); | ||
}; | ||
this.registerNode = function (node, options) { | ||
if (!node || _this.descendants.has(node)) return; | ||
var keys = Array.from(_this.descendants.keys()).concat(node); | ||
var sorted = sortNodes(keys); | ||
if (options != null && options.disabled) { | ||
options.disabled = !!options.disabled; | ||
} | ||
var descendant = _extends({ | ||
node: node, | ||
index: -1 | ||
}, options); | ||
_this.descendants.set(node, descendant); | ||
_this.assignIndex(sorted); | ||
}; | ||
}; | ||
/** | ||
@@ -190,5 +254,7 @@ * @internal | ||
function useDescendants() { | ||
const descendants = react.useRef(new DescendantsManager()); | ||
useSafeLayoutEffect(() => { | ||
return () => descendants.current.destroy(); | ||
var descendants = react.useRef(new DescendantsManager()); | ||
useSafeLayoutEffect(function () { | ||
return function () { | ||
return descendants.current.destroy(); | ||
}; | ||
}); | ||
@@ -205,6 +271,8 @@ return descendants.current; | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const [DescendantsContextProvider, useDescendantsContext] = reactUtils.createContext({ | ||
var _createContext = reactUtils.createContext({ | ||
name: "DescendantsProvider", | ||
errorMessage: "useDescendantsContext must be used within DescendantsProvider" | ||
}); | ||
}), | ||
DescendantsContextProvider = _createContext[0], | ||
useDescendantsContext = _createContext[1]; | ||
/** | ||
@@ -218,8 +286,13 @@ * @internal | ||
function useDescendant(options) { | ||
const descendants = useDescendantsContext(); | ||
const [index, setIndex] = react.useState(-1); | ||
const ref = react.useRef(null); | ||
useSafeLayoutEffect(() => { | ||
return () => { | ||
var descendants = useDescendantsContext(); | ||
var _useState = react.useState(-1), | ||
index = _useState[0], | ||
setIndex = _useState[1]; | ||
var ref = react.useRef(null); | ||
useSafeLayoutEffect(function () { | ||
return function () { | ||
if (!ref.current) return; | ||
@@ -229,5 +302,5 @@ descendants.unregister(ref.current); | ||
}, []); | ||
useSafeLayoutEffect(() => { | ||
useSafeLayoutEffect(function () { | ||
if (!ref.current) return; | ||
const dataIndex = Number(ref.current.dataset.index); | ||
var dataIndex = Number(ref.current.dataset.index); | ||
@@ -238,6 +311,6 @@ if (index != dataIndex && !Number.isNaN(dataIndex)) { | ||
}); | ||
const refCallback = options ? cast(descendants.register(options)) : cast(descendants.register); | ||
var refCallback = options ? cast(descendants.register(options)) : cast(descendants.register); | ||
return { | ||
descendants, | ||
index, | ||
descendants: descendants, | ||
index: index, | ||
enabledIndex: descendants.enabledIndexOf(ref.current), | ||
@@ -254,9 +327,15 @@ register: reactUtils.mergeRefs(refCallback, ref) | ||
function createDescendantContext() { | ||
const ContextProvider = cast(DescendantsContextProvider); | ||
var ContextProvider = cast(DescendantsContextProvider); | ||
const _useDescendantsContext = () => cast(useDescendantsContext()); | ||
var _useDescendantsContext = function _useDescendantsContext() { | ||
return cast(useDescendantsContext()); | ||
}; | ||
const _useDescendant = options => useDescendant(options); | ||
var _useDescendant = function _useDescendant(options) { | ||
return useDescendant(options); | ||
}; | ||
const _useDescendants = () => useDescendants(); | ||
var _useDescendants = function _useDescendants() { | ||
return useDescendants(); | ||
}; | ||
@@ -263,0 +342,0 @@ return [// context provider |
import { createContext, mergeRefs } from '@chakra-ui/react-utils'; | ||
import { useLayoutEffect, useEffect, useRef, useState } from 'react'; | ||
function _extends() { | ||
_extends = Object.assign || function (target) { | ||
for (var i = 1; i < arguments.length; i++) { | ||
var source = arguments[i]; | ||
for (var key in source) { | ||
if (Object.prototype.hasOwnProperty.call(source, key)) { | ||
target[key] = source[key]; | ||
} | ||
} | ||
} | ||
return target; | ||
}; | ||
return _extends.apply(this, arguments); | ||
} | ||
/** | ||
@@ -10,4 +28,4 @@ * Sort an array of DOM nodes according to the HTML tree order | ||
function sortNodes(nodes) { | ||
return nodes.sort((a, b) => { | ||
const compare = a.compareDocumentPosition(b); | ||
return nodes.sort(function (a, b) { | ||
var compare = a.compareDocumentPosition(b); | ||
@@ -31,5 +49,7 @@ if (compare & Node.DOCUMENT_POSITION_FOLLOWING || compare & Node.DOCUMENT_POSITION_CONTAINED_BY) { | ||
} | ||
const isElement = el => typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
var isElement = function isElement(el) { | ||
return typeof el == "object" && "nodeType" in el && el.nodeType === Node.ELEMENT_NODE; | ||
}; | ||
function getNextIndex(current, max, loop) { | ||
let next = current + 1; | ||
var next = current + 1; | ||
if (loop && next >= max) next = 0; | ||
@@ -39,8 +59,10 @@ return next; | ||
function getPrevIndex(current, max, loop) { | ||
let next = current - 1; | ||
var next = current - 1; | ||
if (loop && next < 0) next = max; | ||
return next; | ||
} | ||
const useSafeLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect; | ||
const cast = value => value; | ||
var useSafeLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect; | ||
var cast = function cast(value) { | ||
return value; | ||
}; | ||
@@ -53,127 +75,169 @@ /** | ||
*/ | ||
class DescendantsManager { | ||
constructor() { | ||
this.descendants = new Map(); | ||
var DescendantsManager = function DescendantsManager() { | ||
var _this = this; | ||
this.register = nodeOrOptions => { | ||
if (nodeOrOptions == null) return; | ||
this.descendants = new Map(); | ||
if (isElement(nodeOrOptions)) { | ||
return this.registerNode(nodeOrOptions); | ||
} | ||
this.register = function (nodeOrOptions) { | ||
if (nodeOrOptions == null) return; | ||
return node => { | ||
this.registerNode(node, nodeOrOptions); | ||
}; | ||
}; | ||
if (isElement(nodeOrOptions)) { | ||
return _this.registerNode(nodeOrOptions); | ||
} | ||
this.unregister = node => { | ||
this.descendants.delete(node); | ||
const sorted = sortNodes(Array.from(this.descendants.keys())); | ||
this.assignIndex(sorted); | ||
return function (node) { | ||
_this.registerNode(node, nodeOrOptions); | ||
}; | ||
}; | ||
this.destroy = () => { | ||
this.descendants.clear(); | ||
}; | ||
this.unregister = function (node) { | ||
_this.descendants["delete"](node); | ||
this.assignIndex = descendants => { | ||
this.descendants.forEach(descendant => { | ||
const index = descendants.indexOf(descendant.node); | ||
descendant.index = index; | ||
descendant.node.dataset.index = descendant.index.toString(); | ||
}); | ||
}; | ||
var sorted = sortNodes(Array.from(_this.descendants.keys())); | ||
this.count = () => this.descendants.size; | ||
_this.assignIndex(sorted); | ||
}; | ||
this.enabledCount = () => this.enabledValues().length; | ||
this.destroy = function () { | ||
_this.descendants.clear(); | ||
}; | ||
this.values = () => { | ||
const values = Array.from(this.descendants.values()); | ||
return values.sort((a, b) => a.index - b.index); | ||
}; | ||
this.assignIndex = function (descendants) { | ||
_this.descendants.forEach(function (descendant) { | ||
var index = descendants.indexOf(descendant.node); | ||
descendant.index = index; | ||
descendant.node.dataset.index = descendant.index.toString(); | ||
}); | ||
}; | ||
this.enabledValues = () => { | ||
return this.values().filter(descendant => !descendant.disabled); | ||
}; | ||
this.count = function () { | ||
return _this.descendants.size; | ||
}; | ||
this.item = index => { | ||
if (this.count() === 0) return undefined; | ||
return this.values()[index]; | ||
}; | ||
this.enabledCount = function () { | ||
return _this.enabledValues().length; | ||
}; | ||
this.enabledItem = index => { | ||
if (this.enabledCount() === 0) return undefined; | ||
return this.enabledValues()[index]; | ||
}; | ||
this.values = function () { | ||
var values = Array.from(_this.descendants.values()); | ||
return values.sort(function (a, b) { | ||
return a.index - b.index; | ||
}); | ||
}; | ||
this.first = () => this.item(0); | ||
this.enabledValues = function () { | ||
return _this.values().filter(function (descendant) { | ||
return !descendant.disabled; | ||
}); | ||
}; | ||
this.firstEnabled = () => this.enabledItem(0); | ||
this.item = function (index) { | ||
if (_this.count() === 0) return undefined; | ||
return _this.values()[index]; | ||
}; | ||
this.last = () => this.item(this.descendants.size - 1); | ||
this.enabledItem = function (index) { | ||
if (_this.enabledCount() === 0) return undefined; | ||
return _this.enabledValues()[index]; | ||
}; | ||
this.lastEnabled = () => { | ||
const lastIndex = this.enabledValues().length - 1; | ||
return this.enabledItem(lastIndex); | ||
}; | ||
this.first = function () { | ||
return _this.item(0); | ||
}; | ||
this.indexOf = node => { | ||
if (!node) return -1; | ||
return this.descendants.get(node)?.index ?? -1; | ||
}; | ||
this.firstEnabled = function () { | ||
return _this.enabledItem(0); | ||
}; | ||
this.enabledIndexOf = node => { | ||
if (node == null) return -1; | ||
return this.enabledValues().findIndex(i => i.node.isSameNode(node)); | ||
}; | ||
this.last = function () { | ||
return _this.item(_this.descendants.size - 1); | ||
}; | ||
this.next = (index, loop = true) => { | ||
const next = getNextIndex(index, this.count(), loop); | ||
return this.item(next); | ||
}; | ||
this.lastEnabled = function () { | ||
var lastIndex = _this.enabledValues().length - 1; | ||
return _this.enabledItem(lastIndex); | ||
}; | ||
this.nextEnabled = (index, loop = true) => { | ||
const item = this.item(index); | ||
if (!item) return; | ||
const enabledIndex = this.enabledIndexOf(item.node); | ||
const nextEnabledIndex = getNextIndex(enabledIndex, this.enabledCount(), loop); | ||
return this.enabledItem(nextEnabledIndex); | ||
}; | ||
this.indexOf = function (node) { | ||
var _this$descendants$get, _this$descendants$get2; | ||
this.prev = (index, loop = true) => { | ||
const prev = getPrevIndex(index, this.count() - 1, loop); | ||
return this.item(prev); | ||
}; | ||
if (!node) return -1; | ||
return (_this$descendants$get = (_this$descendants$get2 = _this.descendants.get(node)) == null ? void 0 : _this$descendants$get2.index) != null ? _this$descendants$get : -1; | ||
}; | ||
this.prevEnabled = (index, loop = true) => { | ||
const item = this.item(index); | ||
if (!item) return; | ||
const enabledIndex = this.enabledIndexOf(item.node); | ||
const prevEnabledIndex = getPrevIndex(enabledIndex, this.enabledCount() - 1, loop); | ||
return this.enabledItem(prevEnabledIndex); | ||
}; | ||
this.enabledIndexOf = function (node) { | ||
if (node == null) return -1; | ||
return _this.enabledValues().findIndex(function (i) { | ||
return i.node.isSameNode(node); | ||
}); | ||
}; | ||
this.registerNode = (node, options) => { | ||
if (!node || this.descendants.has(node)) return; | ||
const keys = Array.from(this.descendants.keys()).concat(node); | ||
const sorted = sortNodes(keys); | ||
this.next = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
if (options?.disabled) { | ||
options.disabled = !!options.disabled; | ||
} | ||
var next = getNextIndex(index, _this.count(), loop); | ||
return _this.item(next); | ||
}; | ||
const descendant = { | ||
node, | ||
index: -1, | ||
...options | ||
}; | ||
this.descendants.set(node, descendant); | ||
this.assignIndex(sorted); | ||
}; | ||
} | ||
this.nextEnabled = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
} | ||
var item = _this.item(index); | ||
if (!item) return; | ||
var enabledIndex = _this.enabledIndexOf(item.node); | ||
var nextEnabledIndex = getNextIndex(enabledIndex, _this.enabledCount(), loop); | ||
return _this.enabledItem(nextEnabledIndex); | ||
}; | ||
this.prev = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
var prev = getPrevIndex(index, _this.count() - 1, loop); | ||
return _this.item(prev); | ||
}; | ||
this.prevEnabled = function (index, loop) { | ||
if (loop === void 0) { | ||
loop = true; | ||
} | ||
var item = _this.item(index); | ||
if (!item) return; | ||
var enabledIndex = _this.enabledIndexOf(item.node); | ||
var prevEnabledIndex = getPrevIndex(enabledIndex, _this.enabledCount() - 1, loop); | ||
return _this.enabledItem(prevEnabledIndex); | ||
}; | ||
this.registerNode = function (node, options) { | ||
if (!node || _this.descendants.has(node)) return; | ||
var keys = Array.from(_this.descendants.keys()).concat(node); | ||
var sorted = sortNodes(keys); | ||
if (options != null && options.disabled) { | ||
options.disabled = !!options.disabled; | ||
} | ||
var descendant = _extends({ | ||
node: node, | ||
index: -1 | ||
}, options); | ||
_this.descendants.set(node, descendant); | ||
_this.assignIndex(sorted); | ||
}; | ||
}; | ||
/** | ||
@@ -185,5 +249,7 @@ * @internal | ||
function useDescendants() { | ||
const descendants = useRef(new DescendantsManager()); | ||
useSafeLayoutEffect(() => { | ||
return () => descendants.current.destroy(); | ||
var descendants = useRef(new DescendantsManager()); | ||
useSafeLayoutEffect(function () { | ||
return function () { | ||
return descendants.current.destroy(); | ||
}; | ||
}); | ||
@@ -200,6 +266,8 @@ return descendants.current; | ||
* -----------------------------------------------------------------------------------------------*/ | ||
const [DescendantsContextProvider, useDescendantsContext] = createContext({ | ||
var _createContext = createContext({ | ||
name: "DescendantsProvider", | ||
errorMessage: "useDescendantsContext must be used within DescendantsProvider" | ||
}); | ||
}), | ||
DescendantsContextProvider = _createContext[0], | ||
useDescendantsContext = _createContext[1]; | ||
/** | ||
@@ -213,8 +281,13 @@ * @internal | ||
function useDescendant(options) { | ||
const descendants = useDescendantsContext(); | ||
const [index, setIndex] = useState(-1); | ||
const ref = useRef(null); | ||
useSafeLayoutEffect(() => { | ||
return () => { | ||
var descendants = useDescendantsContext(); | ||
var _useState = useState(-1), | ||
index = _useState[0], | ||
setIndex = _useState[1]; | ||
var ref = useRef(null); | ||
useSafeLayoutEffect(function () { | ||
return function () { | ||
if (!ref.current) return; | ||
@@ -224,5 +297,5 @@ descendants.unregister(ref.current); | ||
}, []); | ||
useSafeLayoutEffect(() => { | ||
useSafeLayoutEffect(function () { | ||
if (!ref.current) return; | ||
const dataIndex = Number(ref.current.dataset.index); | ||
var dataIndex = Number(ref.current.dataset.index); | ||
@@ -233,6 +306,6 @@ if (index != dataIndex && !Number.isNaN(dataIndex)) { | ||
}); | ||
const refCallback = options ? cast(descendants.register(options)) : cast(descendants.register); | ||
var refCallback = options ? cast(descendants.register(options)) : cast(descendants.register); | ||
return { | ||
descendants, | ||
index, | ||
descendants: descendants, | ||
index: index, | ||
enabledIndex: descendants.enabledIndexOf(ref.current), | ||
@@ -249,9 +322,15 @@ register: mergeRefs(refCallback, ref) | ||
function createDescendantContext() { | ||
const ContextProvider = cast(DescendantsContextProvider); | ||
var ContextProvider = cast(DescendantsContextProvider); | ||
const _useDescendantsContext = () => cast(useDescendantsContext()); | ||
var _useDescendantsContext = function _useDescendantsContext() { | ||
return cast(useDescendantsContext()); | ||
}; | ||
const _useDescendant = options => useDescendant(options); | ||
var _useDescendant = function _useDescendant(options) { | ||
return useDescendant(options); | ||
}; | ||
const _useDescendants = () => useDescendants(); | ||
var _useDescendants = function _useDescendants() { | ||
return useDescendants(); | ||
}; | ||
@@ -258,0 +337,0 @@ return [// context provider |
@@ -52,1 +52,2 @@ export declare type DescendantOptions<T = {}> = T & { | ||
} | ||
//# sourceMappingURL=descendant.d.ts.map |
export type { DescendantsManager, Descendant, DescendantOptions, } from "./descendant"; | ||
export { createDescendantContext } from "./use-descendant"; | ||
export { createDescendantContext as default } from "./use-descendant"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -17,1 +17,2 @@ /// <reference types="react" /> | ||
export {}; | ||
//# sourceMappingURL=use-descendant.d.ts.map |
@@ -12,1 +12,2 @@ import { useEffect } from "react"; | ||
export declare const cast: <T>(value: any) => T; | ||
//# sourceMappingURL=utils.d.ts.map |
{ | ||
"name": "@chakra-ui/descendant", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "Register child nodes of a react element for better accessibility", | ||
@@ -23,3 +23,4 @@ "keywords": [ | ||
"files": [ | ||
"dist" | ||
"dist", | ||
"src" | ||
], | ||
@@ -38,3 +39,3 @@ "publishConfig": { | ||
"dependencies": { | ||
"@chakra-ui/react-utils": "^1.2.0" | ||
"@chakra-ui/react-utils": "^1.2.1" | ||
}, | ||
@@ -41,0 +42,0 @@ "peerDependencies": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
58576
20
1173