@ariakit/react-core
Version:
Ariakit React core
158 lines (141 loc) • 5.13 kB
JavaScript
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
var _5RHEK3U2cjs = require('./5RHEK3U2.cjs');
var _GCJBB4ZTcjs = require('./GCJBB4ZT.cjs');
var _EMYYI4CZcjs = require('./EMYYI4CZ.cjs');
var _WBFXWJUHcjs = require('./WBFXWJUH.cjs');
var _MZ2HG624cjs = require('./MZ2HG624.cjs');
// src/composite/composite-renderer.tsx
var _react = require('react');
var TagName = "div";
function getItemObject(item) {
if (!item || typeof item !== "object") {
return { value: item };
}
return item;
}
function countItems(items) {
if (!items) return [0];
if (typeof items === "number") {
return Array.from({ length: items }, (_, index) => index + 1);
}
return items.reduce((count, item, index) => {
var _a, _b;
const object = getItemObject(item);
if (!object.items) {
}
if (!object.items) {
count[index] = index + 1;
return count;
}
const prevCount = (_a = count[index - 1]) != null ? _a : 0;
const itemsCount = (_b = countItems(object.items)[object.items.length - 1]) != null ? _b : 0;
count[index] = prevCount + itemsCount;
return count;
}, []);
}
function findFirst(items, offset = 1) {
for (let index = offset > 0 ? 0 : items.length - 1; index >= 0 && index < items.length; index += offset) {
const item = items[index];
const object = getItemObject(item);
if (object.items && findFirst(object.items, offset) !== -1) return index;
if (!object.disabled) return index;
}
return -1;
}
function findLast(items) {
return findFirst(items, -1);
}
function findById(items, id, baseId) {
return items.findIndex((item, index) => {
var _a;
const itemId = _5RHEK3U2cjs.getCollectionRendererItemId.call(void 0, item, index, baseId);
if (itemId === id) return true;
const object = getItemObject(item);
if ((_a = object.items) == null ? void 0 : _a.length) return findById(object.items, id, itemId) !== -1;
const ids = id.split("/");
if (ids.length === 1) return false;
return ids.some((id2) => itemId === id2);
});
}
function useCompositeRenderer({
store,
orientation: orientationProp,
persistentIndices: persistentIndicesProp,
children: renderItem,
"aria-setsize": ariaSetSize,
"aria-posinset": ariaPosInSet = 1,
...props
}) {
const context = _GCJBB4ZTcjs.useCompositeContext.call(void 0, );
store = store || context;
const orientation = _EMYYI4CZcjs.useStoreState.call(void 0,
store,
(state) => (orientationProp != null ? orientationProp : (state == null ? void 0 : state.orientation) === "both") ? "vertical" : state == null ? void 0 : state.orientation
);
const items = _EMYYI4CZcjs.useStoreState.call(void 0, store, (state) => {
var _a;
if (!state) return props.items;
if ("mounted" in state && !state.mounted) return 0;
return (_a = props.items) != null ? _a : state.items;
});
const id = _MZ2HG624cjs.useId.call(void 0, props.id);
const itemsCount = _react.useMemo.call(void 0, () => countItems(items), [items]);
const setSize = _react.useMemo.call(void 0,
() => {
var _a;
return (_a = ariaSetSize != null ? ariaSetSize : itemsCount[itemsCount.length - 1]) != null ? _a : 0;
},
[ariaSetSize, itemsCount]
);
const firstIndex = _react.useMemo.call(void 0, () => {
if (!items) return -1;
if (typeof items === "number") return 0;
if (!items.length) return -1;
return findFirst(items);
}, [items]);
const lastIndex = _react.useMemo.call(void 0, () => {
if (!items) return -1;
if (typeof items === "number") return items - 1;
if (!items.length) return -1;
return findLast(items);
}, [items]);
const activeId = _EMYYI4CZcjs.useStoreState.call(void 0, store, "activeId");
const activeIndex = _react.useMemo.call(void 0, () => {
if (!id) return -1;
if (!items) return -1;
if (activeId == null) return -1;
if (typeof items === "number") return -1;
if (!items.length) return -1;
return findById(items, activeId, id);
}, [id, items, activeId]);
const persistentIndices = _react.useMemo.call(void 0, () => {
const indices = [firstIndex, activeIndex, lastIndex].filter(
(index) => index >= 0
);
if (persistentIndicesProp) {
return [...persistentIndicesProp, ...indices];
}
return indices;
}, [firstIndex, activeIndex, lastIndex, persistentIndicesProp]);
return _5RHEK3U2cjs.useCollectionRenderer.call(void 0, {
id,
store,
orientation,
persistentIndices,
...props,
children: (item) => {
var _a;
const nextItem = {
...item,
"aria-setsize": setSize,
"aria-posinset": ariaPosInSet + ((_a = itemsCount[item.index - 1]) != null ? _a : 0)
};
return renderItem == null ? void 0 : renderItem(nextItem);
}
});
}
var CompositeRenderer = _WBFXWJUHcjs.forwardRef.call(void 0, function CompositeRenderer2(props) {
const htmlProps = useCompositeRenderer(props);
return _WBFXWJUHcjs.createElement.call(void 0, TagName, htmlProps);
});
exports.useCompositeRenderer = useCompositeRenderer; exports.CompositeRenderer = CompositeRenderer;