@octanejs/floating-ui 0.1.31 → 0.1.33

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.
@@ -0,0 +1,235 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var Composite_exports = {};
20
+ __export(Composite_exports, {
21
+ Composite: () => Composite,
22
+ CompositeContext: () => CompositeContext,
23
+ CompositeItem: () => CompositeItem
24
+ });
25
+ module.exports = __toCommonJS(Composite_exports);
26
+ var import_octane = require("octane");
27
+ var import_FloatingList = require("./FloatingList.cjs");
28
+ var import_internal = require("./internal.cjs");
29
+ var import_useMergeRefs = require("./useMergeRefs.cjs");
30
+ var import_utils = require("./utils/index.cjs");
31
+ const ARROW_LEFT = "ArrowLeft";
32
+ const ARROW_RIGHT = "ArrowRight";
33
+ const ARROW_UP = "ArrowUp";
34
+ const ARROW_DOWN = "ArrowDown";
35
+ const horizontalKeys = [ARROW_LEFT, ARROW_RIGHT];
36
+ const verticalKeys = [ARROW_UP, ARROW_DOWN];
37
+ const allKeys = [...horizontalKeys, ...verticalKeys];
38
+ function cloneElement(el, props) {
39
+ return (0, import_octane.createElement)(el.type, { ...el.props, ...props });
40
+ }
41
+ function renderJsx(render, computedProps) {
42
+ if (typeof render === "function") {
43
+ return render(computedProps);
44
+ }
45
+ if (render) {
46
+ return cloneElement(render, computedProps);
47
+ }
48
+ return (0, import_octane.createElement)("div", { ...computedProps });
49
+ }
50
+ const CompositeContext = (0, import_octane.createContext)({
51
+ activeIndex: 0,
52
+ onNavigate: () => {
53
+ }
54
+ });
55
+ function Composite(props) {
56
+ const render = props.render;
57
+ const orientation = props.orientation ?? "both";
58
+ const loop = props.loop ?? true;
59
+ const rtl = props.rtl ?? false;
60
+ const cols = props.cols ?? 1;
61
+ const disabledIndices = props.disabledIndices;
62
+ const externalActiveIndex = props.activeIndex;
63
+ const externalSetActiveIndex = props.onNavigate;
64
+ const itemSizes = props.itemSizes;
65
+ const dense = props.dense ?? false;
66
+ const forwardedRef = props.ref;
67
+ const {
68
+ render: _r,
69
+ orientation: _o,
70
+ loop: _l,
71
+ rtl: _rtl,
72
+ cols: _c,
73
+ disabledIndices: _di,
74
+ activeIndex: _ai,
75
+ onNavigate: _on,
76
+ itemSizes: _is,
77
+ dense: _d,
78
+ ref: _ref,
79
+ ...domProps
80
+ } = props;
81
+ const [internalActiveIndex, internalSetActiveIndex] = (0, import_octane.useState)(0, (0, import_internal.S)("Composite:active"));
82
+ const activeIndex = externalActiveIndex != null ? externalActiveIndex : internalActiveIndex;
83
+ const onNavigate = (0, import_utils.useEffectEvent)(
84
+ externalSetActiveIndex != null ? externalSetActiveIndex : internalSetActiveIndex,
85
+ (0, import_internal.S)("Composite:nav")
86
+ );
87
+ const elementsRef = (0, import_octane.useRef)([], (0, import_internal.S)("Composite:els"));
88
+ const renderElementProps = render && typeof render !== "function" ? render.props : {};
89
+ const contextValue = (0, import_octane.useMemo)(
90
+ () => ({ activeIndex, onNavigate }),
91
+ [activeIndex, onNavigate],
92
+ (0, import_internal.S)("Composite:ctx")
93
+ );
94
+ const isGrid = cols > 1;
95
+ function handleKeyDown(event) {
96
+ if (!allKeys.includes(event.key)) return;
97
+ let nextIndex = activeIndex;
98
+ const minIndex = (0, import_utils.getMinListIndex)(elementsRef, disabledIndices);
99
+ const maxIndex = (0, import_utils.getMaxListIndex)(elementsRef, disabledIndices);
100
+ const horizontalEndKey = rtl ? ARROW_LEFT : ARROW_RIGHT;
101
+ const horizontalStartKey = rtl ? ARROW_RIGHT : ARROW_LEFT;
102
+ if (isGrid) {
103
+ const sizes = itemSizes || Array.from({ length: elementsRef.current.length }, () => ({ width: 1, height: 1 }));
104
+ const cellMap = (0, import_utils.createGridCellMap)(sizes, cols, dense);
105
+ const minGridIndex = cellMap.findIndex(
106
+ (index) => index != null && !(0, import_utils.isListIndexDisabled)(elementsRef, index, disabledIndices)
107
+ );
108
+ const maxGridIndex = cellMap.reduce(
109
+ (foundIndex, index, cellIndex) => index != null && !(0, import_utils.isListIndexDisabled)(elementsRef, index, disabledIndices) ? cellIndex : foundIndex,
110
+ -1
111
+ );
112
+ const maybeNextIndex = cellMap[(0, import_utils.getGridNavigatedIndex)(
113
+ {
114
+ current: cellMap.map(
115
+ (itemIndex) => itemIndex ? elementsRef.current[itemIndex] : null
116
+ )
117
+ },
118
+ {
119
+ event,
120
+ orientation,
121
+ loop,
122
+ rtl,
123
+ cols,
124
+ disabledIndices: (0, import_utils.getGridCellIndices)(
125
+ [
126
+ ...(typeof disabledIndices !== "function" ? disabledIndices : null) || elementsRef.current.map(
127
+ (_, index) => (0, import_utils.isListIndexDisabled)(elementsRef, index, disabledIndices) ? index : void 0
128
+ ),
129
+ void 0
130
+ ],
131
+ cellMap
132
+ ),
133
+ minIndex: minGridIndex,
134
+ maxIndex: maxGridIndex,
135
+ prevIndex: (0, import_utils.getGridCellIndexOfCorner)(
136
+ activeIndex > maxIndex ? minIndex : activeIndex,
137
+ sizes,
138
+ cellMap,
139
+ cols,
140
+ event.key === ARROW_DOWN ? "bl" : event.key === horizontalEndKey ? "tr" : "tl"
141
+ )
142
+ }
143
+ )];
144
+ if (maybeNextIndex != null) {
145
+ nextIndex = maybeNextIndex;
146
+ }
147
+ }
148
+ const toEndKeys = {
149
+ horizontal: [horizontalEndKey],
150
+ vertical: [ARROW_DOWN],
151
+ both: [horizontalEndKey, ARROW_DOWN]
152
+ }[orientation];
153
+ const toStartKeys = {
154
+ horizontal: [horizontalStartKey],
155
+ vertical: [ARROW_UP],
156
+ both: [horizontalStartKey, ARROW_UP]
157
+ }[orientation];
158
+ const preventedKeys = isGrid ? allKeys : {
159
+ horizontal: horizontalKeys,
160
+ vertical: verticalKeys,
161
+ both: allKeys
162
+ }[orientation];
163
+ if (nextIndex === activeIndex && [...toEndKeys, ...toStartKeys].includes(event.key)) {
164
+ if (loop && nextIndex === maxIndex && toEndKeys.includes(event.key)) {
165
+ nextIndex = minIndex;
166
+ } else if (loop && nextIndex === minIndex && toStartKeys.includes(event.key)) {
167
+ nextIndex = maxIndex;
168
+ } else {
169
+ nextIndex = (0, import_utils.findNonDisabledListIndex)(elementsRef, {
170
+ startingIndex: nextIndex,
171
+ decrement: toStartKeys.includes(event.key),
172
+ disabledIndices
173
+ });
174
+ }
175
+ }
176
+ if (nextIndex !== activeIndex && !(0, import_utils.isIndexOutOfListBounds)(elementsRef, nextIndex)) {
177
+ event.stopPropagation();
178
+ if (preventedKeys.includes(event.key)) {
179
+ event.preventDefault();
180
+ }
181
+ onNavigate(nextIndex);
182
+ elementsRef.current[nextIndex]?.focus();
183
+ }
184
+ }
185
+ const computedProps = {
186
+ ...domProps,
187
+ ...renderElementProps,
188
+ ref: forwardedRef,
189
+ "aria-orientation": orientation === "both" ? void 0 : orientation,
190
+ onKeyDown(e) {
191
+ domProps.onKeyDown?.(e);
192
+ renderElementProps.onKeyDown?.(e);
193
+ handleKeyDown(e);
194
+ }
195
+ };
196
+ return (0, import_octane.createElement)(CompositeContext.Provider, {
197
+ value: contextValue,
198
+ children: (0, import_octane.createElement)(import_FloatingList.FloatingList, {
199
+ elementsRef,
200
+ children: renderJsx(render, computedProps)
201
+ })
202
+ });
203
+ }
204
+ function CompositeItem(props) {
205
+ const render = props.render;
206
+ const forwardedRef = props.ref;
207
+ const { render: _r, ref: _ref, ...domProps } = props;
208
+ const renderElementProps = render && typeof render !== "function" ? render.props : {};
209
+ const { activeIndex, onNavigate } = (0, import_octane.useContext)(CompositeContext);
210
+ const { ref, index } = (0, import_FloatingList.useListItem)((0, import_internal.S)("CompositeItem:listItem"));
211
+ const mergedRef = (0, import_useMergeRefs.useMergeRefs)(
212
+ [ref, forwardedRef, renderElementProps.ref],
213
+ (0, import_internal.S)("CompositeItem:merge")
214
+ );
215
+ const isActive = activeIndex === index;
216
+ const computedProps = {
217
+ ...domProps,
218
+ ...renderElementProps,
219
+ ref: mergedRef,
220
+ tabIndex: isActive ? 0 : -1,
221
+ "data-active": isActive ? "" : void 0,
222
+ onFocus(e) {
223
+ domProps.onFocus?.(e);
224
+ renderElementProps.onFocus?.(e);
225
+ onNavigate(index);
226
+ }
227
+ };
228
+ return renderJsx(render, computedProps);
229
+ }
230
+ // Annotate the CommonJS export names for ESM import in node:
231
+ 0 && (module.exports = {
232
+ Composite,
233
+ CompositeContext,
234
+ CompositeItem
235
+ });
@@ -0,0 +1,143 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var FloatingArrow_exports = {};
20
+ __export(FloatingArrow_exports, {
21
+ FloatingArrow: () => FloatingArrow
22
+ });
23
+ module.exports = __toCommonJS(FloatingArrow_exports);
24
+ var import_dom = require("@floating-ui/utils/dom");
25
+ var import_octane = require("octane");
26
+ var import_internal = require("./internal.cjs");
27
+ var import_useId = require("./useId.cjs");
28
+ var import_utils = require("./utils/index.cjs");
29
+ function FloatingArrow(props) {
30
+ const ref = props.ref;
31
+ const context = props.context;
32
+ const placement = context.placement;
33
+ const floating = context.elements.floating;
34
+ const arrow = context.middlewareData.arrow;
35
+ const shift = context.middlewareData.shift;
36
+ const width = props.width ?? 14;
37
+ const height = props.height ?? 7;
38
+ const tipRadius = props.tipRadius ?? 0;
39
+ const strokeWidth = props.strokeWidth ?? 0;
40
+ const staticOffset = props.staticOffset;
41
+ const stroke = props.stroke;
42
+ const d = props.d;
43
+ const { transform, ...restStyle } = props.style ?? {};
44
+ const {
45
+ context: _c,
46
+ width: _w,
47
+ height: _h,
48
+ tipRadius: _t,
49
+ strokeWidth: _s,
50
+ staticOffset: _so,
51
+ stroke: _st,
52
+ d: _d,
53
+ style: _sty,
54
+ ref: _r,
55
+ ...rest
56
+ } = props;
57
+ const clipPathId = (0, import_useId.useId)((0, import_internal.S)("FloatingArrow:id"));
58
+ const [isRTL, setIsRTL] = (0, import_octane.useState)(false, (0, import_internal.S)("FloatingArrow:rtl"));
59
+ (0, import_utils.useModernLayoutEffect)(
60
+ () => {
61
+ if (!floating) return;
62
+ const rtl = (0, import_dom.getComputedStyle)(floating).direction === "rtl";
63
+ if (rtl) {
64
+ setIsRTL(true);
65
+ }
66
+ },
67
+ [floating],
68
+ (0, import_internal.S)("FloatingArrow:eff")
69
+ );
70
+ if (!floating) {
71
+ return null;
72
+ }
73
+ const [side, alignment] = placement.split("-");
74
+ const isVerticalSide = side === "top" || side === "bottom";
75
+ let computedStaticOffset = staticOffset;
76
+ if (isVerticalSide && shift != null && shift.x || !isVerticalSide && shift != null && shift.y) {
77
+ computedStaticOffset = null;
78
+ }
79
+ const computedStrokeWidth = strokeWidth * 2;
80
+ const halfStrokeWidth = computedStrokeWidth / 2;
81
+ const svgX = width / 2 * (tipRadius / -8 + 1);
82
+ const svgY = height / 2 * tipRadius / 4;
83
+ const isCustomShape = !!d;
84
+ const yOffsetProp = computedStaticOffset && alignment === "end" ? "bottom" : "top";
85
+ let xOffsetProp = computedStaticOffset && alignment === "end" ? "right" : "left";
86
+ if (computedStaticOffset && isRTL) {
87
+ xOffsetProp = alignment === "end" ? "left" : "right";
88
+ }
89
+ const arrowX = arrow?.x != null ? computedStaticOffset || arrow.x : "";
90
+ const arrowY = arrow?.y != null ? computedStaticOffset || arrow.y : "";
91
+ const dValue = d || "M0,0" + (" H" + width) + (" L" + (width - svgX) + "," + (height - svgY)) + (" Q" + width / 2 + "," + height + " " + svgX + "," + (height - svgY)) + " Z";
92
+ const rotation = {
93
+ top: isCustomShape ? "rotate(180deg)" : "",
94
+ left: isCustomShape ? "rotate(90deg)" : "rotate(-90deg)",
95
+ bottom: isCustomShape ? "" : "rotate(180deg)",
96
+ right: isCustomShape ? "rotate(-90deg)" : "rotate(90deg)"
97
+ }[side];
98
+ return (0, import_octane.createElement)(
99
+ "svg",
100
+ {
101
+ ...rest,
102
+ "aria-hidden": true,
103
+ ref,
104
+ width: isCustomShape ? width : width + computedStrokeWidth,
105
+ height: width,
106
+ viewBox: "0 0 " + width + " " + (height > width ? height : width),
107
+ style: {
108
+ position: "absolute",
109
+ pointerEvents: "none",
110
+ [xOffsetProp]: arrowX,
111
+ [yOffsetProp]: arrowY,
112
+ [side]: isVerticalSide || isCustomShape ? "100%" : "calc(100% - " + computedStrokeWidth / 2 + "px)",
113
+ transform: [rotation, transform].filter((t) => !!t).join(" "),
114
+ ...restStyle
115
+ }
116
+ },
117
+ computedStrokeWidth > 0 && (0, import_octane.createElement)("path", {
118
+ "clip-path": "url(#" + clipPathId + ")",
119
+ fill: "none",
120
+ stroke,
121
+ "stroke-width": computedStrokeWidth + (d ? 0 : 1),
122
+ d: dValue
123
+ }),
124
+ (0, import_octane.createElement)("path", {
125
+ stroke: computedStrokeWidth && !d ? rest.fill : "none",
126
+ d: dValue
127
+ }),
128
+ (0, import_octane.createElement)(
129
+ "clipPath",
130
+ { id: clipPathId },
131
+ (0, import_octane.createElement)("rect", {
132
+ x: -halfStrokeWidth,
133
+ y: halfStrokeWidth * (isCustomShape ? -1 : 1),
134
+ width: width + computedStrokeWidth,
135
+ height: width
136
+ })
137
+ )
138
+ );
139
+ }
140
+ // Annotate the CommonJS export names for ESM import in node:
141
+ 0 && (module.exports = {
142
+ FloatingArrow
143
+ });