Skip to content

Commit b367528

Browse files
fix: dynamic accordions now get proper index
1 parent 1ed747b commit b367528

File tree

3 files changed

+2219
-1654
lines changed

3 files changed

+2219
-1654
lines changed

apps/website/src/routes/docs/headless/modal/index.mdx

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ Unfortunately, to create modals that open on page load in the web, it needs to b
259259

260260
## API
261261

262+
### Modal.Root
263+
262264
<APITable
263265
propDescriptors={[
264266
{
@@ -273,20 +275,13 @@ Unfortunately, to create modals that open on page load in the web, it needs to b
273275
description: 'A way to tell the modal to not hide when the ::backdrop is clicked.',
274276
},
275277
{
276-
name: '<dialog>-Attributes',
277-
type: `Qwik`,
278-
description:
279-
'A way to configure the modal with all native attributes the HTMLDialog defines.',
280-
info: `PropsOf<'dialog'>`,
281-
},
282-
{
283-
name: 'onClose()$',
278+
name: 'onClose$',
284279
type: 'function',
285280
info: '() => void',
286281
description: 'An event hook that gets notified whenever the modal gets closed.',
287282
},
288283
{
289-
name: 'onShow()$',
284+
name: 'onShow$',
290285
type: 'function',
291286
info: '() => void',
292287
description: 'An event hook that gets notified whenever the modal shows up.',

packages/kit-headless/src/components/accordion/accordion-item.tsx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
PropsOf,
33
Slot,
44
component$,
5+
useComputed$,
56
useContext,
67
useContextProvider,
78
useId,
@@ -26,22 +27,26 @@ export const HAccordionItem = component$(
2627
const context = useContext(accordionContextId);
2728
const localId = useId();
2829
const itemId = id ?? localId + '-item';
29-
const localIndexSig = useSignal<number>(_index!);
30+
31+
// gets the index dynamically
32+
const localIndexSig = useComputed$(() => {
33+
return _index ?? -1;
34+
});
35+
3036
const isOpenSig = useSignal<boolean>(context.initialIndex === localIndexSig.value);
3137
const initialLoadSig = useSignal<boolean>(true);
3238
const triggerRef = useSignal<HTMLButtonElement>(null as unknown as HTMLButtonElement);
3339

3440
useTask$(function internalState({ track }) {
3541
track(() => context.selectedIndexSig.value);
3642

37-
// collect trigger refs for keyboard navigation
38-
context.triggerRefsArray.value[localIndexSig.value] = triggerRef;
39-
4043
if (context.multiple || isServer) return;
4144

4245
if (context.selectedIndexSig.value !== localIndexSig.value) {
4346
isOpenSig.value = false;
4447
} else {
48+
// never executes when first item is opened, then new item added, then new first item opened
49+
4550
// onChange$ behavior - called once when the value changes
4651
if (!initialLoadSig.value && value && context.onChange$) {
4752
context.onChange$(value);
@@ -67,6 +72,13 @@ export const HAccordionItem = component$(
6772
}
6873
});
6974

75+
useTask$(({ track }) => {
76+
track(() => localIndexSig.value);
77+
78+
// collect trigger refs for keyboard navigation
79+
context.triggerRefsArray.value[localIndexSig.value] = triggerRef;
80+
});
81+
7082
useTask$(() => {
7183
initialLoadSig.value = false;
7284
});

0 commit comments

Comments
 (0)