Skip to content

Commit 4ce2dae

Browse files
committed
update readme
1 parent 0f6345c commit 4ce2dae

File tree

1 file changed

+64
-40
lines changed

1 file changed

+64
-40
lines changed

README.md

Lines changed: 64 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ var split = Split(<HTMLElement|selector[]> elements, <options> options?)
8181
| Options | Type | Default | Description |
8282
|---|---|---|---|
8383
| `sizes` | Array | | Initial sizes of each element in percents or CSS values. |
84-
| `minSize` | Number or Array | 100 | Minimum size of each element. |
85-
| `gutterSize` | Number | 10 | Gutter size in pixels. |
86-
| `snapOffset` | Number | 30 | Snap to minimum size offset in pixels. |
87-
| `direction` | String | 'horizontal' | Direction to split: horizontal or vertical. |
88-
| `cursor` | String | 'col-resize' | Cursor to display while dragging. |
84+
| `minSize` | Number or Array | `100` | Minimum size of each element. |
85+
| `expandToMin` | Boolean | `false` | Grow initial sizes to `minSize` |
86+
| `gutterSize` | Number | `10` | Gutter size in pixels. |
87+
| `snapOffset` | Number | `30` | Snap to minimum size offset in pixels. |
88+
| `direction` | String | `'horizontal'` | Direction to split: horizontal or vertical. |
89+
| `cursor` | String | `'col-resize'` | Cursor to display while dragging. |
8990
| `gutter` | Function | | Called to create each gutter element |
9091
| `elementStyle` | Function | | Called to set the style of each element. |
9192
| `gutterStyle` | Function | | Called to set the style of the gutter. |
@@ -106,17 +107,17 @@ __THIS IS THE #1 QUESTION ABOUT THE LIBRARY__.
106107
107108
#### sizes
108109
109-
An array of initial sizes of the elements, specified as percentage values. Example: Setting the initial sizes to 25% and 75%.
110+
An array of initial sizes of the elements, specified as percentage values. Example: Setting the initial sizes to `25%` and `75%`.
110111
111112
```js
112113
Split(['#one', '#two'], {
113114
sizes: [25, 75]
114115
})
115116
```
116117
117-
#### minSize. Default: 100
118+
#### minSize. Default: `100`
118119
119-
An array of minimum sizes of the elements, specified as pixel values. Example: Setting the minimum sizes to 100px and 300px, respectively.
120+
An array of minimum sizes of the elements, specified as pixel values. Example: Setting the minimum sizes to `100px` and `300px`, respectively.
120121
121122
```js
122123
Split(['#one', '#two'], {
@@ -132,39 +133,53 @@ Split(['#one', '#two'], {
132133
})
133134
```
134135
135-
#### gutterSize. Default: 10
136+
#### expandToMin. Default: `false`
136137
137-
Gutter size in pixels. Example: Setting the gutter size to 20px.
138+
When the split is created, if `expandToMin` is `true`, the minSize for each element overrides the percentage value from the `sizes` option.
139+
Example: The first element (`#one`) is set to 25% width of the parent container. However, it's `minSize` is `300px`. Using `expandToMin: true` means that
140+
the first element will always load at at least `300px`, even if `25%` were smaller.
141+
142+
```js
143+
Split(['#one', '#two'], {
144+
sizes: [25, 75],
145+
minSize: [300, 100],
146+
expanedToMin: true,
147+
})
148+
```
149+
150+
#### gutterSize. Default: `10`
151+
152+
Gutter size in pixels. Example: Setting the gutter size to `20px`.
138153
139154
```js
140155
Split(['#one', '#two'], {
141156
gutterSize: 20
142157
})
143158
```
144159
145-
#### snapOffset. Default: 30
160+
#### snapOffset. Default: `30`
146161
147-
Snap to minimum size at this offset in pixels. Example: Set to 0 to disable to snap effect.
162+
Snap to minimum size at this offset in pixels. Example: Set to `0` to disable to snap effect.
148163
149164
```js
150165
Split(['#one', '#two'], {
151166
snapOffset: 0
152167
})
153168
```
154169
155-
#### direction. Default: 'horizontal'
170+
#### direction. Default: `'horizontal'`
156171
157-
Direction to split in. Can be 'vertical' or 'horizontal'. Determines which CSS properties are applied (ie. width/height) to each element and gutter. Example: split vertically:
172+
Direction to split in. Can be `'vertical'` or `'horizontal'`. Determines which CSS properties are applied (ie. width/height) to each element and gutter. Example: split vertically:
158173
159174
```js
160175
Split(['#one', '#two'], {
161176
direction: 'vertical'
162177
})
163178
```
164179
165-
#### cursor. Default: 'col-resize'
180+
#### cursor. Default: `'col-resize'`
166181
167-
Cursor to show on the gutter (also applied to the two adjacent elements when dragging to prevent flickering). Defaults to 'col-resize', so should be switched to 'row-resize' when using direction: 'vertical':
182+
Cursor to show on the gutter (also applied to the two adjacent elements when dragging to prevent flickering). Defaults to `'col-resize'`, so should be switched to `'row-resize'` when using `direction: 'vertical'`:
168183
169184
```js
170185
Split(['#one', '#two'], {
@@ -178,7 +193,7 @@ Split(['#one', '#two'], {
178193
Optional function called to create each gutter element. The signature looks like this:
179194
180195
```js
181-
(index, direction) => HTMLElement
196+
(index, direction, pairElement) => HTMLElement
182197
```
183198
184199
Defaults to creating a `div` with `class="gutter gutter-horizontal"` or `class="gutter gutter-vertical"`, depending on the direction. The default gutter function looks like this:
@@ -193,6 +208,15 @@ Defaults to creating a `div` with `class="gutter gutter-horizontal"` or `class="
193208
194209
The returned element is then inserted into the DOM, and it's width or height are set. This option can be used to clone an existing DOM element, or to create a new element with custom styles.
195210
211+
Returning a falsey value like `null` or `false` will not insert a gutter. This behavior was added in v1.4.1.
212+
An additional argument, `pairElement`, is passed to the gutter function: this is the DOM element after (to the right or below) the gutter. This argument was added in v1.4.1.
213+
214+
This final argument makes it easy to return the gutter that has already been created, for example, if `split.destroy()` was called with the option to preserve the gutters.
215+
216+
```js
217+
(index, direction, pairElement) => pairElement.previousSibling
218+
```
219+
196220
#### elementStyle
197221
198222
Optional function called setting the CSS style of the elements. The signature looks like this:
@@ -201,7 +225,7 @@ Optional function called setting the CSS style of the elements. The signature lo
201225
(dimension, elementSize, gutterSize) => Object
202226
```
203227
204-
Dimension will be a string, 'width' or 'height', and can be used in the return style. elementSize is the target percentage value of the element, and gutterSize is the target pixel value of the gutter.
228+
Dimension will be a string, `'width'` or `'height'`, and can be used in the return style. `elementSize` is the target percentage value of the element, and `gutterSize` is the target pixel value of the gutter.
205229
206230
It should return an object with CSS properties to apply to the element. For horizontal splits, the return object looks like this:
207231
@@ -235,7 +259,7 @@ Optional function called when setting the CSS style of the gutters. The signatur
235259
(dimension, gutterSize) => Object
236260
```
237261
238-
Dimension is a string, either 'width' or 'height', and gutterSize is a pixel value representing the width of the gutter.
262+
Dimension is a string, either `'width'` or `'height'`, and `gutterSize` is a pixel value representing the width of the gutter.
239263
240264
It should return a similar object as `elementStyle`, an object with CSS properties to apply to the gutter. Since gutters have fixed widths, it will generally look like this:
241265
@@ -245,12 +269,20 @@ It should return a similar object as `elementStyle`, an object with CSS properti
245269
}
246270
```
247271
248-
Both `elementStyle` and `gutterStyle` are called continously while dragging, so don't do anything besides return the style object in these functions.
272+
Both `elementStyle` and `gutterStyle` are called continously while dragging, so don't do anything besides return the style object in these functions. Both of these functions should be *pure*, returning the same values for the same inputs and not modifying any external state.
249273
250274
#### onDrag, onDragStart, onDragEnd
251275
252276
Callbacks that can be added on drag (fired continously), drag start and drag end. If doing more than basic operations in `onDrag`, add a debounce function to rate limit the callback.
253277
278+
`onDragStart` and `onDragEnd` are passed the initial and final sizes of the split since it's a common pattern to access the sizes this way.
279+
280+
Their function signature looks like this, where `sizes` is an array of percentage values like returned by `getSizes()`:
281+
282+
```js
283+
(sizes) => {}
284+
```
285+
254286
## Usage Examples
255287
256288
Reference HTML for examples. Gutters are inserted automatically:
@@ -263,7 +295,7 @@ Reference HTML for examples. Gutters are inserted automatically:
263295
</div>
264296
```
265297
266-
A split with two elements, starting at 25% and 75% wide with 200px minimum width.
298+
A split with two elements, starting at `25%` and `75%` wide, with `200px` minimum width.
267299
268300
```js
269301
Split(['#one', '#two'], {
@@ -272,7 +304,7 @@ Split(['#one', '#two'], {
272304
});
273305
```
274306
275-
A split with three elements, starting with even widths with 100px, 100px and 300px minimum widths, respectively.
307+
A split with three elements, starting with even (default) widths and minimum widths set to `100px`, `100px` and `300px`, respectively.
276308
277309
```js
278310
Split(['#one', '#two', '#three'], {
@@ -288,16 +320,6 @@ Split(['#one', '#two'], {
288320
});
289321
```
290322
291-
Specifying the initial widths with CSS values. Not recommended, the size/gutter calculations would have to be done before hand and won't scale on viewport resize.
292-
293-
```js
294-
Split(['#one', '#two'], {
295-
sizes: ['200px', '500px']
296-
});
297-
```
298-
299-
JSFiddle style is also possible: [Demo](http://nathancahill.github.io/Split.js/examples/jsfiddle.html).
300-
301323
## Saving State
302324
303325
Use local storage to save the most recent state:
@@ -313,15 +335,16 @@ if (sizes) {
313335

314336
var split = Split(['#one', '#two'], {
315337
sizes: sizes,
316-
onDragEnd: function () {
317-
localStorage.setItem('split-sizes', JSON.stringify(split.getSizes()));
338+
onDragEnd: function (sizes) {
339+
localStorage.setItem('split-sizes', JSON.stringify(sizes));
318340
}
319341
})
320342
```
321343
322-
## Flexbox
344+
## Flex Layout
323345
324-
Flexbox layout is supported by customizing the `elementStyle` and `gutterStyle` CSS. Given a layout like this:
346+
Flex layout is supported easily by adding a `display: flex` to the parent element. The `width` or `height` CSS values
347+
assigned by default by Split.js work well with flex.
325348
326349
```html
327350
<div id="flex">
@@ -339,7 +362,7 @@ And CSS style like this:
339362
}
340363
```
341364
342-
Then the `elementStyle` and `gutterStyle` can be used to set flex-basis:
365+
For more complicated flex layouts, the `elementStyle` and `gutterStyle` can be used to set flex-basis:
343366
344367
```js
345368
Split(['#flex-1', '#flex-2'], {
@@ -383,16 +406,17 @@ instance.getSizes()
383406
384407
#### .collapse(index)
385408
386-
collapse changes the size of element at `index` to 0. Every element except the last is collapsed towards the front (left or top). The last is collapsed towards the back. Not supported in IE8. Added in v1.1.0:
409+
collapse changes the size of element at `index` to it's `minSize`. Every element except the last is collapsed towards the front (left or top). The last is collapsed towards the back. Not supported in IE8. Added in v1.1.0:
387410
388411
```
389412
instance.collapse(0)
390413
```
391414
392-
#### .destroy(preserve? = false)
415+
#### .destroy(preserveStyles? = false, preserveGutters? = false)
393416
394417
Destroy the instance. It removes the gutter elements, and the size CSS styles Split.js set. Added in v1.1.1.
395-
Passing `preserve = true` does not remove the CSS styles. Option added in v1.4.0.
418+
Passing `preserveStyles = true` does not remove the CSS styles. Option added in v1.4.0.
419+
Passing `preserveGutters = true` does not remove the gutter elements. Option added in v1.4.1.
396420
397421
```
398422
instance.destroy()

0 commit comments

Comments
 (0)