@ultraviolet/plus
Version:
Ultraviolet Plus
130 lines (129 loc) • 4.19 kB
JavaScript
"use client";
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const jsxRuntime = require("@emotion/react/jsx-runtime");
const react = require("react");
const constants = require("./constants.cjs");
const en = require("./locales/en.cjs");
const NavigationContext = react.createContext({
expanded: true,
/**
* This function will trigger the expand/collapse of the navigation and
* will also trigger the animation
*/
toggleExpand: () => {
},
animation: false,
locales: en,
pinItem: () => [],
unpinItem: () => [],
pinnedItems: [],
pinLimit: 7,
navigationRef: {
current: null
},
width: constants.NAVIGATION_WIDTH,
setWidth: () => {
},
reorderItems: () => [],
items: {},
registerItem: () => {
},
setPinnedItems: () => {
},
allowNavigationResize: true,
setAllowNavigationResize: () => {
},
shouldAnimate: true,
animationType: "simple"
});
const useNavigation = () => react.useContext(NavigationContext);
const NavigationProvider = ({
children,
pinnedFeature = false,
initialPinned,
initialExpanded = true,
locales = en,
pinLimit = 7,
onExpandChange,
initialWidth = constants.NAVIGATION_WIDTH,
initialAllowNavigationResize = true,
animation: shouldAnimate = true,
animationType
}) => {
const [expanded, setExpanded] = react.useState(initialExpanded);
const [pinnedItems, setPinnedItems] = react.useState(initialPinned ?? []);
const [animation, setAnimation] = react.useState(false);
const [width, setWidth] = react.useState(initialWidth);
const [allowNavigationResize, setAllowNavigationResize] = react.useState(initialAllowNavigationResize);
const [items, registerItem] = react.useReducer((oldState, newState) => ({
...oldState,
...newState
}), {});
const navigationRef = react.useRef(null);
const toggleExpand = react.useCallback((toggle) => {
if (typeof toggle !== "boolean" && toggle !== void 0) {
throw new Error("toggleExpand only accepts boolean or undefined as parameter. You most likely did <button onClick={toggleExpand}> instead of <button onClick={() => toggleExpand()}>");
}
if (toggle !== void 0 && toggle === expanded) {
return;
}
onExpandChange?.(!expanded);
if (navigationRef.current) {
navigationRef.current.style.width = "";
}
if (shouldAnimate) {
setAnimation(expanded ? "collapse" : "expand");
setTimeout(() => {
setExpanded(toggle !== void 0 ? toggle : !expanded);
setAnimation(false);
}, constants.ANIMATION_DURATION);
} else {
setExpanded(toggle !== void 0 ? toggle : !expanded);
}
}, [expanded, onExpandChange, shouldAnimate]);
const pinItem = react.useCallback((item) => {
const newValue = [...pinnedItems, item];
setPinnedItems(newValue);
return newValue;
}, [pinnedItems]);
const unpinItem = react.useCallback((item) => {
const newValue = pinnedItems.filter((localItem) => localItem !== item);
setPinnedItems(newValue);
return newValue;
}, [pinnedItems]);
const reorderItems = react.useCallback((initialIndex, endIndex) => {
const newPinnedItems = [...pinnedItems];
const [removed] = newPinnedItems.splice(initialIndex, 1);
newPinnedItems.splice(endIndex, 0, removed);
setPinnedItems(newPinnedItems);
return newPinnedItems;
}, [pinnedItems]);
const value = react.useMemo(() => ({
expanded,
toggleExpand,
pinnedItems,
pinItem,
unpinItem,
pinnedFeature,
locales,
pinLimit,
animation,
setAnimation,
navigationRef,
width,
setWidth,
reorderItems,
registerItem,
items,
setPinnedItems,
allowNavigationResize,
setAllowNavigationResize,
shouldAnimate,
animationType
}), [expanded, toggleExpand, pinnedItems, pinItem, unpinItem, pinnedFeature, locales, pinLimit, animation, width, reorderItems, items, allowNavigationResize, shouldAnimate, animationType]);
return /* @__PURE__ */ jsxRuntime.jsx(NavigationContext.Provider, { value, children });
};
exports.NavigationContext = NavigationContext;
exports.NavigationProvider = NavigationProvider;
exports.useNavigation = useNavigation;