You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: apps/website/src/routes/docs/headless/collapsible/index.mdx
+102-9Lines changed: 102 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,7 @@ An interactive component which expands/collapses a panel.
19
19
'Full keyboard navigation',
20
20
'Controlled or uncontrolled',
21
21
'Initial open state does not wake up the component',
22
-
'Automatic animation detection',
22
+
'Automatic entry/exit animation detection',
23
23
'Executes on interaction or programmatically',
24
24
]}
25
25
/>
@@ -66,28 +66,121 @@ As much as we love the native elements, they come with a couple of problems:
66
66
67
67
> To read more about the reasons for implementing an ARIA disclosure widget, check out [Scott O'Hara's article](https://www.scottohara.me/blog/2022/09/12/details-summary.html), as well as the [interactive elements](https://html.spec.whatwg.org/multipage/interactive-elements.html#interactive-elements) section of the HTML spec.
68
68
69
+
## Component State
70
+
71
+
### Uncontrolled / Initial value
72
+
73
+
We can select an initial uncontrolled value by passing the `open` prop to the `<Collapsible />` component.
74
+
75
+
<Showcasename="open" />
76
+
77
+
The above example expands the collapsible by default. Something to notice, is there isn't any layout shift when refreshing the page.
78
+
79
+
This is because the content is rendered on the server. Animations applied to `data-open` take effect after the initial render to prevent layout shift.
80
+
81
+
> Thanks to Qwik's JavaScript Streaming behavior, we can have open state components without even resuming them!
82
+
83
+
### Controlled / Reactive value
84
+
85
+
We can pass reactive state by using the bind:open prop to the `<Collapsible />` component.
86
+
87
+
<Showcasename="programmatic" />
88
+
89
+
bind:open is a signal prop, it allows us to programmatically control the expanded state of the collapsible.
90
+
91
+
### Handling open / close
92
+
93
+
We may want to handle the open / close of the collapsible. For example, we may want execute some code when the collapsible is opened or closed.
94
+
95
+
<Showcasename="open-change" />
96
+
97
+
To do that, we can use the onOpenChange$ prop. A parameter is passed to the handler, which is a boolean indicating whether the collapsible is open or closed.
98
+
99
+
### Disabled collapsible
100
+
101
+
<Showcasename="disabled" />
102
+
103
+
The collapsible can be disabled by adding the `disabled` prop to the `<Collapsible />` component.
104
+
69
105
## Animating the content
70
106
107
+
To animate the height of the content, we can use a keyframe animation on the height property.
108
+
71
109
<Showcasename="animation" />
72
110
111
+
### Height animation
112
+
113
+
By default, the `--qwikui-collapsible-content-height` CSS variable will automatically be set to the height of the content.
114
+
115
+
<CodeSnippetname="animation.css" />
116
+
73
117
### Why does padding or border break the animation?
74
118
75
-
Padding or border applied to `CollapsibleContent` breaks height animations. This is because the content height has changed.
119
+
Padding or border applied to `CollapsibleContent` breaks our keyframe animation above. This is because the content height has changed.
76
120
77
121
To fix this, add a child element to the content, and set the padding or border on that element.
78
122
123
+
<CodeSnippetname="content-child" />
124
+
79
125
> Rather than dealing with this under the hood, we thought it'd be appropriate to keep style management as simple as possible. Let us know if you have a better solution!
80
126
81
-
## Component State
127
+
## Example CSS
82
128
83
-
### Uncontrolled / Initial value
129
+
<CodeSnippetname="collapsible.css" />
84
130
85
-
<Showcasename="open" />
131
+
Every code example uses the following CSS:
86
132
87
-
### Controlled / Reactive value
133
+
Some CSS variables are specific to the docs, feel free to plug in your own values or variables!
88
134
89
-
<Showcasename="programmatic" />
135
+
## API
90
136
91
-
### Handling open / close
137
+
### Data Attributes
92
138
93
-
<Showcasename="open-change" />
139
+
`Collapsible`, `CollapsibleTrigger`, and `CollapsibleContent` all have the following data attributes that are used to track state:
140
+
141
+
<AnatomyTable
142
+
firstColumnLabel="Attribute"
143
+
propDescriptors={[
144
+
{
145
+
name: 'data-open',
146
+
description: 'If the collapsible is open (Boolean).',
147
+
},
148
+
{
149
+
name: 'data-closed',
150
+
description: 'If the collapsible is closed (Boolean).',
151
+
},
152
+
{
153
+
name: 'data-disabled',
154
+
description: 'If the collapsible is disabled (Boolean).',
0 commit comments