UNPKG

@headless-tree/react

Version:

The definitive tree component for the Web

153 lines (149 loc) 6.36 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __defProps = Object.defineProperties; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropDescs = Object.getOwnPropertyDescriptors; var __getOwnPropNames = Object.getOwnPropertyNames; var __getOwnPropSymbols = Object.getOwnPropertySymbols; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __propIsEnum = Object.prototype.propertyIsEnumerable; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __spreadValues = (a, b) => { for (var prop in b || (b = {})) if (__hasOwnProp.call(b, prop)) __defNormalProp(a, prop, b[prop]); if (__getOwnPropSymbols) for (var prop of __getOwnPropSymbols(b)) { if (__propIsEnum.call(b, prop)) __defNormalProp(a, prop, b[prop]); } return a; }; var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b)); var __objRest = (source, exclude) => { var target = {}; for (var prop in source) if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0) target[prop] = source[prop]; if (source != null && __getOwnPropSymbols) for (var prop of __getOwnPropSymbols(source)) { if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop)) target[prop] = source[prop]; } return target; }; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var index_exports = {}; __export(index_exports, { AssistiveTreeDescription: () => AssistiveTreeDescription, useTree: () => useTree }); module.exports = __toCommonJS(index_exports); // src/assistive-tree-description.tsx var import_react = __toESM(require("react")); var import_core = require("@headless-tree/core"); var styles = { position: "absolute", margin: "-1px", width: "1px", height: "1px", overflow: "hidden", clip: "rect(0 0 0 0)" }; var getDefaultLabel = (dnd, assistiveState, hotkeys) => { var _a, _b, _c, _d, _e, _f, _g, _h, _i; if (!hotkeys.startDrag) return ""; const itemNames = (_b = (_a = dnd == null ? void 0 : dnd.draggedItems) == null ? void 0 : _a.map((item) => item.getItemName()).join(", ")) != null ? _b : ""; const position = !(dnd == null ? void 0 : dnd.dragTarget) ? "None" : "childIndex" in dnd.dragTarget ? `${dnd.dragTarget.childIndex} of ${dnd.dragTarget.item.getChildren().length} in ${dnd.dragTarget.item.getItemName()}` : `in ${dnd.dragTarget.item.getItemName()}`; const navGuide = `Press ${(_c = hotkeys.dragUp) == null ? void 0 : _c.hotkey} and ${(_d = hotkeys.dragDown) == null ? void 0 : _d.hotkey} to move up or down, ${(_e = hotkeys.completeDrag) == null ? void 0 : _e.hotkey} to drop, ${(_f = hotkeys.cancelDrag) == null ? void 0 : _f.hotkey} to abort.`; switch (assistiveState) { case import_core.AssistiveDndState.Started: return itemNames ? `Dragging ${itemNames}. Current position: ${position}. ${navGuide}` : `Current position: ${position}. ${navGuide}`; case import_core.AssistiveDndState.Dragging: return itemNames ? `${itemNames}, ${position}` : position; case import_core.AssistiveDndState.Completed: return `Drag completed. Press ${(_g = hotkeys.startDrag) == null ? void 0 : _g.hotkey} to move selected items`; case import_core.AssistiveDndState.Aborted: return `Drag cancelled. Press ${(_h = hotkeys.startDrag) == null ? void 0 : _h.hotkey} to move selected items`; case import_core.AssistiveDndState.None: default: return `Press ${(_i = hotkeys.startDrag) == null ? void 0 : _i.hotkey} to move selected items`; } }; var AssistiveTreeDescription = (_a) => { var _b = _a, { tree, getLabel = getDefaultLabel } = _b, props = __objRest(_b, [ "tree", "getLabel" ]); var _a2; const state = tree.getState(); return /* @__PURE__ */ import_react.default.createElement( "span", __spreadProps(__spreadValues({ "aria-live": "assertive" }, props), { style: __spreadValues(__spreadValues({}, styles), props.style) }), getLabel( state.dnd, (_a2 = state.assistiveDndState) != null ? _a2 : import_core.AssistiveDndState.None, tree.getHotkeyPresets() ) ); }; // src/use-tree.tsx var import_react2 = require("react"); var import_core2 = require("@headless-tree/core"); var useTree = (config) => { const [tree] = (0, import_react2.useState)(() => ({ current: (0, import_core2.createTree)(config) })); const [state, setState] = (0, import_react2.useState)( () => tree.current.getState() ); (0, import_react2.useEffect)(() => { tree.current.setMounted(true); tree.current.rebuildTree(); return () => { tree.current.setMounted(false); }; }, [tree]); tree.current.setConfig((prev) => __spreadProps(__spreadValues(__spreadValues({}, prev), config), { state: __spreadValues(__spreadValues({}, state), config.state), setState: (state2) => { var _a; setState(state2); (_a = config.setState) == null ? void 0 : _a.call(config, state2); } })); return tree.current; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AssistiveTreeDescription, useTree });