# Split Grid [](https://circleci.com/gh/nathancahill/split)  [](https://unpkg.com/split-grid/dist/split-grid.min.js) The spiritual successor of [Split.js](https://github.com/nathancahill/split/tree/master/packages/splitjs), built for CSS Grid. - __Zero Deps__ - __Tiny:__ Weights 2kb gzipped. - __Fast:__ No overhead or attached window event listeners, uses pure CSS for resizing. - __Unopinionated:__ Only modifies `grid-template-*` rules, the rest of the layout is up to you. ## Table of Contents - [Installation](#installation) - [Example](#example) - [Reference](#reference) - [API](#api) - [Migrating from Split.js](#migrating-from-splitjs) - [License](#license) ## Installation Yarn: ``` $ yarn add split-grid ``` npm: ``` $ npm install --save split-grid ``` Include with a module bundler like [rollup](http://rollupjs.org/) or [webpack](https://webpack.github.io/): ```js // using ES6 modules import Split from 'split-grid' // using CommonJS modules var Split = require('split-grid') ``` The [UMD](https://github.com/umdjs/umd) build is also available on [unpkg](http://unpkg.com/): ```html ``` You can find the library on `window.Split`. ## Example ```js import Split from 'split-grid' Split({ columnGutters: [{ track: 1, element: document.querySelector('.column-1'), }, { track: 3, element: document.querySelector('.column-3'), }], rowGutters: [{ track: 1, element: document.querySelector('.row-1'), }] }) ``` ## Reference ```js Split(options: Options) ``` ### Supported CSS values Current CSS values that are supported in `grid-template, grid-template-columns, grid-template-rows`: - [x] `fr` - [x] `px` - [x] `%` - [x] `repeat` Not supported (yet): - [ ] `auto` - [ ] other CSS values (`em, vmin, cm, etc`) ### Options Most of the options can be specified as `option`, `columnOption` and `rowOption`. This allows default option values to be set for both, or specified individually for each axis. ##### `columnGutters: [{ element: HTMLElement, track: number }]` An array of objects, with `element` and `track` keys. `element` is the element in the grid to enable as a draggable gutter. `track` is the grid track the gutter element is positioned on. These must match. ##### `rowGutters: [{ element: HTMLElement, track: number }]` An array of objects, with `element` and `track` keys. `element` is the element in the grid to enable as a draggable gutter. `track` is the grid track the gutter element is positioned on. These must match. ##### `minSize: number` The minimum size in pixels for all tracks. Default: `0` ##### `maxSize: number` The maximum size in pixels for all tracks. Default: `Infinity` ##### `columnMinSize: number` The minimum size in pixels for all tracks. Default: `options.minSize` ##### `rowMinSize: number` The minimum size in pixels for all tracks. Default: `options.minSize` ##### `columnMaxSize: number` The maximum size in pixels for all tracks. Default: `options.maxSize` ##### `rowMaxSize: number` The maximum size in pixels for all tracks. Default: `options.maxSize` ##### `columnMinSizes: { [track: number]: number }` An object keyed by `track` index, with values set to the minimum size in pixels for the track at that index. Allows individual minSizes to be specified by track. Note this option is plural with an `s`, while the two fallback options are singular. Default: `options.columnMinSize` ##### `rowMinSizes: { [track: number]: number }` An object keyed by `track` index, with values set to the minimum size in pixels for the track at that index. Allows individual minSizes to be specified by track. Note this option is plural with an `s`, while the two fallback options are singular. Default: `options.rowMinSize` ##### `columnMaxSizes: { [track: number]: number }` An object keyed by `track` index, with values set to the maximum size in pixels for the track at that index. Allows individual maxSizes to be specified by track. Note this option is plural with an `s`, while the two fallback options are singular. Default: `options.columnMaxSize` ##### `rowMaxSizes: { [track: number]: number }` An object keyed by `track` index, with values set to the maximum size in pixels for the track at that index. Allows individual maxSizes to be specified by track. Note this option is plural with an `s`, while the two fallback options are singular. Default: `options.rowMaxSize` ##### `snapOffset: number` Snap to minimum size at this offset in pixels. Set to `0` to disable snap. Default: `30` ##### `columnSnapOffset: number` Snap to minimum size at this offset in pixels. Set to `0` to disable snap. Default: `options.snapOffset` ##### `rowSnapOffset: number` Snap to minimum size at this offset in pixels. Set to `0` to disable snap. Default: `options.snapOffset` ##### `dragInterval: number` Drag this number of pixels at a time. Defaults to `1` for smooth dragging, but can be set to a pixel value to give more control over the resulting sizes. Default: `1` ##### `columnDragInterval: number` Drag this number of pixels at a time. Defaults to `1` for smooth dragging, but can be set to a pixel value to give more control over the resulting sizes. Default: `options.dragInterval` ##### `rowDragInterval: number` Drag this number of pixels at a time. Defaults to `1` for smooth dragging, but can be set to a pixel value to give more control over the resulting sizes. Default: `options.dragInterval` ##### `cursor: string` Cursor to show while dragging. Defaults to `'col-resize'` for column gutters and `'row-resize'` for row gutters. ##### `columnCursor: string` Cursor to show while dragging. Default: `'col-resize'` ##### `rowCursor: string` Cursor to show while dragging. Default: `'row-resize'` ##### `onDrag: (direction: 'row' | 'column', track: number, gridTemplateStyle: string) => void` Called continously on drag. For process intensive code, add a debounce function to rate limit this callback. `gridTemplateStyle` is the computed CSS value for `grid-template-column` or `grid-template-row`, depending on `direction`. ##### `onDragStart: (direction: 'row' | 'column', track: number) => void` Called on drag start. ##### `onDragEnd: (direction: 'row' | 'column', track: number) => void` Called on drag end. ##### `writeStyle: (grid: HTMLElement, gridTemplateProp: 'grid-template-column' | 'grid-template-row', gridTemplateStyle: string) => void` Called to update the CSS properties of the grid element. Must eventually apply the CSS value to the CSS prop, or the grid will not change. `gridTemplateStyle` is the computed CSS value of CSS rule `gridTemplateProp`. Default: ```js writeStyle: (grid, gridTemplateProp, gridTemplateStyle) => { grid.style[gridTemplateProp] = gridTemplateStyle } ``` ##### `gridTemplateColumns` `gridTemplateRows` Helper options for determining initial CSS values for `grid-template-columns` and `grid-template-rows`. Most of the time this option is not needed, as Split Grid reads the CSS rules applied to the grid element, but security settings may prevent that, for example, when the CSS is served from a 3rd-party domain. This is ONLY NEEDED if the default method of reading the CSS values errors. This option does not immediately apply CSS rules, it's only used on drag. ## API ```js const split = Split(options: Options) ``` Split Grid returns an instance with a couple of functions. The instance is returned on creation. ##### `split.addColumnGutter(element: HTMLElement, track: number)` Adds a draggable row gutter. The element must be a direct descendant of the element with grid layout, and positioned in the specified track. ```js const grid = document.querySelector('.grid') const gutter = document.createElement('div') grid.appendChild(gutter) // append to DOM split.addColumnGutter(gutter, 1) // add to Split Grid ``` ##### `split.addRowGutter(element: HTMLElement, track: number)` Adds a draggable row gutter. The element must be a direct descendant of the element with grid layout, and positioned in the specified track. ```js const grid = document.querySelector('.grid') const gutter = document.createElement('div') grid.appendChild(gutter) // append to DOM split.addRowGutter(gutter, 1) // add to Split Grid ``` ##### `split.removeColumnGutter(track: number, immediate?: true)` Removes event listeners from a column gutter by track number. If `immediate = false` is passed, event handlers are removed after dragging ends. If a gutter isn't currently being dragged, it's event handlers are removed immediately. ##### `split.removeRowGutter(track: number, immediate?: true)` Removes event listeners from a row gutter by track number. If `immediate = false` is passed, event handlers are removed after dragging ends. If a gutter isn't currently being dragged, it's event handlers are removed immediately. ##### `split.destroy(immediate?: true)` Destroy the instance by removing the attached event listeners. If `immediate = false` is passed, the instance is destroyed after dragging ends. If a gutter isn't currently being dragged, it's destroyed immediately. ## Migrating from Split.js #### Bring your own gutters In Split.js, gutter elements were created by Split.js and inserted in to the DOM. This is not the case in Split Grid. Create the gutter elements in the HTML as children of the grid element, and lay them out in the tracks like any other element. Pass the gutter elements in the options with their track index. __Split.js__ ```js Split(options) // gutters created implicitly ``` __Split Grid__ Gutters are part of the grid layout: ```html