@tamagui/accordion 2.7.7 → 3.0.0-beta.643.1

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.
@@ -1,8 +1,8 @@
1
+ import { View, createRefComponent, createStyledContext, createStyledHOC, styled } from "@tamagui/core";
1
2
  import { Collapsible } from "@tamagui/collapsible";
2
3
  import { createCollection } from "@tamagui/collection";
3
4
  import { useComposedRefs } from "@tamagui/compose-refs";
4
5
  import { isWeb, useIsomorphicLayoutEffect } from "@tamagui/constants";
5
- import { View, createStyledContext, styled } from "@tamagui/core";
6
6
  import { composeEventHandlers, withStaticProperties } from "@tamagui/helpers";
7
7
  import { YStack } from "@tamagui/stacks";
8
8
  import { H1 } from "@tamagui/text";
@@ -10,454 +10,371 @@ import { useControllableState } from "@tamagui/use-controllable-state";
10
10
  import { useDirection } from "@tamagui/use-direction";
11
11
  import * as React from "react";
12
12
  import { jsx } from "react/jsx-runtime";
13
+
13
14
  const ACCORDION_NAME = "Accordion";
14
- const ACCORDION_KEYS = ["Home", "End", "ArrowDown", "ArrowUp", "ArrowLeft", "ArrowRight"];
15
+ const ACCORDION_KEYS = [
16
+ "Home",
17
+ "End",
18
+ "ArrowDown",
19
+ "ArrowUp",
20
+ "ArrowLeft",
21
+ "ArrowRight"
22
+ ];
15
23
  const [Collection, useCollection] = createCollection(ACCORDION_NAME);
16
24
  const ACCORDION_CONTEXT = "Accordion";
17
- const AccordionComponent = React.forwardRef((props, forwardedRef) => {
18
- const {
19
- type,
20
- ...accordionProps
21
- } = props;
22
- const singleProps = accordionProps;
23
- const multipleProps = accordionProps;
24
- return /* @__PURE__ */jsx(Collection.Provider, {
25
- scope: props.__scopeAccordion || ACCORDION_CONTEXT,
26
- children: type === "multiple" ? /* @__PURE__ */jsx(AccordionImplMultiple, {
27
- ...multipleProps,
28
- ref: forwardedRef
29
- }) : /* @__PURE__ */jsx(AccordionImplSingle, {
30
- ...singleProps,
31
- ref: forwardedRef
32
- })
33
- });
25
+ const AccordionComponent = createRefComponent((props, forwardedRef) => {
26
+ const { type, ...accordionProps } = props;
27
+ const singleProps = accordionProps;
28
+ const multipleProps = accordionProps;
29
+ return /* @__PURE__ */ jsx(Collection.Provider, {
30
+ scope: props.__scopeAccordion || ACCORDION_CONTEXT,
31
+ children: type === "multiple" ? /* @__PURE__ */ jsx(AccordionImplMultiple, {
32
+ ...multipleProps,
33
+ ref: forwardedRef
34
+ }) : /* @__PURE__ */ jsx(AccordionImplSingle, {
35
+ ...singleProps,
36
+ ref: forwardedRef
37
+ })
38
+ });
34
39
  });
35
40
  AccordionComponent.displayName = ACCORDION_NAME;
36
- AccordionComponent.propTypes = {
37
- type(props) {
38
- const value = props.value || props.defaultValue;
39
- if (props.type && !["single", "multiple"].includes(props.type)) {
40
- return new Error("Invalid prop `type` supplied to `Accordion`. Expected one of `single | multiple`.");
41
- }
42
- if (props.type === "multiple" && typeof value === "string") {
43
- return new Error("Invalid prop `type` supplied to `Accordion`. Expected `single` when `defaultValue` or `value` is type `string`.");
44
- }
45
- if (props.type === "single" && Array.isArray(value)) {
46
- return new Error("Invalid prop `type` supplied to `Accordion`. Expected `multiple` when `defaultValue` or `value` is type `string[]`.");
47
- }
48
- return null;
49
- }
50
- };
51
- const {
52
- Provider: AccordionValueProvider,
53
- useStyledContext: useAccordionValueContext
54
- } = createStyledContext();
55
- const {
56
- Provider: AccordionCollapsibleProvider,
57
- useStyledContext: useAccordionCollapsibleContext
58
- } = createStyledContext();
59
- const AccordionImplSingle = React.forwardRef((props, forwardedRef) => {
60
- const {
61
- value: valueProp,
62
- defaultValue,
63
- control,
64
- onValueChange = () => {},
65
- collapsible = false,
66
- ...accordionSingleProps
67
- } = props;
68
- const [value, setValue] = useControllableState({
69
- prop: valueProp,
70
- defaultProp: defaultValue || "",
71
- onChange: onValueChange
72
- });
73
- return /* @__PURE__ */jsx(AccordionValueProvider, {
74
- scope: props.__scopeAccordion,
75
- value: value ? [value] : [],
76
- onItemOpen: setValue,
77
- onItemClose: React.useCallback(() => collapsible && setValue(""), [setValue, collapsible]),
78
- children: /* @__PURE__ */jsx(AccordionCollapsibleProvider, {
79
- scope: props.__scopeAccordion,
80
- collapsible,
81
- children: /* @__PURE__ */jsx(AccordionImpl, {
82
- ...accordionSingleProps,
83
- ref: forwardedRef
84
- })
85
- })
86
- });
41
+ AccordionComponent.propTypes = { type(props) {
42
+ const value = props.value || props.defaultValue;
43
+ if (props.type && !["single", "multiple"].includes(props.type)) {
44
+ return new Error("Invalid prop `type` supplied to `Accordion`. Expected one of `single | multiple`.");
45
+ }
46
+ if (props.type === "multiple" && typeof value === "string") {
47
+ return new Error("Invalid prop `type` supplied to `Accordion`. Expected `single` when `defaultValue` or `value` is type `string`.");
48
+ }
49
+ if (props.type === "single" && Array.isArray(value)) {
50
+ return new Error("Invalid prop `type` supplied to `Accordion`. Expected `multiple` when `defaultValue` or `value` is type `string[]`.");
51
+ }
52
+ return null;
53
+ } };
54
+ const { Provider: AccordionValueProvider, useStyledContext: useAccordionValueContext } = createStyledContext();
55
+ const { Provider: AccordionCollapsibleProvider, useStyledContext: useAccordionCollapsibleContext } = createStyledContext();
56
+ const AccordionImplSingle = createRefComponent((props, forwardedRef) => {
57
+ const { value: valueProp, defaultValue, control, onValueChange = () => {}, collapsible = false, ...accordionSingleProps } = props;
58
+ const [value, setValue] = useControllableState({
59
+ prop: valueProp,
60
+ defaultProp: defaultValue || "",
61
+ onChange: onValueChange
62
+ });
63
+ return /* @__PURE__ */ jsx(AccordionValueProvider, {
64
+ scope: props.__scopeAccordion,
65
+ value: value ? [value] : [],
66
+ onItemOpen: setValue,
67
+ onItemClose: React.useCallback(() => collapsible && setValue(""), [setValue, collapsible]),
68
+ children: /* @__PURE__ */ jsx(AccordionCollapsibleProvider, {
69
+ scope: props.__scopeAccordion,
70
+ collapsible,
71
+ children: /* @__PURE__ */ jsx(AccordionImpl, {
72
+ ...accordionSingleProps,
73
+ ref: forwardedRef
74
+ })
75
+ })
76
+ });
87
77
  });
88
- const AccordionImplMultiple = React.forwardRef((props, forwardedRef) => {
89
- const {
90
- value: valueProp,
91
- defaultValue,
92
- onValueChange = () => {},
93
- ...accordionMultipleProps
94
- } = props;
95
- const [value, setValue] = useControllableState({
96
- prop: valueProp,
97
- defaultProp: defaultValue || [],
98
- onChange: onValueChange
99
- });
100
- const handleItemOpen = React.useCallback(itemValue => setValue((prevValue = []) => [...prevValue, itemValue]), [setValue]);
101
- const handleItemClose = React.useCallback(itemValue => setValue((prevValue = []) => prevValue.filter(value2 => value2 !== itemValue)), [setValue]);
102
- return /* @__PURE__ */jsx(AccordionValueProvider, {
103
- scope: props.__scopeAccordion,
104
- value: value || [],
105
- onItemOpen: handleItemOpen,
106
- onItemClose: handleItemClose,
107
- children: /* @__PURE__ */jsx(AccordionCollapsibleProvider, {
108
- scope: props.__scopeAccordion,
109
- collapsible: true,
110
- children: /* @__PURE__ */jsx(AccordionImpl, {
111
- ...accordionMultipleProps,
112
- ref: forwardedRef
113
- })
114
- })
115
- });
78
+ const AccordionImplMultiple = createRefComponent((props, forwardedRef) => {
79
+ const { value: valueProp, defaultValue, onValueChange = () => {}, ...accordionMultipleProps } = props;
80
+ const [value, setValue] = useControllableState({
81
+ prop: valueProp,
82
+ defaultProp: defaultValue || [],
83
+ onChange: onValueChange
84
+ });
85
+ const handleItemOpen = React.useCallback((itemValue) => setValue((prevValue = []) => [...prevValue, itemValue]), [setValue]);
86
+ const handleItemClose = React.useCallback((itemValue) => setValue((prevValue = []) => prevValue.filter((value2) => value2 !== itemValue)), [setValue]);
87
+ return /* @__PURE__ */ jsx(AccordionValueProvider, {
88
+ scope: props.__scopeAccordion,
89
+ value: value || [],
90
+ onItemOpen: handleItemOpen,
91
+ onItemClose: handleItemClose,
92
+ children: /* @__PURE__ */ jsx(AccordionCollapsibleProvider, {
93
+ scope: props.__scopeAccordion,
94
+ collapsible: true,
95
+ children: /* @__PURE__ */ jsx(AccordionImpl, {
96
+ ...accordionMultipleProps,
97
+ ref: forwardedRef
98
+ })
99
+ })
100
+ });
116
101
  });
117
- const {
118
- Provider: AccordionImplProvider,
119
- useStyledContext: useAccordionContext
120
- } = createStyledContext();
121
- const AccordionImpl = React.forwardRef((props, forwardedRef) => {
122
- const {
123
- __scopeAccordion,
124
- disabled,
125
- dir,
126
- orientation = "vertical",
127
- ...accordionProps
128
- } = props;
129
- const accordionRef = React.useRef(null);
130
- const composedRef = useComposedRefs(accordionRef, forwardedRef);
131
- const getItems = useCollection(__scopeAccordion || ACCORDION_CONTEXT);
132
- const direction = useDirection(dir);
133
- const isDirectionLTR = direction === "ltr";
134
- const handleKeyDown = composeEventHandlers(props.onKeyDown, event => {
135
- if (!ACCORDION_KEYS.includes(event.key)) return;
136
- const target = event.target;
137
- const triggerCollection = getItems().filter(item => {
138
- const el = item.ref.current;
139
- return !el?.disabled;
140
- });
141
- const triggerIndex = triggerCollection.findIndex(item => item.ref.current === target);
142
- const triggerCount = triggerCollection.length;
143
- if (triggerIndex === -1) return;
144
- event.preventDefault();
145
- let nextIndex = triggerIndex;
146
- const homeIndex = 0;
147
- const endIndex = triggerCount - 1;
148
- const moveNext = () => {
149
- nextIndex = triggerIndex + 1;
150
- if (nextIndex > endIndex) {
151
- nextIndex = homeIndex;
152
- }
153
- };
154
- const movePrev = () => {
155
- nextIndex = triggerIndex - 1;
156
- if (nextIndex < homeIndex) {
157
- nextIndex = endIndex;
158
- }
159
- };
160
- switch (event.key) {
161
- case "Home":
162
- nextIndex = homeIndex;
163
- break;
164
- case "End":
165
- nextIndex = endIndex;
166
- break;
167
- case "ArrowRight":
168
- if (orientation === "horizontal") {
169
- if (isDirectionLTR) {
170
- moveNext();
171
- } else {
172
- movePrev();
173
- }
174
- }
175
- break;
176
- case "ArrowDown":
177
- if (orientation === "vertical") {
178
- moveNext();
179
- }
180
- break;
181
- case "ArrowLeft":
182
- if (orientation === "horizontal") {
183
- if (isDirectionLTR) {
184
- movePrev();
185
- } else {
186
- moveNext();
187
- }
188
- }
189
- break;
190
- case "ArrowUp":
191
- if (orientation === "vertical") {
192
- movePrev();
193
- }
194
- break;
195
- }
196
- const clampedIndex = nextIndex % triggerCount;
197
- triggerCollection[clampedIndex].ref.current?.focus();
198
- });
199
- return /* @__PURE__ */jsx(AccordionImplProvider, {
200
- scope: __scopeAccordion,
201
- disabled,
202
- direction: dir,
203
- orientation,
204
- children: /* @__PURE__ */jsx(Collection.Slot, {
205
- scope: __scopeAccordion || ACCORDION_CONTEXT,
206
- children: /* @__PURE__ */jsx(YStack, {
207
- "data-orientation": orientation,
208
- ref: composedRef,
209
- ...accordionProps,
210
- ...(isWeb && {
211
- onKeyDown: handleKeyDown
212
- })
213
- })
214
- })
215
- });
102
+ const { Provider: AccordionImplProvider, useStyledContext: useAccordionContext } = createStyledContext();
103
+ const AccordionImpl = createRefComponent((props, forwardedRef) => {
104
+ const { __scopeAccordion, disabled, dir, orientation = "vertical", ...accordionProps } = props;
105
+ const accordionRef = React.useRef(null);
106
+ const composedRef = useComposedRefs(accordionRef, forwardedRef);
107
+ const getItems = useCollection(__scopeAccordion || ACCORDION_CONTEXT);
108
+ const direction = useDirection(dir);
109
+ const isDirectionLTR = direction === "ltr";
110
+ const handleKeyDown = composeEventHandlers(props.onKeyDown, (event) => {
111
+ if (!ACCORDION_KEYS.includes(event.key)) return;
112
+ const target = event.target;
113
+ const triggerCollection = getItems().filter((item) => {
114
+ const el = item.ref.current;
115
+ return !el?.disabled;
116
+ });
117
+ const triggerIndex = triggerCollection.findIndex((item) => item.ref.current === target);
118
+ const triggerCount = triggerCollection.length;
119
+ if (triggerIndex === -1) return;
120
+ event.preventDefault();
121
+ let nextIndex = triggerIndex;
122
+ const homeIndex = 0;
123
+ const endIndex = triggerCount - 1;
124
+ const moveNext = () => {
125
+ nextIndex = triggerIndex + 1;
126
+ if (nextIndex > endIndex) {
127
+ nextIndex = homeIndex;
128
+ }
129
+ };
130
+ const movePrev = () => {
131
+ nextIndex = triggerIndex - 1;
132
+ if (nextIndex < homeIndex) {
133
+ nextIndex = endIndex;
134
+ }
135
+ };
136
+ switch (event.key) {
137
+ case "Home":
138
+ nextIndex = homeIndex;
139
+ break;
140
+ case "End":
141
+ nextIndex = endIndex;
142
+ break;
143
+ case "ArrowRight":
144
+ if (orientation === "horizontal") {
145
+ if (isDirectionLTR) {
146
+ moveNext();
147
+ } else {
148
+ movePrev();
149
+ }
150
+ }
151
+ break;
152
+ case "ArrowDown":
153
+ if (orientation === "vertical") {
154
+ moveNext();
155
+ }
156
+ break;
157
+ case "ArrowLeft":
158
+ if (orientation === "horizontal") {
159
+ if (isDirectionLTR) {
160
+ movePrev();
161
+ } else {
162
+ moveNext();
163
+ }
164
+ }
165
+ break;
166
+ case "ArrowUp":
167
+ if (orientation === "vertical") {
168
+ movePrev();
169
+ }
170
+ break;
171
+ }
172
+ const clampedIndex = nextIndex % triggerCount;
173
+ triggerCollection[clampedIndex].ref.current?.focus();
174
+ });
175
+ return /* @__PURE__ */ jsx(AccordionImplProvider, {
176
+ scope: __scopeAccordion,
177
+ disabled,
178
+ direction: dir,
179
+ orientation,
180
+ children: /* @__PURE__ */ jsx(Collection.Slot, {
181
+ scope: __scopeAccordion || ACCORDION_CONTEXT,
182
+ children: /* @__PURE__ */ jsx(YStack, {
183
+ "data-orientation": orientation,
184
+ ref: composedRef,
185
+ ...accordionProps,
186
+ ...isWeb && { onKeyDown: handleKeyDown }
187
+ })
188
+ })
189
+ });
216
190
  });
217
191
  const ITEM_NAME = "AccordionItem";
218
- const {
219
- Provider: AccordionItemProvider,
220
- useStyledContext: useAccordionItemContext
221
- } = createStyledContext();
222
- const AccordionItem = React.forwardRef((props, forwardedRef) => {
223
- const {
224
- __scopeAccordion,
225
- value,
226
- ...accordionItemProps
227
- } = props;
228
- const accordionContext = useAccordionContext(__scopeAccordion);
229
- const valueContext = useAccordionValueContext(__scopeAccordion);
230
- const triggerId = React.useId();
231
- const open = value && valueContext.value.includes(value) || false;
232
- const disabled = accordionContext.disabled || props.disabled;
233
- return /* @__PURE__ */jsx(AccordionItemProvider, {
234
- scope: __scopeAccordion,
235
- open,
236
- disabled,
237
- triggerId,
238
- children: /* @__PURE__ */jsx(Collapsible, {
239
- "data-orientation": accordionContext.orientation,
240
- "data-state": open ? "open" : "closed",
241
- __scopeCollapsible: __scopeAccordion || ACCORDION_CONTEXT,
242
- ...accordionItemProps,
243
- ref: forwardedRef,
244
- disabled,
245
- open,
246
- onOpenChange: open2 => {
247
- if (open2) {
248
- valueContext.onItemOpen(value);
249
- } else {
250
- valueContext.onItemClose(value);
251
- }
252
- }
253
- })
254
- });
192
+ const { Provider: AccordionItemProvider, useStyledContext: useAccordionItemContext } = createStyledContext();
193
+ const AccordionItem = createRefComponent((props, forwardedRef) => {
194
+ const { __scopeAccordion, value, ...accordionItemProps } = props;
195
+ const accordionContext = useAccordionContext(__scopeAccordion);
196
+ const valueContext = useAccordionValueContext(__scopeAccordion);
197
+ const triggerId = React.useId();
198
+ const open = value && valueContext.value.includes(value) || false;
199
+ const disabled = accordionContext.disabled || props.disabled;
200
+ return /* @__PURE__ */ jsx(AccordionItemProvider, {
201
+ scope: __scopeAccordion,
202
+ open,
203
+ disabled,
204
+ triggerId,
205
+ children: /* @__PURE__ */ jsx(Collapsible, {
206
+ "data-orientation": accordionContext.orientation,
207
+ "data-state": open ? "open" : "closed",
208
+ __scopeCollapsible: __scopeAccordion || ACCORDION_CONTEXT,
209
+ ...accordionItemProps,
210
+ ref: forwardedRef,
211
+ disabled,
212
+ open,
213
+ onOpenChange: (open2) => {
214
+ if (open2) {
215
+ valueContext.onItemOpen(value);
216
+ } else {
217
+ valueContext.onItemClose(value);
218
+ }
219
+ }
220
+ })
221
+ });
255
222
  });
256
223
  AccordionItem.displayName = ITEM_NAME;
257
224
  const HEADER_NAME = "AccordionHeader";
258
- const AccordionHeader = React.forwardRef((props, forwardedRef) => {
259
- const {
260
- __scopeAccordion,
261
- ...headerProps
262
- } = props;
263
- const accordionContext = useAccordionContext(__scopeAccordion);
264
- const itemContext = useAccordionItemContext(__scopeAccordion);
265
- return /* @__PURE__ */jsx(H1, {
266
- "data-orientation": accordionContext.orientation,
267
- "data-state": getState(itemContext.open),
268
- "data-disabled": itemContext.disabled ? "" : void 0,
269
- ...headerProps,
270
- ref: forwardedRef
271
- });
225
+ const AccordionHeader = createRefComponent((props, forwardedRef) => {
226
+ const { __scopeAccordion, ...headerProps } = props;
227
+ const accordionContext = useAccordionContext(__scopeAccordion);
228
+ const itemContext = useAccordionItemContext(__scopeAccordion);
229
+ return /* @__PURE__ */ jsx(H1, {
230
+ "data-orientation": accordionContext.orientation,
231
+ "data-state": getState(itemContext.open),
232
+ "data-disabled": itemContext.disabled ? "" : void 0,
233
+ ...headerProps,
234
+ ref: forwardedRef
235
+ });
272
236
  });
273
237
  AccordionHeader.displayName = HEADER_NAME;
274
- const AccordionTriggerFrame = styled(Collapsible.Trigger, {
275
- variants: {
276
- unstyled: {
277
- false: {
278
- cursor: "pointer",
279
- backgroundColor: "$background",
280
- padding: "$true",
281
- hoverStyle: {
282
- backgroundColor: "$backgroundHover"
283
- },
284
- focusStyle: {
285
- backgroundColor: "$backgroundFocus"
286
- },
287
- pressStyle: {
288
- backgroundColor: "$backgroundPress"
289
- }
290
- }
291
- }
292
- },
293
- defaultVariants: {
294
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
295
- }
296
- });
297
- const AccordionTrigger = AccordionTriggerFrame.styleable(function AccordionTrigger2(props, forwardedRef) {
298
- const {
299
- __scopeAccordion,
300
- ...triggerProps
301
- } = props;
302
- const accordionContext = useAccordionContext(__scopeAccordion);
303
- const itemContext = useAccordionItemContext(__scopeAccordion);
304
- const collapsibleContext = useAccordionCollapsibleContext(__scopeAccordion);
305
- return /* @__PURE__ */jsx(Collection.ItemSlot, {
306
- scope: __scopeAccordion || ACCORDION_CONTEXT,
307
- children: /* @__PURE__ */jsx(AccordionTriggerFrame, {
308
- __scopeCollapsible: __scopeAccordion || ACCORDION_CONTEXT,
309
- "aria-disabled": itemContext.open && !collapsibleContext.collapsible || void 0,
310
- "data-orientation": accordionContext.orientation,
311
- id: itemContext.triggerId,
312
- ...triggerProps,
313
- ref: forwardedRef
314
- })
315
- });
316
- });
317
- const AccordionContentFrame = styled(Collapsible.Content, {
318
- variants: {
319
- unstyled: {
320
- false: {
321
- padding: "$true",
322
- backgroundColor: "$background"
323
- }
324
- }
325
- },
326
- defaultVariants: {
327
- unstyled: process.env.TAMAGUI_HEADLESS === "1"
328
- }
238
+ const AccordionTriggerFrame = styled(Collapsible.Trigger, { displayName: "AccordionTrigger" });
239
+ const AccordionTrigger = createStyledHOC(AccordionTriggerFrame, function AccordionTrigger2(props, forwardedRef) {
240
+ const { __scopeAccordion, ...triggerProps } = props;
241
+ const accordionContext = useAccordionContext(__scopeAccordion);
242
+ const itemContext = useAccordionItemContext(__scopeAccordion);
243
+ const collapsibleContext = useAccordionCollapsibleContext(__scopeAccordion);
244
+ return /* @__PURE__ */ jsx(Collection.ItemSlot, {
245
+ scope: __scopeAccordion || ACCORDION_CONTEXT,
246
+ children: /* @__PURE__ */ jsx(AccordionTriggerFrame, {
247
+ __scopeCollapsible: __scopeAccordion || ACCORDION_CONTEXT,
248
+ "aria-disabled": itemContext.open && !collapsibleContext.collapsible || void 0,
249
+ "data-orientation": accordionContext.orientation,
250
+ id: itemContext.triggerId,
251
+ ...triggerProps,
252
+ ref: forwardedRef
253
+ })
254
+ });
329
255
  });
256
+ const AccordionContentFrame = styled(Collapsible.Content, { displayName: "AccordionContent" });
330
257
  const AccordionHeightAnimatorContext = React.createContext(false);
331
- const AccordionContent = AccordionContentFrame.styleable(function AccordionContent2(props, forwardedRef) {
332
- const {
333
- __scopeAccordion,
334
- forceMount,
335
- ...contentProps
336
- } = props;
337
- const accordionContext = useAccordionContext(__scopeAccordion);
338
- const itemContext = useAccordionItemContext(__scopeAccordion);
339
- const heightAnimatorPresent = React.useContext(AccordionHeightAnimatorContext);
340
- return /* @__PURE__ */jsx(AccordionContentFrame, {
341
- role: "region",
342
- "aria-labelledby": itemContext.triggerId,
343
- "data-orientation": accordionContext.orientation,
344
- __scopeCollapsible: __scopeAccordion || ACCORDION_CONTEXT,
345
- ...contentProps,
346
- forceMount: forceMount || heightAnimatorPresent,
347
- ref: forwardedRef
348
- });
258
+ const AccordionContent = createStyledHOC(AccordionContentFrame, function AccordionContent2(props, forwardedRef) {
259
+ const { __scopeAccordion, forceMount, ...contentProps } = props;
260
+ const accordionContext = useAccordionContext(__scopeAccordion);
261
+ const itemContext = useAccordionItemContext(__scopeAccordion);
262
+ const heightAnimatorPresent = React.useContext(AccordionHeightAnimatorContext);
263
+ return /* @__PURE__ */ jsx(AccordionContentFrame, {
264
+ role: "region",
265
+ "aria-labelledby": itemContext.triggerId,
266
+ "data-orientation": accordionContext.orientation,
267
+ __scopeCollapsible: __scopeAccordion || ACCORDION_CONTEXT,
268
+ ...contentProps,
269
+ forceMount: forceMount || heightAnimatorPresent,
270
+ ref: forwardedRef
271
+ });
349
272
  });
350
- const HeightAnimator = View.styleable((props, ref) => {
351
- const itemContext = useAccordionItemContext();
352
- const {
353
- children,
354
- transition,
355
- ...rest
356
- } = props;
357
- const open = !!itemContext.open;
358
- const [fixed, setFixed] = React.useState(!open);
359
- const [pinned, setPinned] = React.useState(false);
360
- const [contentPresent, setContentPresent] = React.useState(open);
361
- const [contentHeight, setContentHeight] = React.useState(0);
362
- const outerRef = React.useRef(null);
363
- const innerRef = React.useRef(null);
364
- const composedRef = useComposedRefs(outerRef, ref);
365
- const lastOuterHeightRef = React.useRef(0);
366
- const pinnedHeightRef = React.useRef(0);
367
- const settleTimerRef = React.useRef(void 0);
368
- const prevOpenRef = React.useRef(open);
369
- if (prevOpenRef.current !== open) {
370
- prevOpenRef.current = open;
371
- clearTimeout(settleTimerRef.current);
372
- if (open) {
373
- setContentPresent(true);
374
- } else if (lastOuterHeightRef.current < 1) {
375
- setContentPresent(false);
376
- }
377
- if (!fixed) {
378
- pinnedHeightRef.current = lastOuterHeightRef.current;
379
- setFixed(true);
380
- setPinned(true);
381
- }
382
- }
383
- useIsomorphicLayoutEffect(() => {
384
- if (!pinned) return;
385
- if (isWeb) {
386
- void outerRef.current?.offsetHeight;
387
- setPinned(false);
388
- return;
389
- }
390
- const frame = requestAnimationFrame(() => setPinned(false));
391
- return () => cancelAnimationFrame(frame);
392
- }, [pinned]);
393
- React.useEffect(() => () => clearTimeout(settleTimerRef.current), []);
394
- useIsomorphicLayoutEffect(() => {
395
- if (!fixed || !open) return;
396
- const el = innerRef.current;
397
- const naturalHeight = el?.getBoundingClientRect?.().height;
398
- if (naturalHeight && naturalHeight > 0 && naturalHeight !== contentHeight) {
399
- setContentHeight(naturalHeight);
400
- }
401
- });
402
- const minHeight = pinned ? pinnedHeightRef.current : open ? contentHeight : 0;
403
- return /* @__PURE__ */jsx(View, {
404
- ref: composedRef,
405
- position: "relative",
406
- ...rest,
407
- transition: fixed ? transition : void 0,
408
- height: "auto",
409
- minHeight,
410
- overflow: "hidden",
411
- onLayout: ({
412
- nativeEvent
413
- }) => {
414
- const outerHeight = nativeEvent.layout.height;
415
- lastOuterHeightRef.current = outerHeight;
416
- if (isWeb && !open && contentPresent && outerHeight < 1) {
417
- setContentPresent(false);
418
- }
419
- clearTimeout(settleTimerRef.current);
420
- if (fixed && !pinned && open && contentHeight > 0) {
421
- if (Math.abs(outerHeight - contentHeight) < 1) {
422
- settleTimerRef.current = setTimeout(() => setFixed(false), 100);
423
- }
424
- }
425
- },
426
- onDidAnimate: () => {
427
- if (!prevOpenRef.current && !pinned) {
428
- setContentPresent(false);
429
- }
430
- },
431
- children: /* @__PURE__ */jsx(View, {
432
- ref: innerRef,
433
- position: fixed ? "absolute" : "relative",
434
- top: fixed ? 0 : void 0,
435
- left: fixed ? 0 : void 0,
436
- right: fixed ? 0 : void 0,
437
- onLayout: ({
438
- nativeEvent
439
- }) => {
440
- const naturalHeight = nativeEvent.layout.height;
441
- if (open && naturalHeight !== contentHeight) {
442
- setContentHeight(naturalHeight);
443
- }
444
- },
445
- children: /* @__PURE__ */jsx(AccordionHeightAnimatorContext.Provider, {
446
- value: contentPresent,
447
- children
448
- })
449
- })
450
- });
273
+ const HeightAnimator = createStyledHOC(View, (props, ref) => {
274
+ const itemContext = useAccordionItemContext();
275
+ const { children, transition, ...rest } = props;
276
+ const open = !!itemContext.open;
277
+ const [fixed, setFixed] = React.useState(!open);
278
+ const [pinned, setPinned] = React.useState(false);
279
+ const [contentPresent, setContentPresent] = React.useState(open);
280
+ const [contentHeight, setContentHeight] = React.useState(0);
281
+ const outerRef = React.useRef(null);
282
+ const innerRef = React.useRef(null);
283
+ const composedRef = useComposedRefs(outerRef, ref);
284
+ const lastOuterHeightRef = React.useRef(0);
285
+ const pinnedHeightRef = React.useRef(0);
286
+ const settleTimerRef = React.useRef(void 0);
287
+ const prevOpenRef = React.useRef(open);
288
+ if (prevOpenRef.current !== open) {
289
+ prevOpenRef.current = open;
290
+ clearTimeout(settleTimerRef.current);
291
+ if (open) {
292
+ setContentPresent(true);
293
+ } else if (lastOuterHeightRef.current < 1) {
294
+ setContentPresent(false);
295
+ }
296
+ if (!fixed) {
297
+ pinnedHeightRef.current = lastOuterHeightRef.current;
298
+ setFixed(true);
299
+ setPinned(true);
300
+ }
301
+ }
302
+ useIsomorphicLayoutEffect(() => {
303
+ if (!pinned) return;
304
+ if (isWeb) {
305
+ void outerRef.current?.offsetHeight;
306
+ setPinned(false);
307
+ return;
308
+ }
309
+ const frame = requestAnimationFrame(() => setPinned(false));
310
+ return () => cancelAnimationFrame(frame);
311
+ }, [pinned]);
312
+ React.useEffect(() => () => clearTimeout(settleTimerRef.current), []);
313
+ useIsomorphicLayoutEffect(() => {
314
+ if (!fixed || !open) return;
315
+ const el = innerRef.current;
316
+ const naturalHeight = el?.getBoundingClientRect?.().height;
317
+ if (naturalHeight && naturalHeight > 0 && naturalHeight !== contentHeight) {
318
+ setContentHeight(naturalHeight);
319
+ }
320
+ });
321
+ const minHeight = pinned ? pinnedHeightRef.current : open ? contentHeight : 0;
322
+ return /* @__PURE__ */ jsx(View, {
323
+ ref: composedRef,
324
+ position: "relative",
325
+ ...rest,
326
+ transition: fixed ? transition : void 0,
327
+ height: "auto",
328
+ minHeight,
329
+ overflow: "hidden",
330
+ onLayout: ({ nativeEvent }) => {
331
+ const outerHeight = nativeEvent.layout.height;
332
+ lastOuterHeightRef.current = outerHeight;
333
+ if (isWeb && !open && contentPresent && outerHeight < 1) {
334
+ setContentPresent(false);
335
+ }
336
+ clearTimeout(settleTimerRef.current);
337
+ if (fixed && !pinned && open && contentHeight > 0) {
338
+ if (Math.abs(outerHeight - contentHeight) < 1) {
339
+ settleTimerRef.current = setTimeout(() => setFixed(false), 100);
340
+ }
341
+ }
342
+ },
343
+ onTransition: (event) => {
344
+ if (event.phase !== "end" || event.cause !== "update" || !event.finished) return;
345
+ if (!prevOpenRef.current && !pinned) {
346
+ setContentPresent(false);
347
+ }
348
+ },
349
+ children: /* @__PURE__ */ jsx(View, {
350
+ ref: innerRef,
351
+ position: fixed ? "absolute" : "relative",
352
+ top: fixed ? 0 : void 0,
353
+ left: fixed ? 0 : void 0,
354
+ right: fixed ? 0 : void 0,
355
+ onLayout: ({ nativeEvent }) => {
356
+ const naturalHeight = nativeEvent.layout.height;
357
+ if (open && naturalHeight !== contentHeight) {
358
+ setContentHeight(naturalHeight);
359
+ }
360
+ },
361
+ children: /* @__PURE__ */ jsx(AccordionHeightAnimatorContext.Provider, {
362
+ value: contentPresent,
363
+ children
364
+ })
365
+ })
366
+ });
451
367
  });
452
368
  function getState(open) {
453
- return open ? "open" : "closed";
369
+ return open ? "open" : "closed";
454
370
  }
455
371
  const Accordion = withStaticProperties(AccordionComponent, {
456
- Trigger: AccordionTrigger,
457
- Header: AccordionHeader,
458
- Content: AccordionContent,
459
- Item: AccordionItem,
460
- HeightAnimator
372
+ Trigger: AccordionTrigger,
373
+ Header: AccordionHeader,
374
+ Content: AccordionContent,
375
+ Item: AccordionItem,
376
+ HeightAnimator
461
377
  });
378
+
462
379
  export { Accordion };
463
380
  //# sourceMappingURL=Accordion.mjs.map