1 | # Vue HeightCollapsible
|
2 |
|
3 | Collapsible component with CSS transition for elements with variable and dynamic height.
|
4 |
|
5 | [![npm version](https://img.shields.io/npm/v/vue-height-collapsible.svg?style=flat-square)](https://www.npmjs.com/package/vue-height-collapsible)
|
6 | [![npm downloads](https://img.shields.io/npm/dm/vue-height-collapsible.svg?style=flat-square)](https://www.npmjs.com/package/vue-height-collapsible)
|
7 | [![gzip](https://img.shields.io/bundlephobia/minzip/vue-height-collapsible.svg)](https://bundlephobia.com/result?p=vue-height-collapsible)
|
8 | [![license](https://img.shields.io/github/license/kunukn/vue-height-collapsible.svg)](https://github.com/kunukn/vue-height-collapsible/blob/master/LICENSE)
|
9 |
|
10 | Vue HeightCollapsible
|
11 |
|
12 | ![logo](logo/collapsible.svg 'logo')
|
13 |
|
14 | ## Demo
|
15 |
|
16 | <img src="demo/height-collapsible.gif"
|
17 | style="max-width: 360px; height: auto;"
|
18 | alt="demo">
|
19 |
|
20 | <table style="border-spacing: 16px;border-collapse: separate;">
|
21 |
|
22 | <tr>
|
23 | <td>Codesandbox simple Vue</td>
|
24 | <td><img width="24" height="24" src="ui-library-logo/Vue-logo.svg"/></td>
|
25 | <td><a href="https://ooisb.csb.app/" target="_blank" rel="noopener noreferrer">View</a></td>
|
26 | <td><a href="https://codesandbox.io/s/vue-height-collapsible-demo-ooisb" target="_blank" rel="noopener noreferrer">Edit</a></td>
|
27 | </tr>
|
28 |
|
29 | <tr>
|
30 | <td>Codesandbox multiple Vue</td>
|
31 | <td><img width="24" height="24" src="ui-library-logo/Vue-logo.svg"/></td>
|
32 | <td><a href="https://y2jjy.csb.app" target="_blank" rel="noopener noreferrer">View</a></td>
|
33 | <td><a href="https://codesandbox.io/s/vue-height-collapsible-demo-y2jjy" target="_blank" rel="noopener noreferrer">Edit</a></td>
|
34 | </tr>
|
35 |
|
36 | <tr>
|
37 | <td>Codesandbox simple Vue 3</td>
|
38 | <td><img width="24" height="24" src="ui-library-logo/Vue-logo.svg"/></td>
|
39 | <td><a href="https://io8x6.csb.app" target="_blank" rel="noopener noreferrer">View</a></td>
|
40 | <td><a href="https://codesandbox.io/s/vue-3-height-collapsible-demo-io8x6" target="_blank" rel="noopener noreferrer">Edit</a></td>
|
41 | </tr>
|
42 |
|
43 | </table>
|
44 |
|
45 | # CSS required
|
46 |
|
47 | :warning: ️You need to add style (transition) in your own stylesheet to add animation. Here is an example.
|
48 |
|
49 | ```html
|
50 | <style>
|
51 | [data-height-collapsible] {
|
52 | transition: height 280ms cubic-bezier(0.4, 0, 0.2, 1);
|
53 | }
|
54 | </style>
|
55 | ```
|
56 |
|
57 | Alternatively you can add it using the `transition` prop.
|
58 |
|
59 | ## Supported versions
|
60 |
|
61 | ### Vue 2 and Vue 3
|
62 |
|
63 | ```bash
|
64 | npm install vue-height-collapsible
|
65 | // or yarn install vue-height-collapsible
|
66 | ```
|
67 |
|
68 | ### Vue 2
|
69 |
|
70 | ```vue
|
71 | import HeightCollapsible from "vue-height-collapsible";
|
72 | ```
|
73 |
|
74 | ### Vue 3
|
75 |
|
76 | ```vue
|
77 | import HeightCollapsible from "vue-height-collapsible/vue3";
|
78 | ```
|
79 |
|
80 | ### Alternative approach
|
81 |
|
82 | The source file could be copied. It is only this file.<br/>
|
83 | `vue-height-collapsible.vue`
|
84 |
|
85 | ## Usage example
|
86 |
|
87 | ### Simple
|
88 |
|
89 | ```vue
|
90 | <template>
|
91 | <div class="my-component">
|
92 | <button @click="isOpen = !isOpen">Toggle</button>
|
93 | <HeightCollapsible :isOpen="isOpen">
|
94 | <p>Paragraph of text.</p>
|
95 | <p>Another paragraph is also OK.</p>
|
96 | </HeightCollapsible>
|
97 | </div>
|
98 | </template>
|
99 |
|
100 | <script>
|
101 | import HeightCollapsible from 'vue-height-collapsible'
|
102 |
|
103 | export default {
|
104 | name: 'MyComponent',
|
105 | components: {
|
106 | HeightCollapsible,
|
107 | },
|
108 | data() {
|
109 | return {
|
110 | isOpen: true,
|
111 | }
|
112 | },
|
113 | }
|
114 | </script>
|
115 | ```
|
116 |
|
117 | ### Using Aria and scoped slots
|
118 |
|
119 | ```vue
|
120 | <template>
|
121 | <div class="my-component">
|
122 | <button
|
123 | @click="isOpen = !isOpen"
|
124 | :aria-expanded="isOpen"
|
125 | aria-controls="my-collapsible-1"
|
126 | >
|
127 | <span>Toggle {{ collapseState }}</span>
|
128 | </button>
|
129 | <HeightCollapsible
|
130 | :isOpen="isOpen"
|
131 | @update="onUpdate"
|
132 | v-slot="{ state }"
|
133 | id="my-collapsible-1"
|
134 | >
|
135 | <p>I know the collapse state: {{ state }}</p>
|
136 | <p>Paragraph of text.</p>
|
137 | <p>Another paragraph is also OK.</p>
|
138 | <p>Images and any other content are ok too.</p>
|
139 | </HeightCollapsible>
|
140 | </div>
|
141 | </template>
|
142 |
|
143 | <script>
|
144 | import HeightCollapsible from 'vue-height-collapsible'
|
145 |
|
146 | export default {
|
147 | name: 'MyComponent',
|
148 | components: {
|
149 | HeightCollapsible,
|
150 | },
|
151 | data() {
|
152 | return {
|
153 | isOpen: true,
|
154 | collapseState: '',
|
155 | }
|
156 | },
|
157 | methods: {
|
158 | onUpdate({ state }) {
|
159 | this.collapseState = state
|
160 | },
|
161 | },
|
162 | }
|
163 | </script>
|
164 | ```
|
165 |
|
166 | ## Properties
|
167 |
|
168 | | Prop | Type | Default |
|
169 | | ------------------ | ------- | ------- |
|
170 | | isOpen | boolean | true |
|
171 | | transition | string | |
|
172 | | tag | string | div |
|
173 | | overflowOnExpanded | boolean | false |
|
174 |
|
175 | <br/>
|
176 |
|
177 | #### `isOpen` : boolean
|
178 |
|
179 | Expands or collapses content.
|
180 |
|
181 | #### `transition` : string
|
182 |
|
183 | You can also specify a CSS transition inline by using the `transition` prop.
|
184 |
|
185 | ```html
|
186 | <HeightCollapsible transition="height 300ms ease-in-out" :isOpen="isOpen">
|
187 | <p>Paragraph of text</p>
|
188 | </HeightCollapsible>
|
189 | ```
|
190 |
|
191 | #### `tag` : string
|
192 |
|
193 | You can specify the HTML element type for the collapse component. By default the element type is a `div` element.
|
194 |
|
195 | ```html
|
196 | <HeightCollapsible tag="article" :isOpen="isOpen">
|
197 | <p>Paragraph of text</p>
|
198 | </HeightCollapsible>
|
199 | ```
|
200 |
|
201 | #### `overflowOnExpanded` : boolean
|
202 |
|
203 | If added, then `overflow: hidden` inline style will not be added when the state is `expanded`.
|
204 |
|
205 | <br>
|
206 |
|
207 | # npm
|
208 |
|
209 | https://www.npmjs.com/package/vue-height-collapsible
|
210 |
|
211 | # CDN files
|
212 |
|
213 | To see all the available CDN files go to
|
214 | https://unpkg.com/browse/vue-height-collapsible/
|
215 |
|
216 | # Design goals
|
217 |
|
218 | - let the browser handle the animation using CSS height transition
|
219 | - minimal in file size
|
220 | - minimalistic API - only have a Collapsible component which updates on isOpen props
|
221 | - flexible - provide your own markup, styling and easing
|
222 | - interruptible - can be reversed during movement
|
223 | - inert - when collapsed you should tab over the collapsed component
|
224 | - availability - from cdn or npm install, commonjs, minified or ES module
|
225 | - collapsible on height only
|
226 |
|
227 | # This was created with heavy inspiration from
|
228 |
|
229 | https://github.com/kunukn/react-collapse
|
230 |
|
231 | # Development
|
232 |
|
233 | The compiler in this repository works for Vue 2 version.<br/>
|
234 | This library was created using https://github.com/team-innovation/vue-sfc-rollup
|