1 | # elle
|
2 |
|
3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges)
|
4 |
|
5 | A tiny DOM utility that wraps a few common features in a chainable structure. Targets modern browsers.
|
6 |
|
7 | - construct from string template (see [domify](https://www.npmjs.com/package/domify))
|
8 | - set CSS styling (see [dom-css](https://www.npmjs.com/package/dom-css))
|
9 | - insertion/removal (see [insert](https://www.npmjs.com/package/insert))
|
10 | - add/remove classes (see [dom-classes](https://www.npmjs.com/package/dom-classes))
|
11 | - aliases for `innerHTML`, `setAttribute/getAttribute` and `parentNode`
|
12 |
|
13 | Other features are left up to the user to implement or extend (see [examples/](examples/)).
|
14 |
|
15 | The main purpose is to provide a thin layer for other components/frameworks to build on. This is better suited for prototyping and higher-level frameworks, rather than small and focused modules.
|
16 |
|
17 | ## example
|
18 |
|
19 | Basic example:
|
20 |
|
21 | ```js
|
22 | require('elle')(document.body)
|
23 | .css({
|
24 | background: 'black',
|
25 | margin: 20, //auto-px'd
|
26 | fontSmoothing: 'antialiased' //vendor-prefixed
|
27 | })
|
28 | .append('<div><a href="#">click</a></div>')
|
29 | ```
|
30 |
|
31 | Another example:
|
32 |
|
33 | ```js
|
34 | //style and append text content to the body
|
35 | var body = elle(document.body)
|
36 | .css('margin', 20)
|
37 | .append('some, ', 'text')
|
38 |
|
39 | //creates a bare <div> with some styles
|
40 | //then attach it to the body
|
41 | elle()
|
42 | .addClass('foobar')
|
43 | .css({
|
44 | width: 20,
|
45 | height: 20,
|
46 | background: 'blue'
|
47 | })
|
48 | .appendTo(body)
|
49 |
|
50 | //creates a Text node and DocumentFragment
|
51 | var text = elle('text node')
|
52 | var frag = elle('<div>a</div><div>document fragment</div>')
|
53 |
|
54 | //attach the nodes to the body
|
55 | elle(body)
|
56 | .append(text)
|
57 | .append(frag)
|
58 | ```
|
59 |
|
60 | ## overview
|
61 |
|
62 | - manipulation
|
63 | - `append()`
|
64 | - `prepend()`
|
65 | - `before()`
|
66 | - `after()`
|
67 | - `insertBefore()`
|
68 | - `insertAfter()`
|
69 | - `appendTo()`
|
70 | - `prependTo()`
|
71 | - classes
|
72 | - `hasClass()`
|
73 | - `addClass()`
|
74 | - `removeClass()`
|
75 | - `toggleClass()`
|
76 | - misc
|
77 | - `css()` - applies dom style with [dom-css](https://www.npmjs.com/package/dom-classes)
|
78 | - `attr()` - getter/setter for `setAttribute`
|
79 | - `html()` - getter/setter for `innerHTML`
|
80 | - `parent()` - returned a wrapped `parentNode`
|
81 | - `view` - returns the underlying DOM node
|
82 |
|
83 | ## Usage
|
84 |
|
85 | [![NPM](https://nodei.co/npm/elle.png)](https://nodei.co/npm/elle/)
|
86 |
|
87 | #### `e = elle([opt])`
|
88 |
|
89 | Creates a new element wrapper with the given options. If nothing is given, the element will default to an empty `<div>`.
|
90 |
|
91 | If a string is specified, it will create a different type depending on the contents. e.g.
|
92 |
|
93 | - `"some text"` results in a Text node
|
94 | - `"<div>text</div>"` results in a `<div>` with a Text node
|
95 | - `"<div></div><div></div>"` results in a DocumentFragment
|
96 |
|
97 | You can also pass an element (like `document.body` etc), or any object that contains a `view` node (like elle objects).
|
98 |
|
99 | #### `e.view`
|
100 |
|
101 | Returns the underlying DOM node for this wrapper. You can also access the node with array dereference, as in:
|
102 |
|
103 | ```js
|
104 | elle(document.body)[0] === document.body
|
105 | ```
|
106 |
|
107 | #### `e.append(content)`
|
108 | #### `e.prepend(content)`
|
109 | #### `e.before(content)`
|
110 | #### `e.after(content)`
|
111 |
|
112 | Inserts `content` (which can be an element, or a string, or an array of elements/strings) into the wrapped element. Returns this for chaining.
|
113 |
|
114 | #### `e.insertBefore(target)`
|
115 | #### `e.insertAfter(target)`
|
116 | #### `e.appendTo(target)`
|
117 | #### `e.prependTo(target)`
|
118 |
|
119 | Inserts the wrapped target into the specified `target` element. Returns this for chaining.
|
120 |
|
121 | #### `e.remove()`
|
122 |
|
123 | Removes this element from its parent. Returns this for chaining.
|
124 |
|
125 | #### `e.parent()`
|
126 |
|
127 | Returns the parent node, also wrapped as an `elle` object.
|
128 |
|
129 | #### `e.hasClass(name)`
|
130 |
|
131 | Returns true if the element contains the given class. Uses a fallback for non-classList browsers.
|
132 |
|
133 | #### `e.addClass(name)`
|
134 | #### `e.toggleClass(name)`
|
135 | #### `e.removeClass(name)`
|
136 |
|
137 | Adds/removes/toggles a single class by string name.
|
138 |
|
139 | If a `RegExp` is given to `removeClass`, it will remove all classes that match.
|
140 |
|
141 | #### `e.css(map)`
|
142 | #### `e.css(name, value)`
|
143 |
|
144 | Applies styling with [dom-css](https://www.npmjs.com/package/dom-classes). You can provide an object of `name:value` pairs, or specify each property `name` and `value` individually.
|
145 |
|
146 | #### `e.html([value])`
|
147 |
|
148 | A getter/setter for `innerHTML`. If `value` is a string, it will set `innerHTML` (returning this for chaining), otherwise it will return the current value.
|
149 |
|
150 | #### `e.attr(name[, value])`
|
151 |
|
152 | A getter/setter for `setAttribute`. If two arguments are provided, this will call `setAttribute` (returning this for chaining). If only one argument is provided, it will return the value of `getAttribtue`.
|
153 |
|
154 | ## License
|
155 |
|
156 | MIT, see [LICENSE.md](http://github.com/mattdesl/elle/blob/master/LICENSE.md) for details.
|